From d986473e174953ce981e1e73058921b670c972a2 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Fri, 28 Apr 2023 16:50:10 +0800 Subject: [PATCH] :art: fix https://github.com/siyuan-note/siyuan/issues/8120 --- app/src/boot/globalShortcut.ts | 70 +--------------------- app/src/business/openRecentDocs.ts | 79 +++++++++++++++++++++++++ app/src/layout/util.ts | 93 ++++++++++++++++++++++++------ app/src/mobile/util/setEmpty.ts | 6 +- 4 files changed, 158 insertions(+), 90 deletions(-) create mode 100644 app/src/business/openRecentDocs.ts diff --git a/app/src/boot/globalShortcut.ts b/app/src/boot/globalShortcut.ts index a05df1099..e2de601e6 100644 --- a/app/src/boot/globalShortcut.ts +++ b/app/src/boot/globalShortcut.ts @@ -52,6 +52,7 @@ import {isWindow} from "../util/functions"; import {reloadProtyle} from "../protyle/util/reload"; import {fullscreen} from "../protyle/breadcrumb/action"; import {setPadding} from "../protyle/ui/initUI"; +import {openRecentDocs} from "../business/openRecentDocs"; const getRightBlock = (element: HTMLElement, x: number, y: number) => { let index = 1; @@ -979,75 +980,6 @@ const dialogArrow = (element: HTMLElement, event: KeyboardEvent) => { } }; -const openRecentDocs = () => { - fetchPost("/api/storage/getRecentDocs", {}, (response) => { - let range: Range; - if (getSelection().rangeCount > 0) { - range = getSelection().getRangeAt(0); - } - let tabHtml = ""; - response.data.forEach((item: any, index: number) => { - tabHtml += `
  • -${unicode2Emoji(item.icon || Constants.SIYUAN_IMAGE_FILE, false, "b3-list-item__graphic", true)} -${escapeHtml(item.title)} -
  • `; - }); - let dockHtml = ""; - if (!isWindow()) { - dockHtml = `"; - } - const dialog = new Dialog({ - title: window.siyuan.languages.recentDocs, - content: `
    -
    -
    ${dockHtml} - ${tabHtml} -
    -
    -
    `, - destroyCallback: () => { - if (range && range.getBoundingClientRect().height !== 0) { - focusByRange(range); - } - } - }); - if (response.data.length > 0) { - fetchPost("/api/filetree/getFullHPathByID", { - id: response.data[0].rootID - }, (response) => { - dialog.element.querySelector(".switch-doc__path").innerHTML = escapeHtml(response.data); - }); - } else { - dialog.element.querySelector(".switch-doc__path").innerHTML = dialog.element.querySelector(".b3-list-item--focus").textContent; - } - dialog.element.querySelector("input").focus(); - dialog.element.setAttribute("data-key", window.siyuan.config.keymap.general.recentDocs.custom); - dialog.element.addEventListener("click", (event) => { - const liElement = hasClosestByClassName(event.target as HTMLElement, "b3-list-item"); - if (liElement) { - dialog.element.querySelector(".b3-list-item--focus").classList.remove("b3-list-item--focus"); - liElement.classList.add("b3-list-item--focus"); - window.dispatchEvent(new KeyboardEvent("keydown", {key: "Enter"})); - event.stopPropagation(); - event.preventDefault(); - } - }); - }); -}; - const editKeydown = (event: KeyboardEvent) => { const activeTabElement = document.querySelector(".layout__wnd--active .item--focus"); let protyle: IProtyle; diff --git a/app/src/business/openRecentDocs.ts b/app/src/business/openRecentDocs.ts new file mode 100644 index 000000000..4ced8f693 --- /dev/null +++ b/app/src/business/openRecentDocs.ts @@ -0,0 +1,79 @@ +import {fetchPost} from "../util/fetch"; +import {unicode2Emoji} from "../emoji"; +import {Constants} from "../constants"; +import {escapeHtml} from "../util/escape"; +import {isWindow} from "../util/functions"; +import {updateHotkeyTip} from "../protyle/util/compatibility"; +import {getAllDocks} from "../layout/getAll"; +import {Dialog} from "../dialog"; +import {focusByRange} from "../protyle/util/selection"; +import {hasClosestByClassName} from "../protyle/util/hasClosest"; + +export const openRecentDocs = () => { + fetchPost("/api/storage/getRecentDocs", {}, (response) => { + let range: Range; + if (getSelection().rangeCount > 0) { + range = getSelection().getRangeAt(0); + } + let tabHtml = ""; + response.data.forEach((item: any, index: number) => { + tabHtml += `
  • +${unicode2Emoji(item.icon || Constants.SIYUAN_IMAGE_FILE, false, "b3-list-item__graphic", true)} +${escapeHtml(item.title)} +
  • `; + }); + let dockHtml = ""; + if (!isWindow()) { + dockHtml = `"; + } + const dialog = new Dialog({ + title: window.siyuan.languages.recentDocs, + content: `
    +
    +
    ${dockHtml} + ${tabHtml} +
    +
    +
    `, + destroyCallback: () => { + if (range && range.getBoundingClientRect().height !== 0) { + focusByRange(range); + } + } + }); + if (response.data.length > 0) { + fetchPost("/api/filetree/getFullHPathByID", { + id: response.data[0].rootID + }, (response) => { + dialog.element.querySelector(".switch-doc__path").innerHTML = escapeHtml(response.data); + }); + } else { + dialog.element.querySelector(".switch-doc__path").innerHTML = dialog.element.querySelector(".b3-list-item--focus").textContent; + } + dialog.element.querySelector("input").focus(); + dialog.element.setAttribute("data-key", window.siyuan.config.keymap.general.recentDocs.custom); + dialog.element.addEventListener("click", (event) => { + const liElement = hasClosestByClassName(event.target as HTMLElement, "b3-list-item"); + if (liElement) { + dialog.element.querySelector(".b3-list-item--focus").classList.remove("b3-list-item--focus"); + liElement.classList.add("b3-list-item--focus"); + window.dispatchEvent(new KeyboardEvent("keydown", {key: "Enter"})); + event.stopPropagation(); + event.preventDefault(); + } + }); + }); +}; diff --git a/app/src/layout/util.ts b/app/src/layout/util.ts index 1401b2fae..eea013ee2 100644 --- a/app/src/layout/util.ts +++ b/app/src/layout/util.ts @@ -36,6 +36,7 @@ import {getIdZoomInByPath} from "../util/pathName"; import {openHistory} from "../history/history"; import {Custom} from "./dock/Custom"; import {newCardModel} from "../card/newCardTab"; +import { openRecentDocs } from "../business/openRecentDocs"; export const setPanelFocus = (element: Element) => { if (element.classList.contains("layout__tab--active") || element.classList.contains("layout__wnd--active")) { @@ -802,26 +803,82 @@ export const newCenterEmptyTab = () => {

    ${window.siyuan.languages.noOpenFile}

    -
    ${window.siyuan.languages.search}${updateHotkeyTip(window.siyuan.config.keymap.general.globalSearch.custom)}
    -
    ${window.siyuan.languages.newFile}${updateHotkeyTip(window.siyuan.config.keymap.general.newFile.custom)}
    -
    ${window.siyuan.languages.newNotebook}
    -
    ${window.siyuan.languages.help}
    +
    + + ${window.siyuan.languages.search} + ${updateHotkeyTip(window.siyuan.config.keymap.general.globalSearch.custom)} +
    +
    + + ${window.siyuan.languages.recentDocs} + ${updateHotkeyTip(window.siyuan.config.keymap.general.recentDocs.custom)} +
    +
    + + ${window.siyuan.languages.dataHistory} + ${updateHotkeyTip(window.siyuan.config.keymap.general.dataHistory.custom)} +
    +
    + + ${window.siyuan.languages.newFile} + ${updateHotkeyTip(window.siyuan.config.keymap.general.newFile.custom)} +
    +
    + + ${window.siyuan.languages.newNotebook} +
    +
    + + ${window.siyuan.languages.help} +
    `, callback(tab: Tab) { - tab.panelElement.querySelector("#editorEmptyHelp").addEventListener("click", () => { - mountHelp(); - }); - tab.panelElement.querySelector("#editorEmptySearch").addEventListener("click", () => { - openSearch(window.siyuan.config.keymap.general.globalSearch.custom); - }); - if (!window.siyuan.config.readonly) { - tab.panelElement.querySelector("#editorEmptyNewNotebook").addEventListener("click", () => { - newNotebook(); - }); - tab.panelElement.querySelector("#editorEmptyFile").addEventListener("click", () => { - newFile(undefined, undefined, undefined, true); - }); - } + tab.panelElement.addEventListener("click", (event) => { + let target = event.target as HTMLElement; + while (target && !target.isEqualNode( tab.panelElement)) { + if (target.id === "editorEmptySearch") { + openSearch(window.siyuan.config.keymap.general.globalSearch.custom); + event.stopPropagation(); + event.preventDefault() + break; + } else if (target.id === "editorEmptyRecent") { + const openRecentDocsDialog = window.siyuan.dialogs.find(item => { + if (item.element.getAttribute("data-key") === window.siyuan.config.keymap.general.recentDocs.custom) { + return true; + } + }); + if (openRecentDocsDialog) { + hideElements(["dialog"]); + return; + } + openRecentDocs(); + event.stopPropagation(); + event.preventDefault() + break; + }else if (target.id === "editorEmptyHistory") { + openHistory(); + event.stopPropagation(); + event.preventDefault() + break; + }else if (target.id === "editorEmptyFile") { + newFile(undefined, undefined, undefined, true); + event.stopPropagation(); + event.preventDefault() + break; + }else if (target.id === "editorEmptyNewNotebook") { + newNotebook(); + event.stopPropagation(); + event.preventDefault() + break; + }else if (target.id === "editorEmptyHelp") { + mountHelp() + event.stopPropagation(); + event.preventDefault() + break; + } + target = target.parentElement + } + }) } }); }; diff --git a/app/src/mobile/util/setEmpty.ts b/app/src/mobile/util/setEmpty.ts index f993bba5e..c0b56eb5b 100644 --- a/app/src/mobile/util/setEmpty.ts +++ b/app/src/mobile/util/setEmpty.ts @@ -22,13 +22,13 @@ export const setEmpty = () => {
    ${window.siyuan.languages.recentDocs}
    -
    +
    ${window.siyuan.languages.dataHistory}
    -
    +
    ${window.siyuan.languages.newFile}
    -
    +
    ${window.siyuan.languages.newNotebook}