@yanqirenshi/d3.pad 0.1.2 → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +8 -2
- package/dist/index.d.ts +5 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -5
- package/dist/renderers/D3PadRenderer.d.ts +1 -1
- package/dist/renderers/D3PadRenderer.d.ts.map +1 -1
- package/dist/renderers/D3PadRenderer.js +2 -2
- package/dist/renderers/PadNodeRenderer.d.ts +1 -1
- package/dist/renderers/PadNodeRenderer.d.ts.map +1 -1
- package/dist/renderers/PadNodeRenderer.js +6 -6
- package/dist/renderers/blocks/EndBlockGeometry.d.ts +1 -1
- package/dist/renderers/blocks/EndBlockGeometry.d.ts.map +1 -1
- package/dist/renderers/blocks/EndRenderer.js +1 -1
- package/dist/renderers/blocks/FunctionContainerGeometry.d.ts +1 -1
- package/dist/renderers/blocks/FunctionContainerGeometry.d.ts.map +1 -1
- package/dist/renderers/blocks/FunctionContainerRenderer.js +1 -1
- package/dist/renderers/blocks/FunctionHeaderGeometry.d.ts +1 -1
- package/dist/renderers/blocks/FunctionHeaderGeometry.d.ts.map +1 -1
- package/dist/renderers/blocks/FunctionHeaderRenderer.js +1 -1
- package/dist/renderers/blocks/IfBlockGeometry.d.ts +2 -2
- package/dist/renderers/blocks/IfBlockGeometry.d.ts.map +1 -1
- package/dist/renderers/blocks/IfRenderer.d.ts +1 -1
- package/dist/renderers/blocks/IfRenderer.d.ts.map +1 -1
- package/dist/renderers/blocks/IfRenderer.js +1 -1
- package/dist/renderers/blocks/IfRendererProps.d.ts +1 -1
- package/dist/renderers/blocks/IfRendererProps.d.ts.map +1 -1
- package/dist/renderers/blocks/StartBlockGeometry.d.ts +1 -1
- package/dist/renderers/blocks/StartBlockGeometry.d.ts.map +1 -1
- package/dist/renderers/blocks/StartRenderer.js +1 -1
- package/dist/utils/layout.d.ts +1 -1
- package/dist/utils/layout.d.ts.map +1 -1
- package/dist/utils/layout.js +2 -2
- package/package.json +16 -5
- package/src/index.ts +12 -12
- package/src/renderers/D3PadRenderer.tsx +145 -145
- package/src/renderers/PadNodeRenderer.ts +517 -517
- package/src/renderers/blocks/EndBlockGeometry.ts +52 -52
- package/src/renderers/blocks/EndRenderer.tsx +48 -48
- package/src/renderers/blocks/EndRendererProps.ts +5 -5
- package/src/renderers/blocks/FunctionContainerGeometry.ts +54 -54
- package/src/renderers/blocks/FunctionContainerRenderer.tsx +62 -62
- package/src/renderers/blocks/FunctionHeaderGeometry.ts +43 -43
- package/src/renderers/blocks/FunctionHeaderProps.ts +6 -6
- package/src/renderers/blocks/FunctionHeaderRenderer.tsx +47 -47
- package/src/renderers/blocks/IfBlockGeometry.ts +98 -98
- package/src/renderers/blocks/IfRenderer.tsx +88 -88
- package/src/renderers/blocks/IfRendererProps.ts +7 -7
- package/src/renderers/blocks/LoopRenderer.tsx +50 -50
- package/src/renderers/blocks/LoopRendererProps.ts +7 -7
- package/src/renderers/blocks/README.md +29 -29
- package/src/renderers/blocks/StartBlockGeometry.ts +66 -66
- package/src/renderers/blocks/StartRenderer.tsx +49 -49
- package/src/renderers/blocks/StartRendererProps.ts +5 -5
- package/src/utils/layout.ts +247 -247
- package/dist/parsers/jsParser.d.ts +0 -2
- package/dist/parsers/jsParser.d.ts.map +0 -1
- package/dist/parsers/jsParser.js +0 -101
- package/dist/types.d.ts +0 -24
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -1
- package/src/parsers/jsParser.ts +0 -122
- package/src/types.ts +0 -7
package/src/utils/layout.ts
CHANGED
|
@@ -1,247 +1,247 @@
|
|
|
1
|
-
import type { PadNode } from '
|
|
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
|
-
}
|
|
1
|
+
import type { PadNode } from '@yanqirenshi/model.pad'; // 型は model.pad に移した(#201)
|
|
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.js';
|
|
67
|
+
import { FunctionContainerGeometry } from '../renderers/blocks/FunctionContainerGeometry.js';
|
|
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
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"jsParser.d.ts","sourceRoot":"","sources":["../../src/parsers/jsParser.ts"],"names":[],"mappings":"AAGA,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAGlD"}
|
package/dist/parsers/jsParser.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import * as acorn from 'acorn';
|
|
2
|
-
export function parse_js_code(code) {
|
|
3
|
-
const parser = new JsParser(code);
|
|
4
|
-
return parser.parse();
|
|
5
|
-
}
|
|
6
|
-
class JsParser {
|
|
7
|
-
code;
|
|
8
|
-
constructor(code) {
|
|
9
|
-
this.code = code;
|
|
10
|
-
}
|
|
11
|
-
parse() {
|
|
12
|
-
try {
|
|
13
|
-
const ast = acorn.parse(this.code, { ecmaVersion: 2020 });
|
|
14
|
-
const nodes = [];
|
|
15
|
-
// @ts-ignore
|
|
16
|
-
for (const node of ast.body) {
|
|
17
|
-
if (node.type === 'FunctionDeclaration') {
|
|
18
|
-
nodes.push(this.parseFunction(node));
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
if (nodes.length === 0) {
|
|
22
|
-
return JSON.stringify({ type: 'error', message: 'No function found' });
|
|
23
|
-
}
|
|
24
|
-
return JSON.stringify({ type: 'sequence', children: nodes });
|
|
25
|
-
}
|
|
26
|
-
catch (e) {
|
|
27
|
-
return JSON.stringify({ type: 'error', message: `Parse error: ${e}` });
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
parseFunction(node) {
|
|
31
|
-
const name = node.id?.name || 'anonymous';
|
|
32
|
-
const body = this.parseBlock(node.body);
|
|
33
|
-
return {
|
|
34
|
-
type: 'block',
|
|
35
|
-
label: `fn ${name}()`, // Use 'fn' label to match Rust style or just 'function'
|
|
36
|
-
children: [body]
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
parseBlock(node) {
|
|
40
|
-
const children = [];
|
|
41
|
-
if (node.body) {
|
|
42
|
-
for (const stmt of node.body) {
|
|
43
|
-
children.push(this.parseStmt(stmt));
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return { type: 'sequence', children };
|
|
47
|
-
}
|
|
48
|
-
parseStmt(stmt) {
|
|
49
|
-
switch (stmt.type) {
|
|
50
|
-
case 'VariableDeclaration':
|
|
51
|
-
return { type: 'command', label: this.snippet(stmt) };
|
|
52
|
-
case 'ExpressionStatement':
|
|
53
|
-
// Remove semicolon if present for cleaner display?
|
|
54
|
-
// Currently just taking snippet.
|
|
55
|
-
return { type: 'command', label: this.snippet(stmt) };
|
|
56
|
-
case 'IfStatement':
|
|
57
|
-
return this.parseIf(stmt);
|
|
58
|
-
case 'WhileStatement':
|
|
59
|
-
return {
|
|
60
|
-
type: 'loop',
|
|
61
|
-
condition: this.snippet(stmt.test),
|
|
62
|
-
body: this.parseStmt(stmt.body)
|
|
63
|
-
};
|
|
64
|
-
case 'ForStatement':
|
|
65
|
-
let condLabel = 'for (;;) ';
|
|
66
|
-
if (stmt.init || stmt.test || stmt.update) {
|
|
67
|
-
const init = stmt.init ? this.snippet(stmt.init) : '';
|
|
68
|
-
const test = stmt.test ? this.snippet(stmt.test) : '';
|
|
69
|
-
const update = stmt.update ? this.snippet(stmt.update) : '';
|
|
70
|
-
// Clean up init if it has semicolon?
|
|
71
|
-
// this.snippet(stmt.init) includes 'let i = 0', no semi usually if it's var decl node inside for
|
|
72
|
-
condLabel = `for (${init}; ${test}; ${update})`;
|
|
73
|
-
}
|
|
74
|
-
return {
|
|
75
|
-
type: 'loop',
|
|
76
|
-
condition: condLabel,
|
|
77
|
-
body: this.parseStmt(stmt.body)
|
|
78
|
-
};
|
|
79
|
-
case 'BlockStatement':
|
|
80
|
-
return this.parseBlock(stmt);
|
|
81
|
-
case 'ReturnStatement':
|
|
82
|
-
return { type: 'command', label: this.snippet(stmt) };
|
|
83
|
-
default:
|
|
84
|
-
return { type: 'command', label: this.snippet(stmt) };
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
parseIf(stmt) {
|
|
88
|
-
const condition = this.snippet(stmt.test);
|
|
89
|
-
const thenBlock = this.parseStmt(stmt.consequent);
|
|
90
|
-
const elseBlock = stmt.alternate ? this.parseStmt(stmt.alternate) : undefined;
|
|
91
|
-
return {
|
|
92
|
-
type: 'if',
|
|
93
|
-
condition,
|
|
94
|
-
then_block: thenBlock,
|
|
95
|
-
else_block: elseBlock
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
snippet(node) {
|
|
99
|
-
return this.code.substring(node.start, node.end);
|
|
100
|
-
}
|
|
101
|
-
}
|
package/dist/types.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
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
|
package/dist/types.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|