Skip to content

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

:bug: Preserve block gutter access across blockquote padding https://github.com/siyuan-note/siyuan/issues/19232

Daniele07dc5b789cc
diff --git a/app/src/protyle/gutter/container.test.ts b/app/src/protyle/gutter/container.test.ts
new file mode 100644
index 0000000000..0b2d0cc76d
--- /dev/null
+++ b/app/src/protyle/gutter/container.test.ts
@@ -0,0 +1,61 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {isContainerGutterBridge} from "./container";
+
+const setup = (type = "NodeBlockquote") => {
+ const block = {
+ getBoundingClientRect: () => ({left: 140, top: 100, bottom: 150}),
+ } as Element;
+ const container = {
+ dataset: {type},
+ contains: (element: Element) => element === block,
+ } as unknown as HTMLElement;
+ const button = {dataset: {type: "NodeParagraph"}} as unknown as HTMLElement;
+ const gutter = {
+ classList: {contains: () => false},
+ getBoundingClientRect: () => ({left: 100, top: 110, bottom: 134}),
+ querySelectorAll: () => [button],
+ } as unknown as HTMLElement;
+ const target = {closest: (): HTMLElement => null} as unknown as HTMLElement;
+ const hit = (x = 130, y = 120, currentBlock = block) =>
+ isContainerGutterBridge(gutter, container, target, x, y, () => currentBlock);
+ return {block, container, button, gutter, target, hit};
+};
+
+describe("isContainerGutterBridge", () => {
+ it("preserves a child gutter while crossing nested container padding", () => {
+ ["NodeBlockquote", "NodeCallout", "NodeSuperBlock"].forEach(type => {
+ assert.equal(setup(type).hit(), true);
+ });
+ });
+
+ it("does not retain a gutter outside the path to its block", () => {
+ const {hit} = setup();
+ [[99, 120], [141, 120], [130, 99], [130, 151]].forEach(([x, y]) => {
+ assert.equal(hit(x, y), false);
+ });
+ });
+
+ it("allows switching to unrelated blocks and the container itself", () => {
+ const {hit, container} = setup();
+ assert.equal(hit(130, 120, {} as Element), false);
+ assert.equal(hit(130, 120, container), false);
+ assert.equal(hit(130, 120, null), false);
+ assert.equal(setup("NodeParagraph").hit(), false);
+ });
+
+ it("does not restore hidden gutters or use folding controls as anchors", () => {
+ const hidden = setup();
+ hidden.gutter.classList.contains = () => true;
+ assert.equal(hidden.hit(), false);
+ const fold = setup();
+ fold.button.dataset.type = "fold";
+ assert.equal(fold.hit(), false);
+ });
+
+ it("keeps callout titles directly accessible", () => {
+ const {target, hit} = setup("NodeCallout");
+ target.closest = () => ({} as HTMLElement);
+ assert.equal(hit(), false);
+ });
+});
diff --git a/app/src/protyle/gutter/container.ts b/app/src/protyle/gutter/container.ts
new file mode 100644
index 0000000000..fb71de03fc
--- /dev/null
+++ b/app/src/protyle/gutter/container.ts
@@ -0,0 +1,22 @@
+// 鼠标经过祖先容器留白移向子块块标时,保留当前块标及其选区。
+export const isContainerGutterBridge = (gutter: HTMLElement, container: HTMLElement, target: HTMLElement,
+ x: number, y: number, getBlock: (button: HTMLElement) => Element) => {
+ if (gutter.classList.contains("fn__none") ||
+ !["NodeBlockquote", "NodeCallout", "NodeSuperBlock"].includes(container.dataset.type) ||
+ target.closest(".callout-info")) {
+ return false;
+ }
+ const gutterRect = gutter.getBoundingClientRect();
+ return Array.from(gutter.querySelectorAll<HTMLElement>("button[data-node-id]")).some(button => {
+ if (button.dataset.type === "fold") {
+ return false;
+ }
+ const block = getBlock(button);
+ if (!block || block === container || !container.contains(block)) {
+ return false;
+ }
+ const blockRect = block.getBoundingClientRect();
+ return x >= gutterRect.left && x <= blockRect.left &&
+ y >= Math.min(blockRect.top, gutterRect.top) && y <= Math.max(blockRect.bottom, gutterRect.bottom);
+ });
+};
diff --git a/app/src/protyle/ui/initUI.ts b/app/src/protyle/ui/initUI.ts
index 13f1b53939..4d58f317a0 100644
--- a/app/src/protyle/ui/initUI.ts
+++ b/app/src/protyle/ui/initUI.ts
@@ -1,5 +1,6 @@
import {setEditMode} from "../util/setEditMode";
import {isTabGutterBridge} from "../gutter/tabs";
+import {isContainerGutterBridge} from "../gutter/container";
import {scrollEvent} from "../scroll/event";
import {isMobile} from "../../util/functions";
import {Constants} from "../../constants";
@@ -248,6 +249,10 @@ export const initUI = (protyle: IProtyle) => {
hideElements(["gutter"], protyle);
return;
}
+ if (isContainerGutterBridge(protyle.gutter.element, nodeElement, event.target,
+ event.clientX, event.clientY, button => protyle.gutter.getNodeElement(protyle, button))) {
+ return;
+ }
if (nodeElement && (nodeElement.classList.contains("list") || nodeElement.classList.contains("li"))) {
// 光标在列表下部应显示右侧的元素,而不是列表本身。放在 windowEvent 中的 mousemove 下处理
return;