Skip to content

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

:art: Improve graph display and capacity https://github.com/siyuan-note/siyuan/issues/18642

Danielf085485310b4
diff --git a/app/appearance/themes/daylight/theme.css b/app/appearance/themes/daylight/theme.css
index b7536c4bc5..875de7b392 100644
--- a/app/appearance/themes/daylight/theme.css
+++ b/app/appearance/themes/daylight/theme.css
@@ -143,28 +143,28 @@
--b3-button-active-shadow: 0 2px 3px 1px rgb(0 0 0 / .3), 0 2px 4px 2px rgb(0 0 0 /.15);
/* 图表颜色 */
- --b3-graph-p-point: #477980;
- --b3-graph-heading-point: #7055a5;
- --b3-graph-math-point: #3f7f5f;
- --b3-graph-code-point: #34798a;
- --b3-graph-table-point: #416fa8;
- --b3-graph-list-point: #a64f75;
- --b3-graph-todo-point: #a87422;
- --b3-graph-olist-point: #894c6b;
- --b3-graph-listitem-point: #af603a;
- --b3-graph-bq-point: #715d97;
- --b3-graph-callout-point: #54814f;
- --b3-graph-super-point: #92589b;
- --b3-graph-doc-point: #4f6f9a;
- --b3-graph-tag-point: #7c852a;
- --b3-graph-asset-point: #307d69;
+ --b3-graph-p-point: #076f7e;
+ --b3-graph-heading-point: #8250df;
+ --b3-graph-math-point: #80FFA5;
+ --b3-graph-code-point: #00DDFF;
+ --b3-graph-table-point: #37A2FF;
+ --b3-graph-list-point: #FF0087;
+ --b3-graph-todo-point: #FFBF00;
+ --b3-graph-olist-point: #b3005f;
+ --b3-graph-listitem-point: #f65b00;
+ --b3-graph-bq-point: #8d48e3;
+ --b3-graph-callout-point: var(--b3-theme-success);
+ --b3-graph-super-point: #dd79ff;
+ --b3-graph-doc-point: #202124;
+ --b3-graph-tag-point: #dbf32f;
+ --b3-graph-asset-point: #05c091;
--b3-graph-line: rgba(0, 0, 0, .6);
--b3-graph-ref-line: #d23f31;
--b3-graph-tag-line: #5f6b06;
--b3-graph-tag-tag-line: #dbf32f;
--b3-graph-asset-line: #037457;
--b3-graph-hl-point: #c98a00;
- --b3-graph-hl-line: #4285f4;
+ --b3-graph-hl-line: #d23f31;
/* 代码片段背景 */
--b3-protyle-code-background: rgba(27, 31, 35, .05);
diff --git a/app/appearance/themes/midnight/theme.css b/app/appearance/themes/midnight/theme.css
index b0dc45a474..bbaa7dea3d 100644
--- a/app/appearance/themes/midnight/theme.css
+++ b/app/appearance/themes/midnight/theme.css
@@ -142,28 +142,28 @@
--b3-button-active-shadow: 0 2px 3px 1px rgb(0 0 0 / .3), 0 2px 4px 2px rgb(255 255 255 /.15);
/* 图表颜色 */
- --b3-graph-p-point: #72a6ad;
- --b3-graph-heading-point: #ad9ad6;
- --b3-graph-math-point: #78b591;
- --b3-graph-code-point: #68adbc;
- --b3-graph-table-point: #79a1d0;
- --b3-graph-list-point: #cf82a5;
- --b3-graph-todo-point: #d0a156;
- --b3-graph-olist-point: #be7397;
- --b3-graph-listitem-point: #d08d67;
- --b3-graph-bq-point: #a393c6;
- --b3-graph-callout-point: #86ae80;
- --b3-graph-super-point: #be86c8;
- --b3-graph-doc-point: #91a9c8;
- --b3-graph-tag-point: #b1ba6a;
- --b3-graph-asset-point: #68ad98;
+ --b3-graph-p-point: #076f7e;
+ --b3-graph-heading-point: hsl(254, 80%, 74.8%);
+ --b3-graph-math-point: #80FFA5;
+ --b3-graph-code-point: #00DDFF;
+ --b3-graph-table-point: #37A2FF;
+ --b3-graph-list-point: #FF0087;
+ --b3-graph-todo-point: #FFBF00;
+ --b3-graph-olist-point: #b3005f;
+ --b3-graph-listitem-point: #f65b00;
+ --b3-graph-bq-point: #8d48e3;
+ --b3-graph-callout-point: var(--b3-theme-success);
+ --b3-graph-super-point: #dd79ff;
+ --b3-graph-doc-point: #e8eaed;
+ --b3-graph-tag-point: #dbf32f;
+ --b3-graph-asset-point: #05c091;
--b3-graph-line: rgba(255, 255, 255, .55);
--b3-graph-ref-line: #d23f31;
--b3-graph-tag-line: #5f6b06;
--b3-graph-tag-tag-line: #dbf32f;
--b3-graph-asset-line: #037457;
--b3-graph-hl-point: #f2c14e;
- --b3-graph-hl-line: #4285f4;
+ --b3-graph-hl-line: #d23f31;
/* 代码片段背景 */
--b3-protyle-code-background: rgba(240, 246, 252, .15);
diff --git a/app/src/layout/dock/Graph.ts b/app/src/layout/dock/Graph.ts
index d653b2b504..b04226d332 100644
--- a/app/src/layout/dock/Graph.ts
+++ b/app/src/layout/dock/Graph.ts
@@ -17,6 +17,7 @@ import {IGraphNodeClick, IGraphSourceLink, IGraphSourceNode} from "./graph/types
export class Graph extends Model {
public inputElement: HTMLInputElement;
+ private countElement: HTMLElement;
private graphElement: HTMLDivElement;
private graphEngine: GraphEngine;
private panelElement: HTMLElement;
@@ -233,7 +234,7 @@ export class Graph extends Model {
<button class="b3-button b3-button--small fn__block">${window.siyuan.languages.reset}</button>`;
}
this.element.innerHTML = `<div class="block__icons">
- <div class="block__logo fn__flex-1">${this.type === "global" ? window.siyuan.languages.globalGraph : window.siyuan.languages.graphView}</div>
+ <div class="block__logo block__logo--counter fn__flex-1">${this.type === "global" ? window.siyuan.languages.globalGraph : window.siyuan.languages.graphView}<span class="counter fn__none" data-type="node-count"></span></div>
<input class="b3-text-field search__label fn__size200 fn__none" placeholder="${window.siyuan.languages.searchPlaceholder}" />
<span data-type="search" class="block__icon ariaLabel" data-position="north" aria-label="${window.siyuan.languages.search}"><svg><use xlink:href='#iconFilter'></use></svg></span>
<span class="fn__space"></span>
@@ -253,6 +254,7 @@ export class Graph extends Model {
${panelHTML}
</div>
<div class="fn__flex-1 graph__svg"></div>`;
+ this.countElement = this.element.querySelector('[data-type="node-count"]');
this.graphElement = this.element.querySelector(".graph__svg");
this.graphEngine = new GraphEngine(this.graphElement, {
onNodeClick: (details) => this.openGraphNode(details),
@@ -611,6 +613,7 @@ export class Graph extends Model {
}
public onGraph(hl: boolean) {
+ this.updateNodeCount();
if (this.graphElement.clientHeight === 0) {
// 界面没有渲染时不能进行渲染
return;
@@ -656,6 +659,16 @@ export class Graph extends Model {
}
}
+ private updateNodeCount() {
+ if (!this.graphData?.nodes) {
+ this.countElement.textContent = "";
+ this.countElement.classList.add("fn__none");
+ return;
+ }
+ this.countElement.textContent = this.graphData.nodes.length.toString();
+ this.countElement.classList.remove("fn__none");
+ }
+
private openGraphNode(details: IGraphNodeClick) {
const node = details.node;
if (-1 < node.type.indexOf("tag")) {
diff --git a/app/src/layout/dock/graph/canvasRenderer.ts b/app/src/layout/dock/graph/canvasRenderer.ts
index 88acb88455..37b6525041 100644
--- a/app/src/layout/dock/graph/canvasRenderer.ts
+++ b/app/src/layout/dock/graph/canvasRenderer.ts
@@ -1,4 +1,5 @@
import {getGraphNodeColor, IGraphRenderer, IGraphRenderState} from "./renderer";
+import {getGraphEdgeOpacity, MIN_GRAPH_EDGE_WIDTH} from "./core";
export class GraphCanvasRenderer implements IGraphRenderer {
private readonly canvas: HTMLCanvasElement;
@@ -23,9 +24,8 @@ export class GraphCanvasRenderer implements IGraphRenderer {
const context = this.context;
context.setTransform(state.devicePixelRatio, 0, 0, state.devicePixelRatio, 0, 0);
context.clearRect(0, 0, state.width, state.height);
- context.lineWidth = Math.max(1, state.options.linkWidth * state.camera.scale);
+ context.lineWidth = Math.max(MIN_GRAPH_EDGE_WIDTH, state.options.linkWidth * state.camera.scale);
context.lineCap = "round";
- context.globalAlpha = state.options.lineOpacity;
this.drawEdgeGroup(state, false, false);
this.drawEdgeGroup(state, true, false);
this.drawEdgeGroup(state, false, true);
@@ -67,6 +67,7 @@ export class GraphCanvasRenderer implements IGraphRenderer {
}
context.strokeStyle = highlighted ? state.palette.highlightLine :
reference ? state.palette.referenceLine : state.palette.line;
+ context.globalAlpha = getGraphEdgeOpacity(state.options.lineOpacity, highlighted);
context.stroke();
}
@@ -105,6 +106,7 @@ export class GraphCanvasRenderer implements IGraphRenderer {
const highlighted = link.source === state.selected || link.target === state.selected ||
link.source === state.hovered || link.target === state.hovered;
context.fillStyle = highlighted ? state.palette.highlightLine : state.palette.referenceLine;
+ context.globalAlpha = getGraphEdgeOpacity(state.options.lineOpacity, highlighted);
context.fill();
});
}
diff --git a/app/src/layout/dock/graph/core.test.ts b/app/src/layout/dock/graph/core.test.ts
index 9e3ced5ce8..fc5c754f32 100644
--- a/app/src/layout/dock/graph/core.test.ts
+++ b/app/src/layout/dock/graph/core.test.ts
@@ -4,6 +4,7 @@ import {
createInitialPositions,
fitGraphCamera,
getDraggedGraphPosition,
+ getGraphEdgeOpacity,
getGraphNodeSize,
normalizeGraphData,
} from "./core";
@@ -64,6 +65,13 @@ describe("graph data normalization", () => {
assert.deepEqual(position, {x: 35, y: 27});
});
+ it("emphasizes highlighted edges without exceeding valid opacity", () => {
+ assert.ok(Math.abs(getGraphEdgeOpacity(0.36, false) - 0.45) < Number.EPSILON);
+ assert.ok(Math.abs(getGraphEdgeOpacity(0.36, true) - 0.9) < Number.EPSILON);
+ assert.equal(getGraphEdgeOpacity(0.8, true), 1);
+ assert.equal(getGraphEdgeOpacity(-0.5, false), 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 17b0d66159..db22919ed9 100644
--- a/app/src/layout/dock/graph/core.ts
+++ b/app/src/layout/dock/graph/core.ts
@@ -8,6 +8,12 @@ import {
} from "./types";
const GOLDEN_ANGLE = Math.PI * (3 - Math.sqrt(5));
+const EDGE_OPACITY_FACTOR = 1.25;
+const HIGHLIGHT_EDGE_OPACITY_FACTOR = 2.5;
+export const MIN_GRAPH_EDGE_WIDTH = 1.5;
+
+export const getGraphEdgeOpacity = (lineOpacity: number, highlighted: boolean) =>
+ Math.max(0, Math.min(1, lineOpacity * (highlighted ? HIGHLIGHT_EDGE_OPACITY_FACTOR : EDGE_OPACITY_FACTOR)));
export const getGraphNodeSize = (baseSize: number, definitions: number) => {
if (definitions < 1) {
diff --git a/app/src/layout/dock/graph/webglRenderer.ts b/app/src/layout/dock/graph/webglRenderer.ts
index d2afb5283e..b9fdb2bd17 100644
--- a/app/src/layout/dock/graph/webglRenderer.ts
+++ b/app/src/layout/dock/graph/webglRenderer.ts
@@ -1,4 +1,5 @@
import {getGraphNodeColor, IGraphRenderer, IGraphRenderState, parseGraphColor} from "./renderer";
+import {getGraphEdgeOpacity, MIN_GRAPH_EDGE_WIDTH} from "./core";
const NODE_VERTEX_SHADER = `#version 300 es
layout(location = 0) in vec2 a_corner;
@@ -51,6 +52,7 @@ layout(location = 3) in vec4 a_color;
layout(location = 4) in float a_state;
uniform vec2 u_viewport;
uniform vec3 u_camera;
+uniform float u_min_width;
uniform float u_width;
out vec4 v_color;
out float v_state;
@@ -65,7 +67,7 @@ void main() {
float distance = max(0.001, length(delta));
vec2 direction = delta / distance;
vec2 normal = vec2(-direction.y, direction.x);
- float width = max(1.0, u_width * u_camera.z);
+ 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);
@@ -89,6 +91,7 @@ in float v_half_width;
in float v_length;
in float v_position;
uniform vec4 u_highlight;
+uniform float u_highlight_opacity;
uniform float u_opacity;
out vec4 out_color;
void main() {
@@ -104,7 +107,8 @@ void main() {
discard;
}
vec4 color = v_state > 0.0 ? u_highlight : v_color;
- out_color = vec4(color.rgb, color.a * u_opacity * coverage);
+ float opacity = v_state > 0.0 ? u_highlight_opacity : u_opacity;
+ out_color = vec4(color.rgb, color.a * opacity * coverage);
}`;
const ARROW_VERTEX_SHADER = `#version 300 es
@@ -144,6 +148,7 @@ in vec4 v_color;
in float v_state;
in vec3 v_barycentric;
uniform vec4 u_highlight;
+uniform float u_highlight_opacity;
uniform float u_opacity;
out vec4 out_color;
void main() {
@@ -151,7 +156,8 @@ void main() {
float antialias = max(fwidth(distance), 0.0001);
float coverage = smoothstep(0.0, antialias, distance);
vec4 color = v_state > 0.0 ? u_highlight : v_color;
- out_color = vec4(color.rgb, color.a * u_opacity * coverage);
+ float opacity = v_state > 0.0 ? u_highlight_opacity : u_opacity;
+ out_color = vec4(color.rgb, color.a * opacity * coverage);
}`;
export class GraphWebGLRenderer implements IGraphRenderer {
@@ -225,7 +231,7 @@ export class GraphWebGLRenderer implements IGraphRenderer {
this.arrowStateBuffer = this.createBuffer();
this.configureVertexArrays();
gl.enable(gl.BLEND);
- gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
+ gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
}
public render(state: IGraphRenderState) {
@@ -378,8 +384,12 @@ 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_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"), state.options.lineOpacity);
+ gl.uniform1f(this.getUniform(this.edgeProgram, "u_opacity"),
+ getGraphEdgeOpacity(state.options.lineOpacity, false));
+ gl.uniform1f(this.getUniform(this.edgeProgram, "u_highlight_opacity"),
+ getGraphEdgeOpacity(state.options.lineOpacity, true));
gl.uniform4fv(this.getUniform(this.edgeProgram, "u_highlight"), this.highlightLine);
gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, state.data.links.length);
}
@@ -393,7 +403,10 @@ export class GraphWebGLRenderer implements IGraphRenderer {
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_opacity"), state.options.lineOpacity);
+ gl.uniform1f(this.getUniform(this.arrowProgram, "u_opacity"),
+ getGraphEdgeOpacity(state.options.lineOpacity, false));
+ gl.uniform1f(this.getUniform(this.arrowProgram, "u_highlight_opacity"),
+ getGraphEdgeOpacity(state.options.lineOpacity, true));
gl.uniform4fv(this.getUniform(this.arrowProgram, "u_highlight"), this.highlightLine);
gl.drawArraysInstanced(gl.TRIANGLES, 0, 3, this.arrowIndices.length);
}
diff --git a/kernel/conf/graph.go b/kernel/conf/graph.go
index 677ea0c767..0984cc9c07 100644
--- a/kernel/conf/graph.go
+++ b/kernel/conf/graph.go
@@ -16,6 +16,11 @@
package conf
+const (
+ defaultGraphMaxBlocks = 1024 * 16
+ legacyGraphMaxBlocks = 1024 * 10
+)
+
type Graph struct {
MaxBlocks int `json:"maxBlocks"` // 内容块最大显示数
@@ -25,12 +30,19 @@ type Graph struct {
func NewGraph() *Graph {
return &Graph{
- MaxBlocks: 1024 * 10,
+ MaxBlocks: defaultGraphMaxBlocks,
Local: NewLocalGraph(),
Global: NewGlobalGraph(),
}
}
+// NormalizeMaxBlocks 将缺失值和历史默认值迁移到当前默认值。
+func (graph *Graph) NormalizeMaxBlocks() {
+ if 1 > graph.MaxBlocks || legacyGraphMaxBlocks == graph.MaxBlocks {
+ graph.MaxBlocks = defaultGraphMaxBlocks
+ }
+}
+
type LocalGraph struct {
DailyNote bool `json:"dailyNote"`
*TypeFilter `json:"type"`
diff --git a/kernel/conf/graph_test.go b/kernel/conf/graph_test.go
new file mode 100644
index 0000000000..ea314f466d
--- /dev/null
+++ b/kernel/conf/graph_test.go
@@ -0,0 +1,44 @@
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package conf
+
+import "testing"
+
+func TestNormalizeGraphMaxBlocks(t *testing.T) {
+ if defaultGraphMaxBlocks != NewGraph().MaxBlocks {
+ t.Fatalf("expected new graph limit to be %d", defaultGraphMaxBlocks)
+ }
+
+ graph := &Graph{MaxBlocks: legacyGraphMaxBlocks}
+ graph.NormalizeMaxBlocks()
+ if defaultGraphMaxBlocks != graph.MaxBlocks {
+ t.Fatalf("expected legacy limit to migrate to %d, got %d", defaultGraphMaxBlocks, graph.MaxBlocks)
+ }
+
+ graph.MaxBlocks = 0
+ graph.NormalizeMaxBlocks()
+ if defaultGraphMaxBlocks != graph.MaxBlocks {
+ t.Fatalf("expected invalid limit to normalize to %d, got %d", defaultGraphMaxBlocks, graph.MaxBlocks)
+ }
+
+ const customLimit = 20_000
+ graph.MaxBlocks = customLimit
+ graph.NormalizeMaxBlocks()
+ if customLimit != graph.MaxBlocks {
+ t.Fatalf("expected custom limit to remain %d, got %d", customLimit, graph.MaxBlocks)
+ }
+}
diff --git a/kernel/model/conf.go b/kernel/model/conf.go
index ddb87f789f..de34248a11 100644
--- a/kernel/model/conf.go
+++ b/kernel/model/conf.go
@@ -523,6 +523,8 @@ func InitConf() {
}
if nil == Conf.Graph || nil == Conf.Graph.Local || nil == Conf.Graph.Global {
Conf.Graph = conf.NewGraph()
+ } else {
+ Conf.Graph.NormalizeMaxBlocks()
}
isNewWorkspace := nil == Conf.System