@yanqirenshi/d3.pad 0.1.0
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.
- package/README.md +39 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/parsers/jsParser.d.ts +2 -0
- package/dist/parsers/jsParser.d.ts.map +1 -0
- package/dist/parsers/jsParser.js +101 -0
- package/dist/renderers/D3PadRenderer.d.ts +34 -0
- package/dist/renderers/D3PadRenderer.d.ts.map +1 -0
- package/dist/renderers/D3PadRenderer.js +112 -0
- package/dist/renderers/PadNodeRenderer.d.ts +145 -0
- package/dist/renderers/PadNodeRenderer.d.ts.map +1 -0
- package/dist/renderers/PadNodeRenderer.js +440 -0
- package/dist/renderers/blocks/EndBlockGeometry.d.ts +33 -0
- package/dist/renderers/blocks/EndBlockGeometry.d.ts.map +1 -0
- package/dist/renderers/blocks/EndBlockGeometry.js +44 -0
- package/dist/renderers/blocks/EndRenderer.d.ts +10 -0
- package/dist/renderers/blocks/EndRenderer.d.ts.map +1 -0
- package/dist/renderers/blocks/EndRenderer.js +38 -0
- package/dist/renderers/blocks/EndRendererProps.d.ts +6 -0
- package/dist/renderers/blocks/EndRendererProps.d.ts.map +1 -0
- package/dist/renderers/blocks/EndRendererProps.js +1 -0
- package/dist/renderers/blocks/FunctionContainerGeometry.d.ts +29 -0
- package/dist/renderers/blocks/FunctionContainerGeometry.d.ts.map +1 -0
- package/dist/renderers/blocks/FunctionContainerGeometry.js +46 -0
- package/dist/renderers/blocks/FunctionContainerRenderer.d.ts +13 -0
- package/dist/renderers/blocks/FunctionContainerRenderer.d.ts.map +1 -0
- package/dist/renderers/blocks/FunctionContainerRenderer.js +49 -0
- package/dist/renderers/blocks/FunctionHeaderGeometry.d.ts +20 -0
- package/dist/renderers/blocks/FunctionHeaderGeometry.d.ts.map +1 -0
- package/dist/renderers/blocks/FunctionHeaderGeometry.js +35 -0
- package/dist/renderers/blocks/FunctionHeaderProps.d.ts +7 -0
- package/dist/renderers/blocks/FunctionHeaderProps.d.ts.map +1 -0
- package/dist/renderers/blocks/FunctionHeaderProps.js +1 -0
- package/dist/renderers/blocks/FunctionHeaderRenderer.d.ts +11 -0
- package/dist/renderers/blocks/FunctionHeaderRenderer.d.ts.map +1 -0
- package/dist/renderers/blocks/FunctionHeaderRenderer.js +36 -0
- package/dist/renderers/blocks/IfBlockGeometry.d.ts +42 -0
- package/dist/renderers/blocks/IfBlockGeometry.d.ts.map +1 -0
- package/dist/renderers/blocks/IfBlockGeometry.js +83 -0
- package/dist/renderers/blocks/IfRenderer.d.ts +10 -0
- package/dist/renderers/blocks/IfRenderer.d.ts.map +1 -0
- package/dist/renderers/blocks/IfRenderer.js +69 -0
- package/dist/renderers/blocks/IfRendererProps.d.ts +7 -0
- package/dist/renderers/blocks/IfRendererProps.d.ts.map +1 -0
- package/dist/renderers/blocks/IfRendererProps.js +1 -0
- package/dist/renderers/blocks/LoopRenderer.d.ts +12 -0
- package/dist/renderers/blocks/LoopRenderer.d.ts.map +1 -0
- package/dist/renderers/blocks/LoopRenderer.js +37 -0
- package/dist/renderers/blocks/LoopRendererProps.d.ts +8 -0
- package/dist/renderers/blocks/LoopRendererProps.d.ts.map +1 -0
- package/dist/renderers/blocks/LoopRendererProps.js +1 -0
- package/dist/renderers/blocks/Point.d.ts +5 -0
- package/dist/renderers/blocks/Point.d.ts.map +1 -0
- package/dist/renderers/blocks/Point.js +1 -0
- package/dist/renderers/blocks/StartBlockGeometry.d.ts +38 -0
- package/dist/renderers/blocks/StartBlockGeometry.d.ts.map +1 -0
- package/dist/renderers/blocks/StartBlockGeometry.js +56 -0
- package/dist/renderers/blocks/StartRenderer.d.ts +10 -0
- package/dist/renderers/blocks/StartRenderer.d.ts.map +1 -0
- package/dist/renderers/blocks/StartRenderer.js +39 -0
- package/dist/renderers/blocks/StartRendererProps.d.ts +6 -0
- package/dist/renderers/blocks/StartRendererProps.d.ts.map +1 -0
- package/dist/renderers/blocks/StartRendererProps.js +1 -0
- package/dist/types.d.ts +24 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/utils/layout.d.ts +14 -0
- package/dist/utils/layout.d.ts.map +1 -0
- package/dist/utils/layout.js +186 -0
- package/package.json +56 -0
- package/src/index.ts +12 -0
- package/src/parsers/jsParser.ts +122 -0
- package/src/renderers/D3PadRenderer.tsx +145 -0
- package/src/renderers/PadNodeRenderer.ts +517 -0
- package/src/renderers/blocks/EndBlockGeometry.ts +52 -0
- package/src/renderers/blocks/EndRenderer.tsx +48 -0
- package/src/renderers/blocks/EndRendererProps.ts +5 -0
- package/src/renderers/blocks/FunctionContainerGeometry.ts +54 -0
- package/src/renderers/blocks/FunctionContainerRenderer.tsx +62 -0
- package/src/renderers/blocks/FunctionHeaderGeometry.ts +43 -0
- package/src/renderers/blocks/FunctionHeaderProps.ts +6 -0
- package/src/renderers/blocks/FunctionHeaderRenderer.tsx +47 -0
- package/src/renderers/blocks/IfBlockGeometry.ts +98 -0
- package/src/renderers/blocks/IfRenderer.tsx +88 -0
- package/src/renderers/blocks/IfRendererProps.ts +7 -0
- package/src/renderers/blocks/LoopRenderer.tsx +50 -0
- package/src/renderers/blocks/LoopRendererProps.ts +7 -0
- package/src/renderers/blocks/Point.ts +5 -0
- package/src/renderers/blocks/README.md +29 -0
- package/src/renderers/blocks/StartBlockGeometry.ts +66 -0
- package/src/renderers/blocks/StartRenderer.tsx +49 -0
- package/src/renderers/blocks/StartRendererProps.ts +5 -0
- package/src/types.ts +7 -0
- package/src/utils/layout.ts +247 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as d3 from 'd3';
|
|
2
|
+
import { StartBlockGeometry } from './StartBlockGeometry';
|
|
3
|
+
/**
|
|
4
|
+
* D3.jsでStartブロック(楕円/カプセル型)を描画します
|
|
5
|
+
* @param parent 親のD3セレクション (SVGGElement)
|
|
6
|
+
* @param x X座標
|
|
7
|
+
* @param y Y座標
|
|
8
|
+
* @param label ラベルテキスト (デフォルト: "Start")
|
|
9
|
+
*/
|
|
10
|
+
export function renderStart(parent, x, y, label = "Start") {
|
|
11
|
+
const calculatedWidth = StartBlockGeometry.calculateWidth(label);
|
|
12
|
+
const geometry = new StartBlockGeometry(x, y, calculatedWidth);
|
|
13
|
+
const width = geometry.getWidth();
|
|
14
|
+
const height = geometry.getHeight();
|
|
15
|
+
const rx = height / 2;
|
|
16
|
+
const g = parent.append('g');
|
|
17
|
+
// 楕円/カプセル型の矩形
|
|
18
|
+
g.append('rect')
|
|
19
|
+
.attr('x', geometry.getX())
|
|
20
|
+
.attr('y', geometry.getY())
|
|
21
|
+
.attr('width', width)
|
|
22
|
+
.attr('height', height)
|
|
23
|
+
.attr('rx', rx)
|
|
24
|
+
.attr('ry', rx)
|
|
25
|
+
.attr('stroke', 'black')
|
|
26
|
+
.attr('fill', 'white')
|
|
27
|
+
.attr('stroke-width', 1.5);
|
|
28
|
+
// ラベルテキスト
|
|
29
|
+
g.append('text')
|
|
30
|
+
.attr('x', geometry.getX() + width / 2)
|
|
31
|
+
.attr('y', geometry.getY() + height / 2)
|
|
32
|
+
.attr('text-anchor', 'middle')
|
|
33
|
+
.attr('alignment-baseline', 'middle')
|
|
34
|
+
.attr('font-family', 'monospace')
|
|
35
|
+
.attr('font-size', 14)
|
|
36
|
+
.attr('font-weight', 'bold')
|
|
37
|
+
.text(label);
|
|
38
|
+
return g;
|
|
39
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StartRendererProps.d.ts","sourceRoot":"","sources":["../../../src/renderers/blocks/StartRendererProps.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,kBAAkB;IAC/B,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,KAAK,CAAC,EAAE,MAAM,CAAC;CAClB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type PadNode = {
|
|
2
|
+
type: 'sequence';
|
|
3
|
+
children: PadNode[];
|
|
4
|
+
} | {
|
|
5
|
+
type: 'block';
|
|
6
|
+
label: string;
|
|
7
|
+
children: PadNode[];
|
|
8
|
+
} | {
|
|
9
|
+
type: 'if';
|
|
10
|
+
condition: string;
|
|
11
|
+
then_block: PadNode;
|
|
12
|
+
else_block?: PadNode;
|
|
13
|
+
} | {
|
|
14
|
+
type: 'loop';
|
|
15
|
+
condition: string;
|
|
16
|
+
body: PadNode;
|
|
17
|
+
} | {
|
|
18
|
+
type: 'command';
|
|
19
|
+
label: string;
|
|
20
|
+
} | {
|
|
21
|
+
type: 'error';
|
|
22
|
+
message: string;
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,OAAO,GACb;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,EAAE,OAAO,EAAE,CAAA;CAAE,GACzC;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,OAAO,EAAE,CAAA;CAAE,GACrD;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,OAAO,CAAC;IAAC,UAAU,CAAC,EAAE,OAAO,CAAA;CAAE,GAC5E;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAA;CAAE,GAClD;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAClC;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PadNode } from '../types';
|
|
2
|
+
export interface LayoutNode {
|
|
3
|
+
id: string;
|
|
4
|
+
padNode: PadNode;
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
children: LayoutNode[];
|
|
10
|
+
label?: string;
|
|
11
|
+
condition?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function calculateLayout(node: PadNode): LayoutNode;
|
|
14
|
+
//# sourceMappingURL=layout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layout.d.ts","sourceRoot":"","sources":["../../src/utils/layout.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAExC,MAAM,WAAW,UAAU;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,OAAO,CAAC;IACjB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,UAAU,EAAE,CAAC;IAEvB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB;AAyBD,wBAAgB,eAAe,CAAC,IAAI,EAAE,OAAO,GAAG,UAAU,CAGzD"}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
const CONFIG = {
|
|
2
|
+
MIN_WIDTH: 100,
|
|
3
|
+
MIN_HEIGHT: 40,
|
|
4
|
+
BLOCK_PADDING: 10,
|
|
5
|
+
FONT_SIZE: 14,
|
|
6
|
+
CHAR_WIDTH: 8, // Approx
|
|
7
|
+
IF_CONDITION_WIDTH: 30, // The small wedge/box on left
|
|
8
|
+
MARGIN_Y: 20, // Vertical spacing between blocks
|
|
9
|
+
GAP_X: 20, // Horizontal gap between condition and body
|
|
10
|
+
};
|
|
11
|
+
// ID生成用のカウンター
|
|
12
|
+
let idCounter = 0;
|
|
13
|
+
function generateId(prefix) {
|
|
14
|
+
return `${prefix}-${idCounter++}`;
|
|
15
|
+
}
|
|
16
|
+
// ID カウンターをリセット(新しいレイアウト計算開始時)
|
|
17
|
+
function resetIdCounter() {
|
|
18
|
+
idCounter = 0;
|
|
19
|
+
}
|
|
20
|
+
export function calculateLayout(node) {
|
|
21
|
+
resetIdCounter();
|
|
22
|
+
return calculateLayoutInternal(node, 'root');
|
|
23
|
+
}
|
|
24
|
+
function calculateLayoutInternal(node, parentPath) {
|
|
25
|
+
switch (node.type) {
|
|
26
|
+
case 'sequence':
|
|
27
|
+
return layoutSequence(node.children, parentPath);
|
|
28
|
+
case 'block':
|
|
29
|
+
return layoutBlock(node.label, node.children, parentPath);
|
|
30
|
+
case 'command':
|
|
31
|
+
return layoutCommand(node.label, parentPath);
|
|
32
|
+
case 'if':
|
|
33
|
+
const ifChildren = [node.then_block];
|
|
34
|
+
if (node.else_block)
|
|
35
|
+
ifChildren.push(node.else_block);
|
|
36
|
+
return layoutIf(node.condition, ifChildren, parentPath);
|
|
37
|
+
case 'loop':
|
|
38
|
+
return layoutLoop(node.condition, node.body, parentPath);
|
|
39
|
+
default:
|
|
40
|
+
return {
|
|
41
|
+
id: generateId(parentPath),
|
|
42
|
+
padNode: node, x: 0, y: 0, width: CONFIG.MIN_WIDTH, height: CONFIG.MIN_HEIGHT, children: []
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
import { StartBlockGeometry } from '../renderers/blocks/StartBlockGeometry';
|
|
47
|
+
import { FunctionContainerGeometry } from '../renderers/blocks/FunctionContainerGeometry';
|
|
48
|
+
function layoutSequence(nodes, parentPath) {
|
|
49
|
+
const id = generateId(`${parentPath}-seq`);
|
|
50
|
+
const childrenLayouts = nodes.map((node, index) => calculateLayoutInternal(node, `${id}-${index}`));
|
|
51
|
+
let currentY = 0;
|
|
52
|
+
let maxWidth = CONFIG.MIN_WIDTH;
|
|
53
|
+
childrenLayouts.forEach((child, index) => {
|
|
54
|
+
child.y = currentY;
|
|
55
|
+
currentY += child.height;
|
|
56
|
+
if (index < childrenLayouts.length - 1) {
|
|
57
|
+
currentY += CONFIG.MARGIN_Y;
|
|
58
|
+
}
|
|
59
|
+
maxWidth = Math.max(maxWidth, child.width);
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
id,
|
|
63
|
+
padNode: { type: 'sequence', children: nodes },
|
|
64
|
+
x: 0,
|
|
65
|
+
y: 0,
|
|
66
|
+
width: maxWidth,
|
|
67
|
+
height: currentY,
|
|
68
|
+
children: childrenLayouts
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function layoutBlock(label, children, parentPath) {
|
|
72
|
+
const id = generateId(`${parentPath}-block`);
|
|
73
|
+
const seqLayout = layoutSequence(children, id);
|
|
74
|
+
const startNodeWidth = StartBlockGeometry.calculateWidth("START");
|
|
75
|
+
const startNodeHeight = 30;
|
|
76
|
+
const endNodeHeight = 30;
|
|
77
|
+
const centerLineX = startNodeWidth / 2;
|
|
78
|
+
const graphWidth = Math.max(seqLayout.width + centerLineX, startNodeWidth);
|
|
79
|
+
const paddingTop = startNodeHeight + CONFIG.MARGIN_Y;
|
|
80
|
+
const paddingBottom = CONFIG.MARGIN_Y;
|
|
81
|
+
const graphHeight = paddingTop + seqLayout.height + paddingBottom + endNodeHeight;
|
|
82
|
+
const containerPadding = FunctionContainerGeometry.PADDING;
|
|
83
|
+
const headerHeight = FunctionContainerGeometry.HEADER_HEIGHT;
|
|
84
|
+
const containerWidth = graphWidth + containerPadding * 2;
|
|
85
|
+
const containerHeight = headerHeight + graphHeight + containerPadding * 2;
|
|
86
|
+
seqLayout.x = containerPadding + centerLineX;
|
|
87
|
+
const contentStartY = headerHeight + containerPadding;
|
|
88
|
+
seqLayout.y = contentStartY + paddingTop;
|
|
89
|
+
return {
|
|
90
|
+
id,
|
|
91
|
+
padNode: { type: 'block', label, children },
|
|
92
|
+
x: 0,
|
|
93
|
+
y: 0,
|
|
94
|
+
width: containerWidth,
|
|
95
|
+
height: containerHeight,
|
|
96
|
+
children: [seqLayout],
|
|
97
|
+
label
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function layoutCommand(label, parentPath) {
|
|
101
|
+
const id = generateId(`${parentPath}-cmd`);
|
|
102
|
+
const canvas = document.createElement('canvas');
|
|
103
|
+
const context = canvas.getContext('2d');
|
|
104
|
+
context.font = '14px monospace';
|
|
105
|
+
const textMetrics = context.measureText(label);
|
|
106
|
+
const textWidth = textMetrics.width + 20;
|
|
107
|
+
const width = Math.max(CONFIG.MIN_WIDTH, textWidth);
|
|
108
|
+
return {
|
|
109
|
+
id,
|
|
110
|
+
padNode: { type: 'command', label },
|
|
111
|
+
x: 0,
|
|
112
|
+
y: 0,
|
|
113
|
+
width: width,
|
|
114
|
+
height: CONFIG.MIN_HEIGHT,
|
|
115
|
+
children: [],
|
|
116
|
+
label
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function layoutIf(condition, children, parentPath) {
|
|
120
|
+
const id = generateId(`${parentPath}-if`);
|
|
121
|
+
const thenLayout = calculateLayoutInternal(children[0], `${id}-then`);
|
|
122
|
+
const elseLayout = children.length > 1 ? calculateLayoutInternal(children[1], `${id}-else`) : null;
|
|
123
|
+
const canvas = document.createElement('canvas');
|
|
124
|
+
const context = canvas.getContext('2d');
|
|
125
|
+
context.font = '14px monospace';
|
|
126
|
+
const labelWidth = Math.max(context.measureText(condition).width, 40) + 60;
|
|
127
|
+
const childX = labelWidth;
|
|
128
|
+
thenLayout.y = 0;
|
|
129
|
+
thenLayout.x = childX;
|
|
130
|
+
const thenHalf = thenLayout.height / 2;
|
|
131
|
+
const topVertexY = thenHalf;
|
|
132
|
+
let height = 0;
|
|
133
|
+
const gap = 40;
|
|
134
|
+
const minWedgeHeight = 60;
|
|
135
|
+
if (elseLayout) {
|
|
136
|
+
const elseHalf = elseLayout.height / 2;
|
|
137
|
+
const minBotVertexY_for_spacing = thenLayout.height + gap + elseHalf;
|
|
138
|
+
const minBotVertexY_for_wedge = topVertexY + minWedgeHeight;
|
|
139
|
+
const botVertexY = Math.max(minBotVertexY_for_spacing, minBotVertexY_for_wedge);
|
|
140
|
+
elseLayout.y = botVertexY - elseHalf;
|
|
141
|
+
elseLayout.x = childX;
|
|
142
|
+
height = elseLayout.y + elseLayout.height;
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
height = Math.max(thenLayout.height, topVertexY + minWedgeHeight);
|
|
146
|
+
}
|
|
147
|
+
const width = childX + Math.max(thenLayout.width, elseLayout ? elseLayout.width : 0) + 50;
|
|
148
|
+
const totalHeight = height + 20;
|
|
149
|
+
return {
|
|
150
|
+
id,
|
|
151
|
+
padNode: { type: 'if', condition, then_block: children[0], else_block: children.length > 1 ? children[1] : undefined },
|
|
152
|
+
x: 0,
|
|
153
|
+
y: 0,
|
|
154
|
+
width,
|
|
155
|
+
height: totalHeight,
|
|
156
|
+
children: elseLayout ? [thenLayout, elseLayout] : [thenLayout],
|
|
157
|
+
label: condition,
|
|
158
|
+
condition
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
function layoutLoop(condition, body, parentPath) {
|
|
162
|
+
const id = generateId(`${parentPath}-loop`);
|
|
163
|
+
const bodyLayout = calculateLayoutInternal(body, `${id}-body`);
|
|
164
|
+
const canvas = document.createElement('canvas');
|
|
165
|
+
const context = canvas.getContext('2d');
|
|
166
|
+
context.font = '14px monospace';
|
|
167
|
+
const textMetrics = context.measureText(condition);
|
|
168
|
+
const doubleLineWidth = 10;
|
|
169
|
+
const textPadding = 40;
|
|
170
|
+
const conditionBoxWidth = doubleLineWidth + textMetrics.width + textPadding;
|
|
171
|
+
const boxWidth = Math.max(CONFIG.MIN_WIDTH, conditionBoxWidth);
|
|
172
|
+
bodyLayout.x = boxWidth + CONFIG.GAP_X;
|
|
173
|
+
bodyLayout.y = 0;
|
|
174
|
+
const width = boxWidth + CONFIG.GAP_X + bodyLayout.width;
|
|
175
|
+
const height = Math.max(CONFIG.MIN_HEIGHT, bodyLayout.height);
|
|
176
|
+
return {
|
|
177
|
+
id,
|
|
178
|
+
padNode: { type: 'loop', condition, body },
|
|
179
|
+
x: 0,
|
|
180
|
+
y: 0,
|
|
181
|
+
width,
|
|
182
|
+
height,
|
|
183
|
+
children: [bodyLayout],
|
|
184
|
+
condition
|
|
185
|
+
};
|
|
186
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yanqirenshi/d3.pad",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "PAD (Problem Analysis Diagram) renderer for JS/TS code using d3.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"development": "./src/index.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"src"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc",
|
|
25
|
+
"docs": "typedoc",
|
|
26
|
+
"prepare": "npm run build",
|
|
27
|
+
"prepublishOnly": "npm run build"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"acorn": "^8.15.0",
|
|
31
|
+
"d3": "^7.9.0"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
35
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/acorn": "^4.0.6",
|
|
39
|
+
"@types/d3": "^7.4.3",
|
|
40
|
+
"@types/react": "^19.2.5",
|
|
41
|
+
"@types/react-dom": "^19.2.3",
|
|
42
|
+
"react": "^19.2.0",
|
|
43
|
+
"react-dom": "^19.2.0",
|
|
44
|
+
"typedoc": "^0.28.16",
|
|
45
|
+
"typescript": "~5.9.3"
|
|
46
|
+
},
|
|
47
|
+
"author": "yanqirenshi@gmail.com",
|
|
48
|
+
"keywords": [
|
|
49
|
+
"pad",
|
|
50
|
+
"diagram",
|
|
51
|
+
"flowchart",
|
|
52
|
+
"parser",
|
|
53
|
+
"d3"
|
|
54
|
+
],
|
|
55
|
+
"license": "MIT"
|
|
56
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Parsers
|
|
2
|
+
export { parse_js_code } from './parsers/jsParser';
|
|
3
|
+
|
|
4
|
+
// Renderers
|
|
5
|
+
export { D3PadRenderer } from './renderers/D3PadRenderer';
|
|
6
|
+
export { PadNodeRenderer } from './renderers/PadNodeRenderer';
|
|
7
|
+
|
|
8
|
+
// Types
|
|
9
|
+
export type { PadNode } from './types';
|
|
10
|
+
|
|
11
|
+
// Utils
|
|
12
|
+
export { calculateLayout, type LayoutNode } from './utils/layout';
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import * as acorn from 'acorn';
|
|
2
|
+
import type { PadNode } from '../types';
|
|
3
|
+
|
|
4
|
+
export function parse_js_code(code: string): string {
|
|
5
|
+
const parser = new JsParser(code);
|
|
6
|
+
return parser.parse();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
class JsParser {
|
|
10
|
+
private code: string;
|
|
11
|
+
|
|
12
|
+
constructor(code: string) {
|
|
13
|
+
this.code = code;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
parse(): string {
|
|
17
|
+
try {
|
|
18
|
+
const ast = acorn.parse(this.code, { ecmaVersion: 2020 });
|
|
19
|
+
const nodes: PadNode[] = [];
|
|
20
|
+
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
for (const node of ast.body) {
|
|
23
|
+
if (node.type === 'FunctionDeclaration') {
|
|
24
|
+
nodes.push(this.parseFunction(node));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (nodes.length === 0) {
|
|
29
|
+
return JSON.stringify({ type: 'error', message: 'No function found' });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return JSON.stringify({ type: 'sequence', children: nodes });
|
|
33
|
+
} catch (e) {
|
|
34
|
+
return JSON.stringify({ type: 'error', message: `Parse error: ${e}` });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private parseFunction(node: any): PadNode {
|
|
39
|
+
const name = node.id?.name || 'anonymous';
|
|
40
|
+
const body = this.parseBlock(node.body);
|
|
41
|
+
return {
|
|
42
|
+
type: 'block',
|
|
43
|
+
label: `fn ${name}()`, // Use 'fn' label to match Rust style or just 'function'
|
|
44
|
+
children: [body]
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private parseBlock(node: any): PadNode {
|
|
49
|
+
const children: PadNode[] = [];
|
|
50
|
+
if (node.body) {
|
|
51
|
+
for (const stmt of node.body) {
|
|
52
|
+
children.push(this.parseStmt(stmt));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return { type: 'sequence', children };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private parseStmt(stmt: any): PadNode {
|
|
59
|
+
switch (stmt.type) {
|
|
60
|
+
case 'VariableDeclaration':
|
|
61
|
+
return { type: 'command', label: this.snippet(stmt) };
|
|
62
|
+
|
|
63
|
+
case 'ExpressionStatement':
|
|
64
|
+
// Remove semicolon if present for cleaner display?
|
|
65
|
+
// Currently just taking snippet.
|
|
66
|
+
return { type: 'command', label: this.snippet(stmt) };
|
|
67
|
+
|
|
68
|
+
case 'IfStatement':
|
|
69
|
+
return this.parseIf(stmt);
|
|
70
|
+
|
|
71
|
+
case 'WhileStatement':
|
|
72
|
+
return {
|
|
73
|
+
type: 'loop',
|
|
74
|
+
condition: this.snippet(stmt.test),
|
|
75
|
+
body: this.parseStmt(stmt.body)
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
case 'ForStatement':
|
|
79
|
+
let condLabel = 'for (;;) ';
|
|
80
|
+
if (stmt.init || stmt.test || stmt.update) {
|
|
81
|
+
const init = stmt.init ? this.snippet(stmt.init) : '';
|
|
82
|
+
const test = stmt.test ? this.snippet(stmt.test) : '';
|
|
83
|
+
const update = stmt.update ? this.snippet(stmt.update) : '';
|
|
84
|
+
// Clean up init if it has semicolon?
|
|
85
|
+
// this.snippet(stmt.init) includes 'let i = 0', no semi usually if it's var decl node inside for
|
|
86
|
+
condLabel = `for (${init}; ${test}; ${update})`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
type: 'loop',
|
|
91
|
+
condition: condLabel,
|
|
92
|
+
body: this.parseStmt(stmt.body)
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
case 'BlockStatement':
|
|
96
|
+
return this.parseBlock(stmt);
|
|
97
|
+
|
|
98
|
+
case 'ReturnStatement':
|
|
99
|
+
return { type: 'command', label: this.snippet(stmt) };
|
|
100
|
+
|
|
101
|
+
default:
|
|
102
|
+
return { type: 'command', label: this.snippet(stmt) };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private parseIf(stmt: any): PadNode {
|
|
107
|
+
const condition = this.snippet(stmt.test);
|
|
108
|
+
const thenBlock = this.parseStmt(stmt.consequent);
|
|
109
|
+
const elseBlock = stmt.alternate ? this.parseStmt(stmt.alternate) : undefined;
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
type: 'if',
|
|
113
|
+
condition,
|
|
114
|
+
then_block: thenBlock,
|
|
115
|
+
else_block: elseBlock
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private snippet(node: any): string {
|
|
120
|
+
return this.code.substring(node.start, node.end);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* PAD図をD3.jsで描画するReactコンポーネント
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { useRef, useEffect, useMemo } from 'react';
|
|
7
|
+
import * as d3 from 'd3';
|
|
8
|
+
import type { PadNode } from '../types';
|
|
9
|
+
import { calculateLayout } from '../utils/layout';
|
|
10
|
+
import { PadNodeRenderer } from './PadNodeRenderer';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* D3PadRendererコンポーネントのプロパティ
|
|
14
|
+
*/
|
|
15
|
+
export interface D3PadRendererProps {
|
|
16
|
+
/**
|
|
17
|
+
* 描画するPADノードのルート
|
|
18
|
+
*/
|
|
19
|
+
node: PadNode;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* PAD(Problem Analysis Diagram)をD3.jsで描画するReactコンポーネント
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* このコンポーネントは以下の機能を提供します:
|
|
27
|
+
* - PadNodeからLayoutNodeへのレイアウト計算
|
|
28
|
+
* - D3.jsによるSVG描画
|
|
29
|
+
* - データ結合パターン(enter/update/exit)による効率的な再描画
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```tsx
|
|
33
|
+
* const padNode: PadNode = { type: 'block', label: 'main', children: [] };
|
|
34
|
+
* <D3PadRenderer node={padNode} />
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @param props - コンポーネントのプロパティ
|
|
38
|
+
* @returns PAD図を描画するSVG要素を含むdiv
|
|
39
|
+
*/
|
|
40
|
+
export const D3PadRenderer: React.FC<D3PadRendererProps> = ({ node }) => {
|
|
41
|
+
/** SVG要素への参照 */
|
|
42
|
+
const svgRef = useRef<SVGSVGElement>(null);
|
|
43
|
+
|
|
44
|
+
/** PadNodeRendererインスタンスへの参照 */
|
|
45
|
+
const rendererRef = useRef<PadNodeRenderer | null>(null);
|
|
46
|
+
|
|
47
|
+
/** ズーム動作への参照 */
|
|
48
|
+
const zoomRef = useRef<d3.ZoomBehavior<SVGSVGElement, unknown> | null>(null);
|
|
49
|
+
|
|
50
|
+
/** ルートグループへの参照 */
|
|
51
|
+
const rootGRef = useRef<d3.Selection<SVGGElement, unknown, null, undefined> | null>(null);
|
|
52
|
+
|
|
53
|
+
/** 計算されたレイアウト(nodeが変更された時のみ再計算) */
|
|
54
|
+
const layout = useMemo(() => calculateLayout(node), [node]);
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* URLクエリパラメータから初期のtransformを取得
|
|
58
|
+
*/
|
|
59
|
+
const getInitialTransform = () => {
|
|
60
|
+
const params = new URLSearchParams(window.location.search);
|
|
61
|
+
const x = parseFloat(params.get('x') ?? '10');
|
|
62
|
+
const y = parseFloat(params.get('y') ?? '10');
|
|
63
|
+
const zoom = parseFloat(params.get('zoom') ?? '1');
|
|
64
|
+
return d3.zoomIdentity.translate(x, y).scale(zoom);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* URLクエリパラメータを更新
|
|
69
|
+
*/
|
|
70
|
+
const updateUrlParams = (transform: d3.ZoomTransform) => {
|
|
71
|
+
const params = new URLSearchParams(window.location.search);
|
|
72
|
+
params.set('x', transform.x.toFixed(2));
|
|
73
|
+
params.set('y', transform.y.toFixed(2));
|
|
74
|
+
params.set('zoom', transform.k.toFixed(2));
|
|
75
|
+
const newUrl = `${window.location.pathname}?${params.toString()}`;
|
|
76
|
+
window.history.replaceState({}, '', newUrl);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
if (!svgRef.current) return;
|
|
81
|
+
|
|
82
|
+
const svg = d3.select(svgRef.current);
|
|
83
|
+
|
|
84
|
+
// SVGサイズを更新(ウィンドウ全体)
|
|
85
|
+
svg.attr('width', '100%')
|
|
86
|
+
.attr('height', '100%');
|
|
87
|
+
|
|
88
|
+
// ズーム動作を初期化(最初の1回のみ)
|
|
89
|
+
if (!zoomRef.current) {
|
|
90
|
+
// ルートグループを作成
|
|
91
|
+
rootGRef.current = svg.append('g').attr('transform', 'translate(10,10)');
|
|
92
|
+
|
|
93
|
+
// レンダラーを初期化
|
|
94
|
+
rendererRef.current = new PadNodeRenderer(rootGRef.current);
|
|
95
|
+
|
|
96
|
+
// ドラッグ中はズームを無効化するコールバックを設定
|
|
97
|
+
rendererRef.current.onDragStart = () => {
|
|
98
|
+
// ズームイベントを一時的に無効化
|
|
99
|
+
svg.on('.zoom', null);
|
|
100
|
+
};
|
|
101
|
+
rendererRef.current.onDragEnd = () => {
|
|
102
|
+
// ズームイベントを再有効化
|
|
103
|
+
if (zoomRef.current) {
|
|
104
|
+
svg.call(zoomRef.current);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// ズーム動作を設定(パンとズーム)
|
|
109
|
+
zoomRef.current = d3.zoom<SVGSVGElement, unknown>()
|
|
110
|
+
.scaleExtent([0.1, 4]) // 0.1倍〜4倍までズーム可能
|
|
111
|
+
.on('zoom', (event) => {
|
|
112
|
+
if (rootGRef.current) {
|
|
113
|
+
rootGRef.current.attr('transform', event.transform.toString());
|
|
114
|
+
// URLパラメータを更新
|
|
115
|
+
updateUrlParams(event.transform);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// SVGにズーム動作を適用
|
|
120
|
+
svg.call(zoomRef.current);
|
|
121
|
+
|
|
122
|
+
// URLパラメータから初期位置を設定
|
|
123
|
+
const initialTransform = getInitialTransform();
|
|
124
|
+
svg.call(zoomRef.current.transform, initialTransform);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// データ結合パターンでレンダリング
|
|
128
|
+
rendererRef.current?.render([layout]);
|
|
129
|
+
|
|
130
|
+
}, [layout]);
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
<div style={{
|
|
134
|
+
position: 'fixed',
|
|
135
|
+
top: 0,
|
|
136
|
+
left: 0,
|
|
137
|
+
width: '100vw',
|
|
138
|
+
height: '100vh',
|
|
139
|
+
overflow: 'auto',
|
|
140
|
+
zIndex: 0
|
|
141
|
+
}}>
|
|
142
|
+
<svg ref={svgRef} style={{ minWidth: '100%', minHeight: '100%' }}></svg>
|
|
143
|
+
</div>
|
|
144
|
+
);
|
|
145
|
+
};
|