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 callouts at the correct level in cross-block selections https://github.com/siyuan-note/siyuan/issues/18998
Daniel596016ac6b54
diff --git a/app/src/protyle/util/blockRangeSelect.test.ts b/app/src/protyle/util/blockRangeSelect.test.tsnew file mode 100644index 0000000000..835413c329--- /dev/null+++ b/app/src/protyle/util/blockRangeSelect.test.ts@@ -0,0 +1,114 @@+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {getBlockRangeSelectElements} from "./blockRangeSelect";++class TestClassList {+ private classes: Set<string>;++ constructor(classes: string[]) {+ this.classes = new Set(classes);+ }++ contains(className: string) {+ return this.classes.has(className);+ }+}++class TestElement {+ nodeType = 1;+ tagName = "DIV";+ parentElement: TestElement | null = null;+ children: TestElement[] = [];+ classList: TestClassList;++ constructor(private name: string, private top: number, classes: string[] = [], private type?: string) {+ this.classList = new TestClassList(classes);+ }++ append(...elements: TestElement[]) {+ elements.forEach(element => {+ element.parentElement = this;+ this.children.push(element);+ });+ return this;+ }++ get nextElementSibling() {+ if (!this.parentElement) {+ return null;+ }+ return this.parentElement.children[this.parentElement.children.indexOf(this) + 1] || null;+ }++ getAttribute(attribute: string) {+ if (attribute === "data-node-id" && this.type) {+ return this.name;+ }+ if (attribute === "data-type") {+ return this.type || null;+ }+ return null;+ }++ hasAttribute(attribute: string) {+ return attribute === "data-node-id" && !!this.type;+ }++ getBoundingClientRect() {+ return {+ bottom: this.top + 10,+ left: 0,+ top: this.top,+ } as DOMRect;+ }+}++const asElement = (element: TestElement) => element as unknown as HTMLElement;++const block = (name: string, top: number, type = "NodeParagraph", classes: string[] = []) =>+ new TestElement(name, top, classes, type);++const getBlock = (element: Node) => {+ let currentElement = element as unknown as TestElement;+ while (currentElement && !currentElement.classList.contains("protyle-wysiwyg")) {+ if (currentElement.getAttribute("data-type")?.startsWith("Node") &&+ currentElement.hasAttribute("data-node-id")) {+ return asElement(currentElement);+ }+ currentElement = currentElement.parentElement;+ }+ return false;+};++describe("getBlockRangeSelectElements", () => {+ it("selects a callout and its following siblings when the range starts in callout content", () => {+ const editor = new TestElement("editor", 0, ["protyle-wysiwyg"]);+ const callout = block("callout", 0, "NodeCallout", ["callout"]);+ const content = new TestElement("content", 0, ["callout-content"]);+ const calloutParagraph = block("callout-paragraph", 10);+ const list = block("list", 20, "NodeList", ["list"]);+ const paragraph = block("paragraph", 30);+ editor.append(callout.append(content.append(calloutParagraph)), list, paragraph);++ const expected = [asElement(callout), asElement(list), asElement(paragraph)];+ assert.deepEqual(getBlockRangeSelectElements(asElement(calloutParagraph), asElement(paragraph), getBlock)+ .selectElements, expected);+ const upwardSelection = getBlockRangeSelectElements(asElement(paragraph), asElement(calloutParagraph),+ getBlock);+ assert.deepEqual(upwardSelection.selectElements, expected);+ assert.equal(upwardSelection.toDown, false);+ });++ it("keeps child blocks selected when the range stays inside a callout", () => {+ const editor = new TestElement("editor", 0, ["protyle-wysiwyg"]);+ const callout = block("callout", 0, "NodeCallout", ["callout"]);+ const content = new TestElement("content", 0, ["callout-content"]);+ const first = block("first", 10);+ const second = block("second", 20);+ const sibling = block("sibling", 30);+ editor.append(callout.append(content.append(first, second)), sibling);++ assert.deepEqual(getBlockRangeSelectElements(asElement(first), asElement(second), getBlock).selectElements,+ [asElement(first), asElement(second)]);+ });+});diff --git a/app/src/protyle/util/blockRangeSelect.ts b/app/src/protyle/util/blockRangeSelect.tsnew file mode 100644index 0000000000..b7be709547--- /dev/null+++ b/app/src/protyle/util/blockRangeSelect.ts@@ -0,0 +1,82 @@+type TGetBlock = (element: Node) => Element | false;++export const getBlockRangeSelectElements = (rangeStartElement: HTMLElement, rangeEndElement: HTMLElement,+ getBlock: TGetBlock) => {+ let startElement = rangeStartElement;+ let endElement = rangeEndElement;+ let toDown = true;+ const startRect = startElement.getBoundingClientRect();+ const endRect = endElement.getBoundingClientRect();+ let startTop = startRect.top;+ let endTop = endRect.top;+ if (startTop === endTop) {+ // 横排 https://ld246.com/article/1663036247544+ startTop = startRect.left;+ endTop = endRect.left;+ }+ if (startTop > endTop) {+ const tempElement = endElement;+ endElement = startElement;+ startElement = tempElement;+ const tempTop = endTop;+ endTop = startTop;+ startTop = tempTop;+ toDown = false;+ }+ let selectElements: HTMLElement[] = [];+ let currentElement: HTMLElement = startElement;+ let hasJump = false;+ while (currentElement) {+ if (currentElement.classList.contains("protyle-breadcrumb__bar")) {+ currentElement = currentElement.nextElementSibling as HTMLElement;+ }+ if (currentElement && !currentElement.classList.contains("protyle-attr")) {+ const currentRect = currentElement.getBoundingClientRect();+ if (startRect.top === endRect.top ? currentRect.left <= endTop : currentRect.top <= endTop) {+ if (hasJump) {+ // 父节点的下个节点在选中范围内才可使用父节点作为选中节点+ if (currentElement.nextElementSibling &&+ !currentElement.nextElementSibling.classList.contains("protyle-attr")) {+ const currentNextRect = currentElement.nextElementSibling.getBoundingClientRect();+ if (startRect.top === endRect.top ?+ currentNextRect.left <= endTop && currentNextRect.bottom <= endRect.bottom :+ currentNextRect.top <= endTop) {+ selectElements = [currentElement];+ currentElement = currentElement.nextElementSibling as HTMLElement;+ hasJump = false;+ } else if (currentElement.parentElement.classList.contains("sb")) {+ currentElement = getBlock(currentElement.parentElement) as HTMLElement;+ hasJump = true;+ } else {+ break;+ }+ } else {+ currentElement = getBlock(currentElement.parentElement) as HTMLElement;+ hasJump = true;+ }+ } else {+ if (!currentElement.classList.contains("sb__resize")) {+ selectElements.push(currentElement);+ }+ const parentElement = currentElement.parentElement;+ currentElement = currentElement.nextElementSibling as HTMLElement;+ // 提示块内容使用无块 ID 的包装层,末尾需回到提示块后继续遍历同级块。+ if (!currentElement && parentElement.classList.contains("callout-content")) {+ currentElement = getBlock(parentElement) as HTMLElement;+ hasJump = true;+ }+ }+ } else if (currentElement.parentElement.classList.contains("sb")) {+ // 跳出超级块横向排版中的未选中元素+ currentElement = getBlock(currentElement.parentElement) as HTMLElement;+ hasJump = true;+ } else {+ break;+ }+ } else {+ currentElement = getBlock(currentElement.parentElement) as HTMLElement;+ hasJump = true;+ }+ }+ return {endElement, selectElements, startElement, toDown};+};diff --git a/app/src/protyle/util/selection.ts b/app/src/protyle/util/selection.tsindex 894485bfcb..fb60f7dbe2 100644--- a/app/src/protyle/util/selection.ts+++ b/app/src/protyle/util/selection.ts@@ -19,6 +19,7 @@ import {hideElements} from "../ui/hideElements";import {genRenderFrame} from "../render/util";import {Constants} from "../../constants";import {getUndoFocusElement} from "./selectionFocus";+import {getBlockRangeSelectElements as getBlockRangeSelectElementsByDOM} from "./blockRangeSelect";const selectIsEditor = (editor: Element, range?: Range) => {if (!range) {@@ -136,79 +137,8 @@ export const selectAll = (protyle: IProtyle, nodeElement: Element, range: Range)return false;};-export const getBlockRangeSelectElements = (rangeStartElement: HTMLElement, rangeEndElement: HTMLElement) => {- let startElement = rangeStartElement;- let endElement = rangeEndElement;- let toDown = true;- const startRect = startElement.getBoundingClientRect();- const endRect = endElement.getBoundingClientRect();- let startTop = startRect.top;- let endTop = endRect.top;- if (startTop === endTop) {- // 横排 https://ld246.com/article/1663036247544- startTop = startRect.left;- endTop = endRect.left;- }- if (startTop > endTop) {- const tempElement = endElement;- endElement = startElement;- startElement = tempElement;- const tempTop = endTop;- endTop = startTop;- startTop = tempTop;- toDown = false;- }- let selectElements: HTMLElement[] = [];- let currentElement: HTMLElement = startElement;- let hasJump = false;- while (currentElement) {- if (currentElement.classList.contains("protyle-breadcrumb__bar")) {- currentElement = currentElement.nextElementSibling as HTMLElement;- }- if (currentElement && !currentElement.classList.contains("protyle-attr")) {- const currentRect = currentElement.getBoundingClientRect();- if (startRect.top === endRect.top ? currentRect.left <= endTop : currentRect.top <= endTop) {- if (hasJump) {- // 父节点的下个节点在选中范围内才可使用父节点作为选中节点- if (currentElement.nextElementSibling &&- !currentElement.nextElementSibling.classList.contains("protyle-attr")) {- const currentNextRect = currentElement.nextElementSibling.getBoundingClientRect();- if (startRect.top === endRect.top ?- currentNextRect.left <= endTop && currentNextRect.bottom <= endRect.bottom :- currentNextRect.top <= endTop) {- selectElements = [currentElement];- currentElement = currentElement.nextElementSibling as HTMLElement;- hasJump = false;- } else if (currentElement.parentElement.classList.contains("sb")) {- currentElement = hasClosestBlock(currentElement.parentElement) as HTMLElement;- hasJump = true;- } else {- break;- }- } else {- currentElement = hasClosestBlock(currentElement.parentElement) as HTMLElement;- hasJump = true;- }- } else {- if (!currentElement.classList.contains("sb__resize")) {- selectElements.push(currentElement);- }- currentElement = currentElement.nextElementSibling as HTMLElement;- }- } else if (currentElement.parentElement.classList.contains("sb")) {- // 跳出超级块横向排版中的未选中元素- currentElement = hasClosestBlock(currentElement.parentElement) as HTMLElement;- hasJump = true;- } else {- break;- }- } else {- currentElement = hasClosestBlock(currentElement.parentElement) as HTMLElement;- hasJump = true;- }- }- return {endElement, selectElements, startElement, toDown};-};+export const getBlockRangeSelectElements = (rangeStartElement: HTMLElement, rangeEndElement: HTMLElement) =>+ getBlockRangeSelectElementsByDOM(rangeStartElement, rangeEndElement, hasClosestBlock);export const getBlockElementsByRange = (range: Range) => {const startBlockElement = hasClosestBlock(range.startContainer);