feat: add some hotkeys (#320)

* feat: add some hotkeys

* fix: hotkeys lose the text behind selected

* chore: adjust insertText params passing
This commit is contained in:
Mahoo Huang 2022-10-23 19:33:45 +08:00 committed by GitHub
parent 65506a5b30
commit 5690dab6bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 6 deletions

View File

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

View File

@ -76,6 +76,10 @@ const MemoEditor: React.FC = () => {
prevGlobalStateRef.current = editorState; prevGlobalStateRef.current = editorState;
}, [state, editorState.editMemoId]); }, [state, editorState.editMemoId]);
const handleInsertMark = (mark: string) => {
editorRef.current?.insertText("", mark);
};
const handleKeyDown = (event: React.KeyboardEvent) => { const handleKeyDown = (event: React.KeyboardEvent) => {
if (event.key === "Escape" && state.fullscreen) { if (event.key === "Escape" && state.fullscreen) {
handleFullscreenBtnClick(); handleFullscreenBtnClick();
@ -90,6 +94,21 @@ const MemoEditor: React.FC = () => {
editorRef.current?.insertText(" ".repeat(TAB_SPACE_WIDTH)); editorRef.current?.insertText(" ".repeat(TAB_SPACE_WIDTH));
return; return;
} }
if (event.ctrlKey || event.metaKey) {
event.preventDefault();
switch (event.key) {
case "b":
handleInsertMark("**");
break;
case "i":
handleInsertMark("*");
break;
case "e":
handleInsertMark("`");
break;
}
return;
}
}; };
const handleDropEvent = async (event: React.DragEvent) => { const handleDropEvent = async (event: React.DragEvent) => {