This is an automated email from the ASF dual-hosted git repository. robin0716 pushed a commit to branch feat/1.3.5/embed in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
commit b6e21bc3b271f42f1ba07f2039dd62d73e21205b Author: robin <[email protected]> AuthorDate: Mon May 27 12:30:03 2024 +0800 feat(Editor): add useEditor hook with EditorProps interface --- ui/src/components/Editor/utils/index.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/ui/src/components/Editor/utils/index.ts b/ui/src/components/Editor/utils/index.ts index d380620b..44fcf09d 100644 --- a/ui/src/components/Editor/utils/index.ts +++ b/ui/src/components/Editor/utils/index.ts @@ -69,6 +69,15 @@ export function htmlRender(el: HTMLElement | null) { }); } +interface EditorProps { + editorRef: React.RefObject<HTMLDivElement>; + placeholder: string; + autoFocus: boolean; + onChange: (value: string) => void; + onFocus: () => void; + onBlur: () => void; +} + export const useEditor = ({ editorRef, placeholder: placeholderText, @@ -76,7 +85,7 @@ export const useEditor = ({ onChange, onFocus, onBlur, -}) => { +}: EditorProps) => { const [editor, setEditor] = useState<Editor | null>(null); const [value, setValue] = useState<string>(''); const init = async () => { @@ -115,7 +124,7 @@ export const useEditor = ({ }); const view = new EditorView({ - parent: editorRef.current, + parent: editorRef.current!, state: startState, }); @@ -150,10 +159,10 @@ export const useEditor = ({ }, [value]); useEffect(() => { - if (!(editorRef.current instanceof HTMLElement) || editor) { + if (editorRef.current!.children.length > 0 || editor) { return; } init(); - }, [editor]); + }, [editor, editorRef.current]); return editor; };
