|
|
|
@ -4,9 +4,10 @@ import "../../less/editor.less";
|
|
|
|
|
|
|
|
|
|
export interface EditorRefActions {
|
|
|
|
|
focus: FunctionType;
|
|
|
|
|
insertText: (text: string) => void;
|
|
|
|
|
insertText: (text: string, prefix?: string, suffix?: string) => void;
|
|
|
|
|
setContent: (text: string) => void;
|
|
|
|
|
getContent: () => string;
|
|
|
|
|
getSelectedContent: () => string;
|
|
|
|
|
getCursorPosition: () => number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -46,16 +47,24 @@ const Editor = forwardRef((props: Props, ref: React.ForwardedRef<EditorRefAction
|
|
|
|
|
focus: () => {
|
|
|
|
|
editorRef.current?.focus();
|
|
|
|
|
},
|
|
|
|
|
insertText: (rawText: string) => {
|
|
|
|
|
insertText: (content = "", prefix = "", suffix = prefix) => {
|
|
|
|
|
if (!editorRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const prevValue = editorRef.current.value;
|
|
|
|
|
const cursorPosition = editorRef.current.selectionStart;
|
|
|
|
|
editorRef.current.value = prevValue.slice(0, cursorPosition) + rawText + prevValue.slice(cursorPosition);
|
|
|
|
|
const endPosition = editorRef.current.selectionEnd;
|
|
|
|
|
const prevValue = editorRef.current.value;
|
|
|
|
|
const value =
|
|
|
|
|
prevValue.slice(0, cursorPosition) +
|
|
|
|
|
prefix +
|
|
|
|
|
prevValue.slice(cursorPosition, endPosition) +
|
|
|
|
|
suffix +
|
|
|
|
|
content +
|
|
|
|
|
prevValue.slice(endPosition);
|
|
|
|
|
|
|
|
|
|
editorRef.current.value = value;
|
|
|
|
|
editorRef.current.focus();
|
|
|
|
|
editorRef.current.selectionEnd = cursorPosition + rawText.length;
|
|
|
|
|
editorRef.current.selectionEnd = endPosition + prefix.length + suffix.length + content.length;
|
|
|
|
|
handleContentChangeCallback(editorRef.current.value);
|
|
|
|
|
refresh();
|
|
|
|
|
},
|
|
|
|
@ -73,6 +82,11 @@ const Editor = forwardRef((props: Props, ref: React.ForwardedRef<EditorRefAction
|
|
|
|
|
getCursorPosition: (): number => {
|
|
|
|
|
return editorRef.current?.selectionStart ?? 0;
|
|
|
|
|
},
|
|
|
|
|
getSelectedContent: () => {
|
|
|
|
|
const start = editorRef.current?.selectionStart;
|
|
|
|
|
const end = editorRef.current?.selectionEnd;
|
|
|
|
|
return editorRef.current?.value.slice(start, end) ?? "";
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
[]
|
|
|
|
|
);
|
|
|
|
|