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: Preserve callout title during full-selection formatting https://github.com/siyuan-note/siyuan/issues/19077
Daniel76827df40832
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.tsindex 19ef323c00..8a771a35ec 100644--- a/app/src/protyle/toolbar/index.ts+++ b/app/src/protyle/toolbar/index.ts@@ -90,7 +90,11 @@ import {stripSemanticMarkersFromRangeText,stripSemanticInternalMarkerPrefix} from "../util/inlineElementMarker";-import {canExpandInlineRangeToParent, getInlineRangeElement} from "./inlineRangeBoundary";+import {+ canExpandInlineRangeToParent,+ getInlineRangeElement,+ normalizeCalloutTitleRange+} from "./inlineRangeBoundary";const filterPluginToolbar = (toolbar: Array<string | IMenuItem>, lite: boolean) => {if (!lite) {@@ -752,7 +756,8 @@ export class Toolbar {if (!nodeElement || nodeElement.getAttribute("data-type") === "NodeCodeBlock") {return;}- const editableElement = getContenteditableElement(nodeElement, this.range.startContainer);+ const editableElement = normalizeCalloutTitleRange(this.range, nodeElement,+ getContenteditableElement(nodeElement, this.range.startContainer));const isBatch = remove !== undefined;let rangeTypes: string[] = [];this.range.cloneContents().childNodes.forEach((item: HTMLElement) => {diff --git a/app/src/protyle/toolbar/inlineRangeBoundary.test.ts b/app/src/protyle/toolbar/inlineRangeBoundary.test.tsindex 83a9ea344d..ad36d2adb4 100644--- a/app/src/protyle/toolbar/inlineRangeBoundary.test.ts+++ b/app/src/protyle/toolbar/inlineRangeBoundary.test.ts@@ -1,6 +1,10 @@import {describe, it} from "node:test";import * as assert from "node:assert/strict";-import {canExpandInlineRangeToParent, getInlineRangeElement} from "./inlineRangeBoundary";+import {+ canExpandInlineRangeToParent,+ getInlineRangeElement,+ normalizeCalloutTitleRange+} from "./inlineRangeBoundary";const element = (tagName: string, parentElement: Element | null = null) => ({tagName,@@ -35,4 +39,35 @@ describe("inline range boundary", () => {assert.equal(canExpandInlineRangeToParent(text(editableElement), editableElement), false);});});++ it("normalizes a browser select-all range to the callout title", () => {+ const infoElement = element("DIV");+ const titleElement = element("SPAN", infoElement);+ const titleText = text(titleElement);+ Object.defineProperty(titleElement, "childNodes", {value: [titleText]});+ infoElement.contains = (node: Node) => node === titleElement || node === titleText;+ titleElement.contains = (node: Node) => node === titleText;+ const blockElement = {+ getAttribute: (name: string) => name === "data-type" ? "NodeCallout" : null,+ querySelector: () => titleElement,+ } as unknown as Element;+ const fallbackEditableElement = element("DIV");+ const range = {+ startContainer: infoElement,+ endContainer: infoElement,+ intersectsNode: (node: Node) => node === titleElement,+ setStart(node: Node, offset: number) {+ this.startContainer = node;+ assert.equal(offset, 0);+ },+ setEnd(node: Node, offset: number) {+ this.endContainer = node;+ assert.equal(offset, 1);+ },+ } as unknown as Range;++ assert.equal(normalizeCalloutTitleRange(range, blockElement, fallbackEditableElement), titleElement);+ assert.equal(range.startContainer, titleElement);+ assert.equal(range.endContainer, titleElement);+ });});diff --git a/app/src/protyle/toolbar/inlineRangeBoundary.ts b/app/src/protyle/toolbar/inlineRangeBoundary.tsindex 29db64257a..86442f958e 100644--- a/app/src/protyle/toolbar/inlineRangeBoundary.ts+++ b/app/src/protyle/toolbar/inlineRangeBoundary.ts@@ -13,3 +13,25 @@ export const canExpandInlineRangeToParent = (container: Node, editableElement: Econst parentElement = container.parentElement;return !!parentElement && parentElement !== editableElement && !INLINE_RANGE_ROOT_TAGS.includes(parentElement.tagName);};++export const normalizeCalloutTitleRange = (range: Range, blockElement: Element,+ editableElement: Element) => {+ if (blockElement.getAttribute("data-type") !== "NodeCallout") {+ return editableElement;+ }+ const titleElement = blockElement.querySelector(":scope > .callout-info > .callout-title");+ const infoElement = titleElement?.parentElement;+ if (!titleElement || !infoElement ||+ !(range.startContainer === infoElement || infoElement.contains(range.startContainer)) ||+ !(range.endContainer === infoElement || infoElement.contains(range.endContainer)) ||+ !range.intersectsNode(titleElement)) {+ return editableElement;+ }+ if (!titleElement.contains(range.startContainer)) {+ range.setStart(titleElement, 0);+ }+ if (!titleElement.contains(range.endContainer)) {+ range.setEnd(titleElement, titleElement.childNodes.length);+ }+ return titleElement;+};