@kubohiroya/turbowarp-plus-editor-code-views 0.22.1 → 0.22.2
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.ja.md +12 -0
- package/README.md +10 -0
- package/dist/code-popout.js +1 -0
- package/dist/horizontal-block-palette.js +18 -0
- package/dist/script-layout-view.d.ts +3 -1
- package/dist/script-layout-view.js +17 -3
- package/dist/semantic-layout-dialog.d.ts +12 -0
- package/dist/semantic-layout-dialog.js +251 -0
- package/dist/semantic-layout-workspace.d.ts +57 -0
- package/dist/semantic-layout-workspace.js +87 -0
- package/dist/semantic-script-layout.d.ts +34 -0
- package/dist/semantic-script-layout.js +271 -0
- package/package.json +4 -2
- package/src/code-popout.tsx +2 -0
- package/src/horizontal-block-palette.ts +15 -0
- package/src/script-layout-view.ts +17 -3
- package/src/semantic-layout-dialog.ts +222 -0
- package/src/semantic-layout-workspace.ts +109 -0
- package/src/semantic-script-layout.ts +254 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import {readScripts, type LayoutBlock, type LayoutEvents, type LayoutWorkspace, type RenderedScript} from './script-layout-workspace.ts';
|
|
2
|
+
import type {ScriptBounds} from './script-layout.ts';
|
|
3
|
+
import type {SemanticPlacement} from './semantic-script-layout.ts';
|
|
4
|
+
|
|
5
|
+
type Point = {x: number; y: number};
|
|
6
|
+
export interface LayoutComment {
|
|
7
|
+
id: string;
|
|
8
|
+
block_?: LayoutBlock & {getRootBlock(): LayoutBlock};
|
|
9
|
+
getXY?(): Point;
|
|
10
|
+
getRelativeToSurfaceXY?(): Point;
|
|
11
|
+
getBoundingRectangle(): {topLeft: Point; bottomRight: Point};
|
|
12
|
+
moveTo?(x: number, y: number): void;
|
|
13
|
+
getText?(): string;
|
|
14
|
+
getSvgRoot?(): SVGElement;
|
|
15
|
+
bubble_?: {bubbleGroup_?: SVGElement};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type SemanticWorkspace = LayoutWorkspace & {getTopComments?(ordered: boolean): LayoutComment[]};
|
|
19
|
+
export type SemanticEvents = LayoutEvents & {
|
|
20
|
+
FIRE_QUEUE_?: {type?: string; commentId?: string; recordUndo?: boolean}[];
|
|
21
|
+
disable?(): void;
|
|
22
|
+
enable?(): void;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export interface CommentSnapshot extends ScriptBounds {
|
|
26
|
+
comment: LayoutComment;
|
|
27
|
+
rootId?: string;
|
|
28
|
+
origin: Point;
|
|
29
|
+
text: string;
|
|
30
|
+
svg?: SVGElement;
|
|
31
|
+
}
|
|
32
|
+
export interface SemanticScript extends RenderedScript {
|
|
33
|
+
/** Full script rectangle before attached comments are included. */
|
|
34
|
+
bounds: ScriptBounds;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Read expanded bounds without changing the user's folding or moving any comments. */
|
|
38
|
+
export function readSemanticWorkspace(workspace: SemanticWorkspace) {
|
|
39
|
+
const scripts: SemanticScript[] = readScripts(workspace).map(script => {
|
|
40
|
+
const {topLeft, bottomRight} = script.block.getBoundingRectangle();
|
|
41
|
+
const bounds = {id: script.id, ...topLeft, width: bottomRight.x - topLeft.x, height: bottomRight.y - topLeft.y};
|
|
42
|
+
return {...script, ...bounds, bounds};
|
|
43
|
+
});
|
|
44
|
+
const byId = new Map(scripts.map(s => [s.id, s]));
|
|
45
|
+
const comments: CommentSnapshot[] = (workspace.getTopComments?.(false) ?? []).map(comment => {
|
|
46
|
+
const {topLeft, bottomRight} = comment.getBoundingRectangle();
|
|
47
|
+
const rootId = comment.block_?.getRootBlock().id;
|
|
48
|
+
if (rootId && (!comment.getXY || !comment.moveTo)) throw new Error('Comment cannot be moved safely');
|
|
49
|
+
return {id: comment.id, comment, rootId, origin: comment.getXY?.() ?? comment.getRelativeToSurfaceXY?.() ?? topLeft,
|
|
50
|
+
text: comment.getText?.() ?? '',
|
|
51
|
+
...topLeft, width: bottomRight.x - topLeft.x, height: bottomRight.y - topLeft.y,
|
|
52
|
+
svg: comment.getSvgRoot?.() ?? comment.bubble_?.bubbleGroup_};
|
|
53
|
+
});
|
|
54
|
+
for (const comment of comments) {
|
|
55
|
+
const script = byId.get(comment.rootId);
|
|
56
|
+
if (!script) continue;
|
|
57
|
+
const right = Math.max(script.x + script.width, comment.x + comment.width);
|
|
58
|
+
const bottom = Math.max(script.y + script.height, comment.y + comment.height);
|
|
59
|
+
script.x = Math.min(script.x, comment.x); script.y = Math.min(script.y, comment.y);
|
|
60
|
+
script.width = right - script.x; script.height = bottom - script.y;
|
|
61
|
+
}
|
|
62
|
+
return {scripts, comments, obstacles: comments.filter(c => !byId.has(c.rootId))};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Roll back queued native moves before any of them reach the VM or the undo stacks. */
|
|
66
|
+
export function applySemanticLayout(workspace: SemanticWorkspace, events: SemanticEvents,
|
|
67
|
+
placements: SemanticPlacement<SemanticScript>[], comments: CommentSnapshot[]) {
|
|
68
|
+
const outer = events.getGroup();
|
|
69
|
+
const queue = events.FIRE_QUEUE_;
|
|
70
|
+
if (!queue || !events.disable || !events.enable) throw new Error('Transactional move events are unavailable');
|
|
71
|
+
const start = queue.length;
|
|
72
|
+
const roots = placements.map(p => ({block: p.script.block, position: p.script.block.getRelativeToSurfaceXY()}));
|
|
73
|
+
const byId = new Map(placements.map(p => [p.script.id, p]));
|
|
74
|
+
events.setGroup(true);
|
|
75
|
+
workspace.setResizesEnabled(false);
|
|
76
|
+
try {
|
|
77
|
+
for (const p of placements) {
|
|
78
|
+
const dx = p.x - p.script.x, dy = p.y - p.script.y;
|
|
79
|
+
if (dx || dy) p.script.block.moveBy(dx, dy);
|
|
80
|
+
}
|
|
81
|
+
for (const c of comments) {
|
|
82
|
+
const p = byId.get(c.rootId);
|
|
83
|
+
if (!p) continue;
|
|
84
|
+
c.comment.moveTo!(c.origin.x + p.x - p.script.x, c.origin.y + p.y - p.script.y);
|
|
85
|
+
}
|
|
86
|
+
// Native block moves already carry their attached comments on undo/redo. Recording an
|
|
87
|
+
// absolute comment move as well would move it twice on redo. Keep the notification for
|
|
88
|
+
// the VM (including child-window serialization), but let the block own its undo step.
|
|
89
|
+
const attached = new Set(comments.filter(c => byId.has(c.rootId)).map(c => c.id));
|
|
90
|
+
for (const event of queue.slice(start)) {
|
|
91
|
+
if (event.type === 'comment_move' && attached.has(event.commentId)) event.recordUndo = false;
|
|
92
|
+
}
|
|
93
|
+
} catch (error) {
|
|
94
|
+
queue.splice(start);
|
|
95
|
+
events.disable();
|
|
96
|
+
try {
|
|
97
|
+
for (const {block, position} of roots) {
|
|
98
|
+
const current = block.getRelativeToSurfaceXY();
|
|
99
|
+
block.moveBy(position.x - current.x, position.y - current.y);
|
|
100
|
+
}
|
|
101
|
+
for (const c of comments) if (byId.has(c.rootId)) c.comment.moveTo!(c.origin.x, c.origin.y);
|
|
102
|
+
} finally {events.enable();}
|
|
103
|
+
throw error;
|
|
104
|
+
} finally {
|
|
105
|
+
workspace.setResizesEnabled(true);
|
|
106
|
+
events.setGroup(outer);
|
|
107
|
+
}
|
|
108
|
+
events.fireNow_();
|
|
109
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import Matter from 'matter-js';
|
|
2
|
+
import type {ScriptBounds} from './script-layout.ts';
|
|
3
|
+
import type {ScriptRelation} from './script-relations.ts';
|
|
4
|
+
|
|
5
|
+
export interface SemanticPlacement<T extends ScriptBounds = ScriptBounds> {
|
|
6
|
+
script: T;
|
|
7
|
+
x: number;
|
|
8
|
+
y: number;
|
|
9
|
+
column: number;
|
|
10
|
+
group: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface SemanticLayoutOptions {
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
/** Yield to the host without making the simulation depend on elapsed time. */
|
|
16
|
+
yieldControl?(): Promise<void>;
|
|
17
|
+
obstacles?: readonly ScriptBounds[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const semanticLayoutLimits = {scripts: 1000, relations: 10000, steps: 120};
|
|
21
|
+
const gap = 64;
|
|
22
|
+
const compareId = (a: string, b: string) => a < b ? -1 : Number(a > b);
|
|
23
|
+
const overlaps = (a: ScriptBounds, b: ScriptBounds) =>
|
|
24
|
+
a.x < b.x + b.width + gap && b.x < a.x + a.width + gap &&
|
|
25
|
+
a.y < b.y + b.height + gap && b.y < a.y + a.height + gap;
|
|
26
|
+
|
|
27
|
+
function check(signal?: AbortSignal) {
|
|
28
|
+
if (signal?.aborted) throw new Error('Layout cancelled');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Iterative Kosaraju traversal: recursive projects must not exhaust the JS stack. */
|
|
32
|
+
function components(ids: string[], outgoing: Map<string, string[]>, incoming: Map<string, string[]>) {
|
|
33
|
+
const seen = new Set<string>();
|
|
34
|
+
const finished: string[] = [];
|
|
35
|
+
for (const id of ids) {
|
|
36
|
+
if (seen.has(id)) continue;
|
|
37
|
+
const stack: [string, number][] = [[id, 0]];
|
|
38
|
+
seen.add(id);
|
|
39
|
+
while (stack.length) {
|
|
40
|
+
const entry = stack.at(-1)!;
|
|
41
|
+
const edges = outgoing.get(entry[0])!;
|
|
42
|
+
if (entry[1] === edges.length) {finished.push(entry[0]); stack.pop(); continue;}
|
|
43
|
+
const next = edges[entry[1]++];
|
|
44
|
+
if (!seen.has(next)) {seen.add(next); stack.push([next, 0]);}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
seen.clear();
|
|
48
|
+
const groups: string[][] = [];
|
|
49
|
+
for (const id of finished.reverse()) {
|
|
50
|
+
if (seen.has(id)) continue;
|
|
51
|
+
const group: string[] = [], stack = [id];
|
|
52
|
+
seen.add(id);
|
|
53
|
+
while (stack.length) {
|
|
54
|
+
const current = stack.pop()!;
|
|
55
|
+
group.push(current);
|
|
56
|
+
for (const next of incoming.get(current)!) if (!seen.has(next)) {seen.add(next); stack.push(next);}
|
|
57
|
+
}
|
|
58
|
+
groups.push(group.sort(compareId));
|
|
59
|
+
}
|
|
60
|
+
return groups.sort((a, b) => compareId(a[0], b[0]));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Lay out the SCC condensation DAG in ranks, then relax rectangles with Matter.js.
|
|
65
|
+
* Canonical ID order, fixed steps and final packing keep repeated runs stable.
|
|
66
|
+
* Relations to other targets are shown by the preview, never used to move those targets.
|
|
67
|
+
*/
|
|
68
|
+
export async function arrangeSemanticScripts<T extends ScriptBounds>(
|
|
69
|
+
scripts: readonly T[], relations: readonly ScriptRelation[], targetId: string, options: SemanticLayoutOptions = {}
|
|
70
|
+
): Promise<{placements: SemanticPlacement<T>[]; metrics: {nodes: number; edges: number; steps: number; fallback: boolean}}> {
|
|
71
|
+
const {signal, obstacles = []} = options;
|
|
72
|
+
const yieldControl = options.yieldControl ?? (() => new Promise<void>(resolve => setTimeout(resolve, 0)));
|
|
73
|
+
check(signal);
|
|
74
|
+
if (scripts.length > semanticLayoutLimits.scripts || relations.length > semanticLayoutLimits.relations ||
|
|
75
|
+
obstacles.length > semanticLayoutLimits.scripts) throw new Error('Layout size limit exceeded');
|
|
76
|
+
const ordered = [...scripts].sort((a, b) => compareId(a.id, b.id));
|
|
77
|
+
if (new Set(ordered.map(s => s.id)).size !== ordered.length || [...ordered, ...obstacles].some(s =>
|
|
78
|
+
![s.x, s.y, s.width, s.height].every(Number.isFinite) || s.width <= 0 || s.height <= 0)) {
|
|
79
|
+
throw new Error('Invalid script bounds');
|
|
80
|
+
}
|
|
81
|
+
const ids = ordered.map(s => s.id);
|
|
82
|
+
const outgoing = new Map(ids.map(id => [id, [] as string[]]));
|
|
83
|
+
const incoming = new Map(ids.map(id => [id, [] as string[]]));
|
|
84
|
+
const edges = new Set<string>();
|
|
85
|
+
for (const {source, destination, unresolved} of relations) {
|
|
86
|
+
if (unresolved || source.targetId !== targetId || destination?.targetId !== targetId ||
|
|
87
|
+
!outgoing.has(source.scriptId) || !incoming.has(destination.scriptId)) continue;
|
|
88
|
+
const key = JSON.stringify([source.scriptId, destination.scriptId]);
|
|
89
|
+
if (edges.has(key)) continue;
|
|
90
|
+
edges.add(key);
|
|
91
|
+
outgoing.get(source.scriptId)!.push(destination.scriptId);
|
|
92
|
+
incoming.get(destination.scriptId)!.push(source.scriptId);
|
|
93
|
+
}
|
|
94
|
+
for (const map of [outgoing, incoming]) for (const entries of map.values()) entries.sort(compareId);
|
|
95
|
+
const scc = components(ids, outgoing, incoming);
|
|
96
|
+
const owner = new Map(scc.flatMap((group, index) => group.map(id => [id, index] as const)));
|
|
97
|
+
const next = scc.map(() => new Set<number>()), before = scc.map(() => new Set<number>());
|
|
98
|
+
for (const [id, children] of outgoing) for (const child of children) {
|
|
99
|
+
const a = owner.get(id)!, b = owner.get(child)!;
|
|
100
|
+
if (a !== b) {next[a].add(b); before[b].add(a);}
|
|
101
|
+
}
|
|
102
|
+
const rank = scc.map(() => 0), remaining = before.map(set => set.size);
|
|
103
|
+
const ready = remaining.flatMap((n, i) => n ? [] : [i]);
|
|
104
|
+
for (let cursor = 0; cursor < ready.length; cursor++) {
|
|
105
|
+
const current = ready[cursor];
|
|
106
|
+
for (const child of next[current]) {
|
|
107
|
+
rank[child] = Math.max(rank[child], rank[current] + 1);
|
|
108
|
+
if (--remaining[child] === 0) ready.push(child);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// Separate weak components, including orphan scripts, into non-overlapping bands.
|
|
112
|
+
const band = new Map<string, number>();
|
|
113
|
+
let bandCount = 0;
|
|
114
|
+
for (const id of ids) {
|
|
115
|
+
if (band.has(id)) continue;
|
|
116
|
+
const stack = [id];
|
|
117
|
+
band.set(id, bandCount);
|
|
118
|
+
while (stack.length) {
|
|
119
|
+
const current = stack.pop()!;
|
|
120
|
+
for (const adjacent of [...outgoing.get(current)!, ...incoming.get(current)!]) {
|
|
121
|
+
if (!band.has(adjacent)) {band.set(adjacent, bandCount); stack.push(adjacent);}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
bandCount++;
|
|
125
|
+
}
|
|
126
|
+
await yieldControl(); check(signal);
|
|
127
|
+
const widths = new Map<number, number>();
|
|
128
|
+
for (const script of ordered) {
|
|
129
|
+
const column = rank[owner.get(script.id)!];
|
|
130
|
+
widths.set(column, Math.max(widths.get(column) ?? 0, script.width));
|
|
131
|
+
}
|
|
132
|
+
const columnX: number[] = [];
|
|
133
|
+
let x = gap;
|
|
134
|
+
for (let column = 0; column < widths.size; column++) {
|
|
135
|
+
columnX[column] = x;
|
|
136
|
+
x += widths.get(column)! + gap * 2;
|
|
137
|
+
}
|
|
138
|
+
const placements: SemanticPlacement<T>[] = [];
|
|
139
|
+
let bandTop = gap;
|
|
140
|
+
for (let group = 0; group < bandCount; group++) {
|
|
141
|
+
const columns = new Map<number, T[]>();
|
|
142
|
+
for (const script of ordered) if (band.get(script.id) === group) {
|
|
143
|
+
const column = rank[owner.get(script.id)!];
|
|
144
|
+
const entries = columns.get(column) ?? [];
|
|
145
|
+
entries.push(script); columns.set(column, entries);
|
|
146
|
+
}
|
|
147
|
+
// Barycentric sweeps reduce crossings; SCC members stay contiguous.
|
|
148
|
+
const order = new Map<string, number>();
|
|
149
|
+
for (const entries of columns.values()) entries.forEach((s, i) => order.set(s.id, i));
|
|
150
|
+
const ranks = [...columns.keys()].sort((a, b) => a - b);
|
|
151
|
+
for (let sweep = 0; sweep < 4; sweep++) {
|
|
152
|
+
const neighbors = sweep % 2 ? outgoing : incoming;
|
|
153
|
+
for (const column of sweep % 2 ? [...ranks].reverse() : ranks) {
|
|
154
|
+
const mean = (id: string) => {
|
|
155
|
+
const members = scc[owner.get(id)!];
|
|
156
|
+
const values = members.flatMap(member => neighbors.get(member)!)
|
|
157
|
+
.filter(n => owner.get(n) !== owner.get(id)).map(n => order.get(n) ?? 0);
|
|
158
|
+
return values.length ? values.reduce((a, b) => a + b, 0) / values.length : order.get(members[0]) ?? 0;
|
|
159
|
+
};
|
|
160
|
+
const entries = columns.get(column)!;
|
|
161
|
+
entries.sort((a, b) => owner.get(a.id) === owner.get(b.id) ? compareId(a.id, b.id) :
|
|
162
|
+
mean(a.id) - mean(b.id) || owner.get(a.id)! - owner.get(b.id)!);
|
|
163
|
+
entries.forEach((s, i) => order.set(s.id, i));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
let bottom = bandTop;
|
|
167
|
+
for (const column of ranks) {
|
|
168
|
+
let y = bandTop;
|
|
169
|
+
for (const script of columns.get(column)!) {
|
|
170
|
+
placements.push({script, x: columnX[column], y, column, group});
|
|
171
|
+
y += script.height + gap;
|
|
172
|
+
}
|
|
173
|
+
bottom = Math.max(bottom, y);
|
|
174
|
+
}
|
|
175
|
+
bandTop = bottom + gap * 2;
|
|
176
|
+
}
|
|
177
|
+
const engine = Matter.Engine.create({gravity: {x: 0, y: 0, scale: 0}, enableSleeping: false});
|
|
178
|
+
let steps = 0;
|
|
179
|
+
let fallback = false;
|
|
180
|
+
try {
|
|
181
|
+
const bodies = placements.map((p, index) => Matter.Bodies.rectangle(p.x + p.script.width / 2, p.y + p.script.height / 2,
|
|
182
|
+
p.script.width + gap, p.script.height + gap,
|
|
183
|
+
{id: index + 1, inertia: Infinity, restitution: 0, frictionAir: .35, friction: 0}));
|
|
184
|
+
const fixed = [...obstacles].sort((a, b) => compareId(a.id, b.id)).map((o, index) =>
|
|
185
|
+
Matter.Bodies.rectangle(o.x + o.width / 2, o.y + o.height / 2,
|
|
186
|
+
o.width + gap, o.height + gap, {id: bodies.length + index + 1, isStatic: true}));
|
|
187
|
+
Matter.Composite.add(engine.world, [...bodies, ...fixed]);
|
|
188
|
+
const byId = new Map(placements.map((p, i) => [p.script.id, i]));
|
|
189
|
+
// Attract related rectangles to the same reading band, without losing their rank.
|
|
190
|
+
const targets = placements.map(p => {
|
|
191
|
+
const parents = incoming.get(p.script.id)!.filter(id => owner.get(id) !== owner.get(p.script.id));
|
|
192
|
+
const centers = parents.map(id => {
|
|
193
|
+
const parent = placements[byId.get(id)!];
|
|
194
|
+
return parent.y + parent.script.height / 2;
|
|
195
|
+
});
|
|
196
|
+
return {x: p.x + p.script.width / 2, y: centers.length ?
|
|
197
|
+
centers.reduce((a, b) => a + b, 0) / centers.length : p.y + p.script.height / 2};
|
|
198
|
+
});
|
|
199
|
+
let stableSteps = 0;
|
|
200
|
+
for (; steps < semanticLayoutLimits.steps && bodies.length; steps++) {
|
|
201
|
+
if (steps % 8 === 0) {await yieldControl(); check(signal);}
|
|
202
|
+
bodies.forEach((body, i) => {
|
|
203
|
+
const target = targets[i];
|
|
204
|
+
Matter.Body.applyForce(body, body.position, {
|
|
205
|
+
x: (target.x - body.position.x) * body.mass * .00002,
|
|
206
|
+
y: (target.y - body.position.y) * body.mass * .00002
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
Matter.Engine.update(engine, 1000 / 60);
|
|
210
|
+
stableSteps = bodies.every(body => body.speed < .01) ? stableSteps + 1 : 0;
|
|
211
|
+
if (stableSteps >= 12) {steps++; break;}
|
|
212
|
+
}
|
|
213
|
+
if (steps === semanticLayoutLimits.steps) fallback = true;
|
|
214
|
+
// Snap ranks and preserve canonical within-column order after the physical relaxation.
|
|
215
|
+
let bottom = gap;
|
|
216
|
+
for (let group = 0; group < bandCount; group++) {
|
|
217
|
+
let groupBottom = bottom;
|
|
218
|
+
for (const column of [...widths.keys()].sort((a, b) => a - b)) {
|
|
219
|
+
let y = bottom;
|
|
220
|
+
for (const p of placements.filter(p => p.group === group && p.column === column)) {
|
|
221
|
+
const body = bodies[byId.get(p.script.id)!];
|
|
222
|
+
const relaxed = body.position.y - p.script.height / 2;
|
|
223
|
+
if (!Number.isFinite(relaxed)) fallback = true;
|
|
224
|
+
p.y = Math.ceil(Math.max(y, Number.isFinite(relaxed) ? relaxed : y));
|
|
225
|
+
// Keep loose comments fixed. Move past any intersecting obstacle.
|
|
226
|
+
let changed = true;
|
|
227
|
+
while (changed) {
|
|
228
|
+
changed = false;
|
|
229
|
+
for (const obstacle of obstacles) if (overlaps({...p.script, x: p.x, y: p.y}, obstacle)) {
|
|
230
|
+
p.y = obstacle.y + obstacle.height + gap;
|
|
231
|
+
changed = true; fallback = true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
y = p.y + p.script.height + gap;
|
|
235
|
+
}
|
|
236
|
+
groupBottom = Math.max(groupBottom, y);
|
|
237
|
+
}
|
|
238
|
+
bottom = groupBottom + gap * 2;
|
|
239
|
+
if (group % 32 === 0) {await yieldControl(); check(signal);}
|
|
240
|
+
}
|
|
241
|
+
const boxes = placements.map(p => ({...p.script, x: p.x, y: p.y}));
|
|
242
|
+
for (let i = 0; i < boxes.length; i++) {
|
|
243
|
+
if (![boxes[i].x, boxes[i].y].every(Number.isFinite) ||
|
|
244
|
+
boxes.slice(i + 1).some(b => overlaps(boxes[i], b)) || obstacles.some(o => overlaps(boxes[i], o))) {
|
|
245
|
+
throw new Error('Layout validation failed');
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
check(signal);
|
|
249
|
+
return {placements, metrics: {nodes: scripts.length, edges: edges.size, steps, fallback}};
|
|
250
|
+
} finally {
|
|
251
|
+
Matter.Composite.clear(engine.world, false);
|
|
252
|
+
Matter.Engine.clear(engine);
|
|
253
|
+
}
|
|
254
|
+
}
|