Skip to content

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

:bug: Keep local graph fitted after layout

Daniel8dce2fa91f36
diff --git a/app/src/editor/util.ts b/app/src/editor/util.ts
index 44ede012ce..7b0eb7a250 100644
--- a/app/src/editor/util.ts
+++ b/app/src/editor/util.ts
@@ -763,7 +763,7 @@ export const updateBacklinkGraph = (models: IModels, protyle: IProtyle) => {
return;
}
item.notebookId = notebookId;
- item.searchGraph(true, blockId);
+ item.searchGraph({id: blockId});
}
});
models.backlink.forEach(item => {
diff --git a/app/src/layout/Wnd.ts b/app/src/layout/Wnd.ts
index ee1c215284..0839491949 100644
--- a/app/src/layout/Wnd.ts
+++ b/app/src/layout/Wnd.ts
@@ -596,7 +596,7 @@ export class Wnd {
if (currentTab && target === currentTab.headElement) {
if (currentTab.model instanceof Graph) {
- currentTab.model.onGraph(false);
+ currentTab.model.onGraph();
} else if (currentTab.model instanceof Asset && currentTab.model.pdfObject && currentTab.model.pdfObject.pdfViewer) {
// https://github.com/siyuan-note/siyuan/issues/5655
currentTab.model.pdfObject.pdfViewer.container.focus();
diff --git a/app/src/layout/dock/Graph.ts b/app/src/layout/dock/Graph.ts
index 92188dbc39..e4b62a67f7 100644
--- a/app/src/layout/dock/Graph.ts
+++ b/app/src/layout/dock/Graph.ts
@@ -15,6 +15,12 @@ import {getDocDisplayName, isEncryptedBox} from "../../util/pathName";
import {GraphEngine} from "./graph/GraphEngine";
import {IGraphNodeClick, IGraphSourceLink, IGraphSourceNode} from "./graph/types";
+interface IGraphSearchOptions {
+ id?: string;
+ refresh?: boolean;
+ resetLayout?: boolean;
+}
+
export class Graph extends Model {
public inputElement: HTMLInputElement;
private countElement: HTMLElement;
@@ -294,7 +300,7 @@ export class Graph extends Model {
target.previousElementSibling.classList.remove("fn__none");
(target.previousElementSibling as HTMLInputElement).select();
} else if (dataType === "refresh") {
- this.searchGraph(false, undefined, true);
+ this.searchGraph({refresh: true});
} else if (dataType === "fullscreen") {
fullscreen(this.element, target);
const minElement = this.element.querySelector('.block__icons .block__icon[data-type="min"]') as HTMLElement;
@@ -347,11 +353,11 @@ export class Graph extends Model {
if (item.getAttribute("data-type") === "arrow") {
this.updateGraphOptions();
} else {
- this.searchGraph(false, undefined, false, true);
+ this.searchGraph({resetLayout: true});
}
});
});
- this.searchGraph(options.type !== "global");
+ this.searchGraph();
}
private handleCallback() {
@@ -369,18 +375,18 @@ export class Graph extends Model {
switch (data.cmd) {
case "mount":
if (this.type === "global" && data.code !== 1) {
- this.searchGraph(false);
+ this.searchGraph();
}
break;
case "rename":
if (this.graphData && data.data.box === this.graphData.box && this.rootId === data.data.id) {
- this.searchGraph(false);
+ this.searchGraph();
if (this.type === "local") {
this.parent.updateTitle(getDocDisplayName(data.data.title, data.data.empty));
}
}
if (this.type === "global") {
- this.searchGraph(false);
+ this.searchGraph();
}
break;
case "closeBox":
@@ -440,10 +446,13 @@ export class Graph extends Model {
(this.panelElement.querySelector("[data-type='blockquote']") as HTMLInputElement).checked = conf.type.blockquote;
(this.panelElement.querySelector("[data-type='callout']") as HTMLInputElement).checked = conf.type.callout;
(this.panelElement.querySelector("[data-type='code']") as HTMLInputElement).checked = conf.type.code;
- this.searchGraph(false, undefined, false, true);
+ this.searchGraph({resetLayout: true});
}
- public searchGraph(focus: boolean, id?: string, refresh = false, resetLayout = refresh) {
+ public searchGraph(options: IGraphSearchOptions = {}) {
+ const refresh = options.refresh || false;
+ const resetLayout = options.resetLayout ?? refresh;
+ const id = options.id;
const element = this.element.querySelector('.block__icon[data-type="refresh"] svg');
if (element.classList.contains("fn__rotate") && refresh) {
return;
@@ -471,7 +480,7 @@ export class Graph extends Model {
}
this.graphData = response.data;
window.siyuan.config.graph.global = response.data.conf;
- this.onGraph(false, resetLayout);
+ this.onGraph(undefined, resetLayout);
element.classList.remove("fn__rotate");
});
} else {
@@ -488,7 +497,7 @@ export class Graph extends Model {
element.classList.remove("fn__rotate");
if (response.code !== 0) {
this.graphData = undefined;
- this.onGraph(false);
+ this.onGraph();
return;
}
if (id) {
@@ -511,17 +520,13 @@ export class Graph extends Model {
}
this.graphData = response.data;
window.siyuan.config.graph.local = response.data.conf;
- this.onGraph(focus, resetLayout);
+ this.onGraph(this.blockId, resetLayout);
});
}
}
- private hlNode(id: string) {
- const graphEngine = this.graphEngine;
- if (this.graphElement.clientHeight === 0 || !graphEngine?.hasNode(id)) {
- return;
- }
- graphEngine.focusNode(id);
+ private selectNode(id: string) {
+ this.graphEngine?.selectNode(id);
}
public destroy() {
@@ -545,7 +550,7 @@ export class Graph extends Model {
}
this.searchTimeout = window.setTimeout(() => {
this.searchTimeout = 0;
- this.searchGraph(false, undefined, false, resetLayout);
+ this.searchGraph({resetLayout});
}, 160);
}
@@ -591,7 +596,7 @@ export class Graph extends Model {
} else {
window.siyuan.config.graph.local = conf;
}
- this.onGraph(false);
+ this.onGraph(this.type === "global" ? undefined : this.blockId);
if (this.saveTimeout) {
window.clearTimeout(this.saveTimeout);
}
@@ -613,7 +618,7 @@ export class Graph extends Model {
this.pendingGraphConf = undefined;
}
- public onGraph(hl: boolean, resetLayout = false) {
+ public onGraph(selectedId?: string, resetLayout = false) {
this.updateNodeCount();
const graphEngine = this.ensureGraphEngine();
if (!this.graphData || !this.graphData.nodes || this.graphData.nodes.length === 0) {
@@ -647,13 +652,15 @@ export class Graph extends Model {
};
if (this.renderedGraphData !== this.graphData) {
this.renderedGraphData = this.graphData;
- graphEngine.setData(this.graphData.nodes, this.graphData.links, options, palette,
- hl ? this.blockId : "", resetLayout);
+ graphEngine.setData(this.graphData.nodes, this.graphData.links, options, palette, {
+ resetLayout,
+ selectedId,
+ });
} else {
graphEngine.updateOptions(options, palette);
graphEngine.resize();
- if (hl) {
- this.hlNode(this.blockId);
+ if (selectedId) {
+ this.selectNode(selectedId);
}
}
}
diff --git a/app/src/layout/dock/graph/GraphEngine.ts b/app/src/layout/dock/graph/GraphEngine.ts
index 6379df7ce0..585b49058b 100644
--- a/app/src/layout/dock/graph/GraphEngine.ts
+++ b/app/src/layout/dock/graph/GraphEngine.ts
@@ -16,6 +16,7 @@ import {
IGraphEngineOptions,
IGraphOptions,
IGraphPalette,
+ IGraphSetDataOptions,
IGraphSourceLink,
IGraphSourceNode,
TGraphLayoutRequest,
@@ -74,7 +75,6 @@ export class GraphEngine {
private layoutWorker: Worker;
private options: IGraphOptions;
private palette: IGraphPalette;
- private pendingFocusId = "";
private readonly pendingNodeReleases = new Map<number, number>();
private readonly pinnedNodes = new Map<number, [number, number]>();
private pinchAction: IPinchAction;
@@ -116,14 +116,13 @@ export class GraphEngine {
links: IGraphSourceLink[],
options: IGraphOptions,
palette: IGraphPalette,
- focusId = "",
- resetLayout = false,
+ setDataOptions: IGraphSetDataOptions = {},
) {
if (this.destroyed) {
return;
}
const previous = new Map<string, [number, number]>();
- if (!resetLayout) {
+ if (!setDataOptions.resetLayout) {
this.data?.nodes.forEach((node, index) => {
previous.set(node.id, [this.positions[index * 2], this.positions[index * 2 + 1]]);
});
@@ -135,10 +134,9 @@ export class GraphEngine {
this.geometryVersion++;
this.positionVersion++;
this.styleVersion++;
- this.selected = -1;
+ this.selected = setDataOptions.selectedId ? this.data.indexById.get(setDataOptions.selectedId) ?? -1 : -1;
this.hovered = -1;
this.selectionVersion++;
- this.pendingFocusId = focusId;
this.autoFitPending = true;
this.cameraTouched = false;
this.focusAnimation++;
@@ -199,7 +197,6 @@ export class GraphEngine {
}
this.focusAnimation++;
this.autoFitPending = false;
- this.pendingFocusId = "";
this.pendingNodeReleases.clear();
this.pinnedNodes.clear();
this.data = undefined;
@@ -213,8 +210,17 @@ export class GraphEngine {
this.labelCanvas.height = 1;
}
- public hasNode(id: string) {
- return this.data?.indexById.has(id) || false;
+ public selectNode(id: string) {
+ if (this.destroyed) {
+ return;
+ }
+ const selected = this.data?.indexById.get(id) ?? -1;
+ if (this.selected === selected) {
+ return;
+ }
+ this.selected = selected;
+ this.selectionVersion++;
+ this.scheduleRender();
}
public focusNode(id: string, animate = true) {
@@ -225,8 +231,7 @@ export class GraphEngine {
if (index === undefined) {
return;
}
- this.selected = index;
- this.selectionVersion++;
+ this.selectNode(id);
const targetX = this.width / 2 - this.positions[index * 2] * this.camera.scale;
const targetY = this.height / 2 - this.positions[index * 2 + 1] * this.camera.scale;
if (!animate) {
@@ -350,10 +355,6 @@ export class GraphEngine {
this.pendingNodeReleases.forEach((_token, index) => this.pinnedNodes.delete(index));
this.pendingNodeReleases.clear();
if (!this.data || this.data.nodes.length < 2) {
- if (this.pendingFocusId) {
- this.focusNode(this.pendingFocusId);
- this.pendingFocusId = "";
- }
this.autoFitPending = false;
return;
}
@@ -391,10 +392,7 @@ export class GraphEngine {
this.positionVersion++;
this.scheduleRender();
if (message.settled) {
- if (this.pendingFocusId) {
- this.focusNode(this.pendingFocusId);
- this.pendingFocusId = "";
- } else if (this.autoFitPending && !this.cameraTouched) {
+ if (this.autoFitPending && !this.cameraTouched) {
this.fit(true);
}
this.autoFitPending = false;
@@ -406,10 +404,6 @@ export class GraphEngine {
if (this.layoutWorker === worker) {
this.layoutWorker = undefined;
}
- if (this.pendingFocusId) {
- this.focusNode(this.pendingFocusId);
- this.pendingFocusId = "";
- }
this.autoFitPending = false;
};
const positions = this.positions.slice();
@@ -443,10 +437,6 @@ export class GraphEngine {
} catch (error) {
console.warn("Unable to start graph layout worker", error);
this.stopLayout();
- if (this.pendingFocusId) {
- this.focusNode(this.pendingFocusId);
- this.pendingFocusId = "";
- }
this.autoFitPending = false;
}
}
diff --git a/app/src/layout/dock/graph/core.test.ts b/app/src/layout/dock/graph/core.test.ts
index f97c362876..a142c47eec 100644
--- a/app/src/layout/dock/graph/core.test.ts
+++ b/app/src/layout/dock/graph/core.test.ts
@@ -109,6 +109,11 @@ describe("graph data normalization", () => {
assert.equal(camera.y, 100);
});
+ it("does not enlarge sparse graphs above their natural scale", () => {
+ const camera = fitGraphCamera(new Float32Array([0, 0]), new Float32Array([15]), 400, 300);
+ assert.deepEqual(camera, {scale: 1, x: 200, y: 150});
+ });
+
it("centers positions after resizing without changing the scale", () => {
const camera = centerGraphCamera(
new Float32Array([0, 0, 200, 100]),
diff --git a/app/src/layout/dock/graph/core.ts b/app/src/layout/dock/graph/core.ts
index f02cbfcb9a..f839147f54 100644
--- a/app/src/layout/dock/graph/core.ts
+++ b/app/src/layout/dock/graph/core.ts
@@ -9,6 +9,7 @@ import {
const GOLDEN_ANGLE = Math.PI * (3 - Math.sqrt(5));
const HIGHLIGHT_EDGE_OPACITY_FACTOR = 2.5;
+const MAX_AUTO_FIT_SCALE = 1;
export const MIN_GRAPH_EDGE_WIDTH = 1;
export const getGraphEdgeOpacity = (lineOpacity: number, highlighted: boolean) =>
@@ -471,7 +472,8 @@ export const fitGraphCamera = (
const contentHeight = Math.max(1, maxY - minY);
const availableWidth = Math.max(1, width - padding * 2);
const availableHeight = Math.max(1, height - padding * 2);
- const scale = Math.max(0.02, Math.min(4, availableWidth / contentWidth, availableHeight / contentHeight));
+ const scale = Math.max(0.02, Math.min(MAX_AUTO_FIT_SCALE, availableWidth / contentWidth,
+ availableHeight / contentHeight));
return {
scale,
x: width / 2 - (minX + maxX) / 2 * scale,
diff --git a/app/src/layout/dock/graph/types.ts b/app/src/layout/dock/graph/types.ts
index e7cb6245bb..84cd425d72 100644
--- a/app/src/layout/dock/graph/types.ts
+++ b/app/src/layout/dock/graph/types.ts
@@ -92,6 +92,11 @@ export interface IGraphEngineOptions {
onNodeClick: (details: IGraphNodeClick) => void;
}
+export interface IGraphSetDataOptions {
+ resetLayout?: boolean;
+ selectedId?: string;
+}
+
export interface IGraphLayoutOptions {
centerStrength: number;
linkDistance: number;
diff --git a/app/src/layout/dock/index.ts b/app/src/layout/dock/index.ts
index 4a38a667c9..f35f926b26 100644
--- a/app/src/layout/dock/index.ts
+++ b/app/src/layout/dock/index.ts
@@ -770,7 +770,7 @@ export class Dock {
resizeTabs(isSaveLayout);
if (target.classList.contains("dock__item--active") && !removeDock && (type === "graph" || type === "globalGraph")) {
const graph = this.data[type] as Graph;
- graph.onGraph(false);
+ graph.onGraph();
}
// 等待 dock 面板动画结束
diff --git a/app/src/layout/dock/util.ts b/app/src/layout/dock/util.ts
index bafc41d45c..e88fd746cf 100644
--- a/app/src/layout/dock/util.ts
+++ b/app/src/layout/dock/util.ts
@@ -260,7 +260,7 @@ export const clearOBG = () => {
}
item.blockId = "";
item.graphData = undefined;
- item.onGraph(false);
+ item.onGraph();
}
});
models.backlink.forEach(item => {
diff --git a/app/src/util/assets.ts b/app/src/util/assets.ts
index 87d019c5cb..f2149ab6dd 100644
--- a/app/src/util/assets.ts
+++ b/app/src/util/assets.ts
@@ -158,7 +158,7 @@ export const loadAssets = (data: Config.IAppearance) => {
}
/// #if !MOBILE
getAllModels().graph.forEach(item => {
- item.searchGraph(false);
+ item.searchGraph();
});
const pdfTheme = window.siyuan.config.appearance.mode === 0 ? window.siyuan.storage[Constants.LOCAL_PDFTHEME].light :
window.siyuan.storage[Constants.LOCAL_PDFTHEME].dark;
diff --git a/app/src/util/reloadSync.ts b/app/src/util/reloadSync.ts
index 64d8a71e3f..6ee324faab 100644
--- a/app/src/util/reloadSync.ts
+++ b/app/src/util/reloadSync.ts
@@ -103,7 +103,7 @@ export const reloadSync = (
if (item.type === "local" && data.removeRootIDs.includes(item.rootId)) {
item.parent.parent.removeTab(item.parent.id, false, false);
} else if (item.type !== "local" || data.upsertRootIDs.includes(item.rootId)) {
- item.searchGraph(false);
+ item.searchGraph();
if (item.type === "local") {
updateTitle(item.rootId, item.parent);
}