@grafloria/element 0.4.55 → 0.4.57

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.
@@ -0,0 +1,55 @@
1
+ /**
2
+ * THE TAB TEAR-OUT (tile first, step 4b-iii: out of the binder).
3
+ *
4
+ * A press that TRAVELLED on a tab drags the whole page onto this board, the
5
+ * way dragging a VS Code tab out of its group makes a group of its own. The
6
+ * page is a GROUP, so it rides the same `adopt` leg a widget uses — entry at
7
+ * the bottom, `placeNear` under the pointer, the dashed placeholder drawn
8
+ * from the live cell — and the commit is one batch: the cell and frame it
9
+ * landed on, out of the container, into this board, plus every tile this
10
+ * board had to push aside.
11
+ *
12
+ * Like VS Code, the page itself does not follow the pointer; a chip carrying
13
+ * the tab's label does, and the placeholder shows where the drop will land.
14
+ */
15
+ import { type Command, type GroupModel } from '@grafloria/engine';
16
+ import { type WorldRect } from './grid-mapping.js';
17
+ import { type BoardCtx } from './board-ctx.js';
18
+ import type { AdoptedLeg, AdoptOptions, BinderPeer, TearOutPlan } from './grid-binder.js';
19
+ /** A torn-out page never arrives shorter than this: a strip with no room under it is not a group. */
20
+ export declare const TEAR_OUT_MIN_ROWS = 2;
21
+ export interface TearOutDeps {
22
+ /** The columns this board is bound at NOW (a responsive board changes it). */
23
+ columns(): number;
24
+ /** This binder's own adoption leg — the page enters this board through it. */
25
+ adopt(node: {
26
+ id: string;
27
+ }, world: {
28
+ x: number;
29
+ y: number;
30
+ }, pxSize: {
31
+ width: number;
32
+ height: number;
33
+ }, opts?: AdoptOptions): AdoptedLeg | null;
34
+ boardArea(): number;
35
+ /** The boards registered on this canvas, and this binder's own peer among them. */
36
+ peersOnCanvas(): Set<BinderPeer>;
37
+ selfPeer(): BinderPeer;
38
+ worldInsideBoard(x: number, y: number): boolean;
39
+ worldInsideGroup(g: GroupModel, x: number, y: number): boolean;
40
+ frameOfGroup(g: GroupModel): WorldRect;
41
+ /** One undoable batch; false when there was nothing to do. */
42
+ execute(name: string, commands: Command[]): boolean;
43
+ project(): void;
44
+ hidePlaceholder(): void;
45
+ armGlide(): void;
46
+ disarmGlideSoon(): void;
47
+ enforceBoardHeight(): void;
48
+ persistLayouts(): void;
49
+ /** A tile or slab gesture of this board is running: a tab press starts nothing. */
50
+ busy(): boolean;
51
+ /** The page this board is tearing out right now (one at a time). */
52
+ tearing(): string | null;
53
+ setTearing(pageId: string | null): void;
54
+ }
55
+ export declare function createTearOut(ctx: BoardCtx, deps: TearOutDeps): (pageId: string, fromGroupId: string, ev: PointerEvent, plan: TearOutPlan) => boolean;
@@ -0,0 +1,613 @@
1
+ import { cellToRect, rowHeightFor, sizeToSpan } from './grid-mapping.js';
2
+ import { BESIDE_BAND, resolveTabZone } from './zones.js';
3
+ import { EDGE_GRACE } from './board-ctx.js';
4
+ /** A torn-out page never arrives shorter than this: a strip with no room under it is not a group. */
5
+ export const TEAR_OUT_MIN_ROWS = 2;
6
+ export function createTearOut(ctx, deps) {
7
+ const { api, group, diagram, options, gap } = ctx;
8
+ const { adopt, boardArea, execute, frameOfGroup, persistLayouts, project, hidePlaceholder, armGlide, disarmGlideSoon, enforceBoardHeight, busy, tearing, setTearing } = deps;
9
+ const eng = () => ctx.engine();
10
+ const frame = () => ctx.frame();
11
+ const geom = () => ctx.geom();
12
+ const rows = () => ctx.rows();
13
+ const columns = () => deps.columns();
14
+ const isStatic = () => ctx.isStatic();
15
+ const disposed = () => ctx.disposed();
16
+ const htmlLayer = () => ctx.htmlLayer();
17
+ const sizeOf = ctx.sizeOf;
18
+ const peersOnCanvas = () => deps.peersOnCanvas();
19
+ const selfPeer = deps.selfPeer;
20
+ const worldInsideBoard = (x, y) => deps.worldInsideBoard(x, y);
21
+ const worldInsideGroup = (g, x, y) => deps.worldInsideGroup(g, x, y);
22
+ const beginTearOut = (pageId, fromGroupId, ev, plan) => {
23
+ var _a;
24
+ if (disposed() || busy() || isStatic() || tearing())
25
+ return false;
26
+ const from = diagram.getGroup(fromGroupId);
27
+ if (!from || !diagram.getGroup(pageId) || !eng().getItem(fromGroupId))
28
+ return false;
29
+ const toWorld = (cx, cy) => {
30
+ var _a;
31
+ const rect = api.container.getBoundingClientRect();
32
+ return ((_a = api.viewport) === null || _a === void 0 ? void 0 : _a.clientToWorld) ? api.viewport.clientToWorld(cx, cy, rect) : { x: cx - rect.left, y: cy - rect.top };
33
+ };
34
+ // The board holds the NEW group, not the bare page: it is adopted under the
35
+ // group's id, sized for the page plus its strip, taking the room under the
36
+ // pointer with its top edge at the pointer — the tab is what the pointer
37
+ // holds, the page hangs below it. NOT at the press: a ghost in the engine
38
+ // costs rows, and on a fit board every tile squeezes the moment it enters,
39
+ // so the ghost enters only when the pointer is over free board space, and
40
+ // leaves whenever it is over a strip, a group or an edge. A full board that
41
+ // refuses the ghost still lets the page join, reorder or split.
42
+ let leg = null;
43
+ /** The board holding the leg: null = this one, else the foreign peer under the pointer. */
44
+ let legPeer = null;
45
+ const ensureLeg = (world, peer = null) => {
46
+ // The page TRAVELS at most half the board tall (the dock's measure): at
47
+ // its natural height — a page fills its container, eight rows on the
48
+ // demo — the ghost crossing the KPI row tore the whole dashboard apart
49
+ // on its way to a cell (identification round, D1 03–08).
50
+ const capPx = dockSpan.h * (rowHeightFor(geom(), rows()) + gap) - gap;
51
+ const size = { width: plan.size.width, height: Math.min(plan.size.height, capPx) };
52
+ // A different board under the pointer takes the leg over, the old one
53
+ // back to its pre-entry layout first. An INNER tab used to know only
54
+ // the board owning its container — over the main board the chip dimmed
55
+ // and the release did nothing (identification round, L2).
56
+ if (leg && legPeer !== peer) {
57
+ leg.abort();
58
+ leg = null;
59
+ }
60
+ if (!leg) {
61
+ leg = peer ? peer.adopt({ id: plan.arrivingId }, world, size, { fit: 'shrink', anchor: 'top' }) : adopt({ id: plan.arrivingId }, world, size, { fit: 'shrink', anchor: 'top' });
62
+ legPeer = leg ? peer : null;
63
+ }
64
+ return leg;
65
+ };
66
+ /** The deepest OTHER board under the point that may take the page — never one of the page's own boards. */
67
+ const foreignAt = (wx, wy) => {
68
+ let best = null;
69
+ for (const p of peersOnCanvas()) {
70
+ if (p === selfPeer() || plan.ownBoards.includes(p.group.id))
71
+ continue;
72
+ if (!p.containsWorld(wx, wy))
73
+ continue;
74
+ if (!best || p.frameArea() < best.frameArea())
75
+ best = p;
76
+ }
77
+ return best;
78
+ };
79
+ const naturalSpan = (() => {
80
+ const sp = sizeToSpan(plan.size.width, plan.size.height, frame(), geom(), rows());
81
+ return { w: Math.max(1, Math.min(columns(), sp.w)), h: Math.max(TEAR_OUT_MIN_ROWS, sp.h) };
82
+ })();
83
+ // A DOCKED group takes at most HALF the board — VS Code's edge drop splits
84
+ // the area in two. A page as tall as the container it left would
85
+ // otherwise shove the whole dashboard out of view. Half of the board as
86
+ // it stood at the press, and of what the canvas shows, whichever is less.
87
+ const dockSpan = (() => {
88
+ const rows0 = rows();
89
+ const rect = api.container.getBoundingClientRect();
90
+ const rh = rowHeightFor(geom(), rows0) + gap;
91
+ const visible = rect.height > 0 && rh > 0 ? Math.max(1, Math.floor((rect.height + gap) / rh)) : rows0;
92
+ const halfRows = Math.max(TEAR_OUT_MIN_ROWS, Math.ceil(Math.min(rows0, visible) / 2));
93
+ const halfCols = Math.max(1, Math.ceil(columns() / 2));
94
+ return { w: Math.min(naturalSpan.w, halfCols), h: Math.min(naturalSpan.h, halfRows) };
95
+ })();
96
+ setTearing(pageId);
97
+ const doc = (_a = api.container.ownerDocument) !== null && _a !== void 0 ? _a : document;
98
+ const chip = doc.createElement('div');
99
+ chip.className = 'axdb-drag-chip axdb-tab-chip';
100
+ chip.textContent = plan.label;
101
+ doc.body.appendChild(chip);
102
+ const moveChip = (cx, cy) => {
103
+ chip.style.left = `${cx + 6}px`;
104
+ chip.style.top = `${cy + 6}px`;
105
+ };
106
+ moveChip(ev.clientX, ev.clientY);
107
+ armGlide();
108
+ api.render();
109
+ const layer = htmlLayer();
110
+ const area = (g) => {
111
+ const sz = sizeOf(g);
112
+ return sz.width * sz.height;
113
+ };
114
+ const targets = plan.joinTargets
115
+ .map((id) => diagram.getGroup(id))
116
+ .filter((g) => !!g && g.id !== fromGroupId)
117
+ .sort((a, b) => area(a) - area(b));
118
+ // GEOMETRY MUST NOT MOVE WITH THE GHOST. On a fit board the rows squeeze
119
+ // while the ghost occupies some and relax when it leaves, so a target's
120
+ // frame changes with the very decision being made about it: a join makes
121
+ // the ghost leave, the frame grows, and the same pointer now reads as a
122
+ // split. The target and its frame are therefore anchored when the pointer
123
+ // enters it and kept until it leaves; the board frame is the one at press.
124
+ let anchor = null;
125
+ const inRect = (r, wx, wy) => wx >= r.x && wx <= r.x + r.width && wy >= r.y && wy <= r.y + r.height;
126
+ const targetAt = (wx, wy) => {
127
+ var _a;
128
+ if (anchor && inRect(anchor.frame, wx, wy))
129
+ return anchor.g;
130
+ const t = (_a = targets.find((x) => worldInsideGroup(x, wx, wy))) !== null && _a !== void 0 ? _a : null;
131
+ if (t)
132
+ leg === null || leg === void 0 ? void 0 : leg.leave(); // the ghost leaves first, so the frame anchored is the relaxed one
133
+ anchor = t ? { g: t, frame: frameOfGroup(t), stripH: plan.stripHeight(t.id) } : null;
134
+ return t;
135
+ };
136
+ // THE BANDS ARE ON THE SCREEN. Dockview measures its container edges on
137
+ // the element, and so must we: the camera moves during the drag (the
138
+ // board glides, the ghost adds rows a bounded scroll answers to), so a
139
+ // band fixed in world space at the press drifts away from the edge the
140
+ // user sees. The frame is mapped to client space through the live
141
+ // camera and clipped to the canvas, so a scrolled board's bands sit at
142
+ // its VISIBLE edges.
143
+ const ROOT_TOP = 20;
144
+ const ROOT_BOTTOM = 20;
145
+ const ROOT_SIDE = 40;
146
+ // THE EDGE BANDS ARE A FIFTH OF THE BODY — Dockview's activation size, not
147
+ // VS Code's third. Two thirds of every group read as "beside" under the
148
+ // thirds, and the user, putting a page back into the panel it came from,
149
+ // never found the "into": "close to the edge means beside it, the content
150
+ // area means inside, the header means a new tab" (0.4.42).
151
+ const EDGE_BAND = BESIDE_BAND; // one band for a tab's split and a widget's beside (zones.ts)
152
+ const visibleFrame = () => {
153
+ const rect = api.container.getBoundingClientRect();
154
+ const o = toWorld(rect.left, rect.top);
155
+ const u = toWorld(rect.left + 100, rect.top + 100);
156
+ const sx = 100 / (u.x - o.x || 100);
157
+ const sy = 100 / (u.y - o.y || 100);
158
+ const f = frame();
159
+ const left = rect.left + (f.x - o.x) * sx;
160
+ const top = rect.top + (f.y - o.y) * sy;
161
+ const right = left + f.width * sx;
162
+ const bottom = top + f.height * sy;
163
+ if (rect.width <= 0 || rect.height <= 0)
164
+ return { left, top, right, bottom }; // unlaid-out: nothing to clip to
165
+ return { left: Math.max(left, rect.left), top: Math.max(top, rect.top), right: Math.min(right, rect.right), bottom: Math.min(bottom, rect.bottom) };
166
+ };
167
+ /** Inside the VISIBLE canvas plus the grace band, on the screen — a camera that moved must not turn a pointer outside the canvas into a world point inside the board. */
168
+ const clientInsideCanvasGrace = (cx, cy) => {
169
+ // The CANVAS, not this board's frame: a nested page's binder runs the
170
+ // tear-out of the tabs inside it, and its pages must still land on the
171
+ // boards around it. An unmeasurable container (no layout) is never off.
172
+ const rect = api.container.getBoundingClientRect();
173
+ if (rect.width <= 0 || rect.height <= 0)
174
+ return true;
175
+ return cx >= rect.left - EDGE_GRACE && cx <= rect.right + EDGE_GRACE && cy >= rect.top - EDGE_GRACE && cy <= rect.bottom + EDGE_GRACE;
176
+ };
177
+ /**
178
+ * The board's rows for a dock: as they stood BEFORE the ghost entered.
179
+ * Read live, the ghost's own pushing inflated them on every re-entry —
180
+ * side docks 14, 21, 104 rows tall, a bottom dock landing at row 29.
181
+ */
182
+ const baseRows = () => {
183
+ const b = leg === null || leg === void 0 ? void 0 : leg.baseline();
184
+ if (!b)
185
+ return rowsWithout(plan.arrivingId);
186
+ let r = 0;
187
+ for (const c of b.values())
188
+ r = Math.max(r, c.y + c.h);
189
+ return r;
190
+ };
191
+ const worldToClient = (wx, wy) => {
192
+ const rect = api.container.getBoundingClientRect();
193
+ const o = toWorld(rect.left, rect.top);
194
+ const u = toWorld(rect.left + 100, rect.top + 100);
195
+ return { x: rect.left + (wx - o.x) * (100 / (u.x - o.x || 100)), y: rect.top + (wy - o.y) * (100 / (u.y - o.y || 100)) };
196
+ };
197
+ /**
198
+ * Would the page land where the user cannot see it? A cell whose room
199
+ * lies below the fold (the only cell left under a locked section — D1
200
+ * hold3) used to take the drop a screen away. Hidden = not one pixel of
201
+ * the cell inside the visible canvas: a landing whose top edge shows,
202
+ * the rest below the fold of a small canvas, is a landing the user can
203
+ * see and a grow board scrolls to (lab L61 dropped at the canvas foot).
204
+ */
205
+ const landingHidden = () => {
206
+ const r = leg === null || leg === void 0 ? void 0 : leg.rect();
207
+ const rect = api.container.getBoundingClientRect();
208
+ if (!r || rect.width <= 0 || rect.height <= 0)
209
+ return false;
210
+ const tl = worldToClient(r.x, r.y);
211
+ const br = worldToClient(r.x + r.width, r.y + r.height);
212
+ return tl.y >= rect.bottom || br.y <= rect.top || tl.x >= rect.right || br.x <= rect.left;
213
+ };
214
+ const rootAt = (cx, cy) => {
215
+ // The bands reach a little OUTSIDE the frame too: a pointer that
216
+ // overshoots the board's top edge by a few pixels means "the top".
217
+ const v = visibleFrame();
218
+ if (cx < v.left - ROOT_SIDE || cx > v.right + ROOT_SIDE || cy < v.top - ROOT_TOP || cy > v.bottom + ROOT_BOTTOM)
219
+ return null;
220
+ const rows = Math.max(TEAR_OUT_MIN_ROWS, baseRows());
221
+ const w = dockSpan.w;
222
+ const h = dockSpan.h;
223
+ if (cy - v.top <= ROOT_TOP)
224
+ return { kind: 'root', side: 'top', cell: { x: 0, y: 0, w: columns(), h } };
225
+ if (cx - v.left <= ROOT_SIDE)
226
+ return { kind: 'root', side: 'left', cell: { x: 0, y: 0, w, h: rows } };
227
+ if (v.right - cx <= ROOT_SIDE)
228
+ return { kind: 'root', side: 'right', cell: { x: Math.max(0, columns() - w), y: 0, w, h: rows } };
229
+ if (v.bottom - cy <= ROOT_BOTTOM)
230
+ return { kind: 'root', side: 'bottom', cell: { x: 0, y: rows, w: columns(), h } };
231
+ return null;
232
+ };
233
+ const rowsWithout = (id) => {
234
+ let r = 0;
235
+ for (const it of eng().getItems())
236
+ if (it.id !== id)
237
+ r = Math.max(r, it.y + it.h);
238
+ return r;
239
+ };
240
+ /** A split being previewed: the target shrunk to its half, to be restored when the pointer leaves. */
241
+ let split = null;
242
+ const halves = (target, side) => {
243
+ // The cell to halve is the target's own — not the half it is already
244
+ // shrunk to while a split is being previewed.
245
+ const live = eng().getItem(target.id);
246
+ const it = split && split.id === target.id ? split.before : live;
247
+ if (!it || !live)
248
+ return null; // a group on another board: no cell of ours to halve
249
+ if (side === 'left' || side === 'right') {
250
+ if (it.w < 2)
251
+ return null;
252
+ const a = Math.ceil(it.w / 2);
253
+ const b = it.w - a;
254
+ return side === 'right'
255
+ ? { keep: { x: it.x, y: it.y, w: a, h: it.h }, born: { x: it.x + a, y: it.y, w: b, h: it.h } }
256
+ : { keep: { x: it.x + b, y: it.y, w: a, h: it.h }, born: { x: it.x, y: it.y, w: b, h: it.h } };
257
+ }
258
+ if (it.h < 2 * TEAR_OUT_MIN_ROWS)
259
+ return null;
260
+ const a = Math.ceil(it.h / 2);
261
+ const b = it.h - a;
262
+ return side === 'bottom'
263
+ ? { keep: { x: it.x, y: it.y, w: it.w, h: a }, born: { x: it.x, y: it.y + a, w: it.w, h: b } }
264
+ : { keep: { x: it.x, y: it.y + b, w: it.w, h: a }, born: { x: it.x, y: it.y, w: it.w, h: b } };
265
+ };
266
+ const zoneAt = (cx, cy, world) => {
267
+ // A pointer OUTSIDE the visible canvas is off, full stop: a camera that
268
+ // moved can map it to a world point inside a group, and that group
269
+ // took a join or a split from a release in the page header. The
270
+ // target is looked up (and anchored) only inside it.
271
+ const clientInside = clientInsideCanvasGrace(cx, cy);
272
+ const target = clientInside ? targetAt(world.x, world.y) : null;
273
+ const root = clientInside ? rootAt(cx, cy) : null;
274
+ const foreign = foreignAt(world.x, world.y);
275
+ // The ORDER is zones.ts's, shared with the split board: a strip, the
276
+ // board's own edges, the target's fifth or middle, home, then a board.
277
+ const tz = resolveTabZone({
278
+ x: world.x,
279
+ y: world.y,
280
+ clientInside,
281
+ ownStrip: plan.stripIndex(fromGroupId, cx, cy),
282
+ stripOf: (id) => plan.stripIndex(id, cx, cy),
283
+ root: root && root.kind === 'root' ? { side: root.side } : null,
284
+ target: target
285
+ ? {
286
+ id: target.id,
287
+ frame: anchor && anchor.g === target ? anchor.frame : frameOfGroup(target),
288
+ stripHeight: anchor && anchor.g === target ? anchor.stripH : plan.stripHeight(target.id),
289
+ }
290
+ : null,
291
+ home: worldInsideGroup(from, world.x, world.y) ? frameOfGroup(from) : null,
292
+ homeBand: 0, // on a grid board the whole source frame is home
293
+ band: EDGE_BAND,
294
+ canSplit: (_id, side) => !!target && !!halves(target, side),
295
+ pane: false,
296
+ foreign: !!foreign && (!worldInsideBoard(world.x, world.y) || foreign.frameArea() < boardArea()),
297
+ });
298
+ switch (tz.kind) {
299
+ case 'off':
300
+ return { kind: 'off' };
301
+ case 'strip':
302
+ return { kind: 'strip', target: target, index: tz.index };
303
+ case 'reorder':
304
+ return { kind: 'reorder', index: tz.index };
305
+ case 'root':
306
+ return root;
307
+ case 'join':
308
+ return { kind: 'join', target: target };
309
+ case 'split': {
310
+ const h = halves(target, tz.side);
311
+ return h ? Object.assign({ kind: 'split', target: target, side: tz.side }, h) : { kind: 'join', target: target };
312
+ }
313
+ case 'home':
314
+ return { kind: 'home' };
315
+ default:
316
+ return tz.kind === 'board' && tz.foreign && foreign ? { kind: 'board', peer: foreign } : { kind: 'board' };
317
+ }
318
+ };
319
+ const zoneKey = (z) => JSON.stringify(z, (k, v) => (k === 'target' ? v.id : k === 'peer' ? v.group.id : v));
320
+ // A TOP DOCK INSERTS ROWS. The engine's push cascade resolves the band's
321
+ // collisions one tile at a time and scrambles what stood beneath it (the
322
+ // demo's KPI row came apart, two of its tiles under the chart). VS Code
323
+ // shoves the whole area down intact, so after the ghost takes the band
324
+ // every tile that stood at or below it is put back at its own column,
325
+ // exactly the band's height lower — a translation of a layout without
326
+ // overlaps has none — and comes back the same way when the pointer leaves.
327
+ // The layout translated is the one from BEFORE the ghost entered (the
328
+ // leg's baseline), not the engine's mid-gesture state: the adoption
329
+ // itself pushes tiles about, and a snapshot taken after it would carry
330
+ // that cascade into the insert.
331
+ let inserted = null;
332
+ const insertRows = (cell, l) => {
333
+ if (cell.w < columns())
334
+ return; // a side dock has no whole rows to insert
335
+ const base = l.baseline();
336
+ inserted = base;
337
+ for (const [id, c] of base) {
338
+ const it = eng().getItem(id);
339
+ if (!it)
340
+ continue;
341
+ it.x = c.x;
342
+ it.y = c.y >= cell.y ? c.y + cell.h : c.y;
343
+ }
344
+ };
345
+ const undoInsertRows = () => {
346
+ if (!inserted)
347
+ return;
348
+ leg === null || leg === void 0 ? void 0 : leg.leave(); // the ghost frees its rows first
349
+ for (const [id, c] of inserted) {
350
+ const it = eng().getItem(id);
351
+ if (it) {
352
+ it.x = c.x;
353
+ it.y = c.y;
354
+ }
355
+ }
356
+ inserted = null;
357
+ project();
358
+ };
359
+ let joinEl = null;
360
+ const showOverlay = (r) => {
361
+ if (!layer)
362
+ return;
363
+ if (!joinEl) {
364
+ joinEl = doc.createElement('div');
365
+ joinEl.className = 'axdb-join';
366
+ layer.prepend(joinEl);
367
+ }
368
+ joinEl.style.left = `${r.x}px`;
369
+ joinEl.style.top = `${r.y}px`;
370
+ joinEl.style.width = `${r.width}px`;
371
+ joinEl.style.height = `${r.height}px`;
372
+ };
373
+ const hideOverlay = () => {
374
+ joinEl === null || joinEl === void 0 ? void 0 : joinEl.remove();
375
+ joinEl = null;
376
+ };
377
+ const undoSplitPreview = () => {
378
+ if (!split)
379
+ return;
380
+ const it = eng().getItem(split.id);
381
+ leg === null || leg === void 0 ? void 0 : leg.leave(); // the born half must be free before the target grows back into it
382
+ if (it) {
383
+ if (it.x !== split.before.x || it.y !== split.before.y)
384
+ eng().moveCheck(split.id, split.before.x, split.before.y, { gate: false });
385
+ if (it.w !== split.before.w || it.h !== split.before.h)
386
+ eng().resizeCheck(split.id, split.before.w, split.before.h);
387
+ }
388
+ split = null;
389
+ project();
390
+ };
391
+ const previewSplit = (z, world) => {
392
+ const it = eng().getItem(z.target.id);
393
+ if (!it)
394
+ return false;
395
+ // The target's cell AT REST is the one the commit restores — read it
396
+ // BEFORE the leg enters: the arriving ghost is placed under the pointer
397
+ // first, and a target that is a tile like any other is pushed by it
398
+ // for a moment (tile first, step 3) until it takes its half below.
399
+ const before = { x: it.x, y: it.y, w: it.w, h: it.h };
400
+ const frameBefore = frameOfGroup(z.target);
401
+ const l = ensureLeg(world, null);
402
+ if (!l)
403
+ return false;
404
+ split = { id: z.target.id, before, frameBefore, keep: z.keep };
405
+ if (it.w !== z.keep.w || it.h !== z.keep.h)
406
+ eng().resizeCheck(z.target.id, z.keep.w, z.keep.h);
407
+ const now = eng().getItem(z.target.id);
408
+ if (now && (now.x !== z.keep.x || now.y !== z.keep.y))
409
+ eng().moveCheck(z.target.id, z.keep.x, z.keep.y, { gate: false });
410
+ const ok = l.place(z.born);
411
+ project();
412
+ hidePlaceholder(); // the accent overlay says which half; the grey placeholder under it is noise
413
+ return ok;
414
+ };
415
+ /** The band a dock is promised: the cell it takes — a BOTTOM dock's lies past the last row, so its tint sits on the board's last rows instead. */
416
+ const dockOverlayRect = (z) => {
417
+ const cell = z.side === 'bottom' ? Object.assign(Object.assign({}, z.cell), { y: Math.max(0, z.cell.y - z.cell.h) }) : z.cell;
418
+ return cellToRect(cell, frame(), geom(), rows());
419
+ };
420
+ /** The dock, applied for real at the release: the ghost takes the band, every member group is pushed, a top dock inserts rows. */
421
+ const realizeDock = (z, world) => {
422
+ const l = ensureLeg(world, null);
423
+ if (!l)
424
+ return false;
425
+ l.place(z.cell, true); // DOCKING PUSHES SECTIONS: everything below the band moves down, solid or not
426
+ insertRows(z.cell, l);
427
+ project();
428
+ hidePlaceholder();
429
+ return true;
430
+ };
431
+ let zone = { kind: 'board' };
432
+ let key = zoneKey(zone);
433
+ const applyZone = (z, world) => {
434
+ var _a, _b, _c;
435
+ const k = zoneKey(z);
436
+ const same = k === key;
437
+ key = k;
438
+ zone = z;
439
+ if (same) {
440
+ if (z.kind === 'board')
441
+ (_b = ensureLeg(world, (_a = z.peer) !== null && _a !== void 0 ? _a : null)) === null || _b === void 0 ? void 0 : _b.move(world);
442
+ return;
443
+ }
444
+ undoSplitPreview();
445
+ undoInsertRows();
446
+ plan.markDrop(null, null);
447
+ hideOverlay();
448
+ // A PREVIEW IS AN OVERLAY. A split and a dock used to be applied LIVE
449
+ // while the pointer hovered — the target halved and moved to its other
450
+ // half, a top dock shoved every tile down — so the very group the user
451
+ // was entering jumped away from under the pointer (the fluid demo's
452
+ // side panel went off the screen in one frame as a page came back to
453
+ // it). VS Code, Dockview and Golden Layout tint the half or the band and
454
+ // move nothing until the drop; so does this now: the layout stays where
455
+ // the user sees it, and the halving or the row insert happens on release.
456
+ switch (z.kind) {
457
+ case 'strip':
458
+ leg === null || leg === void 0 ? void 0 : leg.leave();
459
+ showOverlay(frameOfGroup(z.target));
460
+ plan.markDrop(z.target.id, z.index);
461
+ break;
462
+ case 'join':
463
+ leg === null || leg === void 0 ? void 0 : leg.leave();
464
+ showOverlay(frameOfGroup(z.target));
465
+ break;
466
+ case 'split':
467
+ leg === null || leg === void 0 ? void 0 : leg.leave(); // the ghost leaves first, so the half is measured on the relaxed board
468
+ showOverlay(cellToRect(z.born, frame(), geom(), rows()));
469
+ break;
470
+ case 'root':
471
+ leg === null || leg === void 0 ? void 0 : leg.leave();
472
+ showOverlay(dockOverlayRect(z));
473
+ break;
474
+ case 'reorder':
475
+ leg === null || leg === void 0 ? void 0 : leg.leave();
476
+ plan.markDrop(fromGroupId, z.index);
477
+ break;
478
+ case 'home':
479
+ case 'off':
480
+ leg === null || leg === void 0 ? void 0 : leg.leave();
481
+ break;
482
+ case 'board': {
483
+ const l = ensureLeg(world, (_c = z.peer) !== null && _c !== void 0 ? _c : null);
484
+ l === null || l === void 0 ? void 0 : l.enter(world);
485
+ break;
486
+ }
487
+ }
488
+ };
489
+ let last = { x: ev.clientX, y: ev.clientY };
490
+ const detach = () => {
491
+ window.removeEventListener('pointermove', onMove, true);
492
+ window.removeEventListener('pointerup', onUp, true);
493
+ window.removeEventListener('pointercancel', onCancel, true);
494
+ window.removeEventListener('keydown', onKey, true);
495
+ };
496
+ const onMove = (e) => {
497
+ if (disposed())
498
+ return detach();
499
+ last = { x: e.clientX, y: e.clientY };
500
+ moveChip(e.clientX, e.clientY);
501
+ const world = toWorld(e.clientX, e.clientY);
502
+ const z = zoneAt(e.clientX, e.clientY, world);
503
+ applyZone(z, world);
504
+ // Dimmed = a release here does nothing: home, off the board, or a board
505
+ // that refused the ghost (bounded and full).
506
+ chip.classList.toggle('axdb-out', zone.kind === 'home' || zone.kind === 'off' || (zone.kind === 'board' && (!leg || landingHidden())));
507
+ api.render();
508
+ };
509
+ const done = (changed, kind) => {
510
+ var _a;
511
+ disarmGlideSoon();
512
+ enforceBoardHeight();
513
+ persistLayouts();
514
+ api.renderNow();
515
+ (_a = options.onGesture) === null || _a === void 0 ? void 0 : _a.call(options, { type: kind, kind: 'move', nodeId: pageId, changed });
516
+ };
517
+ const finish = (commit) => {
518
+ var _a, _b, _c, _d;
519
+ detach();
520
+ chip.remove();
521
+ hideOverlay();
522
+ plan.markDrop(null, null);
523
+ setTearing(null);
524
+ if (disposed())
525
+ return;
526
+ const world = toWorld(last.x, last.y);
527
+ const z = commit ? zoneAt(last.x, last.y, world) : { kind: 'home' };
528
+ if (z.kind !== 'root')
529
+ undoInsertRows();
530
+ if (!commit || z.kind === 'home' || z.kind === 'off' || (z.kind === 'root' && !ensureLeg(world, null)) || (z.kind === 'board' && (!ensureLeg(world, (_a = z.peer) !== null && _a !== void 0 ? _a : null) || landingHidden()))) {
531
+ undoSplitPreview();
532
+ leg === null || leg === void 0 ? void 0 : leg.abort();
533
+ done(false, 'cancel');
534
+ return;
535
+ }
536
+ if (z.kind === 'reorder') {
537
+ undoSplitPreview();
538
+ leg === null || leg === void 0 ? void 0 : leg.abort();
539
+ const cmds = plan.reorder(z.index);
540
+ const changed = cmds.length > 0 ? execute('Reorder tab', cmds) : false;
541
+ done(changed, changed ? 'commit' : 'cancel');
542
+ return;
543
+ }
544
+ if (z.kind === 'strip' || z.kind === 'join') {
545
+ // JOIN: no cell on this board — the leg is abandoned (its displaced
546
+ // tiles are already home) and the page becomes the target's tab.
547
+ undoSplitPreview();
548
+ const index = z.kind === 'strip' ? z.index : plan.dropIndex(z.target.id, last.x, last.y);
549
+ leg === null || leg === void 0 ? void 0 : leg.abort();
550
+ const planned = plan.join(z.target.id, index, group.id);
551
+ done(execute('Move tab', [...planned.move, ...planned.collapse]), 'commit');
552
+ return;
553
+ }
554
+ if (z.kind === 'split') {
555
+ // SPLIT: the target keeps one half of its cell, the page's new group the
556
+ // other. The hover only tinted the half; the halving happens NOW.
557
+ hideOverlay();
558
+ plan.markDrop(null, null);
559
+ if (!previewSplit(z, world)) {
560
+ // The half cannot be taken (a bounded board with no room for the
561
+ // ghost): the page joins the target instead, as it would have had
562
+ // the target been too small to halve.
563
+ leg === null || leg === void 0 ? void 0 : leg.abort();
564
+ const planned = plan.join(z.target.id, plan.dropIndex(z.target.id, last.x, last.y), group.id);
565
+ done(execute('Move tab', [...planned.move, ...planned.collapse]), 'commit');
566
+ return;
567
+ }
568
+ const fin = (_b = leg === null || leg === void 0 ? void 0 : leg.finalize()) !== null && _b !== void 0 ? _b : null;
569
+ const halved = !!split && !!eng().getItem(z.target.id);
570
+ split = null;
571
+ if (!fin || !halved) {
572
+ leg === null || leg === void 0 ? void 0 : leg.abort();
573
+ done(false, 'cancel');
574
+ return;
575
+ }
576
+ const planned = plan.commands(fin.cell, fin.rect, group.id);
577
+ // The target's halving is in the leg's own deltas: its cell at rest was
578
+ // snapshotted when the ghost entered, and its half is where it stands now.
579
+ const changed = execute('Split group', [...fin.commands, ...planned.move, ...planned.collapse]);
580
+ done(changed, 'commit');
581
+ return;
582
+ }
583
+ // ROOT DOCK: the hover only tinted the band; the ghost takes it NOW. A
584
+ // free cell on the board: the leg already sits where it will land.
585
+ if (z.kind === 'root')
586
+ realizeDock(z, world);
587
+ else if (zoneKey(zone) !== zoneKey(z))
588
+ applyZone(z, world);
589
+ hideOverlay(); // a re-applied zone repaints its preview; the release is not a preview
590
+ plan.markDrop(null, null);
591
+ const fin = (_c = leg === null || leg === void 0 ? void 0 : leg.finalize()) !== null && _c !== void 0 ? _c : null;
592
+ if (!fin) {
593
+ done(false, 'cancel');
594
+ return;
595
+ }
596
+ const planned = plan.commands(fin.cell, fin.rect, (_d = leg === null || leg === void 0 ? void 0 : leg.groupId) !== null && _d !== void 0 ? _d : group.id);
597
+ done(execute(z.kind === 'root' ? 'Dock tab' : 'Move tab out', [...fin.commands, ...planned.move, ...planned.collapse]), 'commit');
598
+ };
599
+ const onUp = () => finish(true);
600
+ const onCancel = () => finish(false);
601
+ const onKey = (e) => {
602
+ if (e.key === 'Escape')
603
+ finish(false);
604
+ };
605
+ window.addEventListener('pointermove', onMove, true);
606
+ window.addEventListener('pointerup', onUp, true);
607
+ window.addEventListener('pointercancel', onCancel, true);
608
+ window.addEventListener('keydown', onKey, true);
609
+ return true;
610
+ };
611
+ return beginTearOut;
612
+ }
613
+ //# sourceMappingURL=tear-out.js.map