Unverified Commit 2658b1fd authored by ChasLui's avatar ChasLui Committed by GitHub

feat: support `command + k` shortcuts insert []() (#793)

* feat: support `command + k` shortcuts insert []()

* fix: eslint

* fix: clear code

* fix: eslint

* feat: insert [](url)

* refactor: rename param

* fix: eslint
parent b7df1f5b
...@@ -9,7 +9,7 @@ export interface EditorRefActions { ...@@ -9,7 +9,7 @@ export interface EditorRefActions {
getContent: () => string; getContent: () => string;
getSelectedContent: () => string; getSelectedContent: () => string;
getCursorPosition: () => number; getCursorPosition: () => number;
setCursorPosition: (pos: number) => void; setCursorPosition: (startPos: number, endPos?: number) => void;
} }
interface Props { interface Props {
...@@ -105,8 +105,9 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef< ...@@ -105,8 +105,9 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
const end = editorRef.current?.selectionEnd; const end = editorRef.current?.selectionEnd;
return editorRef.current?.value.slice(start, end) ?? ""; return editorRef.current?.value.slice(start, end) ?? "";
}, },
setCursorPosition: (pos: number) => { setCursorPosition: (startPos: number, endPos?: number) => {
editorRef.current?.setSelectionRange(pos, pos); const _endPos = isNaN(endPos as number) ? startPos : (endPos as number);
editorRef.current?.setSelectionRange(startPos, _endPos);
}, },
}), }),
[] []
......
...@@ -122,6 +122,16 @@ const MemoEditor = () => { ...@@ -122,6 +122,16 @@ const MemoEditor = () => {
editorRef.current.insertText("", "`", "`"); editorRef.current.insertText("", "`", "`");
return; return;
} }
if (event.key === "k") {
event.preventDefault();
const selectedContent = editorRef.current.getSelectedContent();
editorRef.current.insertText("", "[", "](url)");
if (selectedContent) {
const startPos = editorRef.current.getCursorPosition() + 2;
const endPos = startPos + 3;
editorRef.current.setCursorPosition(startPos, endPos);
}
}
} }
if (event.key === "Enter") { if (event.key === "Enter") {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment