From 806c4c086d2840a6b3716b36f17e2ba0d043e66e Mon Sep 17 00:00:00 2001 From: brzhang Group <1595819400@qq.com> Date: Tue, 31 Oct 2023 19:04:16 +0800 Subject: [PATCH] feat: support keyboard shortcuts on Mac (#248) Co-authored-by: hoollyzhang --- src/stores/index.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/stores/index.js b/src/stores/index.js index 729947f..f7c1cb8 100644 --- a/src/stores/index.js +++ b/src/stores/index.js @@ -88,6 +88,9 @@ export const useStore = defineStore(`store`, { editorDom.value = localStorage.getItem(`__editor_content`) || formatDoc(DEFAULT_CONTENT) } + const defaultKeyMap = CodeMirror.keyMap[`default`] + const modPrefix = + defaultKeyMap === CodeMirror.keyMap[`macDefault`] ? `Cmd` : `Ctrl` this.editor = CodeMirror.fromTextArea(editorDom, { mode: `text/x-markdown`, theme: `xq-light`, @@ -96,24 +99,27 @@ export const useStore = defineStore(`store`, { styleActiveLine: true, autoCloseBrackets: true, extraKeys: { - 'Ctrl-F': function autoFormat(editor) { + [`${modPrefix}-F`]: function autoFormat(editor) { const doc = formatDoc(editor.getValue(0)) localStorage.setItem(`__editor_content`, doc) editor.setValue(doc) }, - 'Ctrl-S': function save(editor) {}, - 'Ctrl-B': function bold(editor) { + [[`${modPrefix}-B`]]: function bold(editor) { const selected = editor.getSelection() editor.replaceSelection(`**${selected}**`) }, - 'Ctrl-D': function del(editor) { + [`${modPrefix}-D`]: function del(editor) { const selected = editor.getSelection() editor.replaceSelection(`~~${selected}~~`) }, - 'Ctrl-I': function italic(editor) { + [`${modPrefix}-I`]: function italic(editor) { const selected = editor.getSelection() editor.replaceSelection(`*${selected}*`) }, + [`${modPrefix}-L`]: function code(editor) { + const selected = editor.getSelection() + editor.replaceSelection(`\`${selected}\``) + }, }, }) },