Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

feat: add some hotkeys (#320)

Mahoo Huang5690dab6bb95
diff --git a/web/src/components/Editor/Editor.tsx b/web/src/components/Editor/Editor.tsx
index 38a7cc868..055257353 100644
--- a/web/src/components/Editor/Editor.tsx
+++ b/web/src/components/Editor/Editor.tsx
@@ -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) ?? "";
+ },
}),
[]
);
diff --git a/web/src/components/MemoEditor.tsx b/web/src/components/MemoEditor.tsx
index e10c28bd0..d746caa75 100644
--- a/web/src/components/MemoEditor.tsx
+++ b/web/src/components/MemoEditor.tsx
@@ -76,6 +76,10 @@ const MemoEditor: React.FC = () => {
prevGlobalStateRef.current = editorState;
}, [state, editorState.editMemoId]);
+ const handleInsertMark = (mark: string) => {
+ editorRef.current?.insertText("", mark);
+ };
+
const handleKeyDown = (event: React.KeyboardEvent) => {
if (event.key === "Escape" && state.fullscreen) {
handleFullscreenBtnClick();
@@ -90,6 +94,21 @@ const MemoEditor: React.FC = () => {
editorRef.current?.insertText(" ".repeat(TAB_SPACE_WIDTH));
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) => {