diff --git a/app/src/boot/globalEvent/keydown.ts b/app/src/boot/globalEvent/keydown.ts index a2bb8f07f..24105e455 100644 --- a/app/src/boot/globalEvent/keydown.ts +++ b/app/src/boot/globalEvent/keydown.ts @@ -56,6 +56,9 @@ import {commandPanel} from "../../plugin/commandPanel"; import {toggleDockBar} from "../../layout/dock/util"; import {workspaceMenu} from "../../menus/workspace"; import {resize} from "../../protyle/util/resize"; +import {Search} from "../../search"; +import {Custom} from "../../layout/dock/Custom"; +import {Protyle} from "../../protyle"; const switchDialogEvent = (app: App, event: MouseEvent) => { event.preventDefault(); @@ -152,22 +155,73 @@ const dialogArrow = (app: App, element: HTMLElement, event: KeyboardEvent) => { const editKeydown = (app: App, event: KeyboardEvent) => { const activeTabElement = document.querySelector(".layout__wnd--active .item--focus"); let protyle: IProtyle; - if (activeTabElement) { - const tab = getInstanceById(activeTabElement.getAttribute("data-id")) as Tab; - if (!(tab.model instanceof Editor)) { - return false; - } - protyle = tab.model.editor.protyle; - } else { - const editor = getAllModels().editor.find(item => { - if (item.parent.headElement.classList.contains("item--focus")) { + let range: Range; + if (getSelection().rangeCount > 0) { + range = getSelection().getRangeAt(0); + } + if (range) { + window.siyuan.dialogs.find(item => { + if (item.editor && item.editor.protyle.element.contains(range.startContainer)) { + protyle = item.editor.protyle; return true; } - }); - if (!editor) { + }) + } + if (!protyle && activeTabElement) { + const tab = getInstanceById(activeTabElement.getAttribute("data-id")) as Tab; + if (tab.model instanceof Editor) { + protyle = tab.model.editor.protyle; + } else if (tab.model instanceof Search) { + protyle = tab.model.edit.protyle; + } else if (tab.model instanceof Custom && tab.model.data?.editor instanceof Protyle) { + protyle = tab.model.data.editor.protyle; + } else { + return false; + } + } else if (!protyle) { + const models = getAllModels(); + if (!protyle && range) { + window.siyuan.blockPanels.find(item => { + item.editors.find(editorItem => { + if (editorItem.protyle.element.contains(range.startContainer)) { + protyle = editorItem.protyle; + return true; + } + }) + if (protyle) { + return true + } + }) + } + if (!protyle) { + models.backlink.find(item => { + if (item.element.classList.contains("layout__tab--active")) { + if (range) { + item.editors.find(editor => { + if (editor.protyle.element.contains(range.startContainer)) { + protyle = editor.protyle; + return true; + } + }); + } + if (!protyle) { + protyle = item.editors[0].protyle; + } + return true; + } + }) + } + if (!protyle) { + models.editor.find(item => { + if (item.parent.headElement.classList.contains("item--focus")) { + protyle = item.editor.protyle; + return true; + } + }); + } + if (!protyle) { return false; } - protyle = editor.editor.protyle; } const activePanelElement = document.querySelector(".layout__tab--active"); let isFileFocus = false; @@ -181,10 +235,6 @@ const editKeydown = (app: App, event: KeyboardEvent) => { searchKey = window.siyuan.config.keymap.general.search.custom; } if (!isFileFocus && searchKey) { - let range: Range; - if (getSelection().rangeCount > 0) { - range = getSelection().getRangeAt(0); - } if (range && protyle.element.contains(range.startContainer)) { openSearch({ app, diff --git a/app/src/card/newCardTab.ts b/app/src/card/newCardTab.ts index 2e34078b3..135dfaeb7 100644 --- a/app/src/card/newCardTab.ts +++ b/app/src/card/newCardTab.ts @@ -43,6 +43,7 @@ export const newCardModel = (options: { cardType: this.data.cardType, blocks: response.data.cards, }); + customObj.data.editor = editor; }); }, destroy() { diff --git a/app/src/card/openCard.ts b/app/src/card/openCard.ts index 002d1cb82..3b1b84c75 100644 --- a/app/src/card/openCard.ts +++ b/app/src/card/openCard.ts @@ -446,6 +446,7 @@ export const openCardByData = (app: App, cardsData: { cardType, dialog }); + dialog.editor = editor; }; const nextCard = (options: { diff --git a/app/src/card/viewCards.ts b/app/src/card/viewCards.ts index 096352206..80c61d95b 100644 --- a/app/src/card/viewCards.ts +++ b/app/src/card/viewCards.ts @@ -65,6 +65,7 @@ export const viewCards = (app: App, deckID: string, title: string, deckType: "Tr if (window.siyuan.mobile) { window.siyuan.mobile.popEditor = edit; } + dialog.editor = edit; getArticle(edit, dialog.element.querySelector(".b3-list-item--focus")?.getAttribute("data-id")); } const previousElement = dialog.element.querySelector('[data-type="previous"]'); diff --git a/app/src/dialog/index.ts b/app/src/dialog/index.ts index c71ecb792..6135fee52 100644 --- a/app/src/dialog/index.ts +++ b/app/src/dialog/index.ts @@ -4,12 +4,14 @@ import {moveResize} from "./moveResize"; /// #endif import {isMobile} from "../util/functions"; import {isCtrl} from "../protyle/util/compatibility"; +import {Protyle} from "../protyle"; export class Dialog { private destroyCallback: (options?: IObject) => void; public element: HTMLElement; private id: string; private disableClose: boolean; + public editor: Protyle constructor(options: { title?: string, diff --git a/app/src/search/spread.ts b/app/src/search/spread.ts index 14f2d2349..a11a2655f 100644 --- a/app/src/search/spread.ts +++ b/app/src/search/spread.ts @@ -113,4 +113,5 @@ export const openSearch = async (options: { }, dialog.element.querySelector(".b3-dialog__body"), () => { dialog.destroy({focus: "false"}); }); + dialog.editor = edit; };