Skip to content

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

:art: Improve graph layout updates, viewport interactions, and arrow rendering https://github.com/siyuan-note/siyuan/issues/18890

Danielcf731e58d290
diff --git a/app/src/layout/dock/Graph.ts b/app/src/layout/dock/Graph.ts
index 509968d232..92188dbc39 100644
--- a/app/src/layout/dock/Graph.ts
+++ b/app/src/layout/dock/Graph.ts
@@ -336,7 +336,7 @@ export class Graph extends Model {
item.addEventListener("input", () => {
item.setAttribute("aria-label", item.value);
if (item.getAttribute("data-type") === "minRefs") {
- this.scheduleGraphSearch();
+ this.scheduleGraphSearch(true);
} else {
this.updateGraphOptions();
}
@@ -347,7 +347,7 @@ export class Graph extends Model {
if (item.getAttribute("data-type") === "arrow") {
this.updateGraphOptions();
} else {
- this.searchGraph(false);
+ this.searchGraph(false, undefined, false, true);
}
});
});
@@ -440,10 +440,10 @@ 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);
+ this.searchGraph(false, undefined, false, true);
}
- public searchGraph(focus: boolean, id?: string, refresh = false) {
+ public searchGraph(focus: boolean, id?: string, refresh = false, resetLayout = refresh) {
const element = this.element.querySelector('.block__icon[data-type="refresh"] svg');
if (element.classList.contains("fn__rotate") && refresh) {
return;
@@ -471,7 +471,7 @@ export class Graph extends Model {
}
this.graphData = response.data;
window.siyuan.config.graph.global = response.data.conf;
- this.onGraph(false, refresh);
+ this.onGraph(false, resetLayout);
element.classList.remove("fn__rotate");
});
} else {
@@ -511,7 +511,7 @@ export class Graph extends Model {
}
this.graphData = response.data;
window.siyuan.config.graph.local = response.data.conf;
- this.onGraph(focus, refresh);
+ this.onGraph(focus, resetLayout);
});
}
}
@@ -539,13 +539,13 @@ export class Graph extends Model {
this.renderedGraphData = undefined;
}
- private scheduleGraphSearch() {
+ private scheduleGraphSearch(resetLayout = false) {
if (this.searchTimeout) {
window.clearTimeout(this.searchTimeout);
}
this.searchTimeout = window.setTimeout(() => {
this.searchTimeout = 0;
- this.searchGraph(false);
+ this.searchGraph(false, undefined, false, resetLayout);
}, 160);
}
diff --git a/app/src/layout/dock/graph/GraphEngine.ts b/app/src/layout/dock/graph/GraphEngine.ts
index 4b874d7b6a..6379df7ce0 100644
--- a/app/src/layout/dock/graph/GraphEngine.ts
+++ b/app/src/layout/dock/graph/GraphEngine.ts
@@ -1,5 +1,6 @@
import {GraphCanvasRenderer} from "./canvasRenderer";
import {
+ centerGraphCamera,
createInitialPositions,
fitGraphCamera,
getDraggedGraphPosition,
@@ -25,6 +26,8 @@ import {GraphWebGLRenderer} from "./webglRenderer";
const MIN_SCALE = 0.02;
const MAX_SCALE = 8;
const CLICK_DISTANCE = 4;
+const WHEEL_ZOOM_SENSITIVITY = 0.0015;
+const TRACKPAD_PINCH_ZOOM_MULTIPLIER = 4;
interface IPointerPosition {
clientX: number;
@@ -260,6 +263,7 @@ export class GraphEngine {
const width = this.container.clientWidth;
const height = this.container.clientHeight;
const wasHidden = this.width < 1 || this.height < 1;
+ const sizeChanged = width !== this.width || height !== this.height;
this.width = width;
this.height = height;
if (width < 1 || height < 1) {
@@ -271,6 +275,9 @@ export class GraphEngine {
}
if (this.data && !this.cameraTouched && (wasHidden || this.autoFitPending)) {
this.fit(false);
+ } else if (this.data && sizeChanged) {
+ this.focusAnimation++;
+ this.camera = centerGraphCamera(this.positions, this.data.sizes, width, height, this.camera.scale);
}
this.scheduleRender();
}
@@ -704,7 +711,8 @@ export class GraphEngine {
const y = event.clientY - rect.top;
const anchor = screenToGraph(x, y, this.camera);
const delta = event.deltaMode === WheelEvent.DOM_DELTA_LINE ? event.deltaY * 16 : event.deltaY;
- const scale = Math.max(MIN_SCALE, Math.min(MAX_SCALE, this.camera.scale * Math.exp(-delta * 0.0005)));
+ const sensitivity = WHEEL_ZOOM_SENSITIVITY * (event.ctrlKey ? TRACKPAD_PINCH_ZOOM_MULTIPLIER : 1);
+ const scale = Math.max(MIN_SCALE, Math.min(MAX_SCALE, this.camera.scale * Math.exp(-delta * sensitivity)));
this.camera.scale = scale;
this.camera.x = x - anchor.x * scale;
this.camera.y = y - anchor.y * scale;
diff --git a/app/src/layout/dock/graph/canvasRenderer.ts b/app/src/layout/dock/graph/canvasRenderer.ts
index 37b6525041..b77bb685f7 100644
--- a/app/src/layout/dock/graph/canvasRenderer.ts
+++ b/app/src/layout/dock/graph/canvasRenderer.ts
@@ -1,5 +1,5 @@
import {getGraphNodeColor, IGraphRenderer, IGraphRenderState} from "./renderer";
-import {getGraphEdgeOpacity, MIN_GRAPH_EDGE_WIDTH} from "./core";
+import {getGraphArrowGeometry, getGraphArrowLength, getGraphEdgeOpacity, MIN_GRAPH_EDGE_WIDTH} from "./core";
export class GraphCanvasRenderer implements IGraphRenderer {
private readonly canvas: HTMLCanvasElement;
@@ -52,14 +52,25 @@ export class GraphCanvasRenderer implements IGraphRenderer {
}
const sourceOffset = link.source * 2;
const targetOffset = link.target * 2;
- context.moveTo(
- state.positions[sourceOffset] * state.camera.scale + state.camera.x,
- state.positions[sourceOffset + 1] * state.camera.scale + state.camera.y,
- );
- context.lineTo(
- state.positions[targetOffset] * state.camera.scale + state.camera.x,
- state.positions[targetOffset + 1] * state.camera.scale + state.camera.y,
- );
+ const sourceX = state.positions[sourceOffset] * state.camera.scale + state.camera.x;
+ const sourceY = state.positions[sourceOffset + 1] * state.camera.scale + state.camera.y;
+ const targetX = state.positions[targetOffset] * state.camera.scale + state.camera.x;
+ const targetY = state.positions[targetOffset + 1] * state.camera.scale + state.camera.y;
+ context.moveTo(sourceX, sourceY);
+ if (link.ref && state.options.arrow && state.camera.scale >= 0.08) {
+ const arrow = getGraphArrowGeometry(
+ sourceX,
+ sourceY,
+ targetX,
+ targetY,
+ Math.max(1, state.data.sizes[link.target] * state.camera.scale),
+ getGraphArrowLength(state.options.linkWidth, state.camera.scale),
+ Math.max(MIN_GRAPH_EDGE_WIDTH, state.options.linkWidth * state.camera.scale),
+ );
+ context.lineTo(arrow.lineEndX, arrow.lineEndY);
+ } else {
+ context.lineTo(targetX, targetY);
+ }
drawn = true;
});
if (!drawn) {
@@ -73,7 +84,7 @@ export class GraphCanvasRenderer implements IGraphRenderer {
private drawArrows(state: IGraphRenderState) {
const context = this.context;
- const arrowLength = Math.max(7, state.options.linkWidth * state.camera.scale * 2.2);
+ const arrowLength = getGraphArrowLength(state.options.linkWidth, state.camera.scale);
state.data.links.forEach((link) => {
if (!link.ref) {
return;
@@ -84,23 +95,19 @@ export class GraphCanvasRenderer implements IGraphRenderer {
const sourceY = state.positions[sourceOffset + 1] * state.camera.scale + state.camera.y;
const targetX = state.positions[targetOffset] * state.camera.scale + state.camera.x;
const targetY = state.positions[targetOffset + 1] * state.camera.scale + state.camera.y;
- const deltaX = targetX - sourceX;
- const deltaY = targetY - sourceY;
- const distance = Math.max(0.001, Math.hypot(deltaX, deltaY));
- const directionX = deltaX / distance;
- const directionY = deltaY / distance;
const targetRadius = Math.max(1, state.data.sizes[link.target] * state.camera.scale);
- const tipX = targetX - directionX * targetRadius * 1.15;
- const tipY = targetY - directionY * targetRadius * 1.15;
+ const arrow = getGraphArrowGeometry(
+ sourceX, sourceY, targetX, targetY, targetRadius, arrowLength,
+ );
context.beginPath();
- context.moveTo(tipX, tipY);
+ context.moveTo(arrow.tipX, arrow.tipY);
context.lineTo(
- tipX - directionX * arrowLength + directionY * arrowLength * 0.6,
- tipY - directionY * arrowLength - directionX * arrowLength * 0.6,
+ arrow.baseX + arrow.directionY * arrowLength * 0.6,
+ arrow.baseY - arrow.directionX * arrowLength * 0.6,
);
context.lineTo(
- tipX - directionX * arrowLength - directionY * arrowLength * 0.6,
- tipY - directionY * arrowLength + directionX * arrowLength * 0.6,
+ arrow.baseX - arrow.directionY * arrowLength * 0.6,
+ arrow.baseY + arrow.directionX * arrowLength * 0.6,
);
context.closePath();
const highlighted = link.source === state.selected || link.target === state.selected ||
diff --git a/app/src/layout/dock/graph/core.test.ts b/app/src/layout/dock/graph/core.test.ts
index 4e29466954..f97c362876 100644
--- a/app/src/layout/dock/graph/core.test.ts
+++ b/app/src/layout/dock/graph/core.test.ts
@@ -1,8 +1,11 @@
import * as assert from "node:assert/strict";
import {describe, it} from "node:test";
import {
+ centerGraphCamera,
createInitialPositions,
fitGraphCamera,
+ getGraphArrowGeometry,
+ getGraphArrowLength,
getDraggedGraphPosition,
getGraphEdgeOpacity,
getGraphNodeSize,
@@ -106,6 +109,17 @@ describe("graph data normalization", () => {
assert.equal(camera.y, 100);
});
+ it("centers positions after resizing without changing the scale", () => {
+ const camera = centerGraphCamera(
+ new Float32Array([0, 0, 200, 100]),
+ new Float32Array([10, 20]),
+ 600,
+ 300,
+ 1.5,
+ );
+ assert.deepEqual(camera, {scale: 1.5, x: 142.5, y: 67.5});
+ });
+
it("keeps the pointer grab offset while dragging", () => {
const position = getDraggedGraphPosition(80, 100, {scale: 2, x: 20, y: 40}, 5, -3);
assert.deepEqual(position, {x: 35, y: 27});
@@ -118,6 +132,22 @@ describe("graph data normalization", () => {
assert.equal(getGraphEdgeOpacity(-0.5, false), 0);
});
+ it("places arrows against the target edge and ends lines at the arrow base", () => {
+ const arrowLength = getGraphArrowLength(2, 1);
+ const arrow = getGraphArrowGeometry(0, 0, 100, 0, 20, arrowLength, 4);
+ assert.equal(arrowLength, 7);
+ assert.deepEqual(arrow, {
+ baseX: 73,
+ baseY: 0,
+ directionX: 1,
+ directionY: 0,
+ lineEndX: 71,
+ lineEndY: 0,
+ tipX: 80,
+ tipY: 0,
+ });
+ });
+
it("scales referenced nodes logarithmically", () => {
assert.equal(getGraphNodeSize(15, 0), 15);
assert.equal(getGraphNodeSize(15, 1), 15);
diff --git a/app/src/layout/dock/graph/core.ts b/app/src/layout/dock/graph/core.ts
index 683fbb865a..f02cbfcb9a 100644
--- a/app/src/layout/dock/graph/core.ts
+++ b/app/src/layout/dock/graph/core.ts
@@ -14,6 +14,37 @@ export const MIN_GRAPH_EDGE_WIDTH = 1;
export const getGraphEdgeOpacity = (lineOpacity: number, highlighted: boolean) =>
Math.max(0, Math.min(1, lineOpacity * (highlighted ? HIGHLIGHT_EDGE_OPACITY_FACTOR : 1)));
+export const getGraphArrowLength = (linkWidth: number, scale: number) =>
+ Math.max(7, linkWidth * scale * 2.2);
+
+export const getGraphArrowGeometry = (
+ sourceX: number,
+ sourceY: number,
+ targetX: number,
+ targetY: number,
+ targetRadius: number,
+ arrowLength: number,
+ lineWidth = 0,
+) => {
+ const deltaX = targetX - sourceX;
+ const deltaY = targetY - sourceY;
+ const distance = Math.max(0.001, Math.hypot(deltaX, deltaY));
+ const directionX = deltaX / distance;
+ const directionY = deltaY / distance;
+ const tipX = targetX - directionX * targetRadius;
+ const tipY = targetY - directionY * targetRadius;
+ return {
+ baseX: tipX - directionX * arrowLength,
+ baseY: tipY - directionY * arrowLength,
+ directionX,
+ directionY,
+ lineEndX: tipX - directionX * (arrowLength + lineWidth / 2),
+ lineEndY: tipY - directionY * (arrowLength + lineWidth / 2),
+ tipX,
+ tipY,
+ };
+};
+
export const getGraphNodeSize = (baseSize: number, definitions: number) => {
if (definitions < 1) {
return baseSize;
@@ -447,3 +478,20 @@ export const fitGraphCamera = (
y: height / 2 - (minY + maxY) / 2 * scale,
};
};
+
+export const centerGraphCamera = (
+ positions: Float32Array,
+ sizes: Float32Array,
+ width: number,
+ height: number,
+ scale: number,
+): IGraphCamera => {
+ const fitted = fitGraphCamera(positions, sizes, width, height);
+ const graphCenterX = (width / 2 - fitted.x) / fitted.scale;
+ const graphCenterY = (height / 2 - fitted.y) / fitted.scale;
+ return {
+ scale,
+ x: width / 2 - graphCenterX * scale,
+ y: height / 2 - graphCenterY * scale,
+ };
+};
diff --git a/app/src/layout/dock/graph/webglRenderer.ts b/app/src/layout/dock/graph/webglRenderer.ts
index b9fdb2bd17..c47b1735e8 100644
--- a/app/src/layout/dock/graph/webglRenderer.ts
+++ b/app/src/layout/dock/graph/webglRenderer.ts
@@ -1,5 +1,5 @@
import {getGraphNodeColor, IGraphRenderer, IGraphRenderState, parseGraphColor} from "./renderer";
-import {getGraphEdgeOpacity, MIN_GRAPH_EDGE_WIDTH} from "./core";
+import {getGraphArrowLength, getGraphEdgeOpacity, MIN_GRAPH_EDGE_WIDTH} from "./core";
const NODE_VERTEX_SHADER = `#version 300 es
layout(location = 0) in vec2 a_corner;
@@ -50,8 +50,12 @@ layout(location = 1) in vec2 a_start;
layout(location = 2) in vec2 a_end;
layout(location = 3) in vec4 a_color;
layout(location = 4) in float a_state;
+layout(location = 5) in float a_reference;
+layout(location = 6) in float a_target_size;
uniform vec2 u_viewport;
uniform vec3 u_camera;
+uniform float u_arrow;
+uniform float u_arrow_length;
uniform float u_min_width;
uniform float u_width;
out vec4 v_color;
@@ -70,7 +74,12 @@ void main() {
float width = max(u_min_width, u_width * u_camera.z);
float half_width = width * 0.5;
float outer_half_width = half_width + 1.0;
- float line_position = mix(-outer_half_width, distance + outer_half_width, a_corner.x);
+ float line_length = distance;
+ if (u_arrow > 0.5 && a_reference > 0.5 && u_camera.z >= 0.08) {
+ line_length = max(0.0,
+ distance - max(1.0, a_target_size * u_camera.z) - u_arrow_length - half_width);
+ }
+ float line_position = mix(-outer_half_width, line_length + outer_half_width, a_corner.x);
vec2 position = start + direction * line_position + normal * a_corner.y * outer_half_width;
vec2 clip = position / u_viewport * 2.0 - 1.0;
gl_Position = vec4(clip.x, -clip.y, 0.0, 1.0);
@@ -78,7 +87,7 @@ void main() {
v_state = a_state;
v_distance = a_corner.y * outer_half_width;
v_half_width = half_width;
- v_length = distance;
+ v_length = line_length;
v_position = line_position;
}`;
@@ -120,7 +129,7 @@ layout(location = 4) in vec4 a_color;
layout(location = 5) in float a_state;
uniform vec2 u_viewport;
uniform vec3 u_camera;
-uniform float u_width;
+uniform float u_arrow_length;
out vec4 v_color;
out float v_state;
out vec3 v_barycentric;
@@ -131,9 +140,8 @@ void main() {
float distance = max(0.001, length(delta));
vec2 direction = delta / distance;
vec2 normal = vec2(-direction.y, direction.x);
- float arrow_length = max(7.0, u_width * u_camera.z * 2.2);
- vec2 tip = end - direction * max(1.0, a_target_size * u_camera.z) * 1.15;
- vec2 position = tip + direction * a_corner.x * arrow_length + normal * a_corner.y * arrow_length;
+ vec2 tip = end - direction * max(1.0, a_target_size * u_camera.z);
+ vec2 position = tip + direction * a_corner.x * u_arrow_length + normal * a_corner.y * u_arrow_length;
vec2 clip = position / u_viewport * 2.0 - 1.0;
gl_Position = vec4(clip.x, -clip.y, 0.0, 1.0);
v_color = a_color;
@@ -172,7 +180,9 @@ export class GraphWebGLRenderer implements IGraphRenderer {
private readonly edgeColorBuffer: WebGLBuffer;
private readonly edgeEndpointBuffer: WebGLBuffer;
private readonly edgeProgram: WebGLProgram;
+ private readonly edgeReferenceBuffer: WebGLBuffer;
private readonly edgeStateBuffer: WebGLBuffer;
+ private readonly edgeTargetSizeBuffer: WebGLBuffer;
private readonly edgeVao: WebGLVertexArrayObject;
private readonly gl: WebGL2RenderingContext;
private readonly nodeColorBuffer: WebGLBuffer;
@@ -188,7 +198,9 @@ export class GraphWebGLRenderer implements IGraphRenderer {
private arrowStates = new Float32Array();
private edgeColors = new Float32Array();
private edgeEndpoints = new Float32Array();
+ private edgeReferences = new Float32Array();
private edgeStates = new Float32Array();
+ private edgeTargetSizes = new Float32Array();
private geometryVersion = -1;
private highlightLine = new Float32Array([0, 0, 0, 1]);
private highlightPoint = new Float32Array([0, 0, 0, 1]);
@@ -225,6 +237,8 @@ export class GraphWebGLRenderer implements IGraphRenderer {
this.edgeEndpointBuffer = this.createBuffer();
this.edgeColorBuffer = this.createBuffer();
this.edgeStateBuffer = this.createBuffer();
+ this.edgeReferenceBuffer = this.createBuffer();
+ this.edgeTargetSizeBuffer = this.createBuffer();
this.arrowEndpointBuffer = this.createBuffer();
this.arrowSizeBuffer = this.createBuffer();
this.arrowColorBuffer = this.createBuffer();
@@ -289,10 +303,16 @@ export class GraphWebGLRenderer implements IGraphRenderer {
this.upload(this.nodeColorBuffer, this.nodeColors, gl.STATIC_DRAW);
const edgeCount = state.data.links.length;
this.edgeColors = new Float32Array(edgeCount * 4);
+ this.edgeReferences = new Float32Array(edgeCount);
+ this.edgeTargetSizes = new Float32Array(edgeCount);
state.data.links.forEach((link, index) => {
this.edgeColors.set(getColor(link.ref ? state.palette.referenceLine : state.palette.line), index * 4);
+ this.edgeReferences[index] = link.ref ? 1 : 0;
+ this.edgeTargetSizes[index] = state.data.sizes[link.target];
});
this.upload(this.edgeColorBuffer, this.edgeColors, gl.STATIC_DRAW);
+ this.upload(this.edgeReferenceBuffer, this.edgeReferences, gl.STATIC_DRAW);
+ this.upload(this.edgeTargetSizeBuffer, this.edgeTargetSizes, gl.DYNAMIC_DRAW);
const arrowIndices: number[] = [];
if (state.options.arrow) {
state.data.links.forEach((link, index) => {
@@ -384,6 +404,9 @@ export class GraphWebGLRenderer implements IGraphRenderer {
gl.useProgram(this.edgeProgram);
gl.bindVertexArray(this.edgeVao);
this.setViewUniforms(this.edgeProgram, state);
+ gl.uniform1f(this.getUniform(this.edgeProgram, "u_arrow"), state.options.arrow ? 1 : 0);
+ gl.uniform1f(this.getUniform(this.edgeProgram, "u_arrow_length"),
+ getGraphArrowLength(state.options.linkWidth, state.camera.scale));
gl.uniform1f(this.getUniform(this.edgeProgram, "u_min_width"), MIN_GRAPH_EDGE_WIDTH);
gl.uniform1f(this.getUniform(this.edgeProgram, "u_width"), state.options.linkWidth);
gl.uniform1f(this.getUniform(this.edgeProgram, "u_opacity"),
@@ -402,7 +425,8 @@ export class GraphWebGLRenderer implements IGraphRenderer {
gl.useProgram(this.arrowProgram);
gl.bindVertexArray(this.arrowVao);
this.setViewUniforms(this.arrowProgram, state);
- gl.uniform1f(this.getUniform(this.arrowProgram, "u_width"), state.options.linkWidth);
+ gl.uniform1f(this.getUniform(this.arrowProgram, "u_arrow_length"),
+ getGraphArrowLength(state.options.linkWidth, state.camera.scale));
gl.uniform1f(this.getUniform(this.arrowProgram, "u_opacity"),
getGraphEdgeOpacity(state.options.lineOpacity, false));
gl.uniform1f(this.getUniform(this.arrowProgram, "u_highlight_opacity"),
@@ -428,6 +452,8 @@ export class GraphWebGLRenderer implements IGraphRenderer {
this.configureAttribute(2, this.edgeEndpointBuffer, 2, 16, 1, 8);
this.configureAttribute(3, this.edgeColorBuffer, 4, 0, 1);
this.configureAttribute(4, this.edgeStateBuffer, 1, 0, 1);
+ this.configureAttribute(5, this.edgeReferenceBuffer, 1, 0, 1);
+ this.configureAttribute(6, this.edgeTargetSizeBuffer, 1, 0, 1);
gl.bindVertexArray(this.arrowVao);
this.configureAttribute(0, this.createStaticBuffer(arrowCorners), 2, 0, 0);
this.configureAttribute(1, this.arrowEndpointBuffer, 2, 16, 1, 0);