Skip to content

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

:bug: Keep embed block gutters accessible https://github.com/siyuan-note/siyuan/issues/18800

Daniela4d56565f61c
diff --git a/app/src/mobile/util/touch.ts b/app/src/mobile/util/touch.ts
index c5a618a758..377c547455 100644
--- a/app/src/mobile/util/touch.ts
+++ b/app/src/mobile/util/touch.ts
@@ -12,7 +12,7 @@ import {isChromeBrowser, isInAndroid, isInHarmony, isIPhone} from "../../protyle
import {getRangeByPoint} from "../../protyle/util/selection";
import {getCurrentEditor} from "../editor";
import {Constants} from "../../constants";
-import {getEmbedChildOperationContext} from "../../protyle/wysiwyg/getBlock";
+import {getEmbedGutterOperationContext} from "../../protyle/wysiwyg/getBlock";
import {backModel} from "../menu/model";
import {hasVisibleSelectionText, shouldRestoreLongPressSelection} from "./touchSelection";
import {getTouchAxis} from "./touchGesture";
@@ -174,7 +174,7 @@ export const handleTouchEnd = (event: TouchEvent) => {
const embedElement = isInEmbedBlock(nodeElement);
if (embedElement) {
editor.protyle.gutter.render(editor.protyle,
- getEmbedChildOperationContext(nodeElement) ? nodeElement : embedElement, target);
+ getEmbedGutterOperationContext(nodeElement) ? nodeElement : embedElement, target);
return;
}
editor.protyle.gutter.render(editor.protyle, nodeElement, target);
diff --git a/app/src/protyle/gutter/button.test.ts b/app/src/protyle/gutter/button.test.ts
new file mode 100644
index 0000000000..ead8bfb4d9
--- /dev/null
+++ b/app/src/protyle/gutter/button.test.ts
@@ -0,0 +1,39 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {canShowGutterInsert, genGutterBlockButtonHTML} from "./button";
+
+describe("genGutterBlockButtonHTML", () => {
+ const baseOptions = {
+ ariaLabel: "Block menu",
+ type: "NodeParagraph",
+ subtype: "p",
+ nodeID: "20260817120000-example",
+ icon: "iconParagraph",
+ draggable: true,
+ };
+
+ it("keeps regular gutters draggable and outside embed routing", () => {
+ const html = genGutterBlockButtonHTML(baseOptions);
+
+ assert.match(html, /draggable="true"/);
+ assert.doesNotMatch(html, /data-embed-id=/);
+ });
+
+ it("routes embedded child gutters without making them draggable", () => {
+ const html = genGutterBlockButtonHTML({
+ ...baseOptions,
+ embedID: "20260817120001-embed",
+ draggable: false,
+ });
+
+ assert.match(html, /data-embed-id="20260817120001-embed"/);
+ assert.doesNotMatch(html, /draggable="true"/);
+ });
+});
+
+describe("canShowGutterInsert", () => {
+ it("allows insertion controls only for regular gutters", () => {
+ assert.equal(canShowGutterInsert(), true);
+ assert.equal(canShowGutterInsert("20260817120001-embed"), false);
+ });
+});
diff --git a/app/src/protyle/gutter/button.ts b/app/src/protyle/gutter/button.ts
new file mode 100644
index 0000000000..67a3d73cd2
--- /dev/null
+++ b/app/src/protyle/gutter/button.ts
@@ -0,0 +1,21 @@
+export interface IGutterBlockButtonOptions {
+ ariaLabel: string;
+ type: string;
+ subtype: string;
+ nodeID: string;
+ icon: string;
+ embedID?: string;
+ popoverHTML?: string;
+ draggable: boolean;
+}
+
+export const genGutterBlockButtonHTML = (options: IGutterBlockButtonOptions) => {
+ const embedHTML = options.embedID ? ` data-embed-id="${options.embedID}"` : "";
+ return `<button class="ariaLabel" data-delay="500" data-position="parentW" aria-label="${options.ariaLabel}"
+data-type="${options.type}" data-subtype="${options.subtype}" data-node-id="${options.nodeID}"${embedHTML}>
+ <svg><use xlink:href="#${options.icon}"></use></svg>
+ <span ${options.popoverHTML || ""} ${options.draggable ? 'draggable="true"' : ""}></span>
+</button>`;
+};
+
+export const canShowGutterInsert = (embedID?: string) => !embedID;
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index ad8092bf5b..23f2d1171d 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -42,7 +42,7 @@ import {highlightRender} from "../render/highlightRender";
import {blockRender} from "../render/blockRender";
import {
getContenteditableElement,
- getEmbedChildOperationContext,
+ getEmbedGutterOperationContext,
getNextBlockSibling,
getParentBlock,
getTopAloneElement,
@@ -103,6 +103,7 @@ import {
import {applyHeadingLevelUpdates, getHeadingLevelUpdateOperations} from "../util/headingTransform";
import {getBacklinkGutterContentTop, getFixedGutterPosition, getGutterMarginHeight} from "./layout";
import {closeSubElement} from "../toolbar/subElementLifecycle";
+import {canShowGutterInsert, genGutterBlockButtonHTML} from "./button";
// 块类型 data-type 到本地化名称键的映射,用于块标提示中的 ${x}
const BLOCK_TYPE_LANG_KEYS: { [key: string]: string } = {
@@ -734,6 +735,11 @@ export class Gutter {
hideInsert();
return;
}
+ // 嵌入内容中的块不允许在当前查询结果边界外插入相邻块。
+ if (!canShowGutterInsert(buttonElement.dataset.embedId)) {
+ hideInsert();
+ return;
+ }
// 情况B:悬浮有效块标 → 显示框线(贴边),并预设+号位置(隐藏)
plusBefore.dataset.nodeId = id;
plusAfter.dataset.nodeId = id;
@@ -1475,7 +1481,7 @@ export class Gutter {
const embedElement = isInEmbedBlock(item, false);
if (embedID) {
return embedElement && embedElement.getAttribute("data-node-id") === embedID &&
- !!getEmbedChildOperationContext(item);
+ !!getEmbedGutterOperationContext(item);
}
return !embedElement;
});
@@ -1501,7 +1507,7 @@ export class Gutter {
if (range) {
focusByRange(range);
}
- const embedContext = getEmbedChildOperationContext(nodeElement);
+ const embedContext = getEmbedGutterOperationContext(nodeElement);
const selectsElement = protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select");
if (!embedContext && selectsElement.length > 1) {
window.siyuan.menus.menu.element.setAttribute("data-name", Constants.MENU_BLOCK_MULTI);
@@ -1519,9 +1525,9 @@ export class Gutter {
}
const isEmbedMenu = !!embedContext;
- // 查询目标容器自身只允许非结构操作,子块可以在目标边界内转换、插入、复制和删除。
+ // 查询目标自身只允许非结构操作;允许子块操作的查询结果可在目标边界内转换、插入、复制和删除。
const allowStructuralMutation = !protyle.disabled &&
- (!embedContext || embedContext.targetElement !== nodeElement);
+ (!embedContext || embedContext.allowChildOperation && embedContext.targetElement !== nodeElement);
const isOnlyTargetListItem = embedContext?.targetElement?.getAttribute("data-type") === "NodeList" &&
nodeElement.getAttribute("data-type") === "NodeListItem" &&
nodeElement.parentElement === embedContext.targetElement &&
@@ -3435,7 +3441,7 @@ export class Gutter {
let index = 0;
let listItem;
let hideParent = false;
- const embedContext = getEmbedChildOperationContext(nodeElement);
+ const embedContext = getEmbedGutterOperationContext(nodeElement);
const embedElement = embedContext ? isInEmbedBlock(nodeElement, false) : false;
const embedID = embedElement ? embedElement.getAttribute("data-node-id") : undefined;
while (nodeElement) {
@@ -3538,12 +3544,16 @@ export class Gutter {
if (protyle.options.backlinkData) {
popoverHTML = `class="popover__block" data-id="${dataNodeId}"`;
}
- const embedHTML = embedID ? ` data-embed-id="${embedID}"` : "";
- const buttonHTML = type ? `<button class="ariaLabel" data-delay="500" data-position="parentW" aria-label="${gutterTip}"
-data-type="${type}" data-subtype="${nodeElement.getAttribute("data-subtype")}" data-node-id="${dataNodeId}"${embedHTML}>
- <svg><use xlink:href="#${getIconByType(type, nodeElement.getAttribute("data-subtype"))}"></use></svg>
- <span ${popoverHTML} ${protyle.disabled || embedContext ? "" : 'draggable="true"'}></span>
-</button>` : "";
+ const buttonHTML = type ? genGutterBlockButtonHTML({
+ ariaLabel: gutterTip,
+ type,
+ subtype: nodeElement.getAttribute("data-subtype"),
+ nodeID: dataNodeId,
+ icon: getIconByType(type, nodeElement.getAttribute("data-subtype")),
+ embedID,
+ popoverHTML,
+ draggable: !protyle.disabled && !embedContext,
+ }) : "";
if (!hideParent) {
html = buttonHTML + html;
}
@@ -3598,11 +3608,31 @@ data-type="fold" style="cursor:inherit;"><svg style="width: 10px;${fold && fold
break;
}
}
+ // 嵌入内容块标左侧同时保留外层嵌入块块标,避免内外块标争用狭窄的悬浮区域。
+ // https://github.com/siyuan-note/siyuan/issues/18800
+ if (embedContext && embedElement) {
+ const type = embedElement.getAttribute("data-type");
+ let gutterTip = (protyle.options.backlinkData ? this.gutterTipBacklink : this.gutterTip)
+ .replace("${x}", () => getBlockTypeName(type));
+ if (protyle.disabled) {
+ gutterTip = gutterTip.split("<br>").splice(0, 2).join("<br>");
+ }
+ html = genGutterBlockButtonHTML({
+ ariaLabel: gutterTip,
+ type,
+ subtype: embedElement.getAttribute("data-subtype"),
+ nodeID: embedElement.getAttribute("data-node-id"),
+ icon: getIconByType(type, embedElement.getAttribute("data-subtype")),
+ popoverHTML: protyle.options.backlinkData ?
+ `class="popover__block" data-id="${embedElement.getAttribute("data-node-id")}"` : "",
+ draggable: !protyle.disabled,
+ }) + html;
+ }
if (isMultiSelect && !html) {
hideElements(["gutter"], protyle);
return;
}
- const shouldRenderInsert = !embedContext && !isMultiSelect;
+ const shouldRenderInsert = !isMultiSelect;
const insertElementCount = this.element.querySelectorAll(".protyle-gutters__line, .protyle-gutters__plus").length;
const renderKey = `${shouldRenderInsert ? "1" : "0"}${html}`;
let shouldRenderContent = this.renderKey !== renderKey || this.element.childElementCount === 0 ||
diff --git a/app/src/protyle/ui/initUI.ts b/app/src/protyle/ui/initUI.ts
index a9fa2d7f3f..a089dfc0f1 100644
--- a/app/src/protyle/ui/initUI.ts
+++ b/app/src/protyle/ui/initUI.ts
@@ -8,7 +8,7 @@ import {fetchPost} from "../../util/fetch";
import {lineNumberRender} from "../render/highlightRender";
import {hideMessage, showMessage} from "../../dialog/message";
import {genUUID} from "../../util/genID";
-import {getContenteditableElement, getEmbedChildOperationContext, getLastBlock} from "../wysiwyg/getBlock";
+import {getContenteditableElement, getEmbedGutterOperationContext, getLastBlock} from "../wysiwyg/getBlock";
import {genEmptyElement, genHeadingElement} from "../../block/util";
import {transaction} from "../wysiwyg/transaction";
import {focusByRange} from "../util/selection";
@@ -287,7 +287,7 @@ export const initUI = (protyle: IProtyle) => {
const embedElement = isInEmbedBlock(nodeElement);
if (embedElement) {
protyle.gutter.render(protyle,
- getEmbedChildOperationContext(nodeElement) ? nodeElement : embedElement, event.target);
+ getEmbedGutterOperationContext(nodeElement) ? nodeElement : embedElement, event.target);
return;
}
protyle.gutter.render(protyle, nodeElement, event.target);
diff --git a/app/src/protyle/wysiwyg/getBlock.test.ts b/app/src/protyle/wysiwyg/getBlock.test.ts
new file mode 100644
index 0000000000..cf7b0a6358
--- /dev/null
+++ b/app/src/protyle/wysiwyg/getBlock.test.ts
@@ -0,0 +1,115 @@
+import {before, describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+
+let getEmbedChildOperationContext: typeof import("./getBlock").getEmbedChildOperationContext;
+let getEmbedGutterOperationContext: typeof import("./getBlock").getEmbedGutterOperationContext;
+
+before(async () => {
+ Object.assign(globalThis, {
+ SIYUAN_VERSION: "test",
+ NODE_ENV: "test",
+ });
+ ({getEmbedChildOperationContext, getEmbedGutterOperationContext} = await import("./getBlock"));
+});
+
+class TestElement {
+ nodeType = 1;
+ tagName = "DIV";
+ nodeName = "DIV";
+ parentElement: TestElement | null = null;
+ children: TestElement[] = [];
+ attributes = new Map<string, string>();
+ classes = new Set<string>();
+ classList = {
+ contains: (className: string) => this.classes.has(className),
+ };
+
+ append(...elements: TestElement[]) {
+ elements.forEach(element => {
+ element.parentElement = this;
+ this.children.push(element);
+ });
+ return this;
+ }
+
+ addClass(className: string) {
+ this.classes.add(className);
+ return this;
+ }
+
+ setAttribute(name: string, value: string) {
+ this.attributes.set(name, value);
+ return this;
+ }
+
+ getAttribute(name: string) {
+ return this.attributes.get(name) || null;
+ }
+
+ hasAttribute(name: string) {
+ return this.attributes.has(name);
+ }
+
+ querySelectorAll(selector: string) {
+ const nodeID = /^\[data-node-id="(.+)"\]$/.exec(selector)?.[1];
+ const matches: TestElement[] = [];
+ const visit = (element: TestElement) => {
+ element.children.forEach(child => {
+ if (nodeID && child.getAttribute("data-node-id") === nodeID) {
+ matches.push(child);
+ }
+ visit(child);
+ });
+ };
+ visit(this);
+ return matches;
+ }
+}
+
+const asNode = (element: TestElement) => element as unknown as Node;
+
+const createEmbed = (allowChildOperation: boolean) => {
+ const editor = new TestElement().addClass("protyle-wysiwyg");
+ const embedBlock = new TestElement()
+ .setAttribute("data-node-id", "20260817120000-embed")
+ .setAttribute("data-type", "NodeBlockQueryEmbed");
+ const result = new TestElement()
+ .addClass("protyle-wysiwyg__embed")
+ .setAttribute("data-id", "20260817120001-target");
+ if (allowChildOperation) {
+ result.setAttribute("data-allow-child-operation", "true");
+ }
+ const target = new TestElement()
+ .setAttribute("data-node-id", "20260817120001-target")
+ .setAttribute("data-type", "NodeBlockquote");
+ const child = new TestElement()
+ .setAttribute("data-node-id", "20260817120002-child")
+ .setAttribute("data-type", "NodeParagraph");
+ editor.append(embedBlock.append(result.append(target.append(child))));
+ return {target, child};
+};
+
+describe("getEmbedGutterOperationContext", () => {
+ it("shows a gutter for a queried target block without enabling child operations", () => {
+ const {target} = createEmbed(false);
+ const context = getEmbedGutterOperationContext(asNode(target));
+
+ assert.equal(context?.targetElement, target as unknown as Element);
+ assert.equal(context?.allowChildOperation, false);
+ assert.equal(getEmbedChildOperationContext(asNode(target)), undefined);
+ });
+
+ it("does not expose nested block gutters when child operations are unavailable", () => {
+ const {child} = createEmbed(false);
+
+ assert.equal(getEmbedGutterOperationContext(asNode(child)), undefined);
+ });
+
+ it("keeps nested block gutters available for supported container queries", () => {
+ const {child} = createEmbed(true);
+ const context = getEmbedGutterOperationContext(asNode(child));
+
+ assert.equal(context?.allowChildOperation, true);
+ assert.equal(getEmbedChildOperationContext(asNode(child))?.targetElement, context?.targetElement);
+ });
+});
diff --git a/app/src/protyle/wysiwyg/getBlock.ts b/app/src/protyle/wysiwyg/getBlock.ts
index 0c6f586640..c6df6956b8 100644
--- a/app/src/protyle/wysiwyg/getBlock.ts
+++ b/app/src/protyle/wysiwyg/getBlock.ts
@@ -1,16 +1,21 @@
import {hasClosestBlock, hasClosestByClassName, isInEmbedBlock} from "../util/hasClosest";
import {Constants} from "../../constants";
-export interface IEmbedChildOperationContext {
+export interface IEmbedOperationContext {
resultElement: HTMLElement;
targetID: string;
targetElement?: Element;
boundaryElement: Element;
+ allowChildOperation: boolean;
}
-export const getEmbedChildOperationContext = (element: Node): IEmbedChildOperationContext | undefined => {
+export interface IEmbedChildOperationContext extends IEmbedOperationContext {
+ allowChildOperation: true;
+}
+
+const getEmbedOperationContext = (element: Node): IEmbedOperationContext | undefined => {
const resultElement = hasClosestByClassName(element, "protyle-wysiwyg__embed");
- if (!resultElement || resultElement.getAttribute("data-allow-child-operation") !== "true") {
+ if (!resultElement) {
return;
}
@@ -28,9 +33,26 @@ export const getEmbedChildOperationContext = (element: Node): IEmbedChildOperati
targetElement,
// 文档块不会渲染自身节点,查询结果容器就是它的子块边界。
boundaryElement: targetElement || resultElement,
+ allowChildOperation: resultElement.getAttribute("data-allow-child-operation") === "true",
};
};
+export const getEmbedChildOperationContext = (element: Node): IEmbedChildOperationContext | undefined => {
+ const context = getEmbedOperationContext(element);
+ return context?.allowChildOperation ? context as IEmbedChildOperationContext : undefined;
+};
+
+export const getEmbedGutterOperationContext = (element: Node): IEmbedOperationContext | undefined => {
+ const context = getEmbedOperationContext(element);
+ if (!context) {
+ return;
+ }
+ const blockElement = hasClosestBlock(element);
+ if (context.allowChildOperation || context.targetElement === blockElement) {
+ return context;
+ }
+};
+
export const getEmbedChildOperationParentID = (element: Element, context = getEmbedChildOperationContext(element)) => {
if (context && !context.targetElement && element.parentElement === context.resultElement) {
return context.targetID;
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 0e72d2fa7c..4d0a09a81c 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -51,7 +51,7 @@ import {dropEvent} from "../util/editorCommonEvent";
import {beforeBlockquoteInput, input} from "./input";
import {
getContenteditableElement,
- getEmbedChildOperationContext,
+ getEmbedGutterOperationContext,
getFirstBlock,
getLastBlock,
getNextBlock,
@@ -211,7 +211,7 @@ const refreshGutterByPointer = (protyle: IProtyle, pointerElement: Element | nul
}
if (gutterElement) {
const embedElement = isInEmbedBlock(gutterElement);
- if (embedElement && !getEmbedChildOperationContext(gutterElement)) {
+ if (embedElement && !getEmbedGutterOperationContext(gutterElement)) {
gutterElement = embedElement;
}
}