Skip to content

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

:bug: Stabilize graph layout interactions https://github.com/siyuan-note/siyuan/issues/18642

Daniel377326a01fd3
diff --git a/app/src/layout/dock/Graph.ts b/app/src/layout/dock/Graph.ts
index b04226d332..fbac2e82e7 100644
--- a/app/src/layout/dock/Graph.ts
+++ b/app/src/layout/dock/Graph.ts
@@ -473,7 +473,7 @@ export class Graph extends Model {
}
this.graphData = response.data;
window.siyuan.config.graph.global = response.data.conf;
- this.onGraph(false);
+ this.onGraph(false, refresh);
element.classList.remove("fn__rotate");
});
} else {
@@ -513,7 +513,7 @@ export class Graph extends Model {
}
this.graphData = response.data;
window.siyuan.config.graph.local = response.data.conf;
- this.onGraph(focus);
+ this.onGraph(focus, refresh);
});
}
}
@@ -612,7 +612,7 @@ export class Graph extends Model {
this.pendingGraphConf = undefined;
}
- public onGraph(hl: boolean) {
+ public onGraph(hl: boolean, resetLayout = false) {
this.updateNodeCount();
if (this.graphElement.clientHeight === 0) {
// 界面没有渲染时不能进行渲染
@@ -649,7 +649,8 @@ export class Graph extends Model {
};
if (this.renderedGraphData !== this.graphData) {
this.renderedGraphData = this.graphData;
- this.graphEngine.setData(this.graphData.nodes, this.graphData.links, options, palette, hl ? this.blockId : "");
+ this.graphEngine.setData(this.graphData.nodes, this.graphData.links, options, palette,
+ hl ? this.blockId : "", resetLayout);
} else {
this.graphEngine.updateOptions(options, palette);
this.graphEngine.resize();
diff --git a/app/src/layout/dock/graph/GraphEngine.ts b/app/src/layout/dock/graph/GraphEngine.ts
index 287277a876..f02b01d2d5 100644
--- a/app/src/layout/dock/graph/GraphEngine.ts
+++ b/app/src/layout/dock/graph/GraphEngine.ts
@@ -112,14 +112,17 @@ export class GraphEngine {
options: IGraphOptions,
palette: IGraphPalette,
focusId = "",
+ resetLayout = false,
) {
if (this.destroyed) {
return;
}
const previous = new Map<string, [number, number]>();
- this.data?.nodes.forEach((node, index) => {
- previous.set(node.id, [this.positions[index * 2], this.positions[index * 2 + 1]]);
- });
+ if (!resetLayout) {
+ this.data?.nodes.forEach((node, index) => {
+ previous.set(node.id, [this.positions[index * 2], this.positions[index * 2 + 1]]);
+ });
+ }
this.options = {...options};
this.palette = palette;
this.data = normalizeGraphData(nodes, links, options.nodeSize);
diff --git a/app/src/layout/dock/graph/layoutCore.test.ts b/app/src/layout/dock/graph/layoutCore.test.ts
index 4bc6d0036d..868c423c71 100644
--- a/app/src/layout/dock/graph/layoutCore.test.ts
+++ b/app/src/layout/dock/graph/layoutCore.test.ts
@@ -49,4 +49,44 @@ describe("graph force layout", () => {
layout.step(10);
layout.positions.forEach((value) => assert.ok(Number.isFinite(value)));
});
+
+ it("balances repulsion across different node masses", () => {
+ const layout = new GraphForceLayout({
+ degrees: new Uint32Array([0, 8]),
+ layout: {
+ centerStrength: 0,
+ linkDistance: 100,
+ repulsion: 600,
+ springStrength: 0,
+ },
+ positions: new Float32Array([-100, 0, 100, 0]),
+ sizes: new Float32Array([15, 15]),
+ sources: new Uint32Array(),
+ targets: new Uint32Array(),
+ });
+ layout.step();
+ const lightDisplacement = -100 - layout.positions[0];
+ const heavyDisplacement = layout.positions[2] - 100;
+ assert.ok(Math.abs(lightDisplacement - heavyDisplacement * (1 + Math.sqrt(8))) < 0.0001);
+ });
+
+ it("pulls isolated nodes inward more strongly", () => {
+ const layout = new GraphForceLayout({
+ degrees: new Uint32Array([0, 1]),
+ layout: {
+ centerStrength: 0.01,
+ linkDistance: 100,
+ repulsion: 0,
+ springStrength: 0,
+ },
+ positions: new Float32Array([100, 0, -100, 0]),
+ sizes: new Float32Array([15, 15]),
+ sources: new Uint32Array(),
+ targets: new Uint32Array(),
+ });
+ layout.step();
+ const isolatedDisplacement = 100 - layout.positions[0];
+ const connectedDisplacement = layout.positions[2] + 100;
+ assert.ok(isolatedDisplacement > connectedDisplacement * 4);
+ });
});
diff --git a/app/src/layout/dock/graph/layoutCore.ts b/app/src/layout/dock/graph/layoutCore.ts
index 73a15d046c..3c4b8bac26 100644
--- a/app/src/layout/dock/graph/layoutCore.ts
+++ b/app/src/layout/dock/graph/layoutCore.ts
@@ -3,6 +3,7 @@ import {IGraphLayoutOptions} from "./types";
const TREE_THETA_SQUARED = 0.25;
const MAX_TREE_DEPTH = 32;
const STABLE_TICKS = 12;
+const ISOLATED_CENTER_STRENGTH_FACTOR = 3;
export class GraphForceLayout {
public readonly positions: Float32Array;
@@ -82,7 +83,6 @@ export class GraphForceLayout {
this.positions[index * 2 + 1] = y;
this.velocities[index * 2] = 0;
this.velocities[index * 2 + 1] = 0;
- this.restart();
}
public release(index: number) {
@@ -131,8 +131,10 @@ export class GraphForceLayout {
this.velocities[offset + 1] = 0;
continue;
}
- this.forces[offset] -= this.positions[offset] * this.options.centerStrength;
- this.forces[offset + 1] -= this.positions[offset + 1] * this.options.centerStrength;
+ const centerStrength = this.options.centerStrength *
+ (this.degrees[index] === 0 ? ISOLATED_CENTER_STRENGTH_FACTOR : 1);
+ this.forces[offset] -= this.positions[offset] * centerStrength;
+ this.forces[offset + 1] -= this.positions[offset + 1] * centerStrength;
let velocityX = (this.velocities[offset] + this.forces[offset] / this.masses[index] * timestep) * damping;
let velocityY = (this.velocities[offset + 1] + this.forces[offset + 1] / this.masses[index] * timestep) * damping;
const velocity = Math.hypot(velocityX, velocityY);
@@ -179,6 +181,7 @@ export class GraphForceLayout {
private applyRepulsion() {
for (let body = 0; body < this.sizes.length; body++) {
const bodyOffset = body * 2;
+ const bodyMass = this.masses[body];
const bodyX = this.positions[bodyOffset];
const bodyY = this.positions[bodyOffset + 1];
let stackLength = 1;
@@ -199,11 +202,11 @@ export class GraphForceLayout {
const width = this.treeHalves[cell] * 2;
if (cellBody >= 0 || width * width / distanceSquared < TREE_THETA_SQUARED) {
const distance = Math.sqrt(distanceSquared);
- let force = this.options.repulsion * mass / distanceSquared;
+ let force = this.options.repulsion * bodyMass * mass / distanceSquared;
if (cellBody >= 0) {
const minimumDistance = (this.sizes[body] + this.sizes[cellBody]) * 1.2;
if (distance < minimumDistance) {
- force += (minimumDistance - distance) * 0.5 / distance;
+ force += (minimumDistance - distance) * 0.5 * bodyMass * this.masses[cellBody] / distance;
}
}
this.forces[bodyOffset] += deltaX * force;
diff --git a/app/src/layout/dock/graph/layoutWorker.ts b/app/src/layout/dock/graph/layoutWorker.ts
index a82ad37bd2..feab8baf04 100644
--- a/app/src/layout/dock/graph/layoutWorker.ts
+++ b/app/src/layout/dock/graph/layoutWorker.ts
@@ -83,7 +83,6 @@ workerScope.onmessage = (event) => {
restart();
} else if (message.type === "pin") {
layout.pin(message.index, message.x, message.y);
- restart();
} else if (message.type === "release") {
layout.release(message.index);
restart();