|
|
@ -10,6 +10,7 @@ export interface EditorRefActions {
|
|
|
|
insertText: (text: string) => void;
|
|
|
|
insertText: (text: string) => void;
|
|
|
|
setContent: (text: string) => void;
|
|
|
|
setContent: (text: string) => void;
|
|
|
|
getContent: () => string;
|
|
|
|
getContent: () => string;
|
|
|
|
|
|
|
|
getCursorPosition: () => number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface EditorProps {
|
|
|
|
interface EditorProps {
|
|
|
@ -64,14 +65,17 @@ const Editor = forwardRef((props: EditorProps, ref: React.ForwardedRef<EditorRef
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const prevValue = editorRef.current.value;
|
|
|
|
const prevValue = editorRef.current.value;
|
|
|
|
editorRef.current.value =
|
|
|
|
const cursorPosition = editorRef.current.selectionStart;
|
|
|
|
prevValue.slice(0, editorRef.current.selectionStart) + rawText + prevValue.slice(editorRef.current.selectionStart);
|
|
|
|
editorRef.current.value = prevValue.slice(0, cursorPosition) + rawText + prevValue.slice(cursorPosition);
|
|
|
|
|
|
|
|
editorRef.current.focus();
|
|
|
|
|
|
|
|
editorRef.current.selectionEnd = cursorPosition + rawText.length;
|
|
|
|
handleContentChangeCallback(editorRef.current.value);
|
|
|
|
handleContentChangeCallback(editorRef.current.value);
|
|
|
|
refresh();
|
|
|
|
refresh();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setContent: (text: string) => {
|
|
|
|
setContent: (text: string) => {
|
|
|
|
if (editorRef.current) {
|
|
|
|
if (editorRef.current) {
|
|
|
|
editorRef.current.value = text;
|
|
|
|
editorRef.current.value = text;
|
|
|
|
|
|
|
|
editorRef.current.focus();
|
|
|
|
handleContentChangeCallback(editorRef.current.value);
|
|
|
|
handleContentChangeCallback(editorRef.current.value);
|
|
|
|
refresh();
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -79,6 +83,9 @@ const Editor = forwardRef((props: EditorProps, ref: React.ForwardedRef<EditorRef
|
|
|
|
getContent: (): string => {
|
|
|
|
getContent: (): string => {
|
|
|
|
return editorRef.current?.value ?? "";
|
|
|
|
return editorRef.current?.value ?? "";
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
getCursorPosition: (): number => {
|
|
|
|
|
|
|
|
return editorRef.current?.selectionStart ?? 0;
|
|
|
|
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
[]
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
);
|
|
|
|