@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,247 @@
|
|
|
1
|
+
import type { PadNode } from '../types';
|
|
2
|
+
|
|
3
|
+
export interface LayoutNode {
|
|
4
|
+
id: string; // 一意のID(D3データ結合用)
|
|
5
|
+
padNode: PadNode;
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
children: LayoutNode[];
|
|
11
|
+
// Extra data for drawing
|
|
12
|
+
label?: string;
|
|
13
|
+
condition?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const CONFIG = {
|
|
17
|
+
MIN_WIDTH: 100,
|
|
18
|
+
MIN_HEIGHT: 40,
|
|
19
|
+
BLOCK_PADDING: 10,
|
|
20
|
+
FONT_SIZE: 14,
|
|
21
|
+
CHAR_WIDTH: 8, // Approx
|
|
22
|
+
IF_CONDITION_WIDTH: 30, // The small wedge/box on left
|
|
23
|
+
MARGIN_Y: 20, // Vertical spacing between blocks
|
|
24
|
+
GAP_X: 20, // Horizontal gap between condition and body
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// ID生成用のカウンター
|
|
28
|
+
let idCounter = 0;
|
|
29
|
+
|
|
30
|
+
function generateId(prefix: string): string {
|
|
31
|
+
return `${prefix}-${idCounter++}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// ID カウンターをリセット(新しいレイアウト計算開始時)
|
|
35
|
+
function resetIdCounter(): void {
|
|
36
|
+
idCounter = 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function calculateLayout(node: PadNode): LayoutNode {
|
|
40
|
+
resetIdCounter();
|
|
41
|
+
return calculateLayoutInternal(node, 'root');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function calculateLayoutInternal(node: PadNode, parentPath: string): LayoutNode {
|
|
45
|
+
switch (node.type) {
|
|
46
|
+
case 'sequence':
|
|
47
|
+
return layoutSequence(node.children, parentPath);
|
|
48
|
+
case 'block':
|
|
49
|
+
return layoutBlock(node.label, node.children, parentPath);
|
|
50
|
+
case 'command':
|
|
51
|
+
return layoutCommand(node.label, parentPath);
|
|
52
|
+
case 'if':
|
|
53
|
+
const ifChildren = [node.then_block];
|
|
54
|
+
if (node.else_block) ifChildren.push(node.else_block);
|
|
55
|
+
return layoutIf(node.condition, ifChildren, parentPath);
|
|
56
|
+
case 'loop':
|
|
57
|
+
return layoutLoop(node.condition, node.body, parentPath);
|
|
58
|
+
default:
|
|
59
|
+
return {
|
|
60
|
+
id: generateId(parentPath),
|
|
61
|
+
padNode: node, x: 0, y: 0, width: CONFIG.MIN_WIDTH, height: CONFIG.MIN_HEIGHT, children: []
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
import { StartBlockGeometry } from '../renderers/blocks/StartBlockGeometry';
|
|
67
|
+
import { FunctionContainerGeometry } from '../renderers/blocks/FunctionContainerGeometry';
|
|
68
|
+
|
|
69
|
+
function layoutSequence(nodes: PadNode[], parentPath: string): LayoutNode {
|
|
70
|
+
const id = generateId(`${parentPath}-seq`);
|
|
71
|
+
const childrenLayouts = nodes.map((node, index) =>
|
|
72
|
+
calculateLayoutInternal(node, `${id}-${index}`)
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
let currentY = 0;
|
|
76
|
+
let maxWidth = CONFIG.MIN_WIDTH;
|
|
77
|
+
|
|
78
|
+
childrenLayouts.forEach((child, index) => {
|
|
79
|
+
child.y = currentY;
|
|
80
|
+
currentY += child.height;
|
|
81
|
+
|
|
82
|
+
if (index < childrenLayouts.length - 1) {
|
|
83
|
+
currentY += CONFIG.MARGIN_Y;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
maxWidth = Math.max(maxWidth, child.width);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
id,
|
|
91
|
+
padNode: { type: 'sequence', children: nodes },
|
|
92
|
+
x: 0,
|
|
93
|
+
y: 0,
|
|
94
|
+
width: maxWidth,
|
|
95
|
+
height: currentY,
|
|
96
|
+
children: childrenLayouts
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function layoutBlock(label: string, children: PadNode[], parentPath: string): LayoutNode {
|
|
101
|
+
const id = generateId(`${parentPath}-block`);
|
|
102
|
+
const seqLayout = layoutSequence(children, id);
|
|
103
|
+
|
|
104
|
+
const startNodeWidth = StartBlockGeometry.calculateWidth("START");
|
|
105
|
+
const startNodeHeight = 30;
|
|
106
|
+
const endNodeHeight = 30;
|
|
107
|
+
|
|
108
|
+
const centerLineX = startNodeWidth / 2;
|
|
109
|
+
const graphWidth = Math.max(seqLayout.width + centerLineX, startNodeWidth);
|
|
110
|
+
|
|
111
|
+
const paddingTop = startNodeHeight + CONFIG.MARGIN_Y;
|
|
112
|
+
const paddingBottom = CONFIG.MARGIN_Y;
|
|
113
|
+
const graphHeight = paddingTop + seqLayout.height + paddingBottom + endNodeHeight;
|
|
114
|
+
|
|
115
|
+
const containerPadding = FunctionContainerGeometry.PADDING;
|
|
116
|
+
const headerHeight = FunctionContainerGeometry.HEADER_HEIGHT;
|
|
117
|
+
|
|
118
|
+
const containerWidth = graphWidth + containerPadding * 2;
|
|
119
|
+
const containerHeight = headerHeight + graphHeight + containerPadding * 2;
|
|
120
|
+
|
|
121
|
+
seqLayout.x = containerPadding + centerLineX;
|
|
122
|
+
|
|
123
|
+
const contentStartY = headerHeight + containerPadding;
|
|
124
|
+
seqLayout.y = contentStartY + paddingTop;
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
id,
|
|
128
|
+
padNode: { type: 'block', label, children },
|
|
129
|
+
x: 0,
|
|
130
|
+
y: 0,
|
|
131
|
+
width: containerWidth,
|
|
132
|
+
height: containerHeight,
|
|
133
|
+
children: [seqLayout],
|
|
134
|
+
label
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function layoutCommand(label: string, parentPath: string): LayoutNode {
|
|
139
|
+
const id = generateId(`${parentPath}-cmd`);
|
|
140
|
+
|
|
141
|
+
const canvas = document.createElement('canvas');
|
|
142
|
+
const context = canvas.getContext('2d')!;
|
|
143
|
+
context.font = '14px monospace';
|
|
144
|
+
const textMetrics = context.measureText(label);
|
|
145
|
+
|
|
146
|
+
const textWidth = textMetrics.width + 20;
|
|
147
|
+
const width = Math.max(CONFIG.MIN_WIDTH, textWidth);
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
id,
|
|
151
|
+
padNode: { type: 'command', label },
|
|
152
|
+
x: 0,
|
|
153
|
+
y: 0,
|
|
154
|
+
width: width,
|
|
155
|
+
height: CONFIG.MIN_HEIGHT,
|
|
156
|
+
children: [],
|
|
157
|
+
label
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function layoutIf(condition: string, children: PadNode[], parentPath: string): LayoutNode {
|
|
162
|
+
const id = generateId(`${parentPath}-if`);
|
|
163
|
+
|
|
164
|
+
const thenLayout = calculateLayoutInternal(children[0], `${id}-then`);
|
|
165
|
+
const elseLayout = children.length > 1 ? calculateLayoutInternal(children[1], `${id}-else`) : null;
|
|
166
|
+
|
|
167
|
+
const canvas = document.createElement('canvas');
|
|
168
|
+
const context = canvas.getContext('2d')!;
|
|
169
|
+
context.font = '14px monospace';
|
|
170
|
+
const labelWidth = Math.max(context.measureText(condition).width, 40) + 60;
|
|
171
|
+
|
|
172
|
+
const childX = labelWidth;
|
|
173
|
+
|
|
174
|
+
thenLayout.y = 0;
|
|
175
|
+
thenLayout.x = childX;
|
|
176
|
+
|
|
177
|
+
const thenHalf = thenLayout.height / 2;
|
|
178
|
+
const topVertexY = thenHalf;
|
|
179
|
+
|
|
180
|
+
let height = 0;
|
|
181
|
+
const gap = 40;
|
|
182
|
+
const minWedgeHeight = 60;
|
|
183
|
+
|
|
184
|
+
if (elseLayout) {
|
|
185
|
+
const elseHalf = elseLayout.height / 2;
|
|
186
|
+
|
|
187
|
+
const minBotVertexY_for_spacing = thenLayout.height + gap + elseHalf;
|
|
188
|
+
const minBotVertexY_for_wedge = topVertexY + minWedgeHeight;
|
|
189
|
+
|
|
190
|
+
const botVertexY = Math.max(minBotVertexY_for_spacing, minBotVertexY_for_wedge);
|
|
191
|
+
|
|
192
|
+
elseLayout.y = botVertexY - elseHalf;
|
|
193
|
+
elseLayout.x = childX;
|
|
194
|
+
|
|
195
|
+
height = elseLayout.y + elseLayout.height;
|
|
196
|
+
} else {
|
|
197
|
+
height = Math.max(thenLayout.height, topVertexY + minWedgeHeight);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const width = childX + Math.max(thenLayout.width, elseLayout ? elseLayout.width : 0) + 50;
|
|
201
|
+
const totalHeight = height + 20;
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
id,
|
|
205
|
+
padNode: { type: 'if', condition, then_block: children[0], else_block: children.length > 1 ? children[1] : undefined },
|
|
206
|
+
x: 0,
|
|
207
|
+
y: 0,
|
|
208
|
+
width,
|
|
209
|
+
height: totalHeight,
|
|
210
|
+
children: elseLayout ? [thenLayout, elseLayout] : [thenLayout],
|
|
211
|
+
label: condition,
|
|
212
|
+
condition
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function layoutLoop(condition: string, body: PadNode, parentPath: string): LayoutNode {
|
|
217
|
+
const id = generateId(`${parentPath}-loop`);
|
|
218
|
+
const bodyLayout = calculateLayoutInternal(body, `${id}-body`);
|
|
219
|
+
|
|
220
|
+
const canvas = document.createElement('canvas');
|
|
221
|
+
const context = canvas.getContext('2d')!;
|
|
222
|
+
context.font = '14px monospace';
|
|
223
|
+
const textMetrics = context.measureText(condition);
|
|
224
|
+
|
|
225
|
+
const doubleLineWidth = 10;
|
|
226
|
+
const textPadding = 40;
|
|
227
|
+
const conditionBoxWidth = doubleLineWidth + textMetrics.width + textPadding;
|
|
228
|
+
|
|
229
|
+
const boxWidth = Math.max(CONFIG.MIN_WIDTH, conditionBoxWidth);
|
|
230
|
+
|
|
231
|
+
bodyLayout.x = boxWidth + CONFIG.GAP_X;
|
|
232
|
+
bodyLayout.y = 0;
|
|
233
|
+
|
|
234
|
+
const width = boxWidth + CONFIG.GAP_X + bodyLayout.width;
|
|
235
|
+
const height = Math.max(CONFIG.MIN_HEIGHT, bodyLayout.height);
|
|
236
|
+
|
|
237
|
+
return {
|
|
238
|
+
id,
|
|
239
|
+
padNode: { type: 'loop', condition, body },
|
|
240
|
+
x: 0,
|
|
241
|
+
y: 0,
|
|
242
|
+
width,
|
|
243
|
+
height,
|
|
244
|
+
children: [bodyLayout],
|
|
245
|
+
condition
|
|
246
|
+
}
|
|
247
|
+
}
|