|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { useEffect, useRef } from 'react'; |
| 21 | + |
| 22 | +import { EditorView } from '@codemirror/view'; |
| 23 | + |
| 24 | +import { BaseEditorProps } from './types'; |
| 25 | +import { useEditor } from './utils'; |
| 26 | + |
| 27 | +interface MarkdownEditorProps extends BaseEditorProps {} |
| 28 | + |
| 29 | +const MarkdownEditor: React.FC<MarkdownEditorProps> = ({ |
| 30 | + value, |
| 31 | + onChange, |
| 32 | + onFocus, |
| 33 | + onBlur, |
| 34 | + placeholder, |
| 35 | + autoFocus, |
| 36 | + onEditorReady, |
| 37 | +}) => { |
| 38 | + const editorRef = useRef<HTMLDivElement>(null); |
| 39 | + const lastSyncedValueRef = useRef<string>(value); |
| 40 | + const isInitializedRef = useRef<boolean>(false); |
| 41 | + |
| 42 | + const editor = useEditor({ |
| 43 | + editorRef, |
| 44 | + onChange, |
| 45 | + onFocus, |
| 46 | + onBlur, |
| 47 | + placeholder, |
| 48 | + autoFocus, |
| 49 | + initialValue: value, |
| 50 | + }); |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + if (!editor || isInitializedRef.current) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + isInitializedRef.current = true; |
| 58 | + onEditorReady?.(editor); |
| 59 | + }, [editor, onEditorReady]); |
| 60 | + |
| 61 | + useEffect(() => { |
| 62 | + if (!editor || value === lastSyncedValueRef.current) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const currentValue = editor.getValue(); |
| 67 | + if (currentValue !== value) { |
| 68 | + editor.setValue(value || ''); |
| 69 | + lastSyncedValueRef.current = value || ''; |
| 70 | + } |
| 71 | + }, [editor, value]); |
| 72 | + |
| 73 | + useEffect(() => { |
| 74 | + lastSyncedValueRef.current = value; |
| 75 | + isInitializedRef.current = false; |
| 76 | + |
| 77 | + return () => { |
| 78 | + if (editor) { |
| 79 | + const view = editor as unknown as EditorView; |
| 80 | + if (view.destroy) { |
| 81 | + view.destroy(); |
| 82 | + } |
| 83 | + } |
| 84 | + isInitializedRef.current = false; |
| 85 | + }; |
| 86 | + }, []); |
| 87 | + |
| 88 | + return ( |
| 89 | + <div className="content-wrap"> |
| 90 | + <div |
| 91 | + className="md-editor position-relative w-100 h-100" |
| 92 | + ref={editorRef} |
| 93 | + /> |
| 94 | + </div> |
| 95 | + ); |
| 96 | +}; |
| 97 | + |
| 98 | +export default MarkdownEditor; |
0 commit comments