Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Restore mobile backlink block opening and gutter positioning https://github.com/siyuan-note/siyuan/issues/20596

Daniel00498ff72a4a
diff --git a/app/src/protyle/gutter/backlinkMenu.test.ts b/app/src/protyle/gutter/backlinkMenu.test.ts
new file mode 100644
index 0000000000..85b2e80b32
--- /dev/null
+++ b/app/src/protyle/gutter/backlinkMenu.test.ts
@@ -0,0 +1,76 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {join} from "node:path";
+import test from "node:test";
+import {createSourceFile, forEachChild, isIfStatement, ScriptTarget, transpileModule} from "typescript";
+
+const preprocess = require("ifdef-loader/preprocessor").parse;
+const source = createSourceFile("index.ts", readFileSync(join(__dirname, "index.ts"), "utf8"), ScriptTarget.Latest, true);
+let menuSource: string;
+const collect = (node: import("typescript").Node) => {
+ if (isIfStatement(node) && node.expression.getText(source) === "!protyle.options.backlinkData") {
+ menuSource = node.getText(source);
+ }
+ forEachChild(node, collect);
+};
+collect(source);
+assert.ok(menuSource);
+
+const fixture = (mobile: boolean, backlink = true, folded = false) => {
+ const items: IMenu[] = [];
+ const opened: unknown[][] = [];
+ const protyle = {app: {}, notebookId: "source-notebook", options: {backlinkData: backlink}};
+ const action = folded ? ["highlight", "all"] : ["highlight", "context", "root-scroll"];
+ const dependencies = {
+ protyle,
+ id: "source-block",
+ window: {siyuan: {menus: {menu: {append: (item: IMenu) => items.push(item)}},
+ config: {keymap: {general: {enter: {custom: "enter"}, enterBack: {custom: "back"}}}},
+ languages: {openBy: "Open", enter: "Enter", enterBack: "Back", click: "Click"}}},
+ MenuItem: class {public element: IMenu; constructor(options: IMenu) {this.element = options;}},
+ updateHotkeyTip: (key: string) => key,
+ updateHotkeyAfterTip: (key: string) => key,
+ checkFold: (id: string, callback: (zoomIn: boolean, action: string[]) => void) => {
+ assert.equal(id, "source-block");
+ callback(folded, action);
+ },
+ openMobileFileById: (...args: unknown[]) => opened.push(args),
+ openFileById: (...args: unknown[]) => opened.push(args),
+ zoomOut: () => {},
+ enterBack: () => {},
+ };
+ const js = transpileModule(preprocess(menuSource, {MOBILE: mobile}, false, true), {
+ compilerOptions: {target: ScriptTarget.ES2021},
+ }).outputText;
+ new Function(...Object.keys(dependencies), js)(...Object.values(dependencies));
+ return {items, opened, protyle, action};
+};
+
+test("mobile backlink menus open the source block with its notebook and fold actions", () => {
+ for (const folded of [false, true]) {
+ const {items, opened, protyle, action} = fixture(true, true, folded);
+ assert.equal(items.length, 1);
+ assert.equal(items[0].id, "enter");
+ assert.equal(items[0].label, "Open");
+ assert.equal(items[0].accelerator, undefined);
+ items[0].click(undefined, undefined);
+ assert.deepEqual(opened, [[protyle.app, "source-block", action, undefined, protyle.notebookId]]);
+ }
+});
+
+test("desktop backlink menus retain their open action and shortcut", () => {
+ for (const folded of [false, true]) {
+ const {items, opened, protyle, action} = fixture(false, true, folded);
+ assert.equal(items.length, 1);
+ assert.ok(items[0].accelerator);
+ items[0].click(undefined, undefined);
+ assert.deepEqual(opened, [[{app: protyle.app, id: "source-block", action, zoomIn: folded}]]);
+ }
+});
+
+test("ordinary block menus retain enter and back actions on both platforms", () => {
+ for (const mobile of [false, true]) {
+ const {items} = fixture(mobile, false);
+ assert.deepEqual(items.map(item => item.id), ["enter", "enterBack"]);
+ }
+});
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index cc38d8cb62..e6944d7e27 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -108,6 +108,9 @@ import {
isGutterInsertStateMatched
} from "./multiSelect";
import {addEditorToDatabase} from "../render/av/addToDatabase";
+/// #if MOBILE
+import {openMobileFileById} from "../../mobile/editor";
+/// #endif
/// #if !MOBILE
import {openFileById} from "../../editor/util";
import {getDockByType} from "../../layout/tabUtil";
@@ -132,6 +135,7 @@ import {
getBacklinkGutterContentTop,
getContainerGutterSpace,
getFixedGutterPosition,
+ getGutterFixedContainerRect,
getGutterMarginHeight,
getListGutterAnchorLeft
} from "./layout";
@@ -200,14 +204,6 @@ const BLOCK_TYPE_LANG_KEYS: { [key: string]: string } = {
NodeCustomBlock: "custom",
};
-const getGutterFixedContainerRect = (protyle: IProtyle) => {
- const floatLayoutElement = protyle.element.closest(".layout--float");
- if (!floatLayoutElement || getComputedStyle(floatLayoutElement).transform === "none") {
- return;
- }
- return floatLayoutElement.getBoundingClientRect();
-};
-
// 根据块 data-type 返回本地化的类型名,用于块标拖拽提示「拖拽 ${x} 移动位置」
const getBlockTypeName = (type: string) => {
const langKey = BLOCK_TYPE_LANG_KEYS[type];
@@ -731,7 +727,7 @@ export class Gutter {
if (protyle.disabled || !lineBefore || !lineAfter || !plusBefore || !plusAfter) {
return;
}
- const fixedContainerRect = getGutterFixedContainerRect(protyle);
+ const fixedContainerRect = getGutterFixedContainerRect(protyle.element);
// 多选或跨块文本选择时不显示插入框线与加号 https://github.com/siyuan-note/siyuan/issues/18592
if (protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select").length > 1 ||
getCrossBlockTextRange(protyle)) {
@@ -2800,24 +2796,28 @@ export class Gutter {
}
}).element);
} else {
- /// #if !MOBILE
window.siyuan.menus.menu.append(new MenuItem({
id: "enter",
icon: "iconEnter",
+ /// #if !MOBILE
accelerator: `${updateHotkeyTip(window.siyuan.config.keymap.general.enter.custom)}/${updateHotkeyTip("⌘" + window.siyuan.languages.click)}`,
+ /// #endif
label: window.siyuan.languages.openBy,
click: () => {
checkFold(id, (zoomIn, action) => {
+ /// #if MOBILE
+ openMobileFileById(protyle.app, id, action, undefined, protyle.notebookId);
+ /// #else
openFileById({
app: protyle.app,
id,
action,
zoomIn
});
+ /// #endif
});
}
}).element);
- /// #endif
}
if (allowStructuralMutation && nodeElement.getAttribute("data-type") !== "NodeTabItem") {
window.siyuan.menus.menu.append(new MenuItem({
@@ -4044,7 +4044,7 @@ data-type="fold"${viewOccurrenceID ? ` data-view-occurrence-id="${encodeURICompo
}
}
const foldElement = hasClosestByAttribute(element.parentElement, "fold", "1") as HTMLElement;
- const fixedContainerRect = getGutterFixedContainerRect(protyle);
+ const fixedContainerRect = getGutterFixedContainerRect(protyle.element);
const top = Math.max(rect.top + marginHeight, contentTop, foldElement ? foldElement.getBoundingClientRect().top : 0);
this.element.style.top = `${getFixedGutterPosition(top, fixedContainerRect?.top)}px`;
// 压缩模式需加 2,否则和折叠标题无法对齐
diff --git a/app/src/protyle/gutter/layout.ts b/app/src/protyle/gutter/layout.ts
index 1e908047e6..02fd1a4eda 100644
--- a/app/src/protyle/gutter/layout.ts
+++ b/app/src/protyle/gutter/layout.ts
@@ -15,6 +15,14 @@ export const getFixedGutterPosition = (viewportPosition: number, fixedContainerP
return viewportPosition - (fixedContainerPosition ?? 0);
};
+// 位移变换会让浮动面板和移动端抽屉成为固定定位块标的坐标参照。
+export const getGutterFixedContainerRect = (element: HTMLElement) => {
+ const container = element.closest(".layout--float, .mobile-bottom-sheet");
+ if (container && getComputedStyle(container).transform !== "none") {
+ return container.getBoundingClientRect();
+ }
+};
+
export const getContainerGutterSpace = (type: string, usesContainerRect: boolean) =>
!usesContainerRect && ["NodeBlockquote", "NodeCallout"].includes(type) ? 10 : 0;
diff --git a/app/tests/mobileBacklinks.test.js b/app/tests/mobileBacklinks.test.js
index 276d868901..e7e807200d 100644
--- a/app/tests/mobileBacklinks.test.js
+++ b/app/tests/mobileBacklinks.test.js
@@ -12,7 +12,7 @@ const sources = () => {
"layout/dock/backlinkReadingAnchor", "layout/dock/backlinkSourceFilter", "mobile/util/secondaryEditors",
"mobile/util/backlinkPanels", "mobile/util/openBacklinks", "mobile/util/MobileEditorDialog", "mobile/util/bindBottomSheetDrag", "mobile/util/bindBottomSheetDialog", "menus/sheetOpen", "protyle/util/transactionQueue",
"util/escape", "util/zIndex", "dialog/index", "layout/dock/panelSearch", "protyle/wysiwyg/backlinkTypeFold",
- "layout/dock/backlinkMentionCache", "layout/dock/bottomBacklinkScroll"]) {
+ "layout/dock/backlinkMentionCache", "layout/dock/bottomBacklinkScroll", "protyle/gutter/layout"]) {
modules[name] = ts.transpileModule(preprocess(
readFileSync(path.join(__dirname, "../src", name + ".ts"), "utf8"),
{MOBILE: true, BROWSER: true}, false, true), {
@@ -106,6 +106,29 @@ const runCases = async (sources) => {
const style = document.createElement("style");
style.textContent = ".fn__none{display:none!important}.protyle{min-height:32px}.panel{height:400px}";
document.head.appendChild(style);
+ const {getGutterFixedContainerRect, getFixedGutterPosition} = load("protyle/gutter/layout");
+ const container = document.createElement("div");
+ container.style.cssText = "position:fixed;top:180px;left:20px;width:300px;height:240px;transform:translateY(0)";
+ container.innerHTML = '<div class="protyle"><button style="position:fixed;width:24px;height:24px">block</button></div>';
+ document.body.appendChild(container);
+ const gutterEditor = container.firstElementChild;
+ const gutter = gutterEditor.firstElementChild;
+ for (const className of ["mobile-bottom-sheet", "layout--float"]) {
+ container.className = className;
+ for (const translate of [0, 30]) {
+ container.style.transform = `translateY(${translate}px)`;
+ const origin = getGutterFixedContainerRect(gutterEditor);
+ gutter.style.top = `${getFixedGutterPosition(260, origin?.top)}px`;
+ gutter.style.left = `${getFixedGutterPosition(40, origin?.left)}px`;
+ assert.equal(gutter.getBoundingClientRect().top, 260, `${className} must retain the visible gutter position`);
+ assert.equal(gutter.getBoundingClientRect().left, 40);
+ }
+ }
+ container.style.transform = "none";
+ assert.equal(getGutterFixedContainerRect(gutterEditor), undefined);
+ container.className = "";
+ assert.equal(getGutterFixedContainerRect(gutterEditor), undefined);
+ container.remove();
const key = {custom: ""};
window.siyuan = {config: {editor: {backlinkSort: 0, backmentionSort: 0, backlinkExpandCount: 0,
backmentionExpandCount: 0}, keymap: {editor: {general: {expand: key, collapse: key}}, general: {closeTab: key}}},