|
| 1 | +import React from "react"; |
1 | 2 | import styled from "styled-components";
|
| 3 | +import Prism from "prismjs"; |
| 4 | +import "prismjs/components/prism-json"; |
| 5 | +import "prismjs/themes/prism-coy.css"; |
2 | 6 |
|
3 |
| -const TextArea = styled.textarea` |
4 |
| - font-family: monospace; |
5 |
| - font-family: monospace; |
| 7 | +const Container = styled.div` |
6 | 8 | width: 100%;
|
7 |
| - min-height: 64ex; |
8 |
| - min-width: 128ex; |
9 |
| - resize: vertical; |
| 9 | + height: 100%; |
| 10 | + resize: both; |
| 11 | + overflow: auto; |
| 12 | + border: ButtonBorder 1px solid; |
| 13 | + border-radius: 4px; |
| 14 | + &:focus-within { |
| 15 | + box-shadow: inset 0 0 3px AccentColor; |
| 16 | + } |
10 | 17 | `;
|
11 | 18 |
|
| 19 | +const Content = styled.div` |
| 20 | + margin: 8px; |
| 21 | + position: relative; |
| 22 | + overflow: hidden; |
| 23 | +`; |
| 24 | + |
| 25 | +const StyledPre = styled.pre` |
| 26 | + position: absolute; |
| 27 | + font-size: 14px; |
| 28 | + width: 100%; |
| 29 | + height: 100%; |
| 30 | + resize: none; |
| 31 | + margin: 0; |
| 32 | + padding: 0; |
| 33 | +`; |
| 34 | + |
| 35 | +const StyledTextarea = styled.textarea` |
| 36 | + position: absolute; |
| 37 | + font-size: 14px; |
| 38 | + width: 100%; |
| 39 | + height: 100%; |
| 40 | + padding: 0; |
| 41 | + margin: 0; |
| 42 | + outline: none; |
| 43 | + border: none; |
| 44 | + resize: none; |
| 45 | + background-color: transparent; |
| 46 | + color: transparent; |
| 47 | + caret-color: CanvasText; |
| 48 | +`; |
| 49 | + |
| 50 | +type Props = React.TextareaHTMLAttributes<HTMLTextAreaElement>; |
| 51 | + |
| 52 | +const TextArea: React.FC<Props> = (props) => { |
| 53 | + const highlightContainer = React.useRef<HTMLDivElement>(null); |
| 54 | + const container = React.useRef<HTMLDivElement>(null); |
| 55 | + const content = React.useRef<HTMLDivElement>(null); |
| 56 | + const textarea = React.useRef<HTMLTextAreaElement>(null); |
| 57 | + const [json, setJson] = React.useState(props.value as string); |
| 58 | + |
| 59 | + React.useEffect(() => { |
| 60 | + highlight(json); |
| 61 | + }, []); |
| 62 | + |
| 63 | + const onChange = React.useCallback( |
| 64 | + (e: React.ChangeEvent<HTMLTextAreaElement>) => { |
| 65 | + setJson(e.target.value); |
| 66 | + highlight(e.target.value); |
| 67 | + autoResize(); |
| 68 | + props.onChange?.(e); |
| 69 | + }, |
| 70 | + [] |
| 71 | + ); |
| 72 | + |
| 73 | + const highlight = React.useCallback((source: string) => { |
| 74 | + if (!highlightContainer.current) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const highlighted = Prism.highlight(source, Prism.languages.json, "json"); |
| 79 | + highlightContainer.current.innerHTML = highlighted; |
| 80 | + autoResize(); |
| 81 | + }, []); |
| 82 | + |
| 83 | + const autoResize = React.useCallback(() => { |
| 84 | + if (!textarea.current || !content.current) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + // shrink scroll area to get the correct scroll area |
| 89 | + content.current.style.width = "0"; |
| 90 | + content.current.style.height = "0"; |
| 91 | + |
| 92 | + const { scrollHeight, scrollWidth } = textarea.current; |
| 93 | + console.log(scrollHeight, scrollWidth); |
| 94 | + content.current.style.width = `${scrollWidth}px`; |
| 95 | + content.current.style.height = `${scrollHeight}px`; |
| 96 | + }, []); |
| 97 | + |
| 98 | + return ( |
| 99 | + <Container ref={container}> |
| 100 | + <Content ref={content}> |
| 101 | + <StyledPre> |
| 102 | + <code ref={highlightContainer} /> |
| 103 | + </StyledPre> |
| 104 | + <StyledTextarea |
| 105 | + {...props} |
| 106 | + wrap="off" |
| 107 | + ref={textarea} |
| 108 | + onChange={onChange} |
| 109 | + spellCheck={false} |
| 110 | + value={json} |
| 111 | + /> |
| 112 | + </Content> |
| 113 | + </Container> |
| 114 | + ); |
| 115 | +}; |
| 116 | + |
12 | 117 | export default TextArea;
|
0 commit comments