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: Preserve selected block gutters after keyboard dismissal https://github.com/siyuan-note/siyuan/issues/19909
Daniel541ba121f472
diff --git a/app/src/protyle/gutter/restore.test.ts b/app/src/protyle/gutter/restore.test.tsindex 0a766894e9..b55fa914e9 100644--- a/app/src/protyle/gutter/restore.test.ts+++ b/app/src/protyle/gutter/restore.test.ts@@ -106,6 +106,7 @@ const fixture = () => {return {root, blocks, editable, selection, ownerDocument, protyle, calls,render: (selectedElement?: TestElement) => restoreExports.restoreGutterBySelection(protyle, selectedElement),+ restore: restoreExports.restoreGutterBySelection,resize: () => resizeExports.resize(protyle),clearGutter: (): void => { rendered = undefined; },rendered: () => rendered,@@ -190,6 +191,72 @@ test("keyboard dismissal can leave focus on body while retaining this editor's sassert.equal(f.rendered(), undefined);});+test("keyboard resize retains the completed block selection after the native caret is cleared", () => {+ const f = fixture();+ f.root.selected = [f.blocks[0], f.blocks[1]];+ f.selection.focusNode = undefined;+ f.ownerDocument.activeElement = f.ownerDocument.body;+ f.render(f.blocks[0]);+ f.resize();+ f.flushTimers();+ f.flushFrames();+ assert.equal(f.rendered(), f.blocks[0]);+ f.blocks[0].bottom = -1;+ f.resize();+ f.flushTimers();+ f.flushFrames();+ assert.equal(f.rendered(), f.blocks[1], "the visible selection supplies the gutter after another layout change");+});++test("native caret removal after a completed selection retains only the current editor's gutter", () => {+ const f = fixture();+ f.root.selected = [f.blocks[0], f.blocks[1]];+ f.render();+ f.selection.focusNode = undefined;+ f.ownerDocument.activeElement = f.ownerDocument.body;+ f.clearGutter();+ f.render();+ assert.equal(f.rendered(), f.blocks[0]);++ const peerRoot = new TestElement(undefined, false, true);+ peerRoot.ownerDocument = f.ownerDocument;+ const peerBlock = new TestElement(peerRoot, true);+ peerRoot.selected = [peerBlock];+ const peerProtyle = {...f.protyle, wysiwyg: {element: peerRoot}};+ f.restore(peerProtyle, peerBlock);+ assert.equal(f.rendered(), peerBlock);+ f.clearGutter();+ f.render();+ assert.equal(f.rendered(), undefined, "an inactive split editor cannot reclaim the gutter");+ f.restore(peerProtyle);+ assert.equal(f.rendered(), peerBlock);+});++test("clearing selection, hiding the editor, or moving focus invalidates retained ownership", () => {+ const changes = [+ (f: ReturnType<typeof fixture>): void => { f.root.selected = []; },+ (f: ReturnType<typeof fixture>) => f.root.visible = false,+ (f: ReturnType<typeof fixture>) => f.ownerDocument.activeElement = new TestElement(),+ (f: ReturnType<typeof fixture>) => f.selection.focusNode = new TestElement(),+ ];+ changes.forEach(change => {+ const f = fixture();+ f.root.selected = [f.blocks[0], f.blocks[1]];+ f.render();+ f.selection.focusNode = undefined;+ change(f);+ f.clearGutter();+ f.render();+ assert.equal(f.rendered(), undefined);+ f.root.visible = true;+ f.root.selected = [f.blocks[0], f.blocks[1]];+ f.ownerDocument.activeElement = f.ownerDocument.body;+ f.selection.focusNode = undefined;+ f.render();+ assert.equal(f.rendered(), undefined, "the previous selection owner must not be resurrected");+ });+});+test("restoration respects hidden editors, active drags, mobile multi-select, and disabled gutters", () => {const changes = [(f: ReturnType<typeof fixture>) => f.root.isConnected = false,diff --git a/app/src/protyle/gutter/restore.ts b/app/src/protyle/gutter/restore.tsindex f61947c81b..c95a5f47b2 100644--- a/app/src/protyle/gutter/restore.ts+++ b/app/src/protyle/gutter/restore.ts@@ -4,6 +4,9 @@ import {BLOCK_SELECTION_CLASS} from "../wysiwyg/blockSelection";import {getEmbedGutterOperationContext} from "../wysiwyg/getBlock";import {isMobile} from "../../util/functions";+// 每个页面只保留当前块选区所属的编辑器,弱引用集合避免保留已关闭的编辑器。+const blockSelectionOwners = new WeakMap<Document, WeakSet<HTMLElement>>();+export const restoreGutterBySelection = (protyle: IProtyle, selectedElement?: Element) => {const root = protyle.wysiwyg.element;const ownerDocument = root.ownerDocument;@@ -15,25 +18,34 @@ export const restoreGutterBySelection = (protyle: IProtyle, selectedElement?: Elroot.classList.contains("protyle-wysiwyg--hiderange") ||(activeElement && activeElement !== ownerDocument.body &&activeElement.closest(".protyle-wysiwyg") !== root)) {+ blockSelectionOwners.get(ownerDocument)?.delete(root);return;}const selection = ownerDocument.getSelection();const selected = Array.from(root.querySelectorAll<HTMLElement>(`.${BLOCK_SELECTION_CLASS}`)).filter(item => item.closest(".protyle-wysiwyg") === root);+ if (selected.length === 0) {+ blockSelectionOwners.get(ownerDocument)?.delete(root);+ }let pointerElement = selection?.focusNode?.nodeType === Node.ELEMENT_NODE ?selection.focusNode as Element : selection?.focusNode?.parentElement;if (pointerElement && pointerElement.closest(".protyle-wysiwyg") !== root) {+ blockSelectionOwners.get(ownerDocument)?.delete(root);return;}if (!pointerElement) {// 在文档末尾外侧结束框选时可能没有文字光标,使用仍然选中的块作为定位目标。- if (!selectedElement || !selected.includes(selectedElement as HTMLElement)) {+ const target = selectedElement && selected.includes(selectedElement as HTMLElement) ? selectedElement :+ (blockSelectionOwners.get(ownerDocument)?.has(root) ? selected[0] : undefined);+ if (!target) {return;}- pointerElement = selectedElement;+ pointerElement = target;}let block = hasClosestBlock(pointerElement);if (selected.length > 0) {+ // 键盘收起可能清空原生文字选区,后续布局刷新仍可定位到当前有效的块选区。+ blockSelectionOwners.set(ownerDocument, new WeakSet([root]));const rect = protyle.contentElement.getBoundingClientRect();const visible = selected.filter(item => {const blockRect = item.getBoundingClientRect();diff --git a/app/src/protyle/wysiwyg/boundedBlockDragSelect.test.ts b/app/src/protyle/wysiwyg/boundedBlockDragSelect.test.tsindex 5e3d503771..accf6baef6 100644--- a/app/src/protyle/wysiwyg/boundedBlockDragSelect.test.ts+++ b/app/src/protyle/wysiwyg/boundedBlockDragSelect.test.ts@@ -21,6 +21,7 @@ const fixture = (mobile = false) => {let modeElement: unknown;let toolbarCount = 0;let gutterRestored = 0;+ let gutterTarget: unknown;const frames: FrameRequestCallback[] = [];const states = [new Set<string>(), new Set<string>()];const blocks = states.map((state, index) => ({@@ -64,7 +65,10 @@ const fixture = (mobile = false) => {getBlockRangeSelectElements: () => ({selectElements: blocks}),},"../ui/hideElements": {hideElements: () => {}},- "../gutter/restore": {restoreGutterBySelection: () => gutterRestored++},+ "../gutter/restore": {restoreGutterBySelection: (_protyle: unknown, block: unknown) => {+ gutterRestored++;+ gutterTarget = block;+ }},"../../util/functions": {isMobile: () => mobile},"../../layout/status": {countBlockWord: () => {}},"../../mobile/util/multiSelectToolbar": {updateMultiSelectToolbar: (_element: unknown, count: number) => toolbarCount = count},@@ -93,7 +97,7 @@ const fixture = (mobile = false) => {detach: () => blockOwned = false,enterMultiSelect: () => multiSelect = true,flushFrames: () => frames.splice(0).forEach(callback => callback(0)),- state: () => ({focused, modeElement, multiSelect, selectionCleared, toolbarCount, gutterRestored}),+ state: () => ({focused, modeElement, multiSelect, selectionCleared, toolbarCount, gutterRestored, gutterTarget}),};};@@ -145,6 +149,7 @@ test("desktop completion uses existing block selection and a focusable block witassert.equal(f.state().gutterRestored, 0);f.flushFrames();assert.equal(f.state().gutterRestored, 1);+ assert.equal(f.state().gutterTarget, f.blocks[1]);});test("mobile completion retains the multi-select toolbar and cancellation does not steal focus", () => {diff --git a/app/src/protyle/wysiwyg/boundedBlockDragSelect.ts b/app/src/protyle/wysiwyg/boundedBlockDragSelect.tsindex dbe5c9cc51..890b86edb3 100644--- a/app/src/protyle/wysiwyg/boundedBlockDragSelect.ts+++ b/app/src/protyle/wysiwyg/boundedBlockDragSelect.ts@@ -99,7 +99,7 @@ export const bindBoundedBlockDragSelect = (protyle: IProtyle, element: HTMLEleme// 保留单块内的折叠光标,让复制、剪切和删除复用已有块选区处理。focusBlock(activeBlock);setBlockSelectionModeElement(element, activeBlock);- requestAnimationFrame(() => restoreGutterBySelection(protyle));+ requestAnimationFrame(() => restoreGutterBySelection(protyle, activeBlock));}}countBlockWord(selected.map(item => item.getAttribute("data-node-id")), protyle);