mirror of
https://github.com/usememos/memos.git
synced 2025-02-22 14:17:48 +01:00
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:
parent
65506a5b30
commit
5690dab6bb
@ -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) ?? "";
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
@ -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) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user