Skip to content

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

:bug: Improve pinned section transitions and document drops https://github.com/siyuan-note/siyuan/issues/19401

Daniel2a42020019f1
diff --git a/app/src/layout/dock/PinnedDocs.test.ts b/app/src/layout/dock/PinnedDocs.test.ts
index 8b3e900fca..505d40a736 100644
--- a/app/src/layout/dock/PinnedDocs.test.ts
+++ b/app/src/layout/dock/PinnedDocs.test.ts
@@ -4,6 +4,7 @@ import {readFileSync} from "node:fs";
import {join} from "node:path";
import {runInNewContext} from "node:vm";
import * as ts from "typescript";
+import {getPinnedDropPosition} from "../../util/pinnedDocsDrop";
interface IPanelHarness {
element: unknown;
@@ -20,13 +21,13 @@ interface IPanelHarness {
names: Map<string, string>;
appendDoc(...args: unknown[]): Promise<void>;
loadChildren(row: unknown, children: unknown, generation: number): Promise<void>;
- drop(ids: string[], x: number, y: number): Promise<void>;
+ drop(ids: string[], x: number, y: number, allowSource?: boolean): Promise<void>;
click(event: unknown): void;
menu(row: unknown, x: number, y: number): void;
toggle(row: unknown): void;
open(id: string, notebook: string): void;
mobile?: boolean;
- previewDrop(): void;
+ previewDrop(x: number, y: number, allowSource?: boolean): boolean | void;
clearDrop(): void;
scheduleRefresh(changedID?: string): void;
dropTarget?: {id: string, position: string};
@@ -36,6 +37,7 @@ interface IPanelHarness {
}
const loadPanel = (fetchCode = 0) => {
+ const hitTest = {target: null as unknown};
const storage = new Map<string, string>();
const docs: {id: string, notebook: string, name: string}[] = [];
const childData = {effectiveSortMode: 6, files: [] as {id: string, name: string, icon?: string}[]};
@@ -53,13 +55,16 @@ const loadPanel = (fetchCode = 0) => {
exports,
localStorage: {setItem: (key: string, value: string) => storage.set(key, value)},
window: {siyuan: {config, notebooks: [], languages: {}}},
- document: {activeElement: null, createElement: (tagName: string) => ({
+ document: {activeElement: null, elementFromPoint: () => hitTest.target, createElement: (tagName: string) => ({
tagName, style: {setProperty: () => {}},
setAttribute: () => {}, addEventListener: () => {},
dataset: {}, children: [] as unknown[],
append(child: unknown) { this.children.push(child); },
})},
require: (name: string) => {
+ if (name.endsWith("/fileTreeAnimation")) { return {setFileTreeVisibility: (element: HTMLElement, visible: boolean) => element.classList.toggle("fn__none", !visible)}; }
+ if (name.endsWith("/pinnedDocsDrop")) { return {getPinnedDropPosition}; }
+ if (name.endsWith("/dragover")) { return {dragOverScroll: () => {}}; }
if (name.endsWith("/compatibility")) { return {isOnlyMeta: (event: MouseEvent) => Boolean(event.ctrlKey)}; }
if (name.endsWith("/fileTreeIcon")) { return {getFileTreeIconHTML: () => ""}; }
if (name.endsWith("/escape")) { return {escapeHtml: (value: string) => value}; }
@@ -81,7 +86,7 @@ const loadPanel = (fetchCode = 0) => {
panel.scheduleRefresh = () => {};
panel.list = {querySelectorAll: (): unknown[] => []};
panel.sourceTree = {querySelectorAll: (): unknown[] => []};
- return {panel, calls, config, docs, storage, childData};
+ return {panel, calls, config, docs, storage, childData, hitTest};
};
test("collapse clears descendant expansion and persists the closed section", async () => {
@@ -198,7 +203,7 @@ test("pinned icons respect editing, expansion and readonly settings", () => {
test("root drops create an entry without invoking source movement", async () => {
const {panel, calls} = loadPanel();
- panel.previewDrop = () => { panel.dropTarget = {id: "parent", position: "pin-before"}; };
+ panel.previewDrop = () => { panel.dropTarget = {id: "parent", position: "pin-before"}; return true; };
await panel.drop(["child"], 0, 0);
assert.deepEqual(calls, [{kind: "pin", args: [["child"], "pin", "parent", false]}]);
});
@@ -216,6 +221,45 @@ test("child ordering uses the existing conflict confirmation and inside drops mo
assert.equal(JSON.stringify(calls[0].args[1]), JSON.stringify({fromIDs: ["child"], toID: "parent"}));
});
+test("dropping on a notebook resolves its outer list ID with or without root documents", async () => {
+ const {panel, calls, hitTest} = loadPanel();
+ const highlights: string[] = [];
+ const row = {dataset: {type: "navigation-root", nodeId: ""},
+ closest: (selector: string) => selector === "ul[data-url]" ? {dataset: {url: "notebook"}} : null,
+ getBoundingClientRect: () => ({top: 0, height: 30}), classList: {add: (name: string) => highlights.push(name)}};
+ hitTest.target = {closest: () => row};
+ panel.sourceTree = {contains: () => true, getBoundingClientRect: () => ({})};
+ panel.element = {contains: () => false};
+ for (const id of ["", "notebook"]) {
+ row.dataset.nodeId = id;
+ await panel.drop(["document"], 10, 15, true);
+ }
+ assert.deepEqual(highlights, ["dragover", "dragover"]);
+ assert.equal(calls.length, 2);
+ calls.forEach(call => {
+ assert.equal(call.args[0], "/api/filetree/moveDocsByID");
+ assert.equal(JSON.stringify(call.args[1]), JSON.stringify({fromIDs: ["document"], toID: "notebook"}));
+ });
+});
+
+test("pinned heading highlights the whole row while root reorder keeps insertion lines", () => {
+ const {panel, hitTest} = loadPanel();
+ const highlights: string[] = [];
+ panel.sourceTree = {contains: () => false};
+ panel.element = {contains: () => true};
+ panel.list = {getBoundingClientRect: () => ({})};
+ panel.heading = {classList: {add: (name: string) => highlights.push(name)}};
+ hitTest.target = {closest: (selector: string) => selector === "[data-pin-heading]" ? {} : null};
+ panel.previewDrop(10, 0);
+ assert.deepEqual(highlights, ["dragover"]);
+ assert.equal(panel.dropTarget.id, "");
+ const row = {dataset: {pinRoot: "true", nodeId: "document"},
+ getBoundingClientRect: () => ({top: 0, height: 30}), classList: {add: (name: string) => highlights.push(name)}};
+ hitTest.target = {closest: () => row};
+ panel.previewDrop(10, 1);
+ assert.equal(highlights[1], "dragover__top");
+});
+
test("invalid drop targets and self moves do not mutate source documents", async () => {
const {panel, calls} = loadPanel();
panel.previewDrop = () => { panel.dropTarget = undefined; };
diff --git a/app/src/layout/dock/PinnedDocs.ts b/app/src/layout/dock/PinnedDocs.ts
index e8cb672c3a..56971f889e 100644
--- a/app/src/layout/dock/PinnedDocs.ts
+++ b/app/src/layout/dock/PinnedDocs.ts
@@ -14,6 +14,7 @@ import {MenuItem} from "../../menus/Menu";
import {newFileInTree} from "../../util/newFile";
import {isOnlyMeta} from "../../protyle/util/compatibility";
import {FILE_TREE_CHILDREN_SORT_MODE, FILE_TREE_EFFECTIVE_SORT_MODE} from "../../util/fileTreeSort";
+import {setFileTreeVisibility} from "./fileTreeAnimation";
interface IPinnedDoc {
id: string;
@@ -202,8 +203,8 @@ export class PinnedDocs {
this.scheduleRefresh();
}
- private setCollapsed(collapsed: boolean) {
- this.list.classList.toggle("fn__none", collapsed);
+ private setCollapsed(collapsed: boolean, animate = false) {
+ setFileTreeVisibility(this.list, !collapsed, animate);
this.heading.setAttribute("aria-expanded", String(!collapsed));
this.heading.querySelector("svg").classList.toggle("b3-list-item__arrow--open", !collapsed);
}
@@ -352,9 +353,12 @@ export class PinnedDocs {
if (expand) {
this.expanded.add(key);
await this.loadChildren(row, row.nextElementSibling as HTMLElement, this.generation);
+ if (this.expanded.has(key)) {
+ setFileTreeVisibility(row.nextElementSibling as HTMLElement, true, true);
+ }
} else {
this.expanded.delete(key);
- row.nextElementSibling.classList.add("fn__none");
+ setFileTreeVisibility(row.nextElementSibling as HTMLElement, false, true);
row.querySelector("svg").classList.remove("b3-list-item__arrow--open");
row.setAttribute("aria-expanded", "false");
}
@@ -385,7 +389,7 @@ export class PinnedDocs {
const target = event.target as Element;
if (target.closest("[data-pin-heading]")) {
const collapsed = this.heading.getAttribute("aria-expanded") === "true";
- this.setCollapsed(collapsed);
+ this.setCollapsed(collapsed, true);
localStorage.setItem("siyuan-pinned-docs-collapsed", String(collapsed));
return;
}
@@ -453,13 +457,16 @@ export class PinnedDocs {
if (source && (!row || row.closest("[data-encrypted=true]"))) { return false; }
if (!row) {
this.dropTarget = {id: "", position: "pin-before"};
- this.heading.classList.add("dragover__bottom");
+ this.heading.classList.add(target.closest("[data-pin-heading]") ? "dragover" : "dragover__bottom");
} else {
if (row.dataset.unavailable === "true") { return false; }
const rect = row.getBoundingClientRect();
const position = source && row.dataset.type === "navigation-root" ? "inside" :
getPinnedDropPosition(row.dataset.pinRoot === "true", (y - rect.top) / rect.height);
- this.dropTarget = {id: row.dataset.nodeId || row.dataset.url, position};
+ const id = source && row.dataset.type === "navigation-root" ?
+ row.closest<HTMLElement>("ul[data-url]")?.dataset.url : row.dataset.nodeId;
+ if (!id) { return false; }
+ this.dropTarget = {id, position};
row.classList.add(position === "inside" ? "dragover" : position.endsWith("before") ? "dragover__top" : "dragover__bottom");
}
const scrollElement = source ? this.sourceTree : this.list;
diff --git a/app/src/layout/dock/fileTreeAnimation.test.ts b/app/src/layout/dock/fileTreeAnimation.test.ts
index 0f6e937fae..1365bb5678 100644
--- a/app/src/layout/dock/fileTreeAnimation.test.ts
+++ b/app/src/layout/dock/fileTreeAnimation.test.ts
@@ -5,6 +5,7 @@ import {
collapseFileTree,
expandFileTree,
isFileTreeCollapsing,
+ setFileTreeVisibility,
toggleFileTree
} from "./fileTreeAnimation";
@@ -112,6 +113,31 @@ const createTreeElements = (expanded = true) => {
};
describe("fileTreeAnimation", () => {
+ it("retains pinned lists and cancels stale collapse when expanding again", async () => {
+ const animations = [createAnimation(), createAnimation()];
+ let hidden = false;
+ let index = 0;
+ const frames: Keyframe[][] = [];
+ const element = {
+ style: {overflow: "", removeProperty() { this.overflow = ""; }},
+ classList: {remove() { hidden = false; }, toggle(_name: string, value: boolean) { hidden = value; }},
+ getBoundingClientRect: () => ({height: 80}),
+ animate(value: Keyframe[]) { frames.push(value); return animations[index++].animation; },
+ } as unknown as HTMLElement;
+ setFileTreeVisibility(element, false, true);
+ assert.equal(hidden, false);
+ setFileTreeVisibility(element, true, true);
+ assert.equal(animations[0].isCanceled(), true);
+ await Promise.resolve();
+ assert.equal(hidden, false);
+ animations[1].finish();
+ await Promise.resolve();
+ assert.equal(hidden, false);
+ assert.equal(element.style.overflow, "");
+ assert.deepEqual(frames[0], [{height: "80px", maxHeight: "80px"}, {height: "0px", maxHeight: "0px"}]);
+ setFileTreeVisibility(element, false);
+ assert.equal(hidden, true);
+ });
it("expands to the actual content height and restores animation styles", async () => {
const tree = createTreeElements();
let finished = false;
diff --git a/app/src/layout/dock/fileTreeAnimation.ts b/app/src/layout/dock/fileTreeAnimation.ts
index 46ea0c06bd..b3fe8f8d8b 100644
--- a/app/src/layout/dock/fileTreeAnimation.ts
+++ b/app/src/layout/dock/fileTreeAnimation.ts
@@ -1,5 +1,35 @@
const FILE_TREE_ANIMATION_DURATION = 200;
const FILE_TREE_ANIMATION_EASING = "cubic-bezier(0, 0, .2, 1)";
+const visibilityAnimations = new WeakMap<HTMLElement, Animation>();
+
+// 保留置顶列表节点,通过高度过渡切换可见性,快速反向操作时取消上一次动画。
+export const setFileTreeVisibility = (element: HTMLElement, visible: boolean, animate = false) => {
+ const previous = visibilityAnimations.get(element);
+ const startHeight = previous ? element.getBoundingClientRect().height : visible ? 0 : element.getBoundingClientRect().height;
+ visibilityAnimations.delete(element);
+ previous?.cancel();
+ element.style.removeProperty("overflow");
+ if (!animate) {
+ element.classList.toggle("fn__none", !visible);
+ return;
+ }
+ element.classList.remove("fn__none");
+ const endHeight = visible ? element.getBoundingClientRect().height : 0;
+ element.style.overflow = "clip";
+ const animation = element.animate([
+ {height: `${startHeight}px`, maxHeight: `${startHeight}px`},
+ {height: `${endHeight}px`, maxHeight: `${endHeight}px`},
+ ], getAnimationOptions());
+ visibilityAnimations.set(element, animation);
+ const finish = () => {
+ if (visibilityAnimations.get(element) !== animation) { return; }
+ visibilityAnimations.delete(element);
+ element.classList.toggle("fn__none", !visible);
+ animation.cancel();
+ element.style.removeProperty("overflow");
+ };
+ animation.finished.then(finish, finish);
+};
const animatingElements = new WeakMap<HTMLElement, {
animation: Animation,
type: "expand" | "collapse",