Skip to content

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

:bug: Prevent text selection while blocks are selected https://github.com/siyuan-note/siyuan/issues/19170

Danielacd2bd4da33a
diff --git a/app/src/protyle/util/selection.ts b/app/src/protyle/util/selection.ts
index 48d3d69396..b05c11c005 100644
--- a/app/src/protyle/util/selection.ts
+++ b/app/src/protyle/util/selection.ts
@@ -27,6 +27,7 @@ import {
getSemanticMarkerPrefixLengthForNode,
stripSemanticMarkersFromRangeText
} from "./inlineElementMarker";
+import {getSelectAllBlockAction} from "../wysiwyg/blockSelection";
const selectIsEditor = (editor: Element, range?: Range) => {
if (!range) {
@@ -63,12 +64,15 @@ export const fixTableRange = (range: Range) => {
};
export const selectAll = (protyle: IProtyle, nodeElement: Element, range: Range): boolean => {
- const selectElements = protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select");
- if (selectElements.length > 0 && protyle.wysiwyg.element.childElementCount === selectElements.length &&
- selectElements[0].parentElement === protyle.wysiwyg.element) {
- return false;
+ const blockSelectionAction = getSelectAllBlockAction(protyle.wysiwyg.element);
+ if (blockSelectionAction !== "none") {
+ range.collapse(true);
+ hideElements(["toolbar"], protyle);
+ if (blockSelectionAction === "keep") {
+ return false;
+ }
}
- const editElement = getContenteditableElement(nodeElement);
+ const editElement = blockSelectionAction === "none" ? getContenteditableElement(nodeElement) : undefined;
if (editElement) {
let position;
if (editElement.tagName === "TABLE") {
@@ -131,7 +135,7 @@ export const selectAll = (protyle: IProtyle, nodeElement: Element, range: Range)
}
}
range.collapse(true);
- hideElements(["select"], protyle);
+ hideElements(["select", "toolbar"], protyle);
const ids: string[] = [];
Array.from(protyle.wysiwyg.element.children).forEach(item => {
const nodeId = item.getAttribute("data-node-id");
diff --git a/app/src/protyle/wysiwyg/blockSelection.test.ts b/app/src/protyle/wysiwyg/blockSelection.test.ts
index 9b1aa25bae..ce700f0b66 100644
--- a/app/src/protyle/wysiwyg/blockSelection.test.ts
+++ b/app/src/protyle/wysiwyg/blockSelection.test.ts
@@ -6,6 +6,7 @@ import {
captureBlockSelectionModeState,
getBlockSelectionToggle,
getDeleteSelectionCandidate,
+ getSelectAllBlockAction,
isContinuousBlockSelection,
restoreBlockSelectionModeState,
runBlockSelectionDelete
@@ -126,6 +127,47 @@ describe("getBlockSelectionToggle", () => {
});
});
+describe("getSelectAllBlockAction", () => {
+ it("continues with text selection when no block is selected", () => {
+ const editor = new TestElement("editor", false);
+ editor.append(new TestElement("first"), new TestElement("second"));
+
+ assert.equal(getSelectAllBlockAction(asElement(editor)), "none");
+ });
+
+ it("expands a partial block selection to all top-level blocks", () => {
+ const editor = new TestElement("editor", false);
+ const first = new TestElement("first");
+ const second = new TestElement("second");
+ editor.append(first, second);
+ first.classList.add(BLOCK_SELECTION_CLASS);
+
+ assert.equal(getSelectAllBlockAction(asElement(editor)), "select-all");
+ });
+
+ it("keeps the selection when every top-level block is selected", () => {
+ const editor = new TestElement("editor", false);
+ const decoration = new TestElement("decoration", false);
+ const first = new TestElement("first");
+ const second = new TestElement("second");
+ editor.append(decoration, first, second);
+ first.classList.add(BLOCK_SELECTION_CLASS);
+ second.classList.add(BLOCK_SELECTION_CLASS);
+
+ assert.equal(getSelectAllBlockAction(asElement(editor)), "keep");
+ });
+
+ it("expands a nested block selection instead of selecting text", () => {
+ const editor = new TestElement("editor", false);
+ const container = new TestElement("container");
+ const child = new TestElement("child");
+ editor.append(container.append(child));
+ child.classList.add(BLOCK_SELECTION_CLASS);
+
+ assert.equal(getSelectAllBlockAction(asElement(editor)), "select-all");
+ });
+});
+
describe("block selection mode state", () => {
it("restores the active block and marked endpoints after replacing a subtree", () => {
const editor = new TestElement("editor", false);
diff --git a/app/src/protyle/wysiwyg/blockSelection.ts b/app/src/protyle/wysiwyg/blockSelection.ts
index 1b787bd52d..c94d344323 100644
--- a/app/src/protyle/wysiwyg/blockSelection.ts
+++ b/app/src/protyle/wysiwyg/blockSelection.ts
@@ -55,6 +55,17 @@ export const clearBlockSelectionMode = (editorElement: Element, clearMarks = fal
}
};
+export const getSelectAllBlockAction = (editorElement: Element): "none" | "select-all" | "keep" => {
+ const selectedElements = editorElement.querySelectorAll(`.${BLOCK_SELECTION_CLASS}`);
+ if (selectedElements.length === 0) {
+ return "none";
+ }
+ const topLevelBlockElements = Array.from(editorElement.children).filter(item =>
+ item.getAttribute("data-node-id"));
+ return topLevelBlockElements.length > 0 && topLevelBlockElements.every(item =>
+ item.classList.contains(BLOCK_SELECTION_CLASS)) ? "keep" : "select-all";
+};
+
export const captureBlockSelectionModeState = (rootElement: Element): IBlockSelectionModeState => {
const elements = [rootElement, ...Array.from(rootElement.querySelectorAll("[data-node-id]"))];
const selectionModeElement = elements.find(item => item.classList.contains(BLOCK_SELECTION_MODE_CLASS));