An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Keep embedded block gutters accessible across padding https://github.com/siyuan-note/siyuan/issues/20343
Daniel757bdc949fa5
diff --git a/app/src/boot/globalEvent/mousemove.test.ts b/app/src/boot/globalEvent/mousemove.test.tsnew file mode 100644index 0000000000..1533f5a045--- /dev/null+++ b/app/src/boot/globalEvent/mousemove.test.ts@@ -0,0 +1,88 @@+import * as assert from "node:assert/strict";+import {readFileSync} from "node:fs";+import {test} from "node:test";+import {runInNewContext} from "node:vm";+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";+import {isContainerGutterBridge} from "../../protyle/gutter/container";++const source = transpileModule(readFileSync("src/boot/globalEvent/mousemove.ts", "utf8"), {+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},+}).outputText;++const fixture = (location: "editor" | "popover" | "backlink") => {+ let hidden = false;+ const block = {getBoundingClientRect: () => ({left: 140, top: 100, bottom: 150})};+ const container = {+ dataset: {type: "NodeBlockQueryEmbed"},+ classList: {contains: () => false},+ contains: (element: unknown) => element === block,+ getBoundingClientRect: () => ({left: 130, top: 80, bottom: 150}),+ };+ const button = {dataset: {type: "NodeParagraph"}};+ const embedButton = {dataset: {type: "NodeBlockQueryEmbed"}};+ const root = {+ nodeType: 1,+ classList: {contains: (name: string) => name === "protyle-wysiwyg"},+ style: {paddingLeft: "130px"},+ getBoundingClientRect: () => ({left: 0}),+ contains: (element: unknown) => element === root,+ closest: (): null => null,+ };+ const calls: unknown[] = [];+ const protyle = {+ wysiwyg: {element: root},+ gutter: {+ element: {+ classList: {contains: () => hidden},+ getBoundingClientRect: () => ({left: 90, top: 110, bottom: 134}),+ querySelectorAll: () => [embedButton, button],+ },+ getNodeElement: (_protyle: unknown, item: unknown) => item === embedButton ? container : block,+ render: (_protyle: unknown, element: unknown) => calls.push(element),+ },+ };+ const modules: Record<string, unknown> = {+ "../../layout/getAll": {+ getAllModels: () => ({+ editor: location === "editor" ? [{editor: {protyle}}] : [],+ backlink: location === "backlink" ? [{editors: [{protyle}]}] : [],+ }),+ },+ "../../protyle/util/hasClosest": {hasClosestBlock: () => container},+ "../../protyle/gutter/container": {isContainerGutterBridge},+ };+ const api: {windowMouseMove: (event: MouseEvent) => void} = {windowMouseMove: () => {}};+ runInNewContext(source, {+ exports: api,+ require: (name: string) => modules[name] || {},+ window: {siyuan: {layout: {}, blockPanels: location === "popover" ? [{editors: [{protyle}]}] : []}},+ document: {+ body: {classList: {contains: () => false}},+ getElementById: (): null => null,+ elementFromPoint: () => container,+ },+ });+ return {+ calls, container,+ hide: () => { hidden = true; },+ move: (x: number, y = 120) => api.windowMouseMove({+ target: root, clientX: x, clientY: y, composedPath: () => [root],+ } as unknown as MouseEvent),+ };+};++test("editor padding preserves embedded content gutters in editors, popovers and backlinks", () => {+ for (const location of ["editor", "popover", "backlink"] as const) {+ const f = fixture(location);+ for (const x of [129, 110, 90]) {+ f.move(x);+ }+ assert.equal(f.calls.length, 0, location);+ f.move(89);+ f.move(110, 90);+ f.hide();+ f.move(110);+ assert.equal(f.calls.length, 3, location);+ assert.ok(f.calls.every(element => element === f.container));+ }+});diff --git a/app/src/boot/globalEvent/mousemove.ts b/app/src/boot/globalEvent/mousemove.tsindex 7f750be631..2be4202a27 100644--- a/app/src/boot/globalEvent/mousemove.ts+++ b/app/src/boot/globalEvent/mousemove.ts@@ -2,6 +2,7 @@ import {getAllEditor, getAllModels} from "../../layout/getAll";import {isWindow} from "../../util/functions";import {hasClosestBlock, hasClosestByClassName, hasClosestByTag} from "../../protyle/util/hasClosest";import {getColIndex} from "../../protyle/util/table";+import {isContainerGutterBridge} from "../../protyle/gutter/container";export const getTableResizeBounds = (tableElement: HTMLTableElement) => {const captionElement = tableElement.querySelector("caption");@@ -150,11 +151,18 @@ export const windowMouseMove = (event: MouseEvent) => {if (!targetBlockElement) {return;}+ const renderGutter = (protyle: IProtyle) => {+ // 鼠标经过编辑器留白移向子块块标时,使用实际坐标保留当前块标。+ if (!isContainerGutterBridge(protyle.gutter.element, targetBlockElement, eventPath0,+ event.clientX, event.clientY, button => protyle.gutter.getNodeElement(protyle, button))) {+ protyle.gutter.render(protyle, targetBlockElement, mouseElement);+ }+ };const allModels = getAllModels();let findNode = false;allModels.editor.find(item => {if (item.editor.protyle.wysiwyg.element === eventPath0) {- item.editor.protyle.gutter.render(item.editor.protyle, targetBlockElement, mouseElement);+ renderGutter(item.editor.protyle);findNode = true;return true;}@@ -163,7 +171,7 @@ export const windowMouseMove = (event: MouseEvent) => {window.siyuan.blockPanels.find(item => {item.editors.find(eItem => {if (eItem.protyle.wysiwyg.element.contains(eventPath0)) {- eItem.protyle.gutter.render(eItem.protyle, targetBlockElement, mouseElement);+ renderGutter(eItem.protyle);findNode = true;return true;}@@ -177,7 +185,7 @@ export const windowMouseMove = (event: MouseEvent) => {allModels.backlink.find(item => {item.editors.find(eItem => {if (eItem.protyle.wysiwyg.element === eventPath0) {- eItem.protyle.gutter.render(eItem.protyle, targetBlockElement, mouseElement);+ renderGutter(eItem.protyle);findNode = true;return true;}diff --git a/app/src/protyle/gutter/container.test.ts b/app/src/protyle/gutter/container.test.tsindex 0b2d0cc76d..ebd5809bf7 100644--- a/app/src/protyle/gutter/container.test.ts+++ b/app/src/protyle/gutter/container.test.ts@@ -11,15 +11,16 @@ const setup = (type = "NodeBlockquote") => {contains: (element: Element) => element === block,} as unknown as HTMLElement;const button = {dataset: {type: "NodeParagraph"}} as unknown as HTMLElement;+ const buttons = [button];const gutter = {classList: {contains: () => false},getBoundingClientRect: () => ({left: 100, top: 110, bottom: 134}),- querySelectorAll: () => [button],+ querySelectorAll: () => buttons,} 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};+ return {block, container, button, buttons, gutter, target, hit};};describe("isContainerGutterBridge", () => {@@ -29,6 +30,21 @@ describe("isContainerGutterBridge", () => {});});+ it("preserves embedded content gutters across embed and editor padding", () => {+ const {container, buttons, gutter, target, block} = setup("NodeBlockQueryEmbed");+ const embedButton = {dataset: {type: "NodeBlockQueryEmbed"}} as unknown as HTMLElement;+ buttons.unshift(embedButton);+ const getBlock = (item: HTMLElement) => item === embedButton ? container : block;+ [139, 130, 110, 100].forEach(x => {+ assert.equal(isContainerGutterBridge(gutter, container, target, x, 120, getBlock), true);+ });+ [[99, 120], [141, 120], [130, 90], [130, 151]].forEach(([x, y]) => {+ assert.equal(isContainerGutterBridge(gutter, container, target, x, y, getBlock), false);+ });+ assert.equal(isContainerGutterBridge(gutter, container, target, 130, 120, () => container), false);+ assert.equal(isContainerGutterBridge(gutter, container, target, 130, 120, () => ({} as Element)), false);+ });+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]) => {diff --git a/app/src/protyle/gutter/container.ts b/app/src/protyle/gutter/container.tsindex fb71de03fc..396e4a6020 100644--- a/app/src/protyle/gutter/container.ts+++ b/app/src/protyle/gutter/container.ts@@ -2,7 +2,7 @@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) ||+ !["NodeBlockquote", "NodeCallout", "NodeSuperBlock", "NodeBlockQueryEmbed"].includes(container.dataset.type) ||target.closest(".callout-info")) {return false;}