An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Improve mobile swipe gestures with configurable left and right sidebars https://github.com/siyuan-note/siyuan/issues/18912
Daniel54cb6cce0845
diff --git a/app/src/assets/scss/main/_mobile.scss b/app/src/assets/scss/main/_mobile.scssindex 2af65a9f0d..fd78e6c26e 100644--- a/app/src/assets/scss/main/_mobile.scss+++ b/app/src/assets/scss/main/_mobile.scss@@ -523,7 +523,11 @@ body.mobile-keyboard--open {transform: translateX(100vw);}- // 窄屏下 dock 图标过多时左侧可横向滚动,关闭按钮固定在右侧始终可触达+ &--right {+ transform: translateX(100vw);+ }++ // 窄屏下 Dock 图标过多时可横向滚动,关闭按钮固定在工具栏末端始终可触达& > .toolbar--border > .toolbar__scroll {flex: 1;min-width: 0;@@ -968,8 +972,9 @@ body.mobile-keyboard--open {#menu {z-index: 7;- transform: translateX(100vw);top: 0;+ transform: none;+ transition: none;// 兼容插件直接插入的图标.b3-menu__item > svg:not(.b3-menu__icon) {diff --git a/app/src/assets/template/mobile/index.tpl b/app/src/assets/template/mobile/index.tplindex 678b747bc7..0f448253eb 100644--- a/app/src/assets/template/mobile/index.tpl+++ b/app/src/assets/template/mobile/index.tpl@@ -25,10 +25,8 @@<div class="toolbar toolbar--border" data-prevent-swipe style="-webkit-user-select: none"><div class="toolbar__scroll"><svg data-type="sidebar-file-tab" class="toolbar__icon toolbar__icon--active"><use xlink:href="#iconFiles"></use></svg>- <svg data-type="sidebar-outline-tab" class="toolbar__icon"><use xlink:href="#iconOutline"></use></svg><svg data-type="sidebar-bookmark-tab" class="toolbar__icon"><use xlink:href="#iconBookmark"></use></svg><svg data-type="sidebar-tag-tab" class="toolbar__icon"><use xlink:href="#iconTag"></use></svg>- <svg data-type="sidebar-backlink-tab" class="toolbar__icon"><use xlink:href="#iconLink"></use></svg><svg data-type="sidebar-inbox-tab" class="toolbar__icon"><use xlink:href="#iconInbox"></use></svg><svg data-menu="true" data-type="sidebar-plugin-tab" class="toolbar__icon fn__none"><use xlink:href="#iconPlugin"></use></svg></div>@@ -36,15 +34,26 @@</div><div class="fn__flex-1 b3-list--mobile"><div class="fn__flex-column" data-type="sidebar-file"></div>- <div class="fn__flex-column fn__none" data-type="sidebar-outline"></div><div class="fn__flex-column fn__none" data-type="sidebar-bookmark"></div><div class="fn__flex-column fn__none" data-type="sidebar-tag"></div>- <div class="fn__flex-column fn__none" data-type="sidebar-backlink"></div><div class="fn__flex-column fn__none" data-type="sidebar-inbox"></div><div class="fn__flex-column fn__none" data-type="sidebar-plugin"></div></div></div>-<div id="menu" class="b3-menu b3-menu--fullscreen"></div>+<div id="sidebarRight" class="side-panel side-panel--right fn__flex-column">+ <div class="toolbar toolbar--border" data-prevent-swipe style="-webkit-user-select: none">+ <div class="toolbar__scroll">+ <svg data-type="sidebar-outline-tab" class="toolbar__icon toolbar__icon--active"><use xlink:href="#iconOutline"></use></svg>+ <svg data-type="sidebar-backlink-tab" class="toolbar__icon"><use xlink:href="#iconLink"></use></svg>+ </div>+ <svg class="toolbar__icon"><use xlink:href="#iconLeft"></use></svg>+ </div>+ <div class="fn__flex-1 b3-list--mobile">+ <div class="fn__flex-column" data-type="sidebar-outline"></div>+ <div class="fn__flex-column fn__none" data-type="sidebar-backlink"></div>+ </div>+</div>+<div id="menu" class="b3-menu b3-menu--fullscreen fn__none"></div><div id="model" class="side-panel side-panel--all fn__flex-column"><div class="toolbar toolbar--border"><svg class="toolbar__icon"><use xlink:href="#iconMenu"></use></svg>@@ -55,7 +64,7 @@</div><div id="modelMain" class="fn__flex-1"></div></div>-<div id="commonMenuScrim" class="b3-menu__scrim fn__none" aria-hidden="true"></div>+<div id="commonMenuScrim" class="b3-menu__scrim fn__none" data-prevent-swipe aria-hidden="true"></div><div id="commonMenu" class="b3-menu fn__none"><div class="b3-menu__title fn__none"><svg class="b3-menu__icon"><use xlink:href="#iconLeft"></use></svg>diff --git a/app/src/config/tabs/appearanceTab.ts b/app/src/config/tabs/appearanceTab.tsindex 0706126af3..3a7cc794fe 100644--- a/app/src/config/tabs/appearanceTab.ts+++ b/app/src/config/tabs/appearanceTab.ts@@ -37,6 +37,7 @@ import {import {showMessage} from "../../dialog/message";/// #if MOBILEimport {genMobileBottomBarSettingHTML, mountMobileBottomBarSetting} from "../../mobile/util/mobileBottomBar";+import {genMobileSidePanelSettingHTML, mountMobileSidePanelSetting} from "../../mobile/util/mobileSidePanelSetting";/// #endif/// #if !MOBILEimport {genEntryVisibilityHtml, mountEntryVisibility} from "../entryVisibility/ui";@@ -612,6 +613,18 @@ const registerAppearanceControlsGroup = (tab: SettingTabBuilder) => {html: genMobileBottomBarSettingHTML,afterMount: mountMobileBottomBarSetting,});+ group.slot({+ key: "mobileSidePanel",+ keywords: [+ window.siyuan.languages.mobile,+ window.siyuan.languages.leftRightLayout,+ window.siyuan.languages.fileTree,+ window.siyuan.languages.outline,+ window.siyuan.languages.reset,+ ],+ html: genMobileSidePanelSettingHTML,+ afterMount: mountMobileSidePanelSetting,+ });/// #endif/// #if !MOBILEgroup.slot({diff --git a/app/src/constants.ts b/app/src/constants.tsindex d64b458df3..d44612ee0f 100644--- a/app/src/constants.ts+++ b/app/src/constants.ts@@ -167,6 +167,7 @@ export abstract class Constants {public static readonly LOCAL_DOCINFO = "local-docinfo"; // only mobilepublic static readonly LOCAL_MOBILE_TABS = "local-mobile-tabs"; // only mobilepublic static readonly LOCAL_MOBILE_BOTTOM_BAR = "local-mobile-bottom-bar"; // only mobile+ public static readonly LOCAL_MOBILE_SIDE_PANEL = "local-mobile-side-panel"; // only mobilepublic static readonly LOCAL_DAILYNOTEID = "local-dailynoteid"; // stringpublic static readonly LOCAL_HISTORY = "local-history";public static readonly LOCAL_CODELANG = "local-codelang"; // stringdiff --git a/app/src/emoji/index.ts b/app/src/emoji/index.tsindex 392d234729..7c7eadefe9 100644--- a/app/src/emoji/index.ts+++ b/app/src/emoji/index.ts@@ -1582,13 +1582,13 @@ export const updateFileTreeEmoji = (unicode: string, id: string, icon = "iconFil/// #if MOBILEif (!isNotebookIcon) {emojiElement = document.querySelector(- `#sidebar [data-type="sidebar-file"] [data-node-id="${id}"] .b3-list-item__icon`+ `[data-type="sidebar-file"] [data-node-id="${id}"] .b3-list-item__icon`);} else {emojiElement = document.querySelector(- `#sidebar [data-type="sidebar-file"] ul[data-url="${id}"] > li[data-type="navigation-root"] .b3-list-item__icon`+ `[data-type="sidebar-file"] ul[data-url="${id}"] > li[data-type="navigation-root"] .b3-list-item__icon`) || document.querySelector(- `#sidebar [data-type="sidebar-file"] li[data-url="${id}"] .b3-list-item__icon`+ `[data-type="sidebar-file"] li[data-url="${id}"] .b3-list-item__icon`);}/// #elsediff --git a/app/src/mobile/dock/MobileBacklinks.ts b/app/src/mobile/dock/MobileBacklinks.tsindex 1137f3dd3f..93f51c6beb 100644--- a/app/src/mobile/dock/MobileBacklinks.ts+++ b/app/src/mobile/dock/MobileBacklinks.ts@@ -10,10 +10,11 @@ export class MobileBacklinks {private tree: Tree;private notebookId: string;private mTree: Tree;+ private updateId = 0;public beforeLen = 10;- constructor(app: App) {- this.element = document.querySelector('#sidebar [data-type="sidebar-backlink"]');+ constructor(app: App, element: HTMLElement) {+ this.element = element;this.element.innerHTML = `<div class="toolbar toolbar--border toolbar--dark"><div class="fn__space"></div><div class="toolbar__text">@@ -107,16 +108,28 @@ export class MobileBacklinks {}public update() {+ const editor = window.siyuan.mobile.editor?.protyle;+ const updateId = ++this.updateId;+ if (!editor) {+ this.tree.updateData([]);+ this.mTree.updateData([]);+ this.element.querySelectorAll(".listCount, .listMCount").forEach(item => item.classList.add("fn__none"));+ return;+ }+ const blockId = editor.block.id;const param: IObject = {- id: window.siyuan.mobile.editor.protyle.block.id,+ id: blockId,beforeLen: this.beforeLen,k: "",mk: "",};- if (isEncryptedBox(window.siyuan.mobile.editor.protyle.notebookId)) {- param.notebook = window.siyuan.mobile.editor.protyle.notebookId;+ if (isEncryptedBox(editor.notebookId)) {+ param.notebook = editor.notebookId;}fetchPost("/api/ref/getBacklink", param, response => {+ if (updateId !== this.updateId || window.siyuan.mobile.editor?.protyle.block.id !== blockId) {+ return;+ }this.notebookId = response.data.box;this.tree.updateData(response.data.backlinks);this.mTree.updateData(response.data.backmentions);diff --git a/app/src/mobile/dock/MobileBookmarks.ts b/app/src/mobile/dock/MobileBookmarks.tsindex 8afad9375d..3ba3fc6655 100644--- a/app/src/mobile/dock/MobileBookmarks.ts+++ b/app/src/mobile/dock/MobileBookmarks.ts@@ -16,8 +16,8 @@ export class MobileBookmarks {private updating = false;private updatePending = false;- constructor(app: App) {- this.element = document.querySelector('#sidebar [data-type="sidebar-bookmark"]');+ constructor(app: App, element: HTMLElement) {+ this.element = element;this.element.innerHTML = `<div class="toolbar toolbar--border toolbar--dark"><div class="fn__space"></div><div class="toolbar__text">diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.tsindex f47859f7db..d174815ea7 100644--- a/app/src/mobile/dock/MobileFiles.ts+++ b/app/src/mobile/dock/MobileFiles.ts@@ -66,14 +66,13 @@ export class MobileFiles extends Model {startTime: number;};- constructor(app: App) {+ constructor(app: App, filesElement: HTMLElement) {super({app});this.connect({id: genUUID(),type: "filetree",msgCallback: this.handleMsgCallback.bind(this)});- const filesElement = document.querySelector('#sidebar [data-type="sidebar-file"]');filesElement.innerHTML = `<div class="toolbar toolbar--border toolbar--dark"><div class="fn__space"></div><div class="toolbar__text">${window.siyuan.languages.fileTree}</div>diff --git a/app/src/mobile/dock/MobileOutline.ts b/app/src/mobile/dock/MobileOutline.tsindex d76fd48232..6ebf62e2dd 100644--- a/app/src/mobile/dock/MobileOutline.ts+++ b/app/src/mobile/dock/MobileOutline.ts@@ -45,7 +45,8 @@ export class MobileOutline extends Model {constructor(options: {app: App,blockId: string,- isPreview: boolean+ isPreview: boolean,+ element: HTMLElement}) {super({app: options.app});this.connect({@@ -56,7 +57,7 @@ export class MobileOutline extends Model {this.isPreview = options.isPreview;this.blockId = options.blockId;- this.element = document.querySelector('#sidebar [data-type="sidebar-outline"]');+ this.element = options.element;this.element.innerHTML = `<div class="toolbar toolbar--border toolbar--dark"><div class="fn__space"></div><div class="toolbar__text">diff --git a/app/src/mobile/dock/MobileTags.ts b/app/src/mobile/dock/MobileTags.tsindex 08d1600552..b10159377f 100644--- a/app/src/mobile/dock/MobileTags.ts+++ b/app/src/mobile/dock/MobileTags.ts@@ -19,8 +19,8 @@ export class MobileTags {private pendingUpdate: boolean;private filterLoadPending = false;- constructor(app: App) {- this.element = document.querySelector('#sidebar [data-type="sidebar-tag"]');+ constructor(app: App, element: HTMLElement) {+ this.element = element;this.element.innerHTML = `<div class="toolbar toolbar--border toolbar--dark"><div class="fn__space"></div><div class="toolbar__text">diff --git a/app/src/mobile/dock/util.ts b/app/src/mobile/dock/util.tsindex 939bde2a3e..849d071d47 100644--- a/app/src/mobile/dock/util.ts+++ b/app/src/mobile/dock/util.ts@@ -2,6 +2,16 @@ import {activeBlur} from "../util/keyboardToolbar";export const openDock = (type: string) => {activeBlur();- document.getElementById("sidebar").style.transform = "translateX(0px)";- document.querySelector("#sidebar .toolbar--border").dispatchEvent(new CustomEvent("click", {detail:type}));+ const tabElement = document.querySelector(`[data-type="sidebar-${type}-tab"]`);+ const sidePanelElement = tabElement?.closest(".side-panel") as HTMLElement;+ if (!sidePanelElement || tabElement.classList.contains("fn__none")) {+ return;+ }+ document.querySelectorAll<HTMLElement>("#sidebar, #sidebarRight").forEach(item => {+ if (item !== sidePanelElement) {+ item.style.transform = "";+ }+ });+ sidePanelElement.style.transform = "translateX(0px)";+ sidePanelElement.firstElementChild.dispatchEvent(new CustomEvent("click", {detail: type}));};diff --git a/app/src/mobile/menu/index.ts b/app/src/mobile/menu/index.tsindex 1544c7640f..6aed6b7627 100644--- a/app/src/mobile/menu/index.ts+++ b/app/src/mobile/menu/index.ts@@ -219,7 +219,10 @@ export const popMenu = () => {return;}activeBlur();- document.getElementById("menu").style.transform = "translateX(0px)";+ closePanel();+ const menuElement = document.getElementById("menu");+ menuElement.style.zIndex = (++window.siyuan.zIndex).toString();+ menuElement.classList.remove("fn__none");};export const initRightMenu = (app: App) => {diff --git a/app/src/mobile/util/MobileBackFoward.ts b/app/src/mobile/util/MobileBackFoward.tsindex f56cd61380..f89c9d2f83 100644--- a/app/src/mobile/util/MobileBackFoward.ts+++ b/app/src/mobile/util/MobileBackFoward.ts@@ -22,6 +22,7 @@ export const goForward = () => {export const goBack = () => {const editor = getCurrentEditor();+ const menuElement = document.getElementById("menu");if (window.siyuan.menus.menu.element.classList.contains("b3-menu--fullscreen") &&!window.siyuan.menus.menu.element.classList.contains("fn__none")) {window.siyuan.menus.menu.element.dispatchEvent(new CustomEvent("click", {detail: "back"}));@@ -46,8 +47,9 @@ export const goBack = () => {searchAssetsPanelElement.classList.add("fn__none");}return;- } else if (document.getElementById("menu").style.transform === "translateX(0px)" ||- document.getElementById("sidebar").style.transform === "translateX(0px)") {+ } else if ((menuElement && !menuElement.classList.contains("fn__none")) ||+ document.getElementById("sidebar")?.style.transform === "translateX(0px)" ||+ document.getElementById("sidebarRight")?.style.transform === "translateX(0px)") {closePanel();return;} else if (editor && !editor.protyle.toolbar.subElement.classList.contains("fn__none")) {diff --git a/app/src/mobile/util/closePanel.ts b/app/src/mobile/util/closePanel.tsindex b46046968b..b6fabcfa82 100644--- a/app/src/mobile/util/closePanel.ts+++ b/app/src/mobile/util/closePanel.ts@@ -2,16 +2,36 @@ import {activeBlur} from "./keyboardToolbar";import {Constants} from "../../constants";import {destroyModel} from "../menu/model";+let hidePanelMaskTimer = 0;++export const showPanelMask = () => {+ clearTimeout(hidePanelMaskTimer);+ hidePanelMaskTimer = 0;+ const maskElement = document.querySelector(".side-mask") as HTMLElement;+ maskElement?.classList.remove("fn__none");+ return maskElement;+};+export const closePanel = () => {destroyModel();- document.getElementById("menu").style.transform = "";- document.getElementById("sidebar").style.transform = "";- document.getElementById("model").style.transform = "";+ const menuElement = document.getElementById("menu");+ if (menuElement) {+ menuElement.classList.add("fn__none");+ menuElement.style.removeProperty("transform");+ menuElement.style.removeProperty("z-index");+ }+ document.getElementById("sidebar")?.style.removeProperty("transform");+ document.getElementById("sidebarRight")?.style.removeProperty("transform");+ document.getElementById("model")?.style.removeProperty("transform");const maskElement = document.querySelector(".side-mask") as HTMLElement;- setTimeout(() => {- maskElement.classList.add("fn__none");+ clearTimeout(hidePanelMaskTimer);+ hidePanelMaskTimer = window.setTimeout(() => {+ maskElement?.classList.add("fn__none");+ hidePanelMaskTimer = 0;}, Constants.TIMEOUT_TRANSITION);- maskElement.style.opacity = "";+ if (maskElement) {+ maskElement.style.opacity = "";+ }window.siyuan.menus.menu.remove();};diff --git a/app/src/mobile/util/initFramework.ts b/app/src/mobile/util/initFramework.tsindex 7629fc7056..bbcc7f68b6 100644--- a/app/src/mobile/util/initFramework.ts+++ b/app/src/mobile/util/initFramework.ts@@ -29,9 +29,62 @@ import {activateQueuedAVLocate, queueAVLocateRequest} from "../../protyle/renderimport {MobileTabs} from "../tabs/MobileTabs";import {initMobileBottomBar} from "./mobileBottomBar";import {initMobileBars} from "./mobileBars";+import {openDock} from "../dock/util";+import {+ MOBILE_SIDE_PANEL_CONFIG_CHANGE_EVENT,+ type IMobileSidePanelConfig,+ type MobileSidePanelDockId,+} from "./mobileSidePanelConfig";+import {getMobileSidePanelConfig} from "./mobileSidePanelSetting";let custom: MobileCustom;-const openDockMenu = (app: App) => {+const getDockTabElement = (type: MobileSidePanelDockId) => {+ return document.querySelector(`[data-type="sidebar-${type}-tab"]`) as HTMLElement;+};++const getDockContentElement = (type: MobileSidePanelDockId) => {+ return document.querySelector(`[data-type="sidebar-${type}"]`) as HTMLElement;+};++const getActiveDockId = (sidePanelElement: HTMLElement) => {+ const activeElement = sidePanelElement.firstElementChild.querySelector("[data-type$='-tab'].toolbar__icon--active");+ return activeElement?.getAttribute("data-type")?.replace("sidebar-", "").replace("-tab", "") as MobileSidePanelDockId;+};++export const renderMobileSidePanelLayout = (config: IMobileSidePanelConfig = getMobileSidePanelConfig()) => {+ const sidePanelElements = {+ left: document.getElementById("sidebar"),+ right: document.getElementById("sidebarRight"),+ } as const;+ const previousActive = {+ left: getActiveDockId(sidePanelElements.left),+ right: getActiveDockId(sidePanelElements.right),+ };+ const previouslyActive = new Set(Object.values(previousActive));++ (["left", "right"] as const).forEach(side => {+ const sidePanelElement = sidePanelElements[side];+ const toolbarScrollElement = sidePanelElement.firstElementChild.firstElementChild;+ const contentElement = sidePanelElement.lastElementChild;+ config[side].forEach(type => {+ toolbarScrollElement.append(getDockTabElement(type));+ contentElement.append(getDockContentElement(type));+ });++ const visibleDockIds = config[side].filter(type => !getDockTabElement(type).classList.contains("fn__none"));+ const activeDockId = visibleDockIds.includes(previousActive[side]) ? previousActive[side] :+ visibleDockIds.find(type => previouslyActive.has(type)) || visibleDockIds[0];+ config[side].forEach(type => {+ getDockTabElement(type).classList.toggle("toolbar__icon--active", type === activeDockId);+ getDockContentElement(type).classList.toggle("fn__none", type !== activeDockId);+ });+ if (!activeDockId) {+ sidePanelElement.style.transform = "";+ }+ });+};++const openDockMenu = (app: App, element: HTMLElement) => {const menu = new Menu(Constants.MENU_DOCK_MOBILE);if (menu.isOpen) {return;@@ -50,7 +103,7 @@ const openDockMenu = (app: App) => {custom.destroy();}}- custom = plugin.docks[dockId].mobileModel(document.querySelector('#sidebar [data-type="sidebar-plugin"]'));+ custom = plugin.docks[dockId].mobileModel(element);window.siyuan.mobile.docks[dockId] = custom;}}@@ -63,99 +116,120 @@ const openDockMenu = (app: App) => {}};-export const initFramework = async (app: App, isStart: boolean) => {- setInlineStyle();- const snippetReady = renderSnippet(Constants.TIMEOUT_SNIPPET_LOAD);- initKeyboardToolbar();- initMobileBottomBar(app);- initMobileBars();- const sidebarElement = document.getElementById("sidebar");+const updateDock = (app: App, type: MobileSidePanelDockId, element: HTMLElement, openPluginMenu = false) => {+ if (type === "outline") {+ if (!window.siyuan.mobile.docks.outline) {+ window.siyuan.mobile.docks.outline = new MobileOutline({+ app,+ blockId: window.siyuan.mobile.editor?.protyle.block.rootID,+ isPreview: window.siyuan.mobile.editor ?+ !window.siyuan.mobile.editor.protyle.preview.element.classList.contains("fn__none") : false,+ element,+ });+ } else {+ window.siyuan.mobile.docks.outline.reload();+ }+ } else if (type === "backlink") {+ if (!window.siyuan.mobile.docks.backlink) {+ window.siyuan.mobile.docks.backlink = new MobileBacklinks(app, element);+ } else {+ window.siyuan.mobile.docks.backlink.update();+ }+ } else if (type === "bookmark") {+ if (!window.siyuan.mobile.docks.bookmark) {+ window.siyuan.mobile.docks.bookmark = new MobileBookmarks(app, element);+ } else {+ window.siyuan.mobile.docks.bookmark.update();+ }+ } else if (type === "tag") {+ if (!window.siyuan.mobile.docks.tag) {+ window.siyuan.mobile.docks.tag = new MobileTags(app, element);+ } else {+ window.siyuan.mobile.docks.tag.update();+ }+ } else if (type === "inbox" && !window.siyuan.mobile.docks.inbox) {+ window.siyuan.mobile.docks.inbox = new Inbox(app, element);+ } else if (type === "plugin") {+ if (!custom || openPluginMenu) {+ if (!custom) {+ element.innerHTML = `<div class="b3-list--empty">${window.siyuan.languages.emptyContent}</div>`;+ }+ openDockMenu(app, element);+ } else if (custom.update) {+ custom.update();+ }+ }+};++const initSidePanelTabs = (app: App, sidePanelElement: HTMLElement) => {// 不能使用 getEventName,否则点击返回会展开右侧栏- const firstToolbarElement = sidebarElement.querySelector(".toolbar--border");- firstToolbarElement.addEventListener("click", (event: MouseEvent) => {+ const toolbarElement = sidePanelElement.firstElementChild as HTMLElement;+ toolbarElement.addEventListener("click", (event: MouseEvent) => {const target = event.target as HTMLElement;+ const isProgrammatic = typeof event.detail === "string";let svgElement: HTMLElement;- if (typeof event.detail === "string") {- svgElement = firstToolbarElement.querySelector(`svg[data-type="sidebar-${event.detail}-tab"]`) as HTMLElement;+ if (isProgrammatic) {+ svgElement = toolbarElement.querySelector(`svg[data-type="sidebar-${event.detail}-tab"]`) as HTMLElement;} else {svgElement = hasTopClosestByTag(target, "svg") as HTMLElement;}if (!svgElement) {return;}- const type = svgElement.getAttribute("data-type");- if (svgElement.classList.contains("toolbar__icon--active")) {- if (type === "sidebar-plugin-tab") {- openDockMenu(app);- }+ const tabType = svgElement.getAttribute("data-type");+ if (!tabType) {+ closePanel();return;}- if (!type) {- closePanel();+ const type = tabType.replace("sidebar-", "").replace("-tab", "") as MobileSidePanelDockId;+ if (svgElement.classList.contains("toolbar__icon--active")) {+ if (isProgrammatic) {+ updateDock(app, type, getDockContentElement(type));+ } else if (type === "plugin") {+ openDockMenu(app, getDockContentElement(type));+ }return;}- firstToolbarElement.querySelectorAll(".toolbar__icon").forEach(item => {- const itemType = item.getAttribute("data-type");- if (!itemType) {++ updateDock(app, type, getDockContentElement(type));+ toolbarElement.querySelectorAll(".toolbar__icon[data-type]").forEach(item => {+ const itemType = item.getAttribute("data-type").replace("sidebar-", "").replace("-tab", "") as MobileSidePanelDockId;+ item.classList.toggle("toolbar__icon--active", itemType === type);+ getDockContentElement(itemType).classList.toggle("fn__none", itemType !== type);+ });+ });+};++export const initFramework = async (app: App, isStart: boolean) => {+ setInlineStyle();+ const snippetReady = renderSnippet(Constants.TIMEOUT_SNIPPET_LOAD);+ initKeyboardToolbar();+ initMobileBottomBar(app);+ initMobileBars();+ const sidebarElement = document.getElementById("sidebar");+ const sidebarRightElement = document.getElementById("sidebarRight");+ renderMobileSidePanelLayout();+ initSidePanelTabs(app, sidebarElement);+ initSidePanelTabs(app, sidebarRightElement);+ window.addEventListener(MOBILE_SIDE_PANEL_CONFIG_CHANGE_EVENT, () => {+ renderMobileSidePanelLayout();+ [sidebarElement, sidebarRightElement].forEach(sidePanelElement => {+ if (sidePanelElement.style.transform !== "translateX(0px)") {return;}- const tabPanelElement = sidebarElement.lastElementChild.querySelector(`[data-type="${itemType.replace("-tab", "")}"]`);- if (itemType === type) {- if (type === "sidebar-outline-tab") {- if (!window.siyuan.mobile.docks.outline) {- window.siyuan.mobile.docks.outline = new MobileOutline({- app,- blockId: window.siyuan.mobile.editor?.protyle.block.rootID,- isPreview: window.siyuan.mobile.editor ? !window.siyuan.mobile.editor.protyle.preview.element.classList.contains("fn__none") : false- });- } else {- window.siyuan.mobile.docks.outline.reload();- }- } else if (type === "sidebar-backlink-tab") {- if (!window.siyuan.mobile.docks.backlink) {- window.siyuan.mobile.docks.backlink = new MobileBacklinks(app);- } else {- window.siyuan.mobile.docks.backlink.update();- }- } else if (type === "sidebar-bookmark-tab") {- if (!window.siyuan.mobile.docks.bookmark) {- window.siyuan.mobile.docks.bookmark = new MobileBookmarks(app);- } else {- window.siyuan.mobile.docks.bookmark.update();- }- } else if (type === "sidebar-tag-tab") {- if (!window.siyuan.mobile.docks.tag) {- window.siyuan.mobile.docks.tag = new MobileTags(app);- } else {- window.siyuan.mobile.docks.tag.update();- }- } else if (type === "sidebar-inbox-tab" && !window.siyuan.mobile.docks.inbox) {- window.siyuan.mobile.docks.inbox = new Inbox(app, document.querySelector('#sidebar [data-type="sidebar-inbox"]'));- } else if (type === "sidebar-plugin-tab") {- if (!custom) {- tabPanelElement.innerHTML = `<div class="b3-list--empty">${window.siyuan.languages.emptyContent}</div>`;- openDockMenu(app);- } else if (custom.update) {- custom.update();- }- }- svgElement.classList.add("toolbar__icon--active");- tabPanelElement.classList.remove("fn__none");- } else {- item.classList.remove("toolbar__icon--active");- tabPanelElement.classList.add("fn__none");+ const activeDockId = getActiveDockId(sidePanelElement);+ if (activeDockId) {+ updateDock(app, activeDockId, getDockContentElement(activeDockId));}});});await snippetReady;- window.siyuan.mobile.docks.file = new MobileFiles(app);+ window.siyuan.mobile.docks.file = new MobileFiles(app, getDockContentElement("file"));document.getElementById("toolbarFile").addEventListener("click", () => {if (getCurrentEditor()?.protyle.toolbar.isMultiSelectMode()) {return;}- activeBlur();- sidebarElement.style.transform = "translateX(0px)";- firstToolbarElement.dispatchEvent(new CustomEvent("click", {detail: "file"}));+ openDock("file");});// 用 touchstart 会导致键盘不收起document.getElementById("toolbarMore").addEventListener("click", () => {diff --git a/app/src/mobile/util/mobileBars.ts b/app/src/mobile/util/mobileBars.tsindex fd899c27cb..f231b74165 100644--- a/app/src/mobile/util/mobileBars.ts+++ b/app/src/mobile/util/mobileBars.ts@@ -4,7 +4,7 @@ import {reduceMobileBarsState,} from "./mobileBarsState";-const PANEL_IDS = ["sidebar", "menu", "model"];+const PANEL_IDS = ["sidebar", "sidebarRight", "menu", "model"];const MOBILE_BARS_TRANSITION_FALLBACK = 320;let barsState = createMobileBarsState();@@ -36,6 +36,9 @@ export const isMobileBlockSelecting = () => {const isPanelOpen = () => PANEL_IDS.some((id) => {const element = document.getElementById(id);+ if (id === "menu") {+ return Boolean(element && !element.classList.contains("fn__none"));+ }return Boolean(element?.style.transform);});@@ -257,7 +260,7 @@ export const initMobileBars = () => {panelObserver = new MutationObserver(updatePanelState);panelElements.forEach((element) => panelObserver.observe(element, {attributes: true,- attributeFilter: ["style"],+ attributeFilter: ["class", "style"],}));barsState = createMobileBarsState(scrollElement?.scrollTop);renderMobileBars();diff --git a/app/src/mobile/util/mobileSidePanelConfig.test.ts b/app/src/mobile/util/mobileSidePanelConfig.test.tsnew file mode 100644index 0000000000..69dbf55e94--- /dev/null+++ b/app/src/mobile/util/mobileSidePanelConfig.test.ts@@ -0,0 +1,157 @@+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {+ createDefaultMobileSidePanelConfig,+ DEFAULT_MOBILE_SIDE_PANEL_LEFT,+ DEFAULT_MOBILE_SIDE_PANEL_RIGHT,+ MOBILE_SIDE_PANEL_CONFIG_VERSION,+ MOBILE_SIDE_PANEL_DOCK_IDS,+ normalizeMobileSidePanelConfig,+ reduceMobileSidePanelConfig,+} from "./mobileSidePanelConfig";++const assertCompleteConfig = (config: ReturnType<typeof normalizeMobileSidePanelConfig>) => {+ const dockIds = [...config.left, ...config.right];+ assert.equal(config.version, MOBILE_SIDE_PANEL_CONFIG_VERSION);+ assert.equal(config.left.length > 0, true);+ assert.equal(config.right.length > 0, true);+ assert.equal(dockIds.length, MOBILE_SIDE_PANEL_DOCK_IDS.length);+ assert.equal(new Set(dockIds).size, MOBILE_SIDE_PANEL_DOCK_IDS.length);+ assert.deepEqual(new Set(dockIds), new Set(MOBILE_SIDE_PANEL_DOCK_IDS));+};++describe("mobile side panel config", () => {+ it("falls back to a fresh default for malformed storage values", () => {+ [+ null,+ false,+ "not json",+ [],+ {version: 2, left: ["file"], right: ["outline"]},+ {version: MOBILE_SIDE_PANEL_CONFIG_VERSION, left: "file", right: ["outline"]},+ {version: MOBILE_SIDE_PANEL_CONFIG_VERSION, left: ["file"], right: null},+ ].forEach((storedValue) => {+ const config = normalizeMobileSidePanelConfig(storedValue);+ assert.deepEqual(config, createDefaultMobileSidePanelConfig());+ assert.notEqual(config.left, DEFAULT_MOBILE_SIDE_PANEL_LEFT);+ assert.notEqual(config.right, DEFAULT_MOBILE_SIDE_PANEL_RIGHT);+ });+ });++ it("accepts a serialized config and preserves the configured order", () => {+ const config = normalizeMobileSidePanelConfig(JSON.stringify({+ version: MOBILE_SIDE_PANEL_CONFIG_VERSION,+ left: ["plugin", "file", "tag"],+ right: ["backlink", "outline", "bookmark", "inbox"],+ }));++ assert.deepEqual(config.left, ["plugin", "file", "tag"]);+ assert.deepEqual(config.right, ["backlink", "outline", "bookmark", "inbox"]);+ assertCompleteConfig(config);+ });++ it("drops unknown and duplicate IDs and fills missing IDs on their default side", () => {+ const config = normalizeMobileSidePanelConfig({+ version: MOBILE_SIDE_PANEL_CONFIG_VERSION,+ left: ["tag", "unknown", "tag", "outline"],+ right: ["outline", "file", "unknown"],+ });++ assert.deepEqual(config.left, ["tag", "outline", "bookmark", "inbox", "plugin"]);+ assert.deepEqual(config.right, ["file", "backlink"]);+ assertCompleteConfig(config);+ });++ it("repairs an empty left side with its default anchor", () => {+ const config = normalizeMobileSidePanelConfig({+ version: MOBILE_SIDE_PANEL_CONFIG_VERSION,+ left: [],+ right: [...MOBILE_SIDE_PANEL_DOCK_IDS],+ });++ assert.deepEqual(config.left, ["file"]);+ assert.deepEqual(config.right, ["outline", "bookmark", "tag", "backlink", "inbox", "plugin"]);+ assertCompleteConfig(config);+ });++ it("repairs an empty right side with its default anchor", () => {+ const config = normalizeMobileSidePanelConfig({+ version: MOBILE_SIDE_PANEL_CONFIG_VERSION,+ left: [...MOBILE_SIDE_PANEL_DOCK_IDS],+ right: [],+ });++ assert.deepEqual(config.left, ["file", "bookmark", "tag", "backlink", "inbox", "plugin"]);+ assert.deepEqual(config.right, ["outline"]);+ assertCompleteConfig(config);+ });++ it("moves a dock between sides at a requested position", () => {+ const config = reduceMobileSidePanelConfig(createDefaultMobileSidePanelConfig(), {+ type: "move",+ id: "bookmark",+ side: "right",+ index: 1,+ });++ assert.deepEqual(config.left, ["file", "tag", "inbox", "plugin"]);+ assert.deepEqual(config.right, ["outline", "bookmark", "backlink"]);+ assertCompleteConfig(config);+ });++ it("does not move the last dock away from a side", () => {+ const state = normalizeMobileSidePanelConfig({+ version: MOBILE_SIDE_PANEL_CONFIG_VERSION,+ left: ["file"],+ right: ["outline", "bookmark", "tag", "backlink", "inbox", "plugin"],+ });+ const config = reduceMobileSidePanelConfig(state, {+ type: "move",+ id: "file",+ side: "right",+ });++ assert.deepEqual(config, state);+ assertCompleteConfig(config);+ });++ it("reorders docks within a side", () => {+ const config = reduceMobileSidePanelConfig(createDefaultMobileSidePanelConfig(), {+ type: "reorder",+ side: "left",+ fromIndex: 4,+ toIndex: 1,+ });++ assert.deepEqual(config.left, ["file", "plugin", "bookmark", "tag", "inbox"]);+ assert.deepEqual(config.right, DEFAULT_MOBILE_SIDE_PANEL_RIGHT);+ assertCompleteConfig(config);+ });++ it("ignores invalid reorder indices", () => {+ const state = createDefaultMobileSidePanelConfig();+ const config = reduceMobileSidePanelConfig(state, {+ type: "reorder",+ side: "left",+ fromIndex: -1,+ toIndex: 2,+ });++ assert.deepEqual(config, state);+ assert.notEqual(config, state);+ });++ it("resets customized panels to a fresh default config", () => {+ const customized = normalizeMobileSidePanelConfig({+ version: MOBILE_SIDE_PANEL_CONFIG_VERSION,+ left: ["plugin", "file", "outline"],+ right: ["backlink", "bookmark", "tag", "inbox"],+ });+ const config = reduceMobileSidePanelConfig(customized, {type: "reset"});++ assert.deepEqual(config.left, DEFAULT_MOBILE_SIDE_PANEL_LEFT);+ assert.deepEqual(config.right, DEFAULT_MOBILE_SIDE_PANEL_RIGHT);+ assert.notEqual(config.left, DEFAULT_MOBILE_SIDE_PANEL_LEFT);+ assert.notEqual(config.right, DEFAULT_MOBILE_SIDE_PANEL_RIGHT);+ });+});diff --git a/app/src/mobile/util/mobileSidePanelConfig.ts b/app/src/mobile/util/mobileSidePanelConfig.tsnew file mode 100644index 0000000000..d9fa787cbf--- /dev/null+++ b/app/src/mobile/util/mobileSidePanelConfig.ts@@ -0,0 +1,184 @@+export const MOBILE_SIDE_PANEL_CONFIG_VERSION = 1 as const;+export const MOBILE_SIDE_PANEL_CONFIG_CHANGE_EVENT = "siyuan-mobile-side-panel-config-change";++export const MOBILE_SIDE_PANEL_DOCK_IDS = [+ "file",+ "outline",+ "bookmark",+ "tag",+ "backlink",+ "inbox",+ "plugin",+] as const;++export type MobileSidePanelDockId = typeof MOBILE_SIDE_PANEL_DOCK_IDS[number];+export type MobileSidePanelSide = "left" | "right";++export interface IMobileSidePanelConfig {+ version: typeof MOBILE_SIDE_PANEL_CONFIG_VERSION,+ left: MobileSidePanelDockId[],+ right: MobileSidePanelDockId[],+}++export type MobileSidePanelConfigEvent = {+ type: "move",+ id: MobileSidePanelDockId,+ side: MobileSidePanelSide,+ index?: number,+} | {+ type: "reorder",+ side: MobileSidePanelSide,+ fromIndex: number,+ toIndex: number,+} | {+ type: "reset",+};++export const DEFAULT_MOBILE_SIDE_PANEL_LEFT: readonly MobileSidePanelDockId[] = [+ "file",+ "bookmark",+ "tag",+ "inbox",+ "plugin",+];++export const DEFAULT_MOBILE_SIDE_PANEL_RIGHT: readonly MobileSidePanelDockId[] = [+ "outline",+ "backlink",+];++export const DEFAULT_MOBILE_SIDE_PANEL_CONFIG: Readonly<IMobileSidePanelConfig> = {+ version: MOBILE_SIDE_PANEL_CONFIG_VERSION,+ left: [...DEFAULT_MOBILE_SIDE_PANEL_LEFT],+ right: [...DEFAULT_MOBILE_SIDE_PANEL_RIGHT],+};++const mobileSidePanelDockIdSet = new Set<string>(MOBILE_SIDE_PANEL_DOCK_IDS);++export const isMobileSidePanelDockId = (value: unknown): value is MobileSidePanelDockId => {+ return typeof value === "string" && mobileSidePanelDockIdSet.has(value);+};++export const createDefaultMobileSidePanelConfig = (): IMobileSidePanelConfig => ({+ version: MOBILE_SIDE_PANEL_CONFIG_VERSION,+ left: [...DEFAULT_MOBILE_SIDE_PANEL_LEFT],+ right: [...DEFAULT_MOBILE_SIDE_PANEL_RIGHT],+});++const addValidDockIds = (+ target: MobileSidePanelDockId[],+ values: unknown[],+ used: Set<MobileSidePanelDockId>,+) => {+ values.forEach((value) => {+ if (isMobileSidePanelDockId(value) && !used.has(value)) {+ target.push(value);+ used.add(value);+ }+ });+};++const moveDefaultDockToEmptySide = (+ config: IMobileSidePanelConfig,+ side: MobileSidePanelSide,+) => {+ if (config[side].length > 0) {+ return;+ }+ const sourceSide: MobileSidePanelSide = side === "left" ? "right" : "left";+ const defaults = side === "left" ? DEFAULT_MOBILE_SIDE_PANEL_LEFT : DEFAULT_MOBILE_SIDE_PANEL_RIGHT;+ const dockId = defaults.find((id) => config[sourceSide].includes(id)) || config[sourceSide][0];+ config[sourceSide].splice(config[sourceSide].indexOf(dockId), 1);+ config[side].push(dockId);+};++export const normalizeMobileSidePanelConfig = (storedValue: unknown): IMobileSidePanelConfig => {+ let value = storedValue;+ if (typeof value === "string") {+ try {+ value = JSON.parse(value);+ } catch {+ return createDefaultMobileSidePanelConfig();+ }+ }+ if (!value || typeof value !== "object" || Array.isArray(value)) {+ return createDefaultMobileSidePanelConfig();+ }++ const record = value as Record<string, unknown>;+ if (record.version !== MOBILE_SIDE_PANEL_CONFIG_VERSION ||+ !Array.isArray(record.left) || !Array.isArray(record.right)) {+ return createDefaultMobileSidePanelConfig();+ }++ const config: IMobileSidePanelConfig = {+ version: MOBILE_SIDE_PANEL_CONFIG_VERSION,+ left: [],+ right: [],+ };+ const used = new Set<MobileSidePanelDockId>();+ addValidDockIds(config.left, record.left, used);+ addValidDockIds(config.right, record.right, used);+ DEFAULT_MOBILE_SIDE_PANEL_LEFT.forEach((id) => {+ if (!used.has(id)) {+ config.left.push(id);+ used.add(id);+ }+ });+ DEFAULT_MOBILE_SIDE_PANEL_RIGHT.forEach((id) => {+ if (!used.has(id)) {+ config.right.push(id);+ used.add(id);+ }+ });+ moveDefaultDockToEmptySide(config, "left");+ moveDefaultDockToEmptySide(config, "right");+ return config;+};++const clampIndex = (index: number | undefined, length: number) => {+ if (typeof index !== "number" || !Number.isInteger(index)) {+ return length;+ }+ return Math.max(0, Math.min(index, length));+};++export const reduceMobileSidePanelConfig = (+ state: IMobileSidePanelConfig,+ event: MobileSidePanelConfigEvent,+): IMobileSidePanelConfig => {+ if (event.type === "reset") {+ return createDefaultMobileSidePanelConfig();+ }++ const currentState = normalizeMobileSidePanelConfig(state);+ const config: IMobileSidePanelConfig = {+ version: MOBILE_SIDE_PANEL_CONFIG_VERSION,+ left: [...currentState.left],+ right: [...currentState.right],+ };+ if (event.type === "reorder") {+ const docks = config[event.side];+ if (!Number.isInteger(event.fromIndex) || !Number.isInteger(event.toIndex) ||+ event.fromIndex < 0 || event.fromIndex >= docks.length ||+ event.toIndex < 0 || event.toIndex >= docks.length || event.fromIndex === event.toIndex) {+ return currentState;+ }+ const [dockId] = docks.splice(event.fromIndex, 1);+ docks.splice(event.toIndex, 0, dockId);+ return config;+ }++ if (!isMobileSidePanelDockId(event.id)) {+ return currentState;+ }+ const sourceSide: MobileSidePanelSide = config.left.includes(event.id) ? "left" : "right";+ const source = config[sourceSide];+ const target = config[event.side];+ if (sourceSide !== event.side && source.length === 1) {+ return currentState;+ }+ source.splice(source.indexOf(event.id), 1);+ target.splice(clampIndex(event.index, target.length), 0, event.id);+ return config;+};diff --git a/app/src/mobile/util/mobileSidePanelSetting.ts b/app/src/mobile/util/mobileSidePanelSetting.tsnew file mode 100644index 0000000000..6cc0b5554c--- /dev/null+++ b/app/src/mobile/util/mobileSidePanelSetting.ts@@ -0,0 +1,157 @@+import {Constants} from "../../constants";+import {setStorageVal} from "../../protyle/util/compatibility";+import {escapeAttr, escapeHtml} from "../../util/escape";+import {+ MOBILE_SIDE_PANEL_CONFIG_CHANGE_EVENT,+ normalizeMobileSidePanelConfig,+ reduceMobileSidePanelConfig,+ type IMobileSidePanelConfig,+ type MobileSidePanelDockId,+ type MobileSidePanelSide,+} from "./mobileSidePanelConfig";++const getDockLabel = (dockId: MobileSidePanelDockId) => {+ switch (dockId) {+ case "file":+ return window.siyuan.languages.fileTree;+ case "outline":+ return window.siyuan.languages.outline;+ case "bookmark":+ return window.siyuan.languages.bookmark;+ case "tag":+ return window.siyuan.languages.tag;+ case "backlink":+ return window.siyuan.languages.backlinks;+ case "inbox":+ return window.siyuan.languages.inbox;+ case "plugin":+ return window.siyuan.languages.plugin;+ }+};++const getDockIcon = (dockId: MobileSidePanelDockId) => {+ switch (dockId) {+ case "file":+ return "iconFiles";+ case "outline":+ return "iconOutline";+ case "bookmark":+ return "iconBookmark";+ case "tag":+ return "iconTag";+ case "backlink":+ return "iconLink";+ case "inbox":+ return "iconInbox";+ case "plugin":+ return "iconPlugin";+ }+};++export const getMobileSidePanelConfig = (): IMobileSidePanelConfig => {+ return normalizeMobileSidePanelConfig(window.siyuan.storage[Constants.LOCAL_MOBILE_SIDE_PANEL]);+};++export const dispatchMobileSidePanelConfigChange = () => {+ window.dispatchEvent(new CustomEvent(MOBILE_SIDE_PANEL_CONFIG_CHANGE_EVENT));+};++export const saveMobileSidePanelConfig = (value: unknown): IMobileSidePanelConfig => {+ const config = normalizeMobileSidePanelConfig(value);+ window.siyuan.storage[Constants.LOCAL_MOBILE_SIDE_PANEL] = config;+ setStorageVal(Constants.LOCAL_MOBILE_SIDE_PANEL, config);+ dispatchMobileSidePanelConfigChange();+ return config;+};++const genDockItemHtml = (+ dockId: MobileSidePanelDockId,+ side: MobileSidePanelSide,+ index: number,+ length: number,+) => {+ const moveLabel = side === "left" ? window.siyuan.languages.moveToRight : window.siyuan.languages.moveToLeft;+ const moveIcon = side === "left" ? "iconRight" : "iconLeft";+ const disabled = window.siyuan.config.readonly || window.siyuan.isPublish;+ return `<div class="b3-list-item" data-dock-id="${dockId}">+ <svg class="b3-list-item__graphic"><use xlink:href="#${getDockIcon(dockId)}"></use></svg>+ <span class="b3-list-item__text">${escapeHtml(getDockLabel(dockId))}</span>+ <button class="block__icon block__icon--show ariaLabel" data-action="up" data-position="north" aria-label="${escapeAttr(window.siyuan.languages.up)}" type="button"${disabled || index === 0 ? " disabled" : ""}><svg><use xlink:href="#iconUp"></use></svg></button>+ <button class="block__icon block__icon--show ariaLabel" data-action="down" data-position="north" aria-label="${escapeAttr(window.siyuan.languages.down)}" type="button"${disabled || index === length - 1 ? " disabled" : ""}><svg><use xlink:href="#iconDown"></use></svg></button>+ <button class="block__icon block__icon--show ariaLabel" data-action="move" data-position="north" aria-label="${escapeAttr(moveLabel)}" type="button"${disabled || length === 1 ? " disabled" : ""}><svg><use xlink:href="#${moveIcon}"></use></svg></button>+</div>`;+};++const genSideHtml = (config: IMobileSidePanelConfig, side: MobileSidePanelSide) => {+ const label = side === "left" ? window.siyuan.languages.marginLeft : window.siyuan.languages.marginRight;+ return `<div class="b3-label__text">${escapeHtml(label)}</div>+<div class="b3-list b3-list--background" data-side="${side}">${config[side].map((dockId, index) =>+ genDockItemHtml(dockId, side, index, config[side].length)).join("")}</div>`;+};++const genMobileSidePanelListsHtml = (config: IMobileSidePanelConfig) => {+ return `${genSideHtml(config, "left")}<div class="fn__hr"></div>${genSideHtml(config, "right")}`;+};++export const genMobileSidePanelSettingHTML = () => {+ const disabled = window.siyuan.config.readonly || window.siyuan.isPublish ? " disabled" : "";+ return `<div id="mobileSidePanelSetting" class="b3-label config-item">+ <div class="fn__flex">+ <div class="fn__flex-1 config-item__main"><div class="config-name">${escapeHtml(window.siyuan.languages.leftRightLayout)}</div></div>+ <button class="b3-button b3-button--outline" data-action="reset" type="button"${disabled}>+ <svg><use xlink:href="#iconUndo"></use></svg>${escapeHtml(window.siyuan.languages.reset)}+ </button>+ </div>+ <div class="fn__hr"></div>+ <div data-type="side-panel-lists">${genMobileSidePanelListsHtml(getMobileSidePanelConfig())}</div>+</div>`;+};++export const mountMobileSidePanelSetting = (root: HTMLElement) => {+ const settingElement = root.querySelector<HTMLElement>("#mobileSidePanelSetting");+ const listsElement = settingElement?.querySelector<HTMLElement>('[data-type="side-panel-lists"]');+ if (!settingElement || !listsElement) {+ return;+ }+ let config = getMobileSidePanelConfig();+ const render = () => {+ listsElement.innerHTML = genMobileSidePanelListsHtml(config);+ };+ settingElement.addEventListener("click", (event) => {+ const actionElement = (event.target as HTMLElement).closest<HTMLButtonElement>("[data-action]");+ if (!actionElement || actionElement.disabled) {+ return;+ }+ if (actionElement.dataset.action === "reset") {+ config = saveMobileSidePanelConfig(reduceMobileSidePanelConfig(config, {type: "reset"}));+ render();+ return;+ }+ const itemElement = actionElement.closest<HTMLElement>("[data-dock-id]");+ const sideElement = actionElement.closest<HTMLElement>("[data-side]");+ const dockId = itemElement?.dataset.dockId as MobileSidePanelDockId;+ const side = sideElement?.dataset.side as MobileSidePanelSide;+ const index = config[side]?.indexOf(dockId);+ if (index === undefined || index < 0) {+ return;+ }+ if (actionElement.dataset.action === "move") {+ config = reduceMobileSidePanelConfig(config, {+ type: "move",+ id: dockId,+ side: side === "left" ? "right" : "left",+ });+ } else if (actionElement.dataset.action === "up" || actionElement.dataset.action === "down") {+ config = reduceMobileSidePanelConfig(config, {+ type: "reorder",+ side,+ fromIndex: index,+ toIndex: index + (actionElement.dataset.action === "up" ? -1 : 1),+ });+ } else {+ return;+ }+ config = saveMobileSidePanelConfig(config);+ render();+ });+};diff --git a/app/src/mobile/util/onMessage.ts b/app/src/mobile/util/onMessage.tsindex 516d853123..accf83e39f 100644--- a/app/src/mobile/util/onMessage.ts+++ b/app/src/mobile/util/onMessage.ts@@ -17,10 +17,15 @@ import {updateServerAddresses} from "../../config/tabs/accessRuntime";import {reloadInlineStyles} from "../../util/assets";import {renderMobileBottomBar} from "./mobileBottomBar";import {Constants} from "../../constants";+import {MOBILE_SIDE_PANEL_CONFIG_CHANGE_EVENT} from "./mobileSidePanelConfig";let statusTimeout: number;const statusElement = document.querySelector("#status") as HTMLElement;+const dispatchMobileSidePanelConfigChange = () => {+ window.dispatchEvent(new CustomEvent(MOBILE_SIDE_PANEL_CONFIG_CHANGE_EVENT));+};+export const onMessage = (app: App, data: IWebSocketData) => {if (data) {switch (data.cmd) {@@ -104,6 +109,9 @@ export const onMessage = (app: App, data: IWebSocketData) => {if (data.data.key === Constants.LOCAL_MOBILE_BOTTOM_BAR) {renderMobileBottomBar();}+ if (data.data.key === Constants.LOCAL_MOBILE_SIDE_PANEL) {+ dispatchMobileSidePanelConfigChange();+ }break;case "setLocalStorageVals":Object.keys(data.data.keyVals).forEach((k) => {@@ -112,12 +120,18 @@ export const onMessage = (app: App, data: IWebSocketData) => {if (Object.prototype.hasOwnProperty.call(data.data.keyVals, Constants.LOCAL_MOBILE_BOTTOM_BAR)) {renderMobileBottomBar();}+ if (Object.prototype.hasOwnProperty.call(data.data.keyVals, Constants.LOCAL_MOBILE_SIDE_PANEL)) {+ dispatchMobileSidePanelConfigChange();+ }break;case "removeLocalStorageVal":delete window.siyuan.storage[data.data.key];if (data.data.key === Constants.LOCAL_MOBILE_BOTTOM_BAR) {renderMobileBottomBar();}+ if (data.data.key === Constants.LOCAL_MOBILE_SIDE_PANEL) {+ dispatchMobileSidePanelConfigChange();+ }break;case "removeLocalStorageVals":data.data.keys.forEach((k: string) => {@@ -126,6 +140,9 @@ export const onMessage = (app: App, data: IWebSocketData) => {if (data.data.keys.includes(Constants.LOCAL_MOBILE_BOTTOM_BAR)) {renderMobileBottomBar();}+ if (data.data.keys.includes(Constants.LOCAL_MOBILE_SIDE_PANEL)) {+ dispatchMobileSidePanelConfigChange();+ }break;case"progress":progressLoading(data);diff --git a/app/src/mobile/util/touch.ts b/app/src/mobile/util/touch.tsindex a34c1dfd22..082c72ea54 100644--- a/app/src/mobile/util/touch.ts+++ b/app/src/mobile/util/touch.ts@@ -5,8 +5,7 @@ import {hasTopClosestByClassName,isInEmbedBlock,} from "../../protyle/util/hasClosest";-import {closeModel, closePanel} from "./closePanel";-import {popMenu} from "../menu";+import {closeModel, closePanel, showPanelMask} from "./closePanel";import {activeBlur, resetAndroidBoundedSelectionGesture} from "./keyboardToolbar";import {isChromeBrowser, isInAndroid, isInHarmony, isIPhone} from "../../protyle/util/compatibility";import {getRangeByPoint} from "../../protyle/util/selection";@@ -20,13 +19,22 @@ import {} from "./touchSelection";import {getTouchAxis, shouldStartLongPressMultiSelect} from "./touchGesture";import {getMobileBlockSelectionElement} from "./blockSelection";+import {+ getOpeningSidebar,+ getOpenSidebarReleaseAction,+ getSidebarClosingOffset,+ getSidebarOpeningOffset,+ type MobileSidebarSide,+ type MobileSwipeDirection,+ shouldDragOpenSidebar,+} from "./touchPanelGesture";let clientX: number;let clientY: number;let xDiff: number;let yDiff: number;let time: number;-let firstDirection: "toLeft" | "toRight";+let firstDirection: MobileSwipeDirection;let firstXY: "x" | "y";let lastClientX: number; // 和起始方向不一致时,记录最后一次的 clientXlet scrollBlock: boolean;@@ -36,12 +44,54 @@ let longPressTimer: number;let longPressBlockElement: HTMLElement;let longPressTouchRange: Range;-const popSide = (render = true) => {+const getSidebarElement = (side: MobileSidebarSide) => {+ return document.getElementById(side === "left" ? "sidebar" : "sidebarRight");+};++const getTargetSidebar = (target: HTMLElement): MobileSidebarSide | undefined => {+ if (hasClosestByAttribute(target, "id", "sidebar", true)) {+ return "left";+ }+ if (hasClosestByAttribute(target, "id", "sidebarRight", true)) {+ return "right";+ }+};++const getSidebarDock = (sidebarElement: HTMLElement | null) => {+ if (!sidebarElement) {+ return;+ }+ const toolbarElement = sidebarElement.querySelector(".toolbar--border");+ const tabElements = Array.from(toolbarElement?.querySelectorAll<HTMLElement>("[data-type]") || []);+ const activeElement = tabElements.find(item =>+ item.classList.contains("toolbar__icon--active") && !item.classList.contains("fn__none")) ||+ tabElements.find(item => !item.classList.contains("fn__none"));+ const type = activeElement?.dataset.type?.replace(/^sidebar-/, "").replace(/-tab$/, "");+ if (toolbarElement && type) {+ return {toolbarElement, type};+ }+};++const popSidebar = (side: MobileSidebarSide, render = true) => {+ activeBlur();+ const sidebarElement = getSidebarElement(side);+ if (!sidebarElement) {+ return;+ }+ let dock: ReturnType<typeof getSidebarDock>;if (render) {- document.getElementById("toolbarFile").dispatchEvent(new CustomEvent("click"));- } else {- activeBlur();- document.getElementById("sidebar").style.transform = "translateX(0px)";+ dock = getSidebarDock(sidebarElement);+ if (!dock) {+ sidebarElement.style.removeProperty("transform");+ closePanel();+ return;+ }+ }+ const otherSidebar = side === "left" ? "right" : "left";+ getSidebarElement(otherSidebar)?.style.removeProperty("transform");+ sidebarElement.style.transform = "translateX(0px)";+ if (render) {+ dock.toolbarElement.dispatchEvent(new CustomEvent("click", {detail: dock.type}));}};@@ -208,10 +258,11 @@ export const handleTouchEnd = (event: TouchEvent) => {return;}const isXScroll = Math.abs(xDiff) > Math.abs(yDiff);+ const reversing = typeof lastClientX !== "undefined";const modelElement = hasClosestByAttribute(target, "id", "model", true);if (modelElement) {// 面板内横向滚动内容(如数据快照操作按钮行)时不触发关闭面板- if (!scrollBlock && isXScroll && firstDirection === "toRight" && !lastClientX &&+ if (!scrollBlock && isXScroll && firstDirection === "toRight" && !reversing &&!hasClosestByClassName(target, "protyle-wysiwyg", true) &&// 划选文字时不触发关闭面板(getSelection().rangeCount === 0 || getSelection().toString() === "")) {@@ -222,8 +273,17 @@ export const handleTouchEnd = (event: TouchEvent) => {return;}+ if (hasClosestByAttribute(target, "id", "menu", true)) {+ return;+ }++ const targetSidebar = getTargetSidebar(target);if (scrollBlock) {- closePanel();+ if (targetSidebar) {+ popSidebar(targetSidebar, false);+ } else {+ closePanel();+ }return;}@@ -234,45 +294,11 @@ export const handleTouchEnd = (event: TouchEvent) => {scrollEnable = true;}- const menuElement = hasClosestByAttribute(target, "id", "menu");- if (menuElement) {- if (isXScroll) {- if (firstDirection === "toRight") {- if (lastClientX) {- popMenu();- } else {- closePanel();- }- } else {- if (lastClientX) {- closePanel();- } else {- popMenu();- }- }- } else {- popMenu();- }- return;- }- const sideElement = hasClosestByAttribute(target, "id", "sidebar");- if (sideElement) {- if (isXScroll) {- if (firstDirection === "toLeft") {- if (lastClientX) {- popSide(false);- } else {- closePanel();- }- } else {- if (lastClientX) {- closePanel();- } else {- popSide(false);- }- }+ if (targetSidebar) {+ if (isXScroll && getOpenSidebarReleaseAction(targetSidebar, firstDirection, reversing) === "close") {+ closePanel();} else {- popSide(false);+ popSidebar(targetSidebar, false);}return;}@@ -281,18 +307,10 @@ export const handleTouchEnd = (event: TouchEvent) => {return;}- if (xDiff > 0) {- if (lastClientX) {- closePanel();- } else {- popMenu();- }+ if (reversing) {+ closePanel();} else {- if (lastClientX) {- closePanel();- } else {- popSide();- }+ popSidebar(getOpeningSidebar(firstDirection));}};@@ -453,14 +471,15 @@ export const handleTouchMove = (event: TouchEvent) => {}firstDirection = xDiff > 0 ? "toLeft" : "toRight";if (firstXY === "x") {- if ((hasClosestByAttribute(target, "id", "menu") && firstDirection === "toLeft") ||- (hasClosestByAttribute(target, "id", "sidebar") && firstDirection === "toRight")) {+ const targetSidebar = getTargetSidebar(target);+ if (hasClosestByAttribute(target, "id", "menu", true) ||+ (targetSidebar && !shouldDragOpenSidebar(targetSidebar, firstDirection))) {firstXY = "y";yDiff = undefined;}}}- if (previousClientX) {+ if (typeof previousClientX !== "undefined") {if (firstDirection === "toRight") {if (previousClientX > event.touches[0].clientX) {lastClientX = event.touches[0].clientX;@@ -484,7 +503,10 @@ export const handleTouchMove = (event: TouchEvent) => {}return;}- if (sideMaskElement.classList.contains("fn__none")) {+ if (hasClosestByAttribute(target, "id", "menu", true)) {+ return;+ }+ if (sideMaskElement.classList.contains("fn__none") || getTargetSidebar(target)) {let scrollElement = hasClosestByAttribute(target, "data-type", "NodeCodeBlock");if (event.touches.length > 1 || (scrollElement && !scrollElement.classList.contains("code-block"))) {scrollBlock = true;@@ -526,42 +548,31 @@ export const handleTouchMove = (event: TouchEvent) => {}if (isFirstMove) {+ const openingSidebar = getOpeningSidebar(firstDirection);+ if (!getTargetSidebar(target) && !getSidebarDock(getSidebarElement(openingSidebar))) {+ scrollBlock = true;+ return;+ }sideMaskElement.style.zIndex = (++window.siyuan.zIndex).toString();- document.getElementById("sidebar").style.zIndex = (++window.siyuan.zIndex).toString();- document.getElementById("menu").style.zIndex = (++window.siyuan.zIndex).toString();+ const activeSidebar = getTargetSidebar(target) || openingSidebar;+ getSidebarElement(activeSidebar).style.zIndex = (++window.siyuan.zIndex).toString();isFirstMove = false;}const windowWidth = window.innerWidth;- const menuElement = hasClosestByAttribute(target, "id", "menu");- if (menuElement) {- if (xDiff < 0) {- menuElement.style.transform = `translateX(${-xDiff}px)`;- transformMask(-xDiff / windowWidth);- } else {- menuElement.style.transform = "translateX(0px)";- transformMask(0);- }- return;- }- const sideElement = hasClosestByAttribute(target, "id", "sidebar");- if (sideElement) {- if (xDiff > 0) {- sideElement.style.transform = `translateX(${-xDiff}px)`;- transformMask(xDiff / windowWidth);- } else {- sideElement.style.transform = "translateX(0px)";- transformMask(0);- }+ const targetSidebar = getTargetSidebar(target);+ if (targetSidebar) {+ const offset = getSidebarClosingOffset(targetSidebar, xDiff, windowWidth);+ getSidebarElement(targetSidebar).style.transform = `translateX(${offset}px)`;+ transformMask(Math.abs(offset) / windowWidth);return;}- if (firstDirection === "toRight") {- document.getElementById("sidebar").style.transform = `translateX(${Math.min(-xDiff - windowWidth, 0)}px)`;- transformMask((windowWidth + xDiff) / windowWidth);- } else {- document.getElementById("menu").style.transform = `translateX(${Math.max(windowWidth - xDiff, 0)}px)`;- transformMask((windowWidth - xDiff) / windowWidth);- }+ const openingSidebar = getOpeningSidebar(firstDirection);+ const otherSidebar = openingSidebar === "left" ? "right" : "left";+ getSidebarElement(otherSidebar)?.style.removeProperty("transform");+ const offset = getSidebarOpeningOffset(openingSidebar, xDiff, windowWidth);+ getSidebarElement(openingSidebar).style.transform = `translateX(${offset}px)`;+ transformMask(Math.abs(offset) / windowWidth);activeBlur();if (window.siyuan.mobile.editor) {window.siyuan.mobile.editor.protyle.contentElement.style.overflow = "hidden";@@ -569,7 +580,7 @@ export const handleTouchMove = (event: TouchEvent) => {}};-const transformMask = (opacity: number) => {- sideMaskElement.classList.remove("fn__none");- sideMaskElement.style.opacity = Math.min((1 - opacity), 0.68).toString();+const transformMask = (closedRatio: number) => {+ const maskElement = showPanelMask();+ maskElement.style.opacity = Math.min(Math.max(1 - closedRatio, 0), 0.68).toString();};diff --git a/app/src/mobile/util/touchPanelGesture.test.ts b/app/src/mobile/util/touchPanelGesture.test.tsnew file mode 100644index 0000000000..8cb2bc6437--- /dev/null+++ b/app/src/mobile/util/touchPanelGesture.test.ts@@ -0,0 +1,51 @@+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {+ getOpeningSidebar,+ getOpenSidebarReleaseAction,+ getSidebarClosingDirection,+ getSidebarClosingOffset,+ getSidebarOpeningOffset,+ shouldDragOpenSidebar,+} from "./touchPanelGesture";++describe("mobile sidebar touch gesture", () => {+ it("opens the sidebar opposite the finger movement", () => {+ assert.equal(getOpeningSidebar("toRight"), "left");+ assert.equal(getOpeningSidebar("toLeft"), "right");+ });++ it("only starts an open sidebar drag toward its outer edge", () => {+ assert.equal(getSidebarClosingDirection("left"), "toLeft");+ assert.equal(getSidebarClosingDirection("right"), "toRight");+ assert.equal(shouldDragOpenSidebar("left", "toLeft"), true);+ assert.equal(shouldDragOpenSidebar("left", "toRight"), false);+ assert.equal(shouldDragOpenSidebar("right", "toRight"), true);+ assert.equal(shouldDragOpenSidebar("right", "toLeft"), false);+ });++ it("reopens a sidebar when a closing drag is pulled back", () => {+ assert.equal(getOpenSidebarReleaseAction("left", "toLeft", false), "close");+ assert.equal(getOpenSidebarReleaseAction("left", "toLeft", true), "open");+ assert.equal(getOpenSidebarReleaseAction("right", "toRight", false), "close");+ assert.equal(getOpenSidebarReleaseAction("right", "toRight", true), "open");+ });++ it("mirrors and clamps closing transforms", () => {+ assert.equal(getSidebarClosingOffset("left", 120, 300), -120);+ assert.equal(getSidebarClosingOffset("left", -120, 300), 0);+ assert.equal(getSidebarClosingOffset("left", 400, 300), -300);+ assert.equal(getSidebarClosingOffset("right", -120, 300), 120);+ assert.equal(getSidebarClosingOffset("right", 120, 300), 0);+ assert.equal(getSidebarClosingOffset("right", -400, 300), 300);+ });++ it("mirrors and clamps opening transforms", () => {+ assert.equal(getSidebarOpeningOffset("left", -120, 300), -180);+ assert.equal(getSidebarOpeningOffset("left", 20, 300), -300);+ assert.equal(getSidebarOpeningOffset("left", -400, 300), 0);+ assert.equal(getSidebarOpeningOffset("right", 120, 300), 180);+ assert.equal(getSidebarOpeningOffset("right", -20, 300), 300);+ assert.equal(getSidebarOpeningOffset("right", 400, 300), 0);+ });+});diff --git a/app/src/mobile/util/touchPanelGesture.ts b/app/src/mobile/util/touchPanelGesture.tsnew file mode 100644index 0000000000..03c34c11b8--- /dev/null+++ b/app/src/mobile/util/touchPanelGesture.ts@@ -0,0 +1,38 @@+export type MobileSidebarSide = "left" | "right";+export type MobileSwipeDirection = "toLeft" | "toRight";+export type MobileSidebarReleaseAction = "close" | "open";++export const getOpeningSidebar = (direction: MobileSwipeDirection): MobileSidebarSide => {+ return direction === "toRight" ? "left" : "right";+};++export const getSidebarClosingDirection = (side: MobileSidebarSide): MobileSwipeDirection => {+ return side === "left" ? "toLeft" : "toRight";+};++export const shouldDragOpenSidebar = (side: MobileSidebarSide, direction: MobileSwipeDirection) => {+ return direction === getSidebarClosingDirection(side);+};++export const getOpenSidebarReleaseAction = (+ side: MobileSidebarSide,+ firstDirection: MobileSwipeDirection,+ reversing: boolean,+): MobileSidebarReleaseAction => {+ const closing = shouldDragOpenSidebar(side, firstDirection);+ return closing !== reversing ? "close" : "open";+};++export const getSidebarClosingOffset = (side: MobileSidebarSide, xDiff: number, width: number) => {+ if (side === "left") {+ return Math.max(Math.min(-xDiff, 0), -width);+ }+ return Math.min(Math.max(-xDiff, 0), width);+};++export const getSidebarOpeningOffset = (side: MobileSidebarSide, xDiff: number, width: number) => {+ if (side === "left") {+ return Math.min(Math.max(-xDiff - width, -width), 0);+ }+ return Math.max(Math.min(width - xDiff, width), 0);+};diff --git a/app/src/plugin/loader.ts b/app/src/plugin/loader.tsindex 36f6cfd353..8b5da4ff6b 100644--- a/app/src/plugin/loader.ts+++ b/app/src/plugin/loader.ts@@ -202,7 +202,7 @@ export const addPluginDock = (plugin: Plugin) => {/// #if MOBILE// 移动端只有存在插件 dock 时才显示插件入口图标if (Object.keys(plugin.docks).length > 0) {- document.querySelector('#sidebar [data-type="sidebar-plugin-tab"]')?.classList.remove("fn__none");+ document.querySelector("[data-type='sidebar-plugin-tab']")?.classList.remove("fn__none");}/// #elseif (isWindow() || !window.siyuan.layout.leftDock) {diff --git a/app/src/plugin/uninstall.ts b/app/src/plugin/uninstall.tsindex d29759cc76..f6c8506d09 100644--- a/app/src/plugin/uninstall.ts+++ b/app/src/plugin/uninstall.ts@@ -94,7 +94,20 @@ export const uninstall = (app: App, name: string, isReload: boolean) => {/// #if MOBILE// 移动端卸载插件后,若无任何插件 dock 则隐藏插件入口图标if (app.plugins.every(p => Object.keys(p.docks).length === 0)) {- document.querySelector('#sidebar [data-type="sidebar-plugin-tab"]')?.classList.add("fn__none");+ const pluginTabElement = document.querySelector("[data-type='sidebar-plugin-tab']");+ pluginTabElement?.classList.add("fn__none");+ if (pluginTabElement?.classList.contains("toolbar__icon--active")) {+ const fallbackTabElement = pluginTabElement.parentElement.querySelector<HTMLElement>(+ "[data-type$='-tab']:not(.fn__none)"+ );+ if (fallbackTabElement) {+ fallbackTabElement.dispatchEvent(new MouseEvent("click", {bubbles: true}));+ } else {+ pluginTabElement.classList.remove("toolbar__icon--active");+ document.querySelector("[data-type='sidebar-plugin']")?.classList.add("fn__none");+ (pluginTabElement.closest(".side-panel") as HTMLElement).style.transform = "";+ }+ }}/// #endif// rm iconsdiff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.tsindex 6465c76bd6..8675487978 100644--- a/app/src/protyle/util/compatibility.ts+++ b/app/src/protyle/util/compatibility.ts@@ -750,6 +750,11 @@ export const getLocalStorage = (cb: () => void) => {version: 1,actions: ["documents", "search", "newDoc", "tabs"],};+ defaultStorage[Constants.LOCAL_MOBILE_SIDE_PANEL] = {+ version: 1,+ left: ["file", "bookmark", "tag", "inbox", "plugin"],+ right: ["outline", "backlink"],+ };defaultStorage[Constants.LOCAL_IMAGES] = {file: "1f4c4",note: "1f5c3",@@ -782,7 +787,7 @@ export const getLocalStorage = (cb: () => void) => {[Constants.LOCAL_EXPORTIMG, Constants.LOCAL_SEARCHKEYS, Constants.LOCAL_PDFTHEME, Constants.LOCAL_BAZAAR,Constants.LOCAL_EXPORTWORD, Constants.LOCAL_EXPORTPDF, Constants.LOCAL_DOCINFO, Constants.LOCAL_MOBILE_TABS,- Constants.LOCAL_MOBILE_BOTTOM_BAR,+ Constants.LOCAL_MOBILE_BOTTOM_BAR, Constants.LOCAL_MOBILE_SIDE_PANEL,Constants.LOCAL_FONTSTYLES,Constants.LOCAL_SEARCHDATA, Constants.LOCAL_ZOOM, Constants.LOCAL_LAYOUTS,Constants.LOCAL_PLUGINTOPUNPIN, Constants.LOCAL_SEARCHASSET, Constants.LOCAL_FLASHCARD,