({ value, onChange }: Props)
| 13 | }; |
| 14 | |
| 15 | export function Editor({ value, onChange }: Props): JSX.Element { |
| 16 | const editor = useRef<HTMLDivElement>(null); |
| 17 | const darkMode = useDarkMode(); |
| 18 | |
| 19 | const onUpdate = EditorView.updateListener.of((v) => { |
| 20 | if (onChange) { |
| 21 | onChange(v.state.doc.toString()); |
| 22 | } |
| 23 | }); |
| 24 | |
| 25 | useEffect(() => { |
| 26 | if (!editor.current) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | const readOnly = !onChange; |
| 31 | |
| 32 | const startState = EditorState.create({ |
| 33 | doc: value, |
| 34 | // readOnly: !onChange, |
| 35 | extensions: [ |
| 36 | readOnly ? minimalSetup : basicSetup, |
| 37 | keymap.of([...defaultKeymap, indentWithTab]), |
| 38 | javascript(), |
| 39 | darkMode ? oneDark : undefined, |
| 40 | readOnly ? undefined : onUpdate, |
| 41 | EditorState.readOnly.of(readOnly), |
| 42 | readOnly ? gutter({}) : undefined, |
| 43 | ].filter( |
| 44 | (value: Extension | undefined): value is Extension => |
| 45 | typeof value !== 'undefined' |
| 46 | ), |
| 47 | }); |
| 48 | |
| 49 | const view = new EditorView({ |
| 50 | state: startState, |
| 51 | parent: editor.current, |
| 52 | }); |
| 53 | |
| 54 | return () => { |
| 55 | view.destroy(); |
| 56 | }; |
| 57 | }, [darkMode]); |
| 58 | |
| 59 | return <div ref={editor}></div>; |
| 60 | } |
nothing calls this directly
no test coverage detected