Skip to content

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

:sparkles: Support selecting all loaded blocks from the mobile multi-select toolbar https://github.com/siyuan-note/siyuan/issues/19590

Danielcd0b8a046a8a
diff --git a/app/src/mobile/util/multiSelectToolbar.test.ts b/app/src/mobile/util/multiSelectToolbar.test.ts
index b8fe7e1150..df1e6ddace 100644
--- a/app/src/mobile/util/multiSelectToolbar.test.ts
+++ b/app/src/mobile/util/multiSelectToolbar.test.ts
@@ -40,7 +40,7 @@ describe("shared mobile multi-select toolbar", () => {
before(() => {
Object.defineProperty(globalThis, "window", {
configurable: true,
- value: {siyuan: {languages: {more: "More", close: "Close"}}},
+ value: {siyuan: {languages: {more: "More", close: "Close", selectAll: "Select all"}}},
});
});
after(() => {
@@ -51,7 +51,7 @@ describe("shared mobile multi-select toolbar", () => {
}
});
- const createToolbar = () => {
+ const createToolbar = (onSelectAll?: () => void) => {
const count = {textContent: ""};
const menu = {dataset: {type: "menu"}, disabled: false};
const close = {dataset: {type: "exitMultiSelectMode"}, disabled: false};
@@ -64,7 +64,7 @@ describe("shared mobile multi-select toolbar", () => {
querySelector: (selector: string) => selector === ".multiSelectCount" ? count : menu,
firstElementChild: {addEventListener: (_type: string, callback: typeof listener) => { listener = callback; }},
} as unknown as HTMLElement;
- renderMultiSelectToolbar(element, 1, () => { menuCalls++; }, () => { exitCalls++; });
+ renderMultiSelectToolbar(element, 1, () => { menuCalls++; }, () => { exitCalls++; }, onSelectAll);
const click = (button: typeof menu | null) => {
let prevented = false;
let stopped = false;
@@ -79,6 +79,17 @@ describe("shared mobile multi-select toolbar", () => {
return {element, count, menu, close, click, calls: () => [menuCalls, exitCalls]};
};
+ it("offers select all only when requested and allows it with zero selected blocks", () => {
+ assert.equal(createToolbar().element.innerHTML.includes('data-type="selectAll"'), false);
+ let calls = 0;
+ const toolbar = createToolbar(() => { calls++; });
+ assert.equal(toolbar.element.innerHTML.includes('data-type="selectAll"'), true);
+ updateMultiSelectToolbar(toolbar.element, 0);
+ toolbar.click({dataset: {type: "selectAll"}, disabled: false});
+ assert.equal(calls, 1);
+ assert.deepEqual(toolbar.calls(), [0, 0]);
+ });
+
it("disables the menu for an empty selection and enables it when selection resumes", () => {
const toolbar = createToolbar();
assert.equal(toolbar.count.textContent, "1");
diff --git a/app/src/mobile/util/multiSelectToolbar.ts b/app/src/mobile/util/multiSelectToolbar.ts
index 12e01c222a..26f26bff02 100644
--- a/app/src/mobile/util/multiSelectToolbar.ts
+++ b/app/src/mobile/util/multiSelectToolbar.ts
@@ -12,7 +12,8 @@ export const updateMultiSelectToolbar = (element: HTMLElement, count: number) =>
(element.querySelector('[data-type="menu"]') as HTMLButtonElement).disabled = count === 0;
};
-export const renderMultiSelectToolbar = (element: HTMLElement, count: number, onMenu: () => void, onExit: () => void) => {
+export const renderMultiSelectToolbar = (element: HTMLElement, count: number, onMenu: () => void, onExit: () => void,
+ onSelectAll?: () => void) => {
element.style.padding = "0";
element.innerHTML = `<div class="block__icons">
<div class="block__logo">
@@ -20,6 +21,7 @@ export const renderMultiSelectToolbar = (element: HTMLElement, count: number, on
<span class="multiSelectCount"></span>
</div>
<span class="fn__flex-1"></span>
+ ${onSelectAll ? `<button class="block__icon block__icon--show" data-type="selectAll" aria-label="${window.siyuan.languages.selectAll}"><svg><use xlink:href="#iconSelectAll"></use></svg></button><span class="fn__space"></span>` : ""}
<button class="block__icon block__icon--show" data-type="menu" data-menu="true" aria-label="${window.siyuan.languages.more}"><svg><use xlink:href="#iconMore"></use></svg></button>
<span class="fn__space"></span>
<button class="block__icon block__icon--show" data-type="exitMultiSelectMode" aria-label="${window.siyuan.languages.close}"><svg><use xlink:href="#iconClose"></use></svg></button>
@@ -32,6 +34,8 @@ export const renderMultiSelectToolbar = (element: HTMLElement, count: number, on
onMenu();
} else if (button.dataset.type === "exitMultiSelectMode") {
onExit();
+ } else if (button.dataset.type === "selectAll") {
+ onSelectAll?.();
}
}
event.preventDefault();
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts
index 14b3a9eb86..c17b6692c0 100644
--- a/app/src/protyle/toolbar/index.ts
+++ b/app/src/protyle/toolbar/index.ts
@@ -1,5 +1,7 @@
import {Divider} from "./Divider";
-import {renderMultiSelectToolbar} from "../../mobile/util/multiSelectToolbar";
+import {renderMultiSelectToolbar, updateMultiSelectToolbar} from "../../mobile/util/multiSelectToolbar";
+import {selectAllLoadedBlocks} from "../wysiwyg/blockSelection";
+import {showSelectAllIncompleteTip} from "../util/selectAllTip";
import {ContractFormData} from "../../util/contractFormData";
import {Font, hasSameTextStyle, setFontStyle} from "./Font";
import {
@@ -2094,6 +2096,12 @@ export class Toolbar {
this.subElement.classList.add("fn__none");
this.subElement.innerHTML = "";
hideElements(["select"], protyle);
+ }, () => {
+ window.getSelection()?.removeAllRanges();
+ activeBlur();
+ const elements = selectAllLoadedBlocks(protyle.wysiwyg.element);
+ updateMultiSelectToolbar(this.subElement, elements.length);
+ showSelectAllIncompleteTip(protyle);
});
this.subElement.style.zIndex = (++window.siyuan.zIndex).toString();
this.subElement.classList.remove("fn__none");
diff --git a/app/src/protyle/util/selectAllTip.ts b/app/src/protyle/util/selectAllTip.ts
new file mode 100644
index 0000000000..fa767b2e6f
--- /dev/null
+++ b/app/src/protyle/util/selectAllTip.ts
@@ -0,0 +1,11 @@
+import {showMessage} from "../../dialog/message";
+import {hasUnloadedDocumentBlocks} from "./documentRange";
+
+export const showSelectAllIncompleteTip = (protyle: IProtyle) => {
+ if (window.siyuan.config.appearance.notifications?.selectAllIncompleteTip === false ||
+ !hasUnloadedDocumentBlocks(protyle.wysiwyg.element,
+ !protyle.lite && !protyle.block.showAll && protyle.block.scroll && !protyle.options.backlinkData)) {
+ return;
+ }
+ showMessage(window.siyuan.languages.selectAllIncompleteTip, 6000, "info", "selectAllIncompleteTip");
+};
diff --git a/app/src/protyle/wysiwyg/blockSelection.test.ts b/app/src/protyle/wysiwyg/blockSelection.test.ts
index ce700f0b66..9460b4019e 100644
--- a/app/src/protyle/wysiwyg/blockSelection.test.ts
+++ b/app/src/protyle/wysiwyg/blockSelection.test.ts
@@ -9,7 +9,8 @@ import {
getSelectAllBlockAction,
isContinuousBlockSelection,
restoreBlockSelectionModeState,
- runBlockSelectionDelete
+ runBlockSelectionDelete,
+ selectAllLoadedBlocks
} from "./blockSelection";
class TestElement {
@@ -127,6 +128,36 @@ describe("getBlockSelectionToggle", () => {
});
});
+describe("selectAllLoadedBlocks", () => {
+ it("replaces nested and partial selections with top-level blocks only", () => {
+ const editor = new TestElement("editor", false);
+ const child = new TestElement("child");
+ const container = new TestElement("container").append(child);
+ const sibling = new TestElement("sibling");
+ const decoration = new TestElement("decoration", false);
+ editor.append(container, decoration, sibling);
+ child.classList.add(BLOCK_SELECTION_CLASS, BLOCK_SELECTION_MODE_CLASS);
+ child.setAttribute("select-start", "1");
+ child.setAttribute("select-end", "2");
+
+ assert.deepEqual(selectAllLoadedBlocks(asElement(editor)), [container, sibling]);
+ assert.deepEqual(editor.querySelectorAll(`.${BLOCK_SELECTION_CLASS}`), [container, sibling]);
+ assert.equal(child.getAttribute("select-start"), null);
+ assert.equal(child.getAttribute("select-end"), null);
+ assert.equal(child.classList.contains(BLOCK_SELECTION_MODE_CLASS), false);
+ assert.deepEqual(selectAllLoadedBlocks(asElement(editor)), [container, sibling]);
+ });
+
+ it("selects from zero marks and supports an empty editor", () => {
+ const editor = new TestElement("editor", false);
+ assert.deepEqual(selectAllLoadedBlocks(asElement(editor)), []);
+ const block = new TestElement("block");
+ editor.append(block);
+ assert.deepEqual(selectAllLoadedBlocks(asElement(editor)), [block]);
+ assert.equal(block.classList.contains(BLOCK_SELECTION_CLASS), true);
+ });
+});
+
describe("getSelectAllBlockAction", () => {
it("continues with text selection when no block is selected", () => {
const editor = new TestElement("editor", false);
diff --git a/app/src/protyle/wysiwyg/blockSelection.ts b/app/src/protyle/wysiwyg/blockSelection.ts
index c94d344323..ba415b39e7 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 selectAllLoadedBlocks = (editorElement: Element) => {
+ clearBlockSelectionMode(editorElement, true);
+ const elements = Array.from(editorElement.children).filter(item => item.getAttribute("data-node-id"));
+ elements.forEach(item => {
+ item.removeAttribute("select-start");
+ item.removeAttribute("select-end");
+ item.classList.add(BLOCK_SELECTION_CLASS);
+ });
+ return elements;
+};
+
export const getSelectAllBlockAction = (editorElement: Element): "none" | "select-all" | "keep" => {
const selectedElements = editorElement.querySelectorAll(`.${BLOCK_SELECTION_CLASS}`);
if (selectedElements.length === 0) {
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index e83a853b90..375035fe31 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -29,7 +29,7 @@ import {
moveCaretForSemanticDelete,
removeEmptySemanticInlineElement
} from "../util/inlineElementMarker";
-import {hasUnloadedDocumentBlocks} from "../util/documentRange";
+import {showSelectAllIncompleteTip} from "../util/selectAllTip";
import {endTrackedRangeInsertion, prepareTrackedRangeInsertion} from "../util/trackedRange";
import {
hasClosestBlock,
@@ -226,13 +226,6 @@ const preserveAVSelectionOnKeyup = (protyle: IProtyle, event: KeyboardEvent) =>
}
};
-const showSelectAllIncompleteTip = () => {
- if (window.siyuan.config.appearance.notifications?.selectAllIncompleteTip === false) {
- return;
- }
- showMessage(window.siyuan.languages.selectAllIncompleteTip, 6000, "info", "selectAllIncompleteTip");
-};
-
const getAdjacentInlineMath = (range: Range, editableElement: Element, previous: boolean): HTMLElement | undefined => {
if (range.startContainer !== editableElement && !editableElement.contains(range.startContainer)) {
return;
@@ -1693,11 +1686,8 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
if (selectedCurrentContent && !protyle.lite &&
!nodeElement.classList.contains("code-block") && !isMobile()) {
showSelectAllTip();
- } else if (!selectedCurrentContent && hasUnloadedDocumentBlocks(
- protyle.wysiwyg.element,
- !protyle.lite && !protyle.block.showAll && protyle.block.scroll && !protyle.options.backlinkData
- )) {
- showSelectAllIncompleteTip();
+ } else if (!selectedCurrentContent) {
+ showSelectAllIncompleteTip(protyle);
}
return true;
}