silkio-flowchart 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +37 -2
  3. package/dist/FlowEdge.d.ts +45 -0
  4. package/dist/FlowEdge.js +390 -0
  5. package/dist/Shape.d.ts +48 -0
  6. package/dist/Shape.js +118 -0
  7. package/dist/ai-protocol.d.ts +146 -0
  8. package/dist/ai-protocol.js +579 -0
  9. package/dist/ai-report.d.ts +3 -0
  10. package/dist/ai-report.js +83 -0
  11. package/dist/ai.d.ts +6 -0
  12. package/dist/ai.js +7 -0
  13. package/dist/auto-layout.d.ts +15 -0
  14. package/dist/auto-layout.js +248 -0
  15. package/dist/commands.d.ts +106 -0
  16. package/dist/commands.js +264 -0
  17. package/dist/data.d.ts +24 -0
  18. package/dist/data.js +1 -0
  19. package/dist/edge-router.d.ts +24 -0
  20. package/dist/edge-router.js +22 -0
  21. package/dist/editing.d.ts +32 -0
  22. package/dist/editing.js +148 -0
  23. package/dist/flow-layout-capability.d.ts +7 -0
  24. package/dist/flow-layout-capability.js +2 -0
  25. package/dist/flowchart.d.ts +118 -0
  26. package/dist/flowchart.js +1446 -0
  27. package/dist/geometry.d.ts +98 -0
  28. package/dist/geometry.js +310 -0
  29. package/dist/history-codec.d.ts +47 -0
  30. package/dist/history-codec.js +410 -0
  31. package/dist/hover.d.ts +22 -0
  32. package/dist/hover.js +96 -0
  33. package/dist/index.d.ts +22 -0
  34. package/dist/index.js +18 -0
  35. package/dist/interactions.d.ts +51 -0
  36. package/dist/interactions.js +636 -0
  37. package/dist/model.d.ts +58 -0
  38. package/dist/model.js +233 -0
  39. package/dist/overlay.d.ts +60 -0
  40. package/dist/overlay.js +242 -0
  41. package/dist/quick-create.d.ts +12 -0
  42. package/dist/quick-create.js +29 -0
  43. package/dist/selection.d.ts +8 -0
  44. package/dist/selection.js +31 -0
  45. package/dist/session.d.ts +68 -0
  46. package/dist/session.js +4 -0
  47. package/dist/shape-registry.d.ts +17 -0
  48. package/dist/shape-registry.js +36 -0
  49. package/dist/shapes.d.ts +56 -0
  50. package/dist/shapes.js +291 -0
  51. package/dist/snap-capability.d.ts +23 -0
  52. package/dist/snap-capability.js +32 -0
  53. package/dist/snap.d.ts +34 -0
  54. package/dist/snap.js +370 -0
  55. package/package.json +36 -3
@@ -0,0 +1,24 @@
1
+ import type { App, Plugin } from "@silkor/core";
2
+ import type { EdgePoint } from "@silkor/core";
3
+ import type { PortDir } from "./FlowEdge";
4
+ export interface EdgeRouteInput {
5
+ from: EdgePoint;
6
+ fromDir: PortDir;
7
+ to: EdgePoint;
8
+ toDir: PortDir;
9
+ fromStub: number;
10
+ toStub: number;
11
+ }
12
+ export interface EdgeRenderStyle {
13
+ color: string;
14
+ lineWidth: number;
15
+ cornerRadius?: number;
16
+ }
17
+ export interface EdgeRouterCapability {
18
+ route(input: EdgeRouteInput): EdgePoint[];
19
+ render(ctx: CanvasRenderingContext2D, points: EdgePoint[], style: EdgeRenderStyle): void;
20
+ }
21
+ export declare const EDGE_ROUTER_CAPABILITY: import("@silkor/core").CapabilityKey<EdgeRouterCapability>;
22
+ export declare class BuiltinEdgeRouter implements Plugin {
23
+ install(app: App): void;
24
+ }
@@ -0,0 +1,22 @@
1
+ import { capabilityKey } from "@silkor/core";
2
+ import { FlowEdge } from "./FlowEdge";
3
+ import { drawEdgeArrow, renderOrthoPolyline, trimEdgeEnd } from "./FlowEdge";
4
+ // 能力键:消费方只认能力名与契约,不认提供方插件
5
+ export const EDGE_ROUTER_CAPABILITY = capabilityKey("flowchart.edge-router");
6
+ // 官方边路由插件:正交折线路由 + 圆角拐点 + 实心箭头(算法在 FlowEdge.routeBetween
7
+ // 静态方法与 FlowEdge.ts 渲染辅助中),install 时自注册 flowchart.edge-router 能力
8
+ export class BuiltinEdgeRouter {
9
+ install(app) {
10
+ const impl = {
11
+ route: (input) => FlowEdge.routeBetween(input.from, input.fromDir, input.to, input.toDir, input.fromStub, input.toStub),
12
+ render: (ctx, points, style) => {
13
+ // 线体末端回缩一个箭头长度再绘制,避免线头从三角箭头下方露出
14
+ renderOrthoPolyline(ctx, trimEdgeEnd(points), style.color, style.lineWidth, style.cornerRadius ?? 8);
15
+ drawEdgeArrow(ctx, points, style.color);
16
+ },
17
+ };
18
+ app.provide(EDGE_ROUTER_CAPABILITY, impl, {
19
+ provider: "flowchart.builtin-edge-router",
20
+ });
21
+ }
22
+ }
@@ -0,0 +1,32 @@
1
+ import { App } from "@silkor/core";
2
+ import type { HistoryCapability } from "@silkor/core";
3
+ import type { TextEditorCapability } from "@silkor/core";
4
+ import { Shape } from "./Shape";
5
+ import { FlowEdge } from "./FlowEdge";
6
+ import type { FlowChangeType } from "./data";
7
+ export interface FlowEditingDeps {
8
+ app: App;
9
+ editor: TextEditorCapability;
10
+ history: HistoryCapability;
11
+ onChange?: (type: FlowChangeType) => void;
12
+ onDelete?: () => void;
13
+ }
14
+ export type EditTarget = Shape | FlowEdge;
15
+ export declare class FlowEditing {
16
+ private deps;
17
+ editing: EditTarget | null;
18
+ private activated;
19
+ private editingOld;
20
+ constructor(deps: FlowEditingDeps);
21
+ startEdit(target: EditTarget, point?: {
22
+ x: number;
23
+ y: number;
24
+ }): void;
25
+ finishEdit(): void;
26
+ cancelFor(target: EditTarget): void;
27
+ activate(target: EditTarget): void;
28
+ deactivate(): void;
29
+ private bindWakeKeys;
30
+ private findTarget;
31
+ private scrollTargetIntoView;
32
+ }
@@ -0,0 +1,148 @@
1
+ import { App } from "@silkor/core";
2
+ import { textCommand, edgeTextCommand } from "./commands";
3
+ import { Shape } from "./Shape";
4
+ import { FlowEdge } from "./FlowEdge";
5
+ // 文本编辑会话:编辑态(正在编辑的形状/连线)与会话起点快照的唯一属主。
6
+ // 形状正文与连线标签共用「激活态」(同 mindmap 打字即编辑):选中即挂载空缓冲
7
+ // 覆盖层(隐藏、已聚焦、常驻),打字/IME 直接落入覆盖层并唤醒为编辑态——唤醒只
8
+ // 切换显隐、不移动焦点,首字符与组合窗口均正常进入
9
+ export class FlowEditing {
10
+ deps;
11
+ // 正在编辑的形状/连线(无编辑会话时为 null)
12
+ editing = null;
13
+ // 激活目标(形状正文或连线标签:空缓冲覆盖层常驻挂载、隐藏已聚焦;
14
+ // 多选/无选中时为 null)
15
+ activated = null;
16
+ // 编辑会话起点快照(目标引用与原文字符串,非整图快照):startEdit/activate
17
+ // 记录,finishEdit 对比决定是否把文本变更记入历史
18
+ editingOld = {
19
+ target: null,
20
+ text: "",
21
+ };
22
+ constructor(deps) {
23
+ this.deps = deps;
24
+ // 覆盖层挂载即绑定唤醒键(el 每次挂载重建,无监听堆积)。
25
+ // 回调载荷为能力契约的 EditableText,本场景以具体 Text 节点工作,在接线处收窄
26
+ deps.editor.onEditStart = (el, text) => this.bindWakeKeys(el, text);
27
+ }
28
+ // 进入编辑:point 为按下时屏幕坐标,编辑器经浏览器命中测试把光标折叠到点击处
29
+ startEdit(target, point) {
30
+ // 编辑会话起点:定格目标与原文,编辑结束对比决定是否把文本变更记入历史
31
+ // (覆盖层编辑是持续同步输入,无法在单次 input 边界判定"一次变更")
32
+ this.editingOld = { target, text: target.text.text };
33
+ // 先 start:其内部 stop() 会触发旧会话的 onEditEnd → finishEdit,
34
+ // 此时 this.editing 仍是旧目标(或 null),不会被新会话误清
35
+ this.deps.editor.start(target.text);
36
+ this.editing = target;
37
+ // 点击路径(有点击点):填入原文并把光标折叠到点击处;打字唤醒路径(无点击
38
+ // 点):空缓冲覆盖层——按键的默认插入动作落在覆盖层,经 input 整体替换文本
39
+ this.deps.editor.show(point ? { fill: true, point } : {});
40
+ }
41
+ finishEdit() {
42
+ const target = this.editing;
43
+ if (!target)
44
+ return;
45
+ this.editing = null;
46
+ target.layout();
47
+ // 文本相对会话起点确有变化才记历史:record 仅入栈不执行,因变更已发生
48
+ const old = this.editingOld;
49
+ if (old.target === target && target.text.text !== old.text) {
50
+ this.deps.history.record(target instanceof Shape
51
+ ? textCommand(target, old.text, target.text.text)
52
+ : edgeTextCommand(target, old.text, target.text.text));
53
+ }
54
+ // 快照刷新为编辑后文本:激活态覆盖层仍在位,后续打字唤醒的新会话
55
+ // 以本次编辑结果为基线对比,避免把上次已入历史的变更重复记账
56
+ this.editingOld = { target, text: target.text.text };
57
+ this.deps.app.requestRender();
58
+ this.deps.onChange?.("text");
59
+ }
60
+ // 结束指定目标的编辑态(目标被删除/顶替时调用,防止悬空编辑会话)
61
+ cancelFor(target) {
62
+ if (this.editing === target) {
63
+ this.editing = null;
64
+ this.deps.editor.stop();
65
+ }
66
+ }
67
+ // 激活目标:挂载空缓冲覆盖层(隐藏态、已聚焦)常驻接收打字/IME,
68
+ // 同 mindmap 激活态;编辑态由唤醒键进入。重复激活同一目标跳过。
69
+ // 覆盖层必须在 IME 组合开始前就位(选中即挂载),组合窗口内再挂载/聚焦
70
+ // 会丢失首个组合字符
71
+ activate(target) {
72
+ if (this.activated === target)
73
+ return;
74
+ // 先 start:其内部 stop() 会触发旧会话的 onEditEnd → finishEdit,
75
+ // 此时 editingOld 仍是旧会话快照(新快照在其后定格,不被误清)
76
+ this.deps.editor.start(target.text);
77
+ this.activated = target;
78
+ this.editingOld = { target, text: target.text.text };
79
+ // 目标越出屏幕时平移视口(原 text-editor 插件行为上移至场景):先对齐
80
+ // 覆盖层再平移,相机变化经插件视口订阅自动重新对齐
81
+ this.scrollTargetIntoView(target);
82
+ }
83
+ // 取消激活:移除覆盖层;编辑会话若在该目标上进行,经 stop 的 onEditEnd
84
+ // 正确收尾(finishEdit 对比记历史)
85
+ deactivate() {
86
+ if (!this.activated)
87
+ return;
88
+ this.activated = null;
89
+ this.deps.editor.stop();
90
+ }
91
+ // 唤醒键绑定:激活态(无编辑会话)按 Delete/Backspace 删除选中;可打印
92
+ // 字符/IME 组合唤醒编辑态(空缓冲,键入经 input 整体替换文本);编辑态
93
+ // 按键全部交给覆盖层默认行为(方向键移光标、回车换行、删除键删字)。
94
+ // 形状与连线标签同一套唤醒语义
95
+ bindWakeKeys(el, text) {
96
+ const target = this.findTarget(text);
97
+ if (!target)
98
+ return;
99
+ const wake = () => {
100
+ if (this.editing)
101
+ return;
102
+ this.editing = target;
103
+ // 空缓冲显示覆盖层:键入内容经 input 同步整体替换文本。挂载后节点/
104
+ // 连线可能已被移动过(覆盖层按挂载时位置对齐),唤醒显形前重新对齐
105
+ this.deps.editor.show();
106
+ this.deps.editor.reposition();
107
+ };
108
+ el.addEventListener("keydown", (e) => {
109
+ if (this.editing)
110
+ return;
111
+ if (e.key === "Delete" || e.key === "Backspace") {
112
+ e.preventDefault();
113
+ this.deps.onDelete?.();
114
+ return;
115
+ }
116
+ if (e.ctrlKey || e.metaKey || e.altKey)
117
+ return;
118
+ if (e.key.length === 1 || e.key === "Process" || e.keyCode === 229)
119
+ wake();
120
+ });
121
+ // 组合开始与输入事件兜底唤醒(粘贴路径:paste 监听先行显示,此处补编辑态)
122
+ el.addEventListener("compositionstart", () => wake());
123
+ el.addEventListener("input", () => wake());
124
+ }
125
+ // 按文本节点反查编辑目标:顶层子节点中匹配形状正文或连线标签
126
+ findTarget(text) {
127
+ for (const child of this.deps.app.children) {
128
+ if (child instanceof Shape && child.text === text)
129
+ return child;
130
+ if (child instanceof FlowEdge && child.label === text)
131
+ return child;
132
+ }
133
+ return null;
134
+ }
135
+ // 目标越出屏幕时平移视口:几何与边界判断收口于相机 scrollIntoView,
136
+ // 此处只提供目标文本的世界包围盒(形状/连线标签均为顶层节点,世界即本地坐标)
137
+ scrollTargetIntoView(target) {
138
+ const t = target.text;
139
+ const p = t.getWorldPoint();
140
+ if (this.deps.app.camera.scrollIntoView({
141
+ minX: p.x,
142
+ minY: p.y,
143
+ maxX: p.x + t.width,
144
+ maxY: p.y + t.height,
145
+ }))
146
+ this.deps.app.requestRender();
147
+ }
148
+ }
@@ -0,0 +1,7 @@
1
+ import type { AIFlowGraph } from "./ai-protocol";
2
+ import type { FlowData } from "./data";
3
+ import type { FlowLayoutOptions } from "./auto-layout";
4
+ export interface FlowAutoLayoutCapability {
5
+ layout(graph: AIFlowGraph, options: FlowLayoutOptions): FlowData;
6
+ }
7
+ export declare const FLOW_AUTO_LAYOUT_CAPABILITY: import("@silkor/core").CapabilityKey<FlowAutoLayoutCapability>;
@@ -0,0 +1,2 @@
1
+ import { capabilityKey } from "@silkor/core";
2
+ export const FLOW_AUTO_LAYOUT_CAPABILITY = capabilityKey("flowchart.auto-layout");
@@ -0,0 +1,118 @@
1
+ import { App } from "@silkor/core";
2
+ import type { ExportOptions, HistorySnapshot, Plugin } from "@silkor/core";
3
+ import type { ClipboardEntry } from "@silkor/core";
4
+ import type { PortDir } from "./FlowEdge";
5
+ import type { FlowChangeType, FlowData, ShapeKind } from "./data";
6
+ import type { AIFlowGenerate, AIFlowGraph, AIFlowRunOptions, AIFlowRunResult, AIFlowSelection } from "./ai-protocol";
7
+ import type { FlowLayoutOptions } from "./auto-layout";
8
+ export interface FlowChartOptions {
9
+ canvas: HTMLCanvasElement;
10
+ data: FlowData;
11
+ app?: App;
12
+ onChange?: (type: FlowChangeType) => void;
13
+ plugins?: Plugin[];
14
+ ai?: AIFlowGenerate;
15
+ }
16
+ export declare class FlowChart {
17
+ private options;
18
+ private app;
19
+ private hostPlugins;
20
+ private ownsApp;
21
+ private editor;
22
+ private model;
23
+ private sel;
24
+ private editing;
25
+ private hover;
26
+ private interactions;
27
+ private overlay;
28
+ private scenePlugins;
29
+ private onChange?;
30
+ private history;
31
+ private registry;
32
+ private snapper;
33
+ private router;
34
+ private cleanups;
35
+ private disposed;
36
+ private clipboard;
37
+ private pasteCount;
38
+ private selectionListeners;
39
+ constructor(options: FlowChartOptions);
40
+ private setupEditor;
41
+ private setupSelection;
42
+ addNode(input: {
43
+ shape: ShapeKind;
44
+ text?: string;
45
+ x?: number;
46
+ y?: number;
47
+ width?: number;
48
+ height?: number;
49
+ }): string;
50
+ private resolveShape;
51
+ createNodeFrom(ref: string, dir: PortDir, kind?: ShapeKind, text?: string): string | null;
52
+ updateEdge(ref: string | {
53
+ from: string;
54
+ to: string;
55
+ text?: string;
56
+ }, patch: {
57
+ from?: string;
58
+ fromDir?: PortDir;
59
+ to?: string;
60
+ toDir?: PortDir;
61
+ text?: string;
62
+ }): boolean;
63
+ selectAll(): void;
64
+ copySelected(): void;
65
+ private serializeSelection;
66
+ paste(): void;
67
+ pasteFromEntry(entry: ClipboardEntry): void;
68
+ pasteText(text: string): void;
69
+ deleteSelected(): void;
70
+ private execRemoveShape;
71
+ private execRemoveEdge;
72
+ removeNode(ref: string): boolean;
73
+ removeEdge(ref: string | {
74
+ from: string;
75
+ to: string;
76
+ text?: string;
77
+ }): boolean;
78
+ replaceSelected(kind: ShapeKind): void;
79
+ replaceShape(ref: string, kind: ShapeKind): boolean;
80
+ setNodeText(ref: string, text: string): boolean;
81
+ getSelection(): AIFlowSelection;
82
+ historyReport(): string;
83
+ private aiHistoryState;
84
+ private aiBatch;
85
+ private execCmd;
86
+ private recordCmd;
87
+ private aiHistory;
88
+ generate(prompt: string, options?: AIFlowRunOptions): Promise<AIFlowRunResult>;
89
+ renderAIGraph(graph: AIFlowGraph, options?: FlowLayoutOptions, viewport?: {
90
+ fit?: boolean;
91
+ }): FlowData;
92
+ setData(data: FlowData): void;
93
+ toJSON(): FlowData;
94
+ exportHistory(): HistorySnapshot | null;
95
+ importHistory(data: HistorySnapshot): boolean;
96
+ screenToWorld(clientX: number, clientY: number): {
97
+ x: number;
98
+ y: number;
99
+ };
100
+ get zoom(): number;
101
+ onViewportChange(listener: (scale: number) => void): () => void;
102
+ setZoom(scale: number): void;
103
+ clearHistory(): void;
104
+ fit(): void;
105
+ center(): void;
106
+ toImage(options?: ExportOptions): Promise<Blob>;
107
+ undo(): boolean;
108
+ redo(): boolean;
109
+ get canUndo(): boolean;
110
+ get canRedo(): boolean;
111
+ get hasShapeSelection(): boolean;
112
+ onSelectionChange(cb: (hasShapeSelection: boolean) => void): () => void;
113
+ destroy(): void;
114
+ private get selShape();
115
+ private get selEdge();
116
+ private get selShapes();
117
+ private get multiBounds();
118
+ }