Skip to content

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

:art: Drag all blocks covered by a cross-block text selection https://github.com/siyuan-note/siyuan/issues/18708

Daniele146eeeb542f
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 1d842c7e81..d15871ef00 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -75,6 +75,7 @@ import {getAVFilteredTipContext, getAVViewID} from "../render/av/filteredTip";
import {avContextmenu, duplicateCompletely} from "../render/av/action";
import {getPlainText} from "../util/paste";
import {
+ getCrossBlockTextSelectionTarget,
getGutterSelection,
getGutterSelectionTarget,
getSameContainerHeadingLevel,
@@ -138,6 +139,23 @@ const getCrossBlockTextRange = (protyle: IProtyle) => {
}
};
+const prepareCrossBlockTextSelection = (protyle: IProtyle, targetElement: Element) => {
+ const range = getCrossBlockTextRange(protyle);
+ const blockSelectElements = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));
+ if (!range || hasMultipleBlockSelection(blockSelectElements)) {
+ return;
+ }
+ const rangeSelectElements = getBlockElementsByRange(range);
+ const selectionTarget = getCrossBlockTextSelectionTarget(blockSelectElements, rangeSelectElements, targetElement);
+ hideElements(["select"], protyle);
+ if (selectionTarget) {
+ selectBlocksByRange(protyle, range);
+ } else {
+ range.collapse(false);
+ }
+ return range;
+};
+
export class Gutter {
public element: HTMLElement;
// 普通块标提示模板(含 ${x} 块类型占位符),反链面板使用 gutterTipBacklink
@@ -214,6 +232,10 @@ export class Gutter {
});
} else {
const gutterId = buttonElement.getAttribute("data-node-id");
+ const gutterNodeElement = this.getNodeElement(protyle, buttonElement) as HTMLElement;
+ if (gutterNodeElement) {
+ prepareCrossBlockTextSelection(protyle, gutterNodeElement);
+ }
selectElements = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));
let selectedIncludeGutter = false;
selectElements.forEach((item => {
@@ -224,13 +246,6 @@ export class Gutter {
selectIds.push(itemId);
}));
if (!selectedIncludeGutter) {
- let gutterNodeElement: HTMLElement;
- Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${gutterId}"]`)).find((item: HTMLElement) => {
- if (!isInEmbedBlock(item) && this.isMatchNode(item)) {
- gutterNodeElement = item;
- return true;
- }
- });
if (gutterNodeElement) {
selectElements.forEach((item => {
item.classList.remove("protyle-wysiwyg--select");
@@ -1467,16 +1482,8 @@ export class Gutter {
return;
}
const editableElement = getContenteditableElement(nodeElement);
- const range = protyle.toolbar.range;
- const blockSelectElements = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));
- if (range && !hasMultipleBlockSelection(blockSelectElements) &&
- isCrossBlockTextRange(range, protyle.wysiwyg.element, hasClosestBlock)) {
- hideElements(["select"], protyle);
- if (range.intersectsNode(hasClosestBlock(editableElement) || nodeElement)) {
- selectBlocksByRange(protyle, range);
- } else {
- range.collapse(false);
- }
+ const range = prepareCrossBlockTextSelection(protyle, nodeElement);
+ if (range) {
focusByRange(range);
}
const embedContext = getEmbedChildOperationContext(nodeElement);
diff --git a/app/src/protyle/gutter/multiSelect.test.ts b/app/src/protyle/gutter/multiSelect.test.ts
index f13c604aca..2a456ad41d 100644
--- a/app/src/protyle/gutter/multiSelect.test.ts
+++ b/app/src/protyle/gutter/multiSelect.test.ts
@@ -1,6 +1,7 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
+ getCrossBlockTextSelectionTarget,
getGutterSelection,
getGutterSelectionTarget,
getSameContainerHeadingLevel,
@@ -177,6 +178,34 @@ describe("getGutterSelectionTarget", () => {
});
});
+describe("getCrossBlockTextSelectionTarget", () => {
+ it("uses a matching cross-block text selection when there is no multiple block selection", () => {
+ const selected = new TestElement();
+ const content = new TestElement();
+ selected.append(content);
+
+ assert.equal(getCrossBlockTextSelectionTarget([], [asElement(selected)], asElement(content)),
+ asElement(selected));
+ assert.equal(getCrossBlockTextSelectionTarget([asElement(new TestElement())], [asElement(selected)],
+ asElement(content)), asElement(selected));
+ });
+
+ it("keeps an existing multiple block selection ahead of a text selection", () => {
+ const first = asElement(new TestElement());
+ const second = asElement(new TestElement());
+ const rangeElement = asElement(new TestElement());
+
+ assert.equal(getCrossBlockTextSelectionTarget([first, second], [rangeElement], rangeElement), undefined);
+ });
+
+ it("ignores a gutter outside the cross-block text selection", () => {
+ const rangeElement = asElement(new TestElement());
+ const unrelated = asElement(new TestElement());
+
+ assert.equal(getCrossBlockTextSelectionTarget([], [rangeElement], unrelated), undefined);
+ });
+});
+
describe("isCrossBlockTextRange", () => {
const getBlock = (node: Node) => node as unknown as Element;
diff --git a/app/src/protyle/gutter/multiSelect.ts b/app/src/protyle/gutter/multiSelect.ts
index 4c759faa4e..dca91a6499 100644
--- a/app/src/protyle/gutter/multiSelect.ts
+++ b/app/src/protyle/gutter/multiSelect.ts
@@ -37,6 +37,14 @@ export const getGutterSelectionTarget = (selectElements: Element[], element: Ele
}
};
+export const getCrossBlockTextSelectionTarget = (blockSelectElements: Element[], rangeSelectElements: Element[],
+ element: Element) => {
+ if (hasMultipleBlockSelection(blockSelectElements)) {
+ return;
+ }
+ return getGutterSelectionTarget(rangeSelectElements, element);
+};
+
export const isCrossBlockTextRange = (range: Range | undefined, boundaryElement: Element,
getBlock: (node: Node) => Element | false) => {
if (!range || range.collapsed || !boundaryElement.contains(range.startContainer) ||