Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

feat(diagram): start new diagrams with labels in the interface language (#594)

Dariusz Korytod96b8f6e69ec
diff --git a/apps/web/src/components/WorkspaceApp.tsx b/apps/web/src/components/WorkspaceApp.tsx
index 28ad24ed..a50da2c1 100644
--- a/apps/web/src/components/WorkspaceApp.tsx
+++ b/apps/web/src/components/WorkspaceApp.tsx
@@ -1957,14 +1957,42 @@ export const WorkspaceApp = ({
});
return;
}
- const diagram = kind ? createDefaultDiagramDocument(kind) : null;
- createMemoMutation.mutate({
+ if (!kind) {
+ createMemoMutation.mutate({ notebookId: targetNotebookId, title: "", contentMarkdown: "", tags: [] });
+ return;
+ }
+ const seed = createDefaultDiagramDocument(kind, {
+ mindMapRoot: t("diagram.seed.mindMapRoot"),
+ mindMapCapture: t("diagram.seed.mindMapCapture"),
+ mindMapOrganize: t("diagram.seed.mindMapOrganize"),
+ mindMapShare: t("diagram.seed.mindMapShare"),
+ mindMapQuickNotes: t("diagram.seed.mindMapQuickNotes"),
+ mindMapSync: t("diagram.seed.mindMapSync"),
+ mindMapNotebooks: t("diagram.seed.mindMapNotebooks"),
+ mindMapPublicLinks: t("diagram.seed.mindMapPublicLinks"),
+ flowchartStart: t("diagram.seed.flowchartStart"),
+ flowchartProcess: t("diagram.seed.flowchartProcess"),
+ flowchartEnd: t("diagram.seed.flowchartEnd"),
+ architectureSystem: t("diagram.seed.architectureSystem"),
+ architectureClient: t("diagram.seed.architectureClient"),
+ architectureApi: t("diagram.seed.architectureApi"),
+ architectureDatabase: t("diagram.seed.architectureDatabase"),
+ architectureStorage: t("diagram.seed.architectureStorage"),
+ architectureQuery: t("diagram.seed.architectureQuery"),
+ architectureFiles: t("diagram.seed.architectureFiles"),
+ });
+ const createDiagramMemo = (diagram: typeof seed) => createMemoMutation.mutate({
notebookId: targetNotebookId,
- title: diagram ? (kind === "mind-map" ? t("diagram.mindMap") : kind === "architecture" ? t("diagram.architecture") : t("diagram.flowchart")) : "",
- contentJson: diagram ? markdownToDoc(diagramFallbackMarkdown(diagram)) : undefined,
- contentMarkdown: diagram ? serializeDiagramDocument(diagram) : "",
+ title: kind === "mind-map" ? t("diagram.mindMap") : kind === "architecture" ? t("diagram.architecture") : t("diagram.flowchart"),
+ contentJson: markdownToDoc(diagramFallbackMarkdown(diagram)),
+ contentMarkdown: serializeDiagramDocument(diagram),
tags: [],
});
+ // The layout module ships with the lazily loaded diagram editor. If it
+ // cannot load, keep the starter geometry rather than failing the note.
+ void import("@/lib/diagram-layout")
+ .then(({ fitDefaultDiagramLabels }) => fitDefaultDiagramLabels(seed), () => seed)
+ .then(createDiagramMemo);
};
const handleImportMarkdownFiles = async (files: File[]) => {
diff --git a/apps/web/src/lib/diagram-layout.test.mjs b/apps/web/src/lib/diagram-layout.test.mjs
index 65622570..22c9c558 100644
--- a/apps/web/src/lib/diagram-layout.test.mjs
+++ b/apps/web/src/lib/diagram-layout.test.mjs
@@ -8,6 +8,7 @@ import {
computeDiagramLayout,
computeDiagramLayoutResult,
DIAGRAM_READABLE_MIN_SCALE,
+ fitDefaultDiagramLabels,
getDiagramLayoutViewport,
} from "./diagram-layout.ts";
@@ -473,4 +474,39 @@ describe("diagram auto layout", () => {
minScale: DIAGRAM_READABLE_MIN_SCALE,
});
});
+
+ test("keeps the default starter geometry and refits longer translated labels", () => {
+ for (const kind of ["mind-map", "flowchart", "architecture"]) {
+ const document = createDefaultDiagramDocument(kind);
+ expect(fitDefaultDiagramLabels(document)).toBe(document);
+ }
+
+ const document = createDefaultDiagramDocument("mind-map", {
+ mindMapRoot: "Temat główny",
+ mindMapCapture: "Zbieranie pomysłów",
+ mindMapQuickNotes: "Szybkie notatki",
+ });
+ const fitted = fitDefaultDiagramLabels(document);
+ expect(fitted).not.toBe(document);
+ expect(fitted.nodes[0].width).toBeGreaterThanOrEqual(compactMindMapNodeSize("Temat główny", true).width);
+ for (const [index, left] of fitted.nodes.entries()) {
+ for (const right of fitted.nodes.slice(index + 1)) {
+ const overlaps = left.x < right.x + right.width && right.x < left.x + left.width
+ && left.y < right.y + right.height && right.y < left.y + left.height;
+ expect(overlaps).toBe(false);
+ }
+ }
+ const byId = Object.fromEntries(fitted.nodes.map((node) => [node.id, node]));
+ expect(byId["topic-1-a"].x).toBeGreaterThanOrEqual(byId["topic-1"].x + byId["topic-1"].width);
+
+ const architecture = fitDefaultDiagramLabels(createDefaultDiagramDocument("architecture", {
+ architectureStorage: "Magazyn obiektów i plików",
+ }));
+ const boundary = architecture.nodes.find((node) => node.id === "system");
+ for (const child of architecture.nodes.filter((node) => node.parentId === "system")) {
+ expect(child.x).toBeGreaterThan(boundary.x);
+ expect(child.x + child.width).toBeLessThan(boundary.x + boundary.width);
+ expect(child.y + child.height).toBeLessThan(boundary.y + boundary.height);
+ }
+ });
});
diff --git a/apps/web/src/lib/diagram-layout.ts b/apps/web/src/lib/diagram-layout.ts
index f00728f2..db7eabc0 100644
--- a/apps/web/src/lib/diagram-layout.ts
+++ b/apps/web/src/lib/diagram-layout.ts
@@ -5,6 +5,7 @@ export {
compactFlowchartNodeSize,
flowchartNodePresentation,
compactMindMapNodeSize,
+ fitDefaultDiagramLabels,
computeDiagramLayout,
computeDiagramLayoutResult,
DIAGRAM_READABLE_MIN_SCALE,
diff --git a/packages/shared/src/diagram-layout.ts b/packages/shared/src/diagram-layout.ts
index a71bf355..1a594199 100644
--- a/packages/shared/src/diagram-layout.ts
+++ b/packages/shared/src/diagram-layout.ts
@@ -24,10 +24,12 @@ import {
ARCHITECTURE_DIAGRAM_SCHEMA_VERSION,
DIAGRAM_DEFAULT_THEME,
DIAGRAM_SCHEMA_VERSION,
+ createDefaultDiagramDocument,
type ArchitectureResourceIcon,
type DiagramDocument,
type DiagramEdgeKind,
type DiagramKind,
+ type DiagramNode,
type DiagramNodeShape,
type DiagramStructure,
type DiagramTheme,
@@ -1189,3 +1191,40 @@ export const compileDiagramIr = (ir: DiagramIr): DiagramDocument => {
}
return document;
};
+
+// Starter documents carry node sizes chosen for their default labels. When a
+// translated label needs more room than the label it replaces, size every node
+// for its label and lay the document out again, as compileDiagramIr does.
+export const fitDefaultDiagramLabels = (document: DiagramDocument): DiagramDocument => {
+ const labelSize = (node: DiagramNode, nodes: DiagramNode[]) => document.kind === "mind-map"
+ ? mindMapNodePresentation(node.label, mindMapNodeRole(nodes, node.id), document.structure)
+ : document.kind === "architecture"
+ ? architectureNodeSizeForLabel(node.shape, node.label)
+ : flowchartNodePresentation(node.shape, node.label);
+ const defaults = createDefaultDiagramDocument(document.kind);
+ const defaultNodes = new Map(defaults.nodes.map((node) => [node.id, node]));
+ const needsRoom = document.nodes.some((node) => {
+ const defaultNode = defaultNodes.get(node.id);
+ if (!defaultNode) return false;
+ const size = labelSize(node, document.nodes);
+ const defaultSize = labelSize(defaultNode, defaults.nodes);
+ return size.width > defaultSize.width || size.height > defaultSize.height;
+ });
+ if (!needsRoom) return document;
+ const fitted: DiagramDocument = {
+ ...document,
+ nodes: document.nodes.map((node) => {
+ const size = labelSize(node, document.nodes);
+ return { ...node, width: Math.max(node.width, size.width), height: Math.max(node.height, size.height) };
+ }),
+ edges: document.edges.map((edge) => ({ ...edge })),
+ };
+ const layout = computeDiagramLayoutResult(fitted, {
+ direction: document.kind === "flowchart" ? "top-to-bottom" : "left-to-right",
+ });
+ for (const node of fitted.nodes) {
+ const geometry = layout.nodes[node.id];
+ if (geometry) Object.assign(node, geometry);
+ }
+ return fitted;
+};
diff --git a/packages/shared/src/diagram.test.mjs b/packages/shared/src/diagram.test.mjs
index fee9c9fd..d6aee0cc 100644
--- a/packages/shared/src/diagram.test.mjs
+++ b/packages/shared/src/diagram.test.mjs
@@ -181,6 +181,32 @@ describe("diagram document", () => {
expect(fallback).toContain("classDef archDatabase");
});
+ test("uses supplied starter labels and keeps the structure of each default diagram", () => {
+ const localized = createDefaultDiagramDocument("architecture", {
+ architectureSystem: "Aplikacja",
+ architectureClient: "Klient webowy",
+ architectureApi: "Usługa API",
+ architectureDatabase: "Baza danych",
+ architectureStorage: "Magazyn obiektów",
+ architectureQuery: "Odczyt / zapis",
+ architectureFiles: "Pliki",
+ });
+ const fallback = createDefaultDiagramDocument("architecture");
+ expect(localized.nodes.map((node) => node.label)).toEqual(["Aplikacja", "Klient webowy", "Usługa API", "Baza danych", "Magazyn obiektów"]);
+ expect(localized.edges.map((edge) => edge.label)).toEqual(["HTTPS", "Odczyt / zapis", "Pliki"]);
+ const withoutLabels = (document) => ({
+ ...document,
+ nodes: document.nodes.map(({ label: _label, ...node }) => node),
+ edges: document.edges.map(({ label: _label, ...edge }) => edge),
+ });
+ expect(withoutLabels(localized)).toEqual(withoutLabels(fallback));
+
+ expect(createDefaultDiagramDocument("mind-map", { mindMapRoot: "Main topic" }).nodes[0].label).toBe("Main topic");
+ expect(createDefaultDiagramDocument("mind-map").nodes[0].label).toBe("核心主题");
+ expect(createDefaultDiagramDocument("flowchart", { flowchartStart: "Start", flowchartProcess: "Process step", flowchartEnd: "End" })
+ .nodes.map((node) => node.label)).toEqual(["Start", "Process step", "End"]);
+ });
+
test("projects forward diagonal architecture edges through horizontal anchors", () => {
const document = createDefaultDiagramDocument("architecture");
const edge = document.edges[0];
diff --git a/packages/shared/src/diagram.ts b/packages/shared/src/diagram.ts
index 69b33035..66ec7bb6 100644
--- a/packages/shared/src/diagram.ts
+++ b/packages/shared/src/diagram.ts
@@ -388,21 +388,42 @@ export const diagramDocumentToMermaid = (document: DiagramDocument) => {
export const serializeDiagramDocument = (document: DiagramDocument) =>
`${diagramFallbackMarkdown(document)}\n\n<!-- ${DIAGRAM_MARKER}:${encodeBase64Url(JSON.stringify(document))} -->`;
-export const createDefaultDiagramDocument = (kind: DiagramKind): DiagramDocument => {
+export type DiagramSeedLabels = {
+ mindMapRoot?: string;
+ mindMapCapture?: string;
+ mindMapOrganize?: string;
+ mindMapShare?: string;
+ mindMapQuickNotes?: string;
+ mindMapSync?: string;
+ mindMapNotebooks?: string;
+ mindMapPublicLinks?: string;
+ flowchartStart?: string;
+ flowchartProcess?: string;
+ flowchartEnd?: string;
+ architectureSystem?: string;
+ architectureClient?: string;
+ architectureApi?: string;
+ architectureDatabase?: string;
+ architectureStorage?: string;
+ architectureQuery?: string;
+ architectureFiles?: string;
+};
+
+export const createDefaultDiagramDocument = (kind: DiagramKind, labels: DiagramSeedLabels = {}): DiagramDocument => {
if (kind === "mind-map") {
return {
schemaVersion: DIAGRAM_SCHEMA_VERSION,
kind,
theme: DIAGRAM_DEFAULT_THEME,
nodes: [
- { id: "topic-root", label: "核心主题", x: 72, y: 168, width: 124, height: 46, shape: "topic" },
- { id: "topic-1", label: "采集想法", x: 268, y: 117, width: 96, height: 36, shape: "topic", parentId: "topic-root" },
- { id: "topic-2", label: "整理结构", x: 268, y: 201, width: 96, height: 36, shape: "topic", parentId: "topic-root" },
- { id: "topic-3", label: "输出分享", x: 268, y: 257, width: 96, height: 36, shape: "topic", parentId: "topic-root" },
- { id: "topic-1-a", label: "快速记录", x: 436, y: 89, width: 96, height: 36, shape: "topic", parentId: "topic-1" },
- { id: "topic-1-b", label: "跨设备同步", x: 436, y: 145, width: 96, height: 36, shape: "topic", parentId: "topic-1" },
- { id: "topic-2-a", label: "笔记本", x: 436, y: 201, width: 96, height: 36, shape: "topic", parentId: "topic-2" },
- { id: "topic-3-a", label: "公开链接", x: 436, y: 257, width: 96, height: 36, shape: "topic", parentId: "topic-3" },
+ { id: "topic-root", label: labels.mindMapRoot ?? "核心主题", x: 72, y: 168, width: 124, height: 46, shape: "topic" },
+ { id: "topic-1", label: labels.mindMapCapture ?? "采集想法", x: 268, y: 117, width: 96, height: 36, shape: "topic", parentId: "topic-root" },
+ { id: "topic-2", label: labels.mindMapOrganize ?? "整理结构", x: 268, y: 201, width: 96, height: 36, shape: "topic", parentId: "topic-root" },
+ { id: "topic-3", label: labels.mindMapShare ?? "输出分享", x: 268, y: 257, width: 96, height: 36, shape: "topic", parentId: "topic-root" },
+ { id: "topic-1-a", label: labels.mindMapQuickNotes ?? "快速记录", x: 436, y: 89, width: 96, height: 36, shape: "topic", parentId: "topic-1" },
+ { id: "topic-1-b", label: labels.mindMapSync ?? "跨设备同步", x: 436, y: 145, width: 96, height: 36, shape: "topic", parentId: "topic-1" },
+ { id: "topic-2-a", label: labels.mindMapNotebooks ?? "笔记本", x: 436, y: 201, width: 96, height: 36, shape: "topic", parentId: "topic-2" },
+ { id: "topic-3-a", label: labels.mindMapPublicLinks ?? "公开链接", x: 436, y: 257, width: 96, height: 36, shape: "topic", parentId: "topic-3" },
],
edges: [
{ id: "branch-1", source: "topic-root", target: "topic-1" },
@@ -420,16 +441,16 @@ export const createDefaultDiagramDocument = (kind: DiagramKind): DiagramDocument
schemaVersion: ARCHITECTURE_DIAGRAM_SCHEMA_VERSION,
kind,
nodes: [
- { id: "system", label: "应用系统", x: 220, y: 64, width: 590, height: 330, shape: "boundary" },
- { id: "client", label: "Web 客户端", x: 72, y: 190, width: 150, height: 64, shape: "client" },
- { id: "api", label: "API 服务", x: 280, y: 190, width: 156, height: 68, shape: "service", parentId: "system" },
- { id: "database", label: "数据库", x: 540, y: 118, width: 150, height: 64, shape: "database", parentId: "system" },
- { id: "storage", label: "对象存储", x: 540, y: 268, width: 150, height: 64, shape: "storage", parentId: "system" },
+ { id: "system", label: labels.architectureSystem ?? "应用系统", x: 220, y: 64, width: 590, height: 330, shape: "boundary" },
+ { id: "client", label: labels.architectureClient ?? "Web 客户端", x: 72, y: 190, width: 150, height: 64, shape: "client" },
+ { id: "api", label: labels.architectureApi ?? "API 服务", x: 280, y: 190, width: 156, height: 68, shape: "service", parentId: "system" },
+ { id: "database", label: labels.architectureDatabase ?? "数据库", x: 540, y: 118, width: 150, height: 64, shape: "database", parentId: "system" },
+ { id: "storage", label: labels.architectureStorage ?? "对象存储", x: 540, y: 268, width: 150, height: 64, shape: "storage", parentId: "system" },
],
edges: [
{ id: "request", source: "client", target: "api", label: "HTTPS", kind: "request" },
- { id: "query", source: "api", target: "database", label: "查询 / 写入", kind: "data" },
- { id: "objects", source: "api", target: "storage", label: "文件", kind: "data" },
+ { id: "query", source: "api", target: "database", label: labels.architectureQuery ?? "查询 / 写入", kind: "data" },
+ { id: "objects", source: "api", target: "storage", label: labels.architectureFiles ?? "文件", kind: "data" },
],
};
}
@@ -438,9 +459,9 @@ export const createDefaultDiagramDocument = (kind: DiagramKind): DiagramDocument
kind,
theme: DIAGRAM_DEFAULT_THEME,
nodes: [
- { id: "flow-start", label: "开始", x: 98, y: 48, width: 140, height: 44, shape: "terminator" },
- { id: "flow-process", label: "处理步骤", x: 80, y: 140, width: 176, height: 56, shape: "process" },
- { id: "flow-end", label: "结束", x: 98, y: 244, width: 140, height: 44, shape: "terminator" },
+ { id: "flow-start", label: labels.flowchartStart ?? "开始", x: 98, y: 48, width: 140, height: 44, shape: "terminator" },
+ { id: "flow-process", label: labels.flowchartProcess ?? "处理步骤", x: 80, y: 140, width: 176, height: 56, shape: "process" },
+ { id: "flow-end", label: labels.flowchartEnd ?? "结束", x: 98, y: 244, width: 140, height: 44, shape: "terminator" },
],
edges: [
{ id: "flow-edge-1", source: "flow-start", target: "flow-process" },
diff --git a/packages/shared/src/i18n/en-US.ts b/packages/shared/src/i18n/en-US.ts
index 9d5ca627..24552329 100644
--- a/packages/shared/src/i18n/en-US.ts
+++ b/packages/shared/src/i18n/en-US.ts
@@ -604,6 +604,26 @@ export const enUS = {
},
},
diagram: {
+ seed: {
+ mindMapRoot: "Main topic",
+ mindMapCapture: "Capture ideas",
+ mindMapOrganize: "Organize",
+ mindMapShare: "Share",
+ mindMapQuickNotes: "Quick notes",
+ mindMapSync: "Device sync",
+ mindMapNotebooks: "Notebooks",
+ mindMapPublicLinks: "Public links",
+ flowchartStart: "Start",
+ flowchartProcess: "Process step",
+ flowchartEnd: "End",
+ architectureSystem: "Application",
+ architectureClient: "Web client",
+ architectureApi: "API service",
+ architectureDatabase: "Database",
+ architectureStorage: "File storage",
+ architectureQuery: "Read / write",
+ architectureFiles: "Files",
+ },
normalNote: "Regular note",
mindMap: "Mind map",
flowchart: "Flowchart",
diff --git a/packages/shared/src/i18n/ja.ts b/packages/shared/src/i18n/ja.ts
index 6534f301..ebe942fd 100644
--- a/packages/shared/src/i18n/ja.ts
+++ b/packages/shared/src/i18n/ja.ts
@@ -593,6 +593,26 @@ export const ja = {
},
},
diagram: {
+ seed: {
+ mindMapRoot: "メインテーマ",
+ mindMapCapture: "アイデアを集める",
+ mindMapOrganize: "整理する",
+ mindMapShare: "共有する",
+ mindMapQuickNotes: "すばやく記録",
+ mindMapSync: "デバイス間同期",
+ mindMapNotebooks: "ノートブック",
+ mindMapPublicLinks: "公開リンク",
+ flowchartStart: "開始",
+ flowchartProcess: "処理ステップ",
+ flowchartEnd: "終了",
+ architectureSystem: "アプリケーション",
+ architectureClient: "Web アプリ",
+ architectureApi: "API サービス",
+ architectureDatabase: "データベース",
+ architectureStorage: "ストレージ",
+ architectureQuery: "読み取り / 書き込み",
+ architectureFiles: "ファイル",
+ },
normalNote: "通常ノート",
mindMap: "マインドマップ",
flowchart: "フローチャート",
diff --git a/packages/shared/src/i18n/pl.ts b/packages/shared/src/i18n/pl.ts
index dce63f7c..a3bfbd9b 100644
--- a/packages/shared/src/i18n/pl.ts
+++ b/packages/shared/src/i18n/pl.ts
@@ -593,6 +593,26 @@ export const pl = {
},
},
diagram: {
+ seed: {
+ mindMapRoot: "Temat główny",
+ mindMapCapture: "Zbieranie pomysłów",
+ mindMapOrganize: "Porządkowanie",
+ mindMapShare: "Udostępnianie",
+ mindMapQuickNotes: "Szybki zapis",
+ mindMapSync: "Wiele urządzeń",
+ mindMapNotebooks: "Notatniki",
+ mindMapPublicLinks: "Linki",
+ flowchartStart: "Początek",
+ flowchartProcess: "Krok procesu",
+ flowchartEnd: "Koniec",
+ architectureSystem: "Aplikacja",
+ architectureClient: "Klient webowy",
+ architectureApi: "Usługa API",
+ architectureDatabase: "Baza danych",
+ architectureStorage: "Magazyn",
+ architectureQuery: "Odczyt / zapis",
+ architectureFiles: "Pliki",
+ },
normalNote: "Zwykła notatka",
mindMap: "Mapa myśli",
flowchart: "Schemat blokowy",
diff --git a/packages/shared/src/i18n/zh-CN.ts b/packages/shared/src/i18n/zh-CN.ts
index bbafd6e9..087ffb77 100644
--- a/packages/shared/src/i18n/zh-CN.ts
+++ b/packages/shared/src/i18n/zh-CN.ts
@@ -604,6 +604,26 @@ export const zhCN = {
},
},
diagram: {
+ seed: {
+ mindMapRoot: "核心主题",
+ mindMapCapture: "采集想法",
+ mindMapOrganize: "整理结构",
+ mindMapShare: "输出分享",
+ mindMapQuickNotes: "快速记录",
+ mindMapSync: "跨设备同步",
+ mindMapNotebooks: "笔记本",
+ mindMapPublicLinks: "公开链接",
+ flowchartStart: "开始",
+ flowchartProcess: "处理步骤",
+ flowchartEnd: "结束",
+ architectureSystem: "应用系统",
+ architectureClient: "Web 客户端",
+ architectureApi: "API 服务",
+ architectureDatabase: "数据库",
+ architectureStorage: "对象存储",
+ architectureQuery: "查询 / 写入",
+ architectureFiles: "文件",
+ },
normalNote: "普通笔记",
mindMap: "思维导图",
flowchart: "流程图",