Skip to content

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

:art: Improve isolated graph label fitting https://github.com/siyuan-note/siyuan/issues/18956

Daniel5ed5fb3d8ac6
diff --git a/app/src/layout/dock/graph/GraphEngine.ts b/app/src/layout/dock/graph/GraphEngine.ts
index 585b49058b..5c4da1548e 100644
--- a/app/src/layout/dock/graph/GraphEngine.ts
+++ b/app/src/layout/dock/graph/GraphEngine.ts
@@ -3,6 +3,7 @@ import {
centerGraphCamera,
createInitialPositions,
fitGraphCamera,
+ fitSingleNodeCamera,
getDraggedGraphPosition,
getGraphNodeSize,
normalizeGraphData,
@@ -182,6 +183,9 @@ export class GraphEngine {
});
}
}
+ if (nodeSizeChanged && this.data.nodes.length === 1 && !this.cameraTouched) {
+ this.fit(false);
+ }
this.scheduleRender();
}
@@ -220,6 +224,9 @@ export class GraphEngine {
}
this.selected = selected;
this.selectionVersion++;
+ if (this.data?.nodes.length === 1 && !this.cameraTouched) {
+ this.fit(false);
+ }
this.scheduleRender();
}
@@ -278,7 +285,8 @@ export class GraphEngine {
if (wasHidden) {
this.postLayoutMessage({type: "resume", generation: this.generation});
}
- if (this.data && !this.cameraTouched && (wasHidden || this.autoFitPending)) {
+ if (this.data && !this.cameraTouched &&
+ (wasHidden || this.autoFitPending || (sizeChanged && this.data.nodes.length === 1))) {
this.fit(false);
} else if (this.data && sizeChanged) {
this.focusAnimation++;
@@ -466,7 +474,19 @@ export class GraphEngine {
if (!this.data || this.width < 1 || this.height < 1) {
return;
}
- const target = fitGraphCamera(this.positions, this.data.sizes, this.width, this.height);
+ let target = fitGraphCamera(this.positions, this.data.sizes, this.width, this.height);
+ const node = this.data.nodes.length === 1 ? this.data.nodes[0] : undefined;
+ if (node?.label) {
+ target = fitSingleNodeCamera(
+ this.positions[0],
+ this.positions[1],
+ this.data.sizes[0],
+ this.width,
+ this.height,
+ target.scale,
+ (scale) => this.labelRenderer.measureLabel(node.label, scale, this.selected === 0 || this.hovered === 0),
+ );
+ }
if (!animate) {
this.focusAnimation++;
this.camera = target;
@@ -745,11 +765,13 @@ export class GraphEngine {
private updateHover(pointer: IPointerPosition) {
const hovered = this.hitTest(pointer.x, pointer.y);
this.setHovered(hovered);
- if (hovered < 0 || !this.data.nodes[hovered].title) {
+ const node = hovered < 0 ? undefined : this.data.nodes[hovered];
+ const title = node?.title || (this.data.nodes.length === 1 ? node?.label : "");
+ if (!title) {
this.hideTooltip();
return;
}
- this.tooltip.textContent = this.data.nodes[hovered].title;
+ this.tooltip.textContent = title;
this.tooltip.style.left = `${pointer.x + 12}px`;
this.tooltip.style.top = `${pointer.y + 12}px`;
this.tooltip.classList.remove("fn__none");
diff --git a/app/src/layout/dock/graph/core.test.ts b/app/src/layout/dock/graph/core.test.ts
index a142c47eec..ef0defd9ea 100644
--- a/app/src/layout/dock/graph/core.test.ts
+++ b/app/src/layout/dock/graph/core.test.ts
@@ -4,6 +4,7 @@ import {
centerGraphCamera,
createInitialPositions,
fitGraphCamera,
+ fitSingleNodeCamera,
getGraphArrowGeometry,
getGraphArrowLength,
getDraggedGraphPosition,
@@ -114,6 +115,20 @@ describe("graph data normalization", () => {
assert.deepEqual(camera, {scale: 1, x: 200, y: 150});
});
+ it("fits and centers a single node together with its label", () => {
+ const measureLabel = (scale: number) => ({height: 32 * scale, width: 180 * scale});
+ const camera = fitSingleNodeCamera(0, 0, 15, 180, 200, 1, measureLabel);
+ const radius = 15 * camera.scale;
+ const label = measureLabel(camera.scale);
+ const left = camera.x - radius;
+ const right = camera.x + radius + 4 + label.width;
+
+ assert.ok(camera.scale < 1);
+ assert.ok(left >= 8 - Number.EPSILON);
+ assert.ok(right <= 172 + Number.EPSILON);
+ assert.ok(Math.abs((left + right) / 2 - 90) < Number.EPSILON);
+ });
+
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 f839147f54..6c86e6ae47 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 MIN_AUTO_FIT_SCALE = 0.02;
const MAX_AUTO_FIT_SCALE = 1;
export const MIN_GRAPH_EDGE_WIDTH = 1;
@@ -472,7 +473,7 @@ 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(MAX_AUTO_FIT_SCALE, availableWidth / contentWidth,
+ const scale = Math.max(MIN_AUTO_FIT_SCALE, Math.min(MAX_AUTO_FIT_SCALE, availableWidth / contentWidth,
availableHeight / contentHeight));
return {
scale,
@@ -481,6 +482,62 @@ export const fitGraphCamera = (
};
};
+export const fitSingleNodeCamera = (
+ x: number,
+ y: number,
+ radius: number,
+ width: number,
+ height: number,
+ maxScale: number,
+ measureLabel: (scale: number) => {height: number, width: number},
+ padding = 8,
+): IGraphCamera => {
+ if (width < 1 || height < 1) {
+ return {scale: 1, x: width / 2, y: height / 2};
+ }
+ const availableWidth = Math.max(1, width - padding * 2);
+ const availableHeight = Math.max(1, height - padding * 2);
+ const getBounds = (scale: number) => {
+ const nodeRadius = Math.max(1, radius * scale);
+ const labelOffset = Math.max(2, radius * scale) + 4;
+ const label = measureLabel(scale);
+ return {
+ height: Math.max(nodeRadius * 2, label.height),
+ left: -nodeRadius,
+ right: labelOffset + label.width,
+ };
+ };
+ const fits = (scale: number) => {
+ const bounds = getBounds(scale);
+ return bounds.right - bounds.left <= availableWidth && bounds.height <= availableHeight;
+ };
+ let scale = Math.max(MIN_AUTO_FIT_SCALE, Math.min(MAX_AUTO_FIT_SCALE, maxScale));
+ if (!fits(scale)) {
+ let minimum = MIN_AUTO_FIT_SCALE;
+ let maximum = scale;
+ if (fits(minimum)) {
+ for (let index = 0; index < 24; index++) {
+ const candidate = (minimum + maximum) / 2;
+ if (fits(candidate)) {
+ minimum = candidate;
+ } else {
+ maximum = candidate;
+ }
+ }
+ scale = minimum;
+ } else {
+ scale = minimum;
+ }
+ }
+ const bounds = getBounds(scale);
+ const centerX = (bounds.left + bounds.right) / 2;
+ return {
+ scale,
+ x: width / 2 - centerX - x * scale,
+ y: height / 2 - y * scale,
+ };
+};
+
export const centerGraphCamera = (
positions: Float32Array,
sizes: Float32Array,
diff --git a/app/src/layout/dock/graph/labelRenderer.ts b/app/src/layout/dock/graph/labelRenderer.ts
index c0fdd2843d..67b5546255 100644
--- a/app/src/layout/dock/graph/labelRenderer.ts
+++ b/app/src/layout/dock/graph/labelRenderer.ts
@@ -4,6 +4,11 @@ const LABEL_FONT_SIZE = 32;
const LABEL_CELL_WIDTH = 72;
const LABEL_CELL_HEIGHT = 20;
+export const getGraphLabelFontSize = (scale: number, priority: boolean) => {
+ const scaledFontSize = LABEL_FONT_SIZE * scale;
+ return priority ? Math.max(12, Math.min(40, scaledFontSize)) : Math.min(40, scaledFontSize);
+};
+
export class GraphLabelRenderer {
private readonly canvas: HTMLCanvasElement;
private readonly context: CanvasRenderingContext2D;
@@ -63,7 +68,7 @@ export class GraphLabelRenderer {
continue;
}
const priorityLabel = index === state.selected || index === state.hovered;
- const fontSize = priorityLabel ? Math.max(12, Math.min(40, scaledFontSize)) : Math.min(40, scaledFontSize);
+ const fontSize = getGraphLabelFontSize(state.camera.scale, priorityLabel);
if (fontSize < 5) {
continue;
}
@@ -94,6 +99,16 @@ export class GraphLabelRenderer {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
+ public measureLabel(label: string, scale: number, priority: boolean) {
+ const fontSize = getGraphLabelFontSize(scale, priority);
+ const fontFamily = getComputedStyle(document.body).getPropertyValue("--b3-font-family-graph").trim() || "sans-serif";
+ this.context.font = `${fontSize}px ${fontFamily}`;
+ return {
+ height: fontSize,
+ width: this.context.measureText(label).width,
+ };
+ }
+
private isOccupied(occupied: Set<number>, x: number, y: number, width: number, height: number) {
const startX = Math.floor(x / LABEL_CELL_WIDTH);
const endX = Math.floor((x + width) / LABEL_CELL_WIDTH);