From dd19b9bc448baf655258fd3c88675a6e7ce31e10 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Fri, 10 Jun 2022 00:53:32 +0800 Subject: [PATCH] :art: fix https://github.com/siyuan-note/siyuan/issues/5145 --- app/src/protyle/header/Title.ts | 32 +++--------- app/src/protyle/wysiwyg/commonHotkey.ts | 68 +++++++++++++++++++++++++ app/src/protyle/wysiwyg/keydown.ts | 51 ++----------------- 3 files changed, 77 insertions(+), 74 deletions(-) create mode 100644 app/src/protyle/wysiwyg/commonHotkey.ts diff --git a/app/src/protyle/header/Title.ts b/app/src/protyle/header/Title.ts index ae29c386a..c28819bb9 100644 --- a/app/src/protyle/header/Title.ts +++ b/app/src/protyle/header/Title.ts @@ -25,6 +25,7 @@ import {openBacklink, openGraph, openOutline, updatePanelByEditor} from "../../e import * as dayjs from "dayjs"; import {setTitle} from "../../dialog/processSystem"; import {getNoContainerElement} from "../wysiwyg/getBlock"; +import {commonHotkey} from "../wysiwyg/commonHotkey"; export class Title { public element: HTMLElement; @@ -82,6 +83,11 @@ export class Title { if (event.isComposing) { return; } + + if (commonHotkey(protyle, event)) { + return true; + } + if (event.key === "ArrowDown" || event.key === "Enter") { const noContainerElement = getNoContainerElement(protyle.wysiwyg.element.firstElementChild); // https://github.com/siyuan-note/siyuan/issues/4923 @@ -114,32 +120,6 @@ export class Title { writeText(`siyuan://blocks/${protyle.block.rootID}`); event.preventDefault(); event.stopPropagation(); - } else if (matchHotKey(window.siyuan.config.keymap.editor.general.copyHPath.custom, event)) { - fetchPost("/api/filetree/getHPathByID", { - id: protyle.block.rootID - }, (response) => { - writeText(response.data); - }); - event.preventDefault(); - event.stopPropagation(); - } else if (matchHotKey(window.siyuan.config.keymap.editor.general.backlinks.custom, event)) { - event.preventDefault(); - event.stopPropagation(); - openBacklink(protyle); - return; - } else if (matchHotKey(window.siyuan.config.keymap.editor.general.graphView.custom, event)) { - event.preventDefault(); - event.stopPropagation(); - openGraph(protyle); - return; - } else if (matchHotKey(window.siyuan.config.keymap.editor.general.outline.custom, event)) { - event.preventDefault(); - event.stopPropagation(); - const offset = getSelectionOffset(this.editElement); - openOutline(protyle); - // switchWnd 后,range会被清空,需要重新设置 - focusByOffset(this.editElement, offset.start, offset.end); - return; } }); const iconElement = this.element.querySelector(".protyle-title__icon"); diff --git a/app/src/protyle/wysiwyg/commonHotkey.ts b/app/src/protyle/wysiwyg/commonHotkey.ts new file mode 100644 index 000000000..300750a5b --- /dev/null +++ b/app/src/protyle/wysiwyg/commonHotkey.ts @@ -0,0 +1,68 @@ +import {matchHotKey} from "../util/hotKey"; +import {fetchPost} from "../../util/fetch"; +import {writeText} from "../util/compatibility"; +import {openBacklink, openGraph, openOutline} from "../../editor/util"; +import {focusByOffset, getSelectionOffset} from "../util/selection"; +import {fullscreen} from "../breadcrumb/action"; +import {addLoading, setPadding} from "../ui/initUI"; +import {Constants} from "../../constants"; +import {onGet} from "../util/onGet"; + +export const commonHotkey = (protyle: IProtyle, event: KeyboardEvent) => { + const target = event.target as HTMLElement + if (matchHotKey(window.siyuan.config.keymap.editor.general.copyHPath.custom, event)) { + fetchPost("/api/filetree/getHPathByID", { + id: protyle.block.rootID + }, (response) => { + writeText(response.data); + }); + event.preventDefault(); + event.stopPropagation(); + return true; + } + if (matchHotKey(window.siyuan.config.keymap.editor.general.refresh.custom, event)) { + protyle.title.render(protyle, true); + addLoading(protyle); + fetchPost("/api/filetree/getDoc", { + id: protyle.block.showAll ? protyle.block.id : protyle.block.rootID, + mode: 0, + size: protyle.block.showAll ? Constants.SIZE_GET_MAX : Constants.SIZE_GET, + }, getResponse => { + onGet(getResponse, protyle, protyle.block.showAll ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS]); + }); + event.preventDefault(); + event.stopPropagation(); + return true; + } + + if (matchHotKey(window.siyuan.config.keymap.editor.general.fullscreen.custom, event)) { + fullscreen(protyle.element); + setPadding(protyle); + event.preventDefault(); + event.stopPropagation(); + return true; + } + if (protyle.model) { + if (matchHotKey(window.siyuan.config.keymap.editor.general.backlinks.custom, event)) { + event.preventDefault(); + event.stopPropagation(); + openBacklink(protyle); + return true; + } + if (matchHotKey(window.siyuan.config.keymap.editor.general.graphView.custom, event)) { + event.preventDefault(); + event.stopPropagation(); + openGraph(protyle); + return true; + } + if (matchHotKey(window.siyuan.config.keymap.editor.general.outline.custom, event)) { + event.preventDefault(); + event.stopPropagation(); + const offset = getSelectionOffset(target); + openOutline(protyle); + // switchWnd 后,range会被清空,需要重新设置 + focusByOffset(target, offset.start, offset.end); + return true; + } + } +} diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts index 9076a5027..7dd8852c4 100644 --- a/app/src/protyle/wysiwyg/keydown.ts +++ b/app/src/protyle/wysiwyg/keydown.ts @@ -45,10 +45,11 @@ import {bindMenuKeydown} from "../../menus/Menu"; import {fetchPost} from "../../util/fetch"; import {onGet} from "../util/onGet"; import {scrollCenter} from "../../util/highlightById"; -import {openBacklink, openBy, openFileById, openGraph, openOutline} from "../../editor/util"; +import {openBy, openFileById} from "../../editor/util"; import {BlockPanel} from "../../block/Panel"; import * as dayjs from "dayjs"; import {highlightRender} from "../markdown/highlightRender"; +import {commonHotkey} from "./commonHotkey"; export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { editorElement.addEventListener("keydown", (event: KeyboardEvent & { target: HTMLElement }) => { @@ -449,19 +450,6 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { event.stopPropagation(); return; } - if (matchHotKey(window.siyuan.config.keymap.editor.general.refresh.custom, event)) { - protyle.title.render(protyle, true); - addLoading(protyle); - fetchPost("/api/filetree/getDoc", { - id: protyle.block.showAll ? protyle.block.id : protyle.block.rootID, - mode: 0, - size: protyle.block.showAll ? Constants.SIZE_GET_MAX : Constants.SIZE_GET, - }, getResponse => { - onGet(getResponse, protyle, protyle.block.showAll ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS]); - }); - event.preventDefault(); - return; - } if (matchHotKey("⌘/", event)) { event.stopPropagation(); event.preventDefault(); @@ -724,13 +712,6 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { return true; } - if (matchHotKey(window.siyuan.config.keymap.editor.general.fullscreen.custom, event)) { - fullscreen(protyle.element); - setPadding(protyle); - event.preventDefault(); - return true; - } - if (matchHotKey(window.siyuan.config.keymap.editor.general.undo.custom, event)) { protyle.undo.undo(protyle); event.preventDefault(); @@ -758,13 +739,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { return true; } - if (matchHotKey(window.siyuan.config.keymap.editor.general.copyHPath.custom, event)) { - fetchPost("/api/filetree/getHPathByID", { - id: protyle.block.rootID - }, (response) => { - writeText(response.data); - }); - event.preventDefault(); + if (commonHotkey(protyle, event)) { return true; } @@ -1397,26 +1372,6 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { } } - if (protyle.model) { - if (matchHotKey(window.siyuan.config.keymap.editor.general.outline.custom, event)) { - event.preventDefault(); - openOutline(protyle); - return; - } - - if (matchHotKey(window.siyuan.config.keymap.editor.general.backlinks.custom, event)) { - event.preventDefault(); - openBacklink(protyle); - return; - } - - if (matchHotKey(window.siyuan.config.keymap.editor.general.graphView.custom, event)) { - event.preventDefault(); - openGraph(protyle); - return; - } - } - /// #if !BROWSER if (matchHotKey(window.siyuan.config.keymap.editor.general.pasteAsPlainText.custom, event)) { writeText(clipboard.readText());