Skip to content

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

:bug: Improve document tree Shift selection https://github.com/siyuan-note/siyuan/issues/19421

Daniel552e2f14713d
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
index a1e3f3ee31..2d80e0ed2d 100644
--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
@@ -7,7 +7,7 @@
"id": "20200924100950-9op5xi1",
"title": "Shortcuts",
"type": "doc",
- "updated": "20260910080119"
+ "updated": "20260913160005"
},
"Children": [
{
@@ -16278,7 +16278,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20220619002135-5dfw1yo",
- "updated": "20260811122156"
+ "updated": "20260913160005"
},
"Children": [
{
@@ -16298,7 +16298,7 @@
"Properties": {
"colgroup": "||",
"id": "20220619002135-s399g7e",
- "updated": "20260811122156"
+ "updated": "20260913160005"
},
"Children": [
{
@@ -16504,25 +16504,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "You need to use "
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Ctrl"
- },
- {
- "Type": "NodeText",
- "Data": "​+"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "click"
- },
- {
- "Type": "NodeText",
- "Data": "​ to select the first document"
+ "Data": "Select visible rows from the most recently clicked document; clicking a parent document name or icon to expand it also sets the anchor. Repeated Shift clicks extend or shrink the range while keeping the anchor. If the anchor is unavailable, use a visible selected document; without one, select only the clicked document"
}
]
}
diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
index 4a50c2d449..a91218434e 100644
--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
@@ -7,7 +7,7 @@
"id": "20200813004551-gm0pbn1",
"title": "快捷键",
"type": "doc",
- "updated": "20260910080119"
+ "updated": "20260913160005"
},
"Children": [
{
@@ -16236,7 +16236,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20220619001156-26volkp",
- "updated": "20260811122156"
+ "updated": "20260913160005"
},
"Children": [
{
@@ -16256,7 +16256,7 @@
"Properties": {
"colgroup": "||",
"id": "20220619001217-e9cwukj",
- "updated": "20260811122156"
+ "updated": "20260913160005"
},
"Children": [
{
@@ -16454,25 +16454,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "需要先使用 "
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Ctrl"
- },
- {
- "Type": "NodeText",
- "Data": "​+"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "点击"
- },
- {
- "Type": "NodeText",
- "Data": "​ 选中首个文档"
+ "Data": "以最近点击的文档为起点选择可见范围,点击父文档名称或图标展开时也会更新起点;连续按住 Shift 点击可扩展或缩小选区,起点保持不变;起点失效时使用可见的已选文档,没有可用起点时仅选中本次点击的文档"
}
]
}
diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
index f17a0876c5..3959392b4b 100644
--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
@@ -7,7 +7,7 @@
"id": "20211226122549-jktxego",
"title": "快捷鍵",
"type": "doc",
- "updated": "20260910080119"
+ "updated": "20260913160005"
},
"Children": [
{
@@ -16266,7 +16266,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20220619002223-zliquo3",
- "updated": "20260811122156"
+ "updated": "20260913160005"
},
"Children": [
{
@@ -16286,7 +16286,7 @@
"Properties": {
"colgroup": "||",
"id": "20220619002223-xwob5s8",
- "updated": "20260811122156"
+ "updated": "20260913160005"
},
"Children": [
{
@@ -16492,25 +16492,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "需要先使用 "
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Ctrl"
- },
- {
- "Type": "NodeText",
- "Data": "​+"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "點擊"
- },
- {
- "Type": "NodeText",
- "Data": "​ 選取首個文件"
+ "Data": "以最近點擊的文檔為起點選取可見範圍,點擊父文檔名稱或圖示展開時也會更新起點;連續按住 Shift 點擊可擴展或縮小選取範圍,起點保持不變;起點失效時使用可見的已選文檔,沒有可用起點時僅選取本次點擊的文檔"
}
]
}
diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
index 383cdf28ce..bb4f6d0934 100644
--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
@@ -9,7 +9,7 @@
"id": "20240530101000-xsbxokr",
"title": "ショートカット",
"type": "doc",
- "updated": "20260908202835"
+ "updated": "20260913160005"
},
"Children": [
{
@@ -16272,7 +16272,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20240530101000-mifghto",
- "updated": "20260811122156"
+ "updated": "20260913160005"
},
"Children": [
{
@@ -16292,7 +16292,7 @@
"Properties": {
"colgroup": "||",
"id": "20240530101000-4iuhg30",
- "updated": "20260811122156"
+ "updated": "20260913160005"
},
"Children": [
{
@@ -16490,25 +16490,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "まずは "
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "Ctrl"
- },
- {
- "Type": "NodeText",
- "Data": "​+"
- },
- {
- "Type": "NodeTextMark",
- "TextMarkType": "kbd",
- "TextMarkTextContent": "クリック"
- },
- {
- "Type": "NodeText",
- "Data": "​ で最初の文書を選択する必要があります"
+ "Data": "最後にクリックしたドキュメントを起点として、表示されている行を範囲選択します。親ドキュメントの名前やアイコンをクリックして展開した場合も起点を更新します。Shift を押しながら繰り返しクリックすると、起点を維持したまま選択範囲を拡大・縮小できます。起点が無効な場合は表示中の選択済みドキュメントを使い、該当するものがなければクリックしたドキュメントだけを選択します"
}
]
}
diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts
index 2a4a3083d4..83f75cf327 100644
--- a/app/src/layout/dock/Files.ts
+++ b/app/src/layout/dock/Files.ts
@@ -77,6 +77,7 @@ import {clearDocumentTabMovePreview} from "../tabDrag";
import {reorderSortedFileTree} from "../../util/fileTreeReorder";
import {getHostCapabilities} from "../../util/hostCapabilities";
import {PinnedDocs} from "./PinnedDocs";
+import {selectFileTreeRange} from "./fileTreeSelection";
export class Files extends Model {
public element: HTMLElement;
@@ -259,7 +260,7 @@ export class Files extends Model {
if (ulElement) {
const notebookId = ulElement.getAttribute("data-url");
while (target && !target.isEqualNode(this.element)) {
- if (isNotCtrl(event) && target.classList.contains("b3-list-item__icon") && window.siyuan.config.system.container !== "ios") {
+ if (isNotCtrl(event) && !event.shiftKey && target.classList.contains("b3-list-item__icon") && window.siyuan.config.system.container !== "ios") {
event.preventDefault();
event.stopPropagation();
const liElement = target.parentElement;
@@ -268,6 +269,8 @@ export class Files extends Model {
const isBoxDoc = isNotebook && liElement.getAttribute("data-node-id");
if ((isFile || isNotebook) && window.siyuan.config.fileTree.docIconClickExpand) {
if (Number(liElement.getAttribute("data-count")) > 0) {
+ this.lastSelectedElement = liElement;
+ this.setCurrent(liElement, false);
this.toggleLeaf(liElement, notebookId);
break;
} else if (isFile || isBoxDoc) {
@@ -367,6 +370,8 @@ export class Files extends Model {
(target.parentElement.getAttribute("data-type") === "navigation-root" && target.parentElement.getAttribute("data-node-id"))) &&
window.siyuan.config.fileTree.parentDocClickExpand &&
Number(target.parentElement.getAttribute("data-count")) > 0) {
+ this.lastSelectedElement = target.parentElement;
+ this.setCurrent(target.parentElement, false);
this.toggleLeaf(target.parentElement, notebookId);
event.preventDefault();
event.stopPropagation();
@@ -377,35 +382,7 @@ export class Files extends Model {
target.classList.toggle("b3-list-item--focus");
this.lastSelectedElement = target;
} else if (event.shiftKey && !event.altKey && isNotCtrl(event)) {
- // Shift+click 多选文档
- if (!document.contains(this.lastSelectedElement)) {
- this.lastSelectedElement = null;
- }
- if (!this.lastSelectedElement) {
- this.lastSelectedElement = this.element.querySelector(".b3-list-item--focus");
- }
- if (!this.lastSelectedElement) {
- this.lastSelectedElement = target.parentElement.firstElementChild;
- }
- this.element.querySelectorAll(".b3-list-item--focus").forEach(item => {
- item.classList.remove("b3-list-item--focus");
- });
-
- // 获取所有文档项
- const allFiles = Array.from(this.element.querySelectorAll("li.b3-list-item"));
-
- // 获取起始和结束索引
- const startIndex = allFiles.indexOf(this.lastSelectedElement);
- const endIndex = allFiles.indexOf(target);
-
- // 确定选择范围
- const start = Math.min(startIndex, endIndex);
- const end = Math.max(startIndex, endIndex);
-
- // 添加新选择
- for (let i = start; i <= end; i++) {
- (allFiles[i] as HTMLElement).classList.add("b3-list-item--focus");
- }
+ this.lastSelectedElement = selectFileTreeRange(this.element, this.lastSelectedElement, target);
} else {
this.lastSelectedElement = target;
this.setCurrent(target, false);
diff --git a/app/src/layout/dock/fileTreeSelection.test.ts b/app/src/layout/dock/fileTreeSelection.test.ts
new file mode 100644
index 0000000000..218744a1ff
--- /dev/null
+++ b/app/src/layout/dock/fileTreeSelection.test.ts
@@ -0,0 +1,81 @@
+import * as assert from "node:assert/strict";
+import test from "node:test";
+import {getVisibleFileTreeItems, selectFileTreeRange} from "./fileTreeSelection";
+
+const fixture = () => {
+ const items = Array.from({length: 5}, () => {
+ const classes = new Set<string>();
+ return {
+ classList: {
+ contains: (name: string) => classes.has(name),
+ add: (name: string) => classes.add(name),
+ remove: (name: string) => classes.delete(name),
+ },
+ getClientRects: () => [{}],
+ parentElement: null,
+ } as unknown as Element;
+ });
+ const root = {
+ querySelectorAll: (selector: string) => selector === "li.b3-list-item" ? items :
+ items.filter((item) => item.classList.contains("b3-list-item--focus")),
+ } as unknown as HTMLElement;
+ const selected = () => items.flatMap((item, index) =>
+ item.classList.contains("b3-list-item--focus") ? [index] : []);
+ return {items, root, selected};
+};
+
+test("successive Shift clicks expand, shrink and reverse around a fixed anchor", () => {
+ const {items, root, selected} = fixture();
+ let anchor = selectFileTreeRange(root, items[1], items[4]);
+ assert.deepEqual(selected(), [1, 2, 3, 4]);
+ anchor = selectFileTreeRange(root, anchor, items[2]);
+ assert.deepEqual(selected(), [1, 2]);
+ assert.equal(selectFileTreeRange(root, anchor, items[0]), items[1]);
+ assert.deepEqual(selected(), [0, 1]);
+});
+
+test("first Shift click selects only its target and establishes an anchor", () => {
+ const {items, root, selected} = fixture();
+ const anchor = selectFileTreeRange(root, null, items[2]);
+ assert.deepEqual(selected(), [2]);
+ selectFileTreeRange(root, anchor, items[4]);
+ assert.deepEqual(selected(), [2, 3, 4]);
+});
+
+test("detached and hidden anchors fall back to a visible selected row", () => {
+ const {items, root, selected} = fixture();
+ items[0].getClientRects = () => [] as unknown as DOMRectList;
+ items[0].classList.add("b3-list-item--focus");
+ items[2].classList.add("b3-list-item--focus");
+ assert.equal(selectFileTreeRange(root, items[0], items[4]), items[2]);
+ assert.deepEqual(selected(), [2, 3, 4]);
+ assert.equal(selectFileTreeRange(root, {} as Element, items[3]), items[2]);
+ assert.deepEqual(selected(), [2, 3]);
+});
+
+test("hidden rows are excluded and an invalid anchor without visible selection uses the target", () => {
+ const {items, root, selected} = fixture();
+ items[2].getClientRects = () => [] as unknown as DOMRectList;
+ selectFileTreeRange(root, items[0], items[4]);
+ assert.deepEqual(selected(), [0, 1, 3, 4]);
+ items.forEach((item) => item.classList.remove("b3-list-item--focus"));
+ assert.equal(selectFileTreeRange(root, items[2], items[3]), items[3]);
+ assert.deepEqual(selected(), [3]);
+});
+
+test("descendants of collapsing lists are excluded before their DOM is removed", () => {
+ const {items, root} = fixture();
+ let expanded = false;
+ const list = {
+ tagName: "UL",
+ parentElement: root,
+ previousElementSibling: {
+ matches: () => true,
+ querySelector: () => expanded ? {} : null,
+ },
+ };
+ Object.defineProperty(items[2], "parentElement", {value: {tagName: "UL", parentElement: list}});
+ assert.deepEqual(getVisibleFileTreeItems(root), [items[0], items[1], items[3], items[4]]);
+ expanded = true;
+ assert.deepEqual(getVisibleFileTreeItems(root), items);
+});
diff --git a/app/src/layout/dock/fileTreeSelection.ts b/app/src/layout/dock/fileTreeSelection.ts
new file mode 100644
index 0000000000..64d543c198
--- /dev/null
+++ b/app/src/layout/dock/fileTreeSelection.ts
@@ -0,0 +1,37 @@
+const focusClass = "b3-list-item--focus";
+
+export const getVisibleFileTreeItems = (root: HTMLElement): Element[] => {
+ return Array.from(root.querySelectorAll("li.b3-list-item")).filter((item) => {
+ if (!item.getClientRects().length) {
+ return false;
+ }
+ // 折叠动画中的子列表仍在 DOM 中,通过父节点的展开状态排除这些行。
+ let parent = item.parentElement;
+ while (parent && parent !== root) {
+ const owner = parent.previousElementSibling;
+ if (parent.tagName === "UL" && owner?.matches("li.b3-list-item") &&
+ !owner.querySelector(".b3-list-item__arrow--open")) {
+ return false;
+ }
+ parent = parent.parentElement;
+ }
+ return true;
+ });
+};
+
+export const selectFileTreeRange = (root: HTMLElement, anchor: Element, target: Element): Element => {
+ const items = getVisibleFileTreeItems(root);
+ const end = items.indexOf(target);
+ if (end < 0) {
+ return items.includes(anchor) ? anchor : null;
+ }
+ if (!items.includes(anchor)) {
+ anchor = items.find((item) => item.classList.contains(focusClass)) || target;
+ }
+ const start = items.indexOf(anchor);
+ root.querySelectorAll(`.${focusClass}`).forEach((item) => item.classList.remove(focusClass));
+ items.slice(Math.min(start, end), Math.max(start, end) + 1).forEach((item) => {
+ item.classList.add(focusClass);
+ });
+ return anchor;
+};