Skip to content

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

:bug: Preserve list items in mobile multi-block selection https://github.com/siyuan-note/siyuan/issues/18881

Daniel6046667bb79c
diff --git a/app/src/mobile/util/blockSelection.test.ts b/app/src/mobile/util/blockSelection.test.ts
new file mode 100644
index 0000000000..61644987ee
--- /dev/null
+++ b/app/src/mobile/util/blockSelection.test.ts
@@ -0,0 +1,89 @@
+import {before, describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+
+let getMobileBlockSelectionElement: typeof import("./blockSelection").getMobileBlockSelectionElement;
+
+before(async () => {
+ Object.assign(globalThis, {
+ SIYUAN_VERSION: "test",
+ NODE_ENV: "test",
+ });
+ ({getMobileBlockSelectionElement} = await import("./blockSelection"));
+});
+
+class TestElement {
+ nodeType = 1;
+ tagName = "DIV";
+ parentElement: TestElement | null = null;
+ children: TestElement[] = [];
+ attributes = new Map<string, string>();
+ classes = new Set<string>();
+ classList = {
+ contains: (className: string) => this.classes.has(className),
+ };
+
+ get childElementCount() {
+ return this.children.length;
+ }
+
+ 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);
+ }
+}
+
+const block = (type: string) => new TestElement().setAttribute("data-type", type).setAttribute("data-node-id", type);
+const asHTMLElement = (element: TestElement) => element as unknown as HTMLElement;
+
+describe("mobile block selection", () => {
+ it("selects a simple list item instead of its paragraph", () => {
+ const editor = new TestElement().addClass("protyle-wysiwyg");
+ const list = block("NodeList");
+ const paragraph = block("NodeParagraph");
+ const listItem = block("NodeListItem").append(new TestElement(), paragraph, new TestElement());
+ const siblingItem = block("NodeListItem").append(new TestElement(), block("NodeParagraph"), new TestElement());
+ editor.append(list.append(listItem, siblingItem, new TestElement()));
+
+ assert.equal(getMobileBlockSelectionElement(asHTMLElement(paragraph)), asHTMLElement(listItem));
+ });
+
+ it("keeps independently movable content blocks inside complex list items", () => {
+ const editor = new TestElement().addClass("protyle-wysiwyg");
+ const paragraph = block("NodeParagraph");
+ const listItem = block("NodeListItem").append(
+ new TestElement(), paragraph, block("NodeParagraph"), new TestElement());
+ editor.append(block("NodeList").append(listItem, block("NodeListItem"), new TestElement()));
+
+ assert.equal(getMobileBlockSelectionElement(asHTMLElement(paragraph)), asHTMLElement(paragraph));
+ });
+
+ it("selects the query embed instead of a rendered child block", () => {
+ const editor = new TestElement().addClass("protyle-wysiwyg");
+ const child = block("NodeParagraph");
+ const embed = block("NodeBlockQueryEmbed").append(child);
+ editor.append(embed);
+
+ assert.equal(getMobileBlockSelectionElement(asHTMLElement(child)), asHTMLElement(embed));
+ });
+});
diff --git a/app/src/mobile/util/blockSelection.ts b/app/src/mobile/util/blockSelection.ts
new file mode 100644
index 0000000000..4dc4402060
--- /dev/null
+++ b/app/src/mobile/util/blockSelection.ts
@@ -0,0 +1,7 @@
+import {isInEmbedBlock} from "../../protyle/util/hasClosest";
+import {getTopAloneElement} from "../../protyle/wysiwyg/getBlock";
+
+export const getMobileBlockSelectionElement = (blockElement: HTMLElement) => {
+ const embedBlockElement = isInEmbedBlock(blockElement);
+ return getTopAloneElement((embedBlockElement || blockElement) as HTMLElement) as HTMLElement;
+};
diff --git a/app/src/mobile/util/touch.ts b/app/src/mobile/util/touch.ts
index c19b3f8c79..a34c1dfd22 100644
--- a/app/src/mobile/util/touch.ts
+++ b/app/src/mobile/util/touch.ts
@@ -19,6 +19,7 @@ import {
shouldRestoreLongPressSelection,
} from "./touchSelection";
import {getTouchAxis, shouldStartLongPressMultiSelect} from "./touchGesture";
+import {getMobileBlockSelectionElement} from "./blockSelection";
let clientX: number;
let clientY: number;
@@ -136,8 +137,9 @@ export const handleTouchEnd = (event: TouchEvent) => {
// 多选模式
window.getSelection()?.removeAllRanges();
activeBlur();
- const blockElement = hasClosestBlock(target);
- if (blockElement) {
+ const touchedBlockElement = hasClosestBlock(target);
+ if (touchedBlockElement) {
+ const blockElement = getMobileBlockSelectionElement(touchedBlockElement as HTMLElement);
// 本次按压已在按住期间触发多选,松手时不切换选中态,仅消费该手势
blockElement.querySelectorAll(".protyle-wysiwyg--select").forEach(item => {
item.classList.remove("protyle-wysiwyg--select");
@@ -378,9 +380,10 @@ export const handleTouchStart = (event: TouchEvent) => {
}
}
window.getSelection()?.removeAllRanges();
- editor.protyle.toolbar.showMultiSelectMode(editor.protyle, blockElement);
+ const selectionBlockElement = getMobileBlockSelectionElement(blockElement as HTMLElement);
+ editor.protyle.toolbar.showMultiSelectMode(editor.protyle, selectionBlockElement);
if (editor.protyle.options.render.gutter) {
- editor.protyle.gutter.render(editor.protyle, blockElement, target);
+ editor.protyle.gutter.render(editor.protyle, selectionBlockElement, target);
}
}, Constants.TIMEOUT_MULTIPLE_SELECT);
}