report-studio 0.0.1 → 1.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/LICENSE +21 -0
- package/README.md +363 -4
- package/dist/designer-qy59eEux.js +3416 -0
- package/dist/designer.js +2 -0
- package/dist/index.js +2 -4
- package/dist/react-designer.js +41 -0
- package/dist/react.js +58 -2
- package/dist/viewer-xa17tyMJ.js +1721 -0
- package/package.json +65 -18
- package/src/asset/images/icon.png +0 -0
- package/src/asset/images/logo.png +0 -0
- package/src/cli/index.js +282 -0
- package/src/cli/serve.js +135 -0
- package/src/designer/blank.js +53 -0
- package/src/designer/canvas.js +518 -0
- package/src/designer/designer.js +2054 -0
- package/src/designer/dialog.js +708 -0
- package/src/designer/fields.js +633 -0
- package/src/designer/geometry.js +154 -0
- package/src/designer/guides.js +370 -0
- package/src/designer/history.js +113 -0
- package/src/designer/icons.js +123 -0
- package/src/designer/index.html +28 -0
- package/src/designer/main.js +39 -0
- package/src/designer/menu.js +126 -0
- package/src/designer/panel.js +498 -0
- package/src/designer/preview.js +95 -0
- package/src/designer/rulers.js +231 -0
- package/src/designer/sample-data.js +411 -0
- package/src/designer/select.js +699 -0
- package/src/designer/structure.js +685 -0
- package/src/designer/styles.css +1654 -0
- package/src/designer/toast.js +62 -0
- package/src/designer/tokens.js +293 -0
- package/src/engine/csv.js +350 -0
- package/src/engine/group.js +367 -0
- package/src/engine/index.js +35 -0
- package/src/engine/measure.js +391 -0
- package/src/engine/paginate.js +865 -0
- package/src/engine/regions.js +175 -0
- package/src/engine/resolve.js +208 -0
- package/src/engine/search.js +129 -0
- package/src/engine/tables.js +87 -0
- package/src/engine/text-metrics.js +206 -0
- package/src/engine/validate.js +403 -0
- package/src/index.html +79 -0
- package/src/index.js +51 -0
- package/src/preview/chrome.css +505 -0
- package/src/preview/chrome.js +147 -0
- package/src/preview/index.html +28 -0
- package/src/preview/main.js +66 -0
- package/src/preview/styles.css +16 -0
- package/src/preview/viewer.js +763 -0
- package/src/react/designer.js +87 -0
- package/src/react/index.js +117 -0
- package/src/render/items.js +518 -0
- package/src/render/render.js +98 -0
- package/src/render/report.css +232 -0
- package/src/server/routes.js +331 -0
- package/src/shared/brand.css +29 -0
- package/src/shared/dropdown.css +136 -0
- package/src/shared/dropdown.js +166 -0
- package/src/shared/modal.css +125 -0
- package/src/shared/prefix.js +13 -0
- package/src/shared/store.js +145 -0
- package/src/shared/theme.css +72 -0
- package/src/shared/window.js +138 -0
- package/src/vite/index.js +53 -0
|
@@ -0,0 +1,699 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* select.js turns pointer events into calls on geometry.js.
|
|
3
|
+
*
|
|
4
|
+
* Spec 6: drag and resize are written by hand. The logic is genuinely small -
|
|
5
|
+
* record the box on pointerdown, apply the delta on move, commit on up - and
|
|
6
|
+
* the libraries in this space are 50KB, impose their own DOM, and fight you
|
|
7
|
+
* over the thing users judge the product on.
|
|
8
|
+
*
|
|
9
|
+
* One listener set, attached to the canvas and never removed. The canvas
|
|
10
|
+
* survives a redraw; everything inside it does not, so per-item handlers would
|
|
11
|
+
* have to be rebound on every change and would leak the ones that were not.
|
|
12
|
+
*
|
|
13
|
+
* Nothing here does arithmetic. It reads a box, hands it to geometry.js, and
|
|
14
|
+
* writes back what comes out - so the awkward cases are specified in a suite
|
|
15
|
+
* that needs no browser.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
itemBox, moveTo, resizeBy, applyBox, resizableAxes, GRID
|
|
20
|
+
} from './geometry.js';
|
|
21
|
+
import { bandUnder, bandBox } from './canvas.js';
|
|
22
|
+
import { guidesFor, gapsAround, unionBox } from './guides.js';
|
|
23
|
+
import {
|
|
24
|
+
addGuide, moveGuide, removeGuide, guideNear, guidesInBand
|
|
25
|
+
} from './rulers.js';
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Wires selection, dragging and resizing onto a designer canvas.
|
|
30
|
+
*
|
|
31
|
+
* @param {object} options
|
|
32
|
+
* @param {Element} options.canvas the scroll container; survives redraws
|
|
33
|
+
* @param {() => object} options.getLayout
|
|
34
|
+
* @param {() => object|null} options.getSelection
|
|
35
|
+
* @param {(selection: object|null, options?: {add?: boolean}) => void}
|
|
36
|
+
* options.select called when the selection changes; `add` is a shift-click,
|
|
37
|
+
* which adds to the selection rather than replacing it
|
|
38
|
+
* @param {() => void} [options.remove] delete the selected item
|
|
39
|
+
* @param {() => boolean} [options.duplicate] copy the selection in place and
|
|
40
|
+
* leave the copies selected; answers whether anything was copied
|
|
41
|
+
* @param {() => void} options.commit called when an edit finishes
|
|
42
|
+
* @param {() => number} [options.getZoom] page pixels per screen pixel
|
|
43
|
+
* @param {() => boolean} [options.enabled] false while the canvas is showing
|
|
44
|
+
* something that is not the design, such as a rendered report
|
|
45
|
+
* @param {(bands: string[]|null) => void} [options.onTarget] the bands the
|
|
46
|
+
* items being dragged are over, or null while none has left its own
|
|
47
|
+
* @param {(guides: object|null) => void} [options.onGuides] what the selection
|
|
48
|
+
* has lined up with, and - while alt is held - the space around it; null when
|
|
49
|
+
* there is nothing to show
|
|
50
|
+
* @param {() => boolean} [options.rulers] whether the rulers are showing, which
|
|
51
|
+
* is the only time a guide can be dragged off one
|
|
52
|
+
* @returns {() => void} detaches every listener
|
|
53
|
+
*/
|
|
54
|
+
export function attachEditing({
|
|
55
|
+
canvas, getLayout, getSelection, select, commit,
|
|
56
|
+
remove = () => { }, duplicate = () => false,
|
|
57
|
+
getZoom = () => 1, enabled = () => true, onTarget = () => { },
|
|
58
|
+
onGuides = () => { }, rulers = () => false
|
|
59
|
+
}) {
|
|
60
|
+
/** the drag in progress, or null */
|
|
61
|
+
let drag = null;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A serial number for guide gestures.
|
|
65
|
+
*
|
|
66
|
+
* history.js collapses consecutive edits that share a key, which is what
|
|
67
|
+
* makes a whole drag one undo step. Every guide gesture sharing the *same*
|
|
68
|
+
* key made every guide edit of the whole session one undo step, so one undo
|
|
69
|
+
* took away the lot. The key has to be per gesture, not per kind of gesture.
|
|
70
|
+
*/
|
|
71
|
+
let gesture = 0;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Whether alt is down, which is the gesture for "show me the distances".
|
|
75
|
+
*
|
|
76
|
+
* Held rather than toggled, and off by default, because a measurement is
|
|
77
|
+
* something you ask for at the moment you want it. Four numbers on screen
|
|
78
|
+
* for the whole of every drag is a heads-up display, and the report being
|
|
79
|
+
* designed is the thing someone is trying to look at.
|
|
80
|
+
*
|
|
81
|
+
* The same key duplicates when it is already down as the drag starts (see
|
|
82
|
+
* onPointerDown). That is not a clash - it is the pair of gestures alt has
|
|
83
|
+
* in every design tool, and which one you get is decided by whether the
|
|
84
|
+
* pointer was already down.
|
|
85
|
+
*/
|
|
86
|
+
let measuring = false;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The document the screen is mounted in, which is not always this one: a
|
|
90
|
+
* designer or a viewer opened in its own window lives in that window's
|
|
91
|
+
* document, and a listener put on the opener's would never hear it.
|
|
92
|
+
*/
|
|
93
|
+
const doc = canvas.ownerDocument;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The pointer's position in the printable area's coordinates - the ones the
|
|
97
|
+
* rulers are marked in, and therefore the ones a guide is stored in.
|
|
98
|
+
*
|
|
99
|
+
* Measured off the page element rather than computed from the layout: the
|
|
100
|
+
* sheet is centred in a scrolling canvas, so where it actually is on screen
|
|
101
|
+
* is a fact only the browser has.
|
|
102
|
+
*
|
|
103
|
+
* @param {PointerEvent} event
|
|
104
|
+
* @returns {{x: number, y: number}|null} null when the page is not drawn
|
|
105
|
+
*/
|
|
106
|
+
function paperPoint(event) {
|
|
107
|
+
const page = canvas.querySelector('.dz-page');
|
|
108
|
+
if (!page) return null;
|
|
109
|
+
|
|
110
|
+
const box = page.getBoundingClientRect();
|
|
111
|
+
const zoom = getZoom() || 1;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The page draws its margins as padding, and an item's origin is inside
|
|
115
|
+
* them - so the padding is what turns a point on the paper into a point
|
|
116
|
+
* in the printable area.
|
|
117
|
+
*/
|
|
118
|
+
const style = doc.defaultView?.getComputedStyle(page);
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
x: (event.clientX - box.left) / zoom - parseFloat(style?.paddingLeft || 0),
|
|
122
|
+
y: (event.clientY - box.top) / zoom - parseFloat(style?.paddingTop || 0)
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
function bandOf(el) {
|
|
128
|
+
return el?.closest('[data-band-type]')?.dataset.bandType ?? null;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function itemAt(el) {
|
|
132
|
+
const node = el?.closest('[data-item-id]');
|
|
133
|
+
if (!node) return null;
|
|
134
|
+
|
|
135
|
+
const band = bandOf(node);
|
|
136
|
+
if (!band) return null;
|
|
137
|
+
|
|
138
|
+
return { band, id: node.dataset.itemId, node };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** the layout item behind one `{ band, id }`, or null if it has since gone */
|
|
142
|
+
function lookup(one) {
|
|
143
|
+
if (!one) return null;
|
|
144
|
+
|
|
145
|
+
const band = (getLayout().bands || []).find(b => b.type === one.band);
|
|
146
|
+
return (band?.items || []).find(i => i.id === one.id) ?? null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Every box in a band except the ones being dragged - what the drag can
|
|
152
|
+
* line itself up against.
|
|
153
|
+
*
|
|
154
|
+
* @param {string} bandType
|
|
155
|
+
* @param {object[]} moving the drag group, whose members align to nothing
|
|
156
|
+
* @returns {object[]} boxes, in band coordinates
|
|
157
|
+
*/
|
|
158
|
+
function neighbours(bandType, moving) {
|
|
159
|
+
const band = (getLayout().bands || []).find(b => b.type === bandType);
|
|
160
|
+
const dragged = new Set(moving.map(entry => entry.where.id));
|
|
161
|
+
|
|
162
|
+
return (band?.items || [])
|
|
163
|
+
.filter(item => !dragged.has(item.id))
|
|
164
|
+
.map(itemBox);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
/** everything currently selected, as `{ where, item }` */
|
|
169
|
+
function chosen() {
|
|
170
|
+
return (getSelection() || [])
|
|
171
|
+
.map(where => ({ where, item: lookup(where) }))
|
|
172
|
+
.filter(entry => entry.item);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
/** the one the handles belong to - the most recently selected */
|
|
177
|
+
function head() {
|
|
178
|
+
return chosen().at(-1) ?? null;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function onPointerDown(event) {
|
|
182
|
+
if (!enabled() || event.button !== 0) return;
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* A ruler and a guide are asked about first, and a guide before the item
|
|
186
|
+
* under it: a guide is one pixel of line lying on top of the design, and
|
|
187
|
+
* anything that grabbed the item instead would make it impossible to
|
|
188
|
+
* pick up. The ruler is not part of the page at all.
|
|
189
|
+
*/
|
|
190
|
+
if (rulers() && beginGuide(event)) return;
|
|
191
|
+
|
|
192
|
+
const handle = event.target.closest?.('[data-handle]');
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* A handle belongs to the current selection, not to whatever is drawn
|
|
196
|
+
* underneath it - the outline overlaps its neighbours by design.
|
|
197
|
+
*/
|
|
198
|
+
if (handle) {
|
|
199
|
+
const entry = head();
|
|
200
|
+
if (!entry) return;
|
|
201
|
+
|
|
202
|
+
begin(event, {
|
|
203
|
+
mode: 'resize',
|
|
204
|
+
handle: handle.dataset.handle,
|
|
205
|
+
item: entry.item
|
|
206
|
+
});
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const hit = itemAt(event.target);
|
|
211
|
+
|
|
212
|
+
if (!hit) {
|
|
213
|
+
select(null);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const where = { band: hit.band, id: hit.id };
|
|
218
|
+
const already = (getSelection() || [])
|
|
219
|
+
.some(one => one.band === where.band && one.id === where.id);
|
|
220
|
+
|
|
221
|
+
if (event.shiftKey) {
|
|
222
|
+
/**
|
|
223
|
+
* Shift adds, and takes out again what was already in - so a
|
|
224
|
+
* mis-click is undone with the same gesture rather than by starting
|
|
225
|
+
* the whole selection over.
|
|
226
|
+
*/
|
|
227
|
+
select(where, { add: true });
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Alt drags a copy and leaves the original where it is - the gesture
|
|
233
|
+
* every design tool has. Checked after shift so that shift-alt still
|
|
234
|
+
* extends the selection, and before the drag begins so that what the
|
|
235
|
+
* pointer carries away is the copy.
|
|
236
|
+
*/
|
|
237
|
+
if (event.altKey) {
|
|
238
|
+
if (!already) select(where);
|
|
239
|
+
|
|
240
|
+
if (duplicate()) {
|
|
241
|
+
begin(event, { mode: 'move', collapseTo: null });
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Clicking one of several already-selected items keeps the group for
|
|
248
|
+
* now, so the group can be picked up by grabbing any of its members.
|
|
249
|
+
*
|
|
250
|
+
* If the pointer then goes up without moving, it was a click and not a
|
|
251
|
+
* drag, and it collapses to the one item - which is how a group is
|
|
252
|
+
* broken up, and how its properties are reached.
|
|
253
|
+
*/
|
|
254
|
+
if (!already) select(where);
|
|
255
|
+
|
|
256
|
+
begin(event, { mode: 'move', collapseTo: already ? where : null });
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Starts a guide gesture, if the pointer went down on one or on a ruler.
|
|
261
|
+
*
|
|
262
|
+
* Dragging off a ruler makes a guide; dragging one already on the page moves
|
|
263
|
+
* it; dragging one off the page throws it away, which is the gesture every
|
|
264
|
+
* tool with rulers has and the only one nobody has to be taught.
|
|
265
|
+
*
|
|
266
|
+
* @param {PointerEvent} event
|
|
267
|
+
* @returns {boolean} whether it took the gesture
|
|
268
|
+
*/
|
|
269
|
+
function beginGuide(event) {
|
|
270
|
+
const ruler = event.target.closest?.('[data-role="ruler"]');
|
|
271
|
+
const point = paperPoint(event);
|
|
272
|
+
|
|
273
|
+
if (!point) return false;
|
|
274
|
+
|
|
275
|
+
if (ruler) {
|
|
276
|
+
const axis = ruler.dataset.axis;
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Created on the way down rather than on release, so what is being
|
|
280
|
+
* dragged is a real guide from the first pixel - and letting go
|
|
281
|
+
* without moving leaves one where it was pressed, which is a
|
|
282
|
+
* legitimate way to place one exactly.
|
|
283
|
+
*/
|
|
284
|
+
const at = addGuide(getLayout(), axis, point[axis]);
|
|
285
|
+
if (at === null) return false;
|
|
286
|
+
|
|
287
|
+
drag = {
|
|
288
|
+
mode: 'guide', axis, at, created: true, moved: false,
|
|
289
|
+
key: `guide:${++gesture}`
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
capture(event);
|
|
293
|
+
commit({ live: true, key: drag.key });
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* On the page. The line itself is the handle, but a one-pixel target is
|
|
299
|
+
* no target - guideNear is what gives it something to grab.
|
|
300
|
+
*/
|
|
301
|
+
if (!event.target.closest?.('.dz-page')) return false;
|
|
302
|
+
|
|
303
|
+
for (const axis of ['x', 'y']) {
|
|
304
|
+
const at = guideNear(getLayout(), axis, point[axis]);
|
|
305
|
+
if (at === null) continue;
|
|
306
|
+
|
|
307
|
+
drag = {
|
|
308
|
+
mode: 'guide', axis, at, created: false, moved: false,
|
|
309
|
+
key: `guide:${++gesture}`
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
capture(event);
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
/** the pointer plumbing every gesture shares */
|
|
321
|
+
function capture(event) {
|
|
322
|
+
canvas.setPointerCapture?.(event.pointerId);
|
|
323
|
+
canvas.classList.add('is-dragging');
|
|
324
|
+
event.preventDefault();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
function begin(event, what) {
|
|
329
|
+
/**
|
|
330
|
+
* Every selected item's starting box, so the whole group moves by one
|
|
331
|
+
* delta rather than each item accumulating its own rounding.
|
|
332
|
+
*/
|
|
333
|
+
const group = what.mode === 'move'
|
|
334
|
+
? chosen().map(entry => ({ ...entry, box: itemBox(entry.item) }))
|
|
335
|
+
: [];
|
|
336
|
+
|
|
337
|
+
drag = {
|
|
338
|
+
...what,
|
|
339
|
+
group,
|
|
340
|
+
band: head()?.where.band ?? null,
|
|
341
|
+
target: null,
|
|
342
|
+
startX: event.clientX,
|
|
343
|
+
startY: event.clientY,
|
|
344
|
+
box: what.item ? itemBox(what.item) : null,
|
|
345
|
+
moved: false
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Capture on the canvas, so a pointer that leaves the page - or the
|
|
350
|
+
* window - keeps reporting. Without it a fast drag ends wherever the
|
|
351
|
+
* cursor crossed the edge and the item is left mid-gesture.
|
|
352
|
+
*/
|
|
353
|
+
capture(event);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function onPointerMove(event) {
|
|
357
|
+
if (!drag) return;
|
|
358
|
+
|
|
359
|
+
if (drag.mode === 'guide') {
|
|
360
|
+
const point = paperPoint(event);
|
|
361
|
+
if (!point) return;
|
|
362
|
+
|
|
363
|
+
drag.moved = true;
|
|
364
|
+
drag.at = moveGuide(getLayout(), drag.axis, drag.at, point[drag.axis]);
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Live, so the sheet is not rebuilt sixty times a second for a line
|
|
368
|
+
* that moved - the same reason an item drag patches. Under a key of
|
|
369
|
+
* its own, so the whole gesture is one undo step and it does not
|
|
370
|
+
* merge with the item move that may have come before it.
|
|
371
|
+
*/
|
|
372
|
+
commit({ live: true, key: drag.key });
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const zoom = getZoom() || 1;
|
|
377
|
+
const dx = (event.clientX - drag.startX) / zoom;
|
|
378
|
+
const dy = (event.clientY - drag.startY) / zoom;
|
|
379
|
+
|
|
380
|
+
setMeasuring(event.altKey);
|
|
381
|
+
|
|
382
|
+
if (!drag.moved && Math.abs(dx) < 1 && Math.abs(dy) < 1) return;
|
|
383
|
+
drag.moved = true;
|
|
384
|
+
|
|
385
|
+
if (drag.mode === 'resize') {
|
|
386
|
+
applyBox(drag.item, resizeBy(
|
|
387
|
+
drag.handle, drag.box, dx, dy, resizableAxes(drag.item)));
|
|
388
|
+
|
|
389
|
+
commit({ live: true });
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Every item moves by the same delta from its own starting box, so a
|
|
395
|
+
* group keeps its shape - applying the delta to wherever each item has
|
|
396
|
+
* got to would let rounding pull them apart over a long drag.
|
|
397
|
+
*/
|
|
398
|
+
for (const entry of drag.group) {
|
|
399
|
+
applyBox(entry.item, { ...entry.box, ...moveTo(entry.box, dx, dy) });
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Then the correction, from whatever the group has come to rest nearly
|
|
404
|
+
* level with. It is applied on top of the grid rather than instead of
|
|
405
|
+
* it: the grid is what a drag does in open space, and an item beside
|
|
406
|
+
* something is being lined up with the something, not with the grid.
|
|
407
|
+
*
|
|
408
|
+
* Worked out from the boxes rather than from the pointer, and applied
|
|
409
|
+
* to the whole group as one delta, so the group keeps its shape here
|
|
410
|
+
* too - and because the move above always starts from `entry.box`, a
|
|
411
|
+
* snap can be dragged out of again without it accumulating.
|
|
412
|
+
*/
|
|
413
|
+
showGuides(drag.group);
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Which band each is over now. Worked out from the items' own positions
|
|
417
|
+
* rather than the pointer's, so it needs no measurement of the page and
|
|
418
|
+
* agrees exactly with where they are drawn.
|
|
419
|
+
*/
|
|
420
|
+
const layout = getLayout();
|
|
421
|
+
const over = new Set();
|
|
422
|
+
|
|
423
|
+
for (const entry of drag.group) {
|
|
424
|
+
const to = bandUnder(layout, entry.where.band, entry.item);
|
|
425
|
+
if (to && to !== entry.where.band) over.add(to);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
drag.target = over.size ? [...over] : null;
|
|
429
|
+
onTarget(drag.target);
|
|
430
|
+
|
|
431
|
+
commit({ live: true });
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Lines the dragged group up with its neighbours and says what it found.
|
|
436
|
+
*
|
|
437
|
+
* The guides themselves are always drawn: they are the explanation for the
|
|
438
|
+
* item having jumped the last few pixels on its own, and a snap nobody can
|
|
439
|
+
* see is a drag that will not go where it is put. The measurements are the
|
|
440
|
+
* ones alt asks for.
|
|
441
|
+
*
|
|
442
|
+
* @param {object[]} group the drag's entries, already moved by the pointer
|
|
443
|
+
*/
|
|
444
|
+
function showGuides(group) {
|
|
445
|
+
const band = drag?.band ? bandBox(getLayout(), drag.band) : null;
|
|
446
|
+
|
|
447
|
+
if (!band || !group.length) {
|
|
448
|
+
onGuides(null);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const others = neighbours(drag.band, group);
|
|
453
|
+
|
|
454
|
+
const found = guidesFor(
|
|
455
|
+
group.map(entry => itemBox(entry.item)), others, band,
|
|
456
|
+
{ guides: rulers() ? guidesInBand(getLayout(), band) : undefined });
|
|
457
|
+
|
|
458
|
+
if (found.dx || found.dy) {
|
|
459
|
+
for (const entry of group) {
|
|
460
|
+
applyBox(entry.item, {
|
|
461
|
+
...itemBox(entry.item),
|
|
462
|
+
x: (entry.item.x ?? 0) + found.dx,
|
|
463
|
+
y: (entry.item.y ?? 0) + found.dy
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
onGuides({
|
|
469
|
+
band: drag.band,
|
|
470
|
+
lines: found.lines,
|
|
471
|
+
gaps: measuring ? found.gaps : []
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* The distances around the selection, without touching it.
|
|
478
|
+
*
|
|
479
|
+
* This is alt held over something already placed - the question is how far
|
|
480
|
+
* it is from its neighbours, not where it should go - so nothing is snapped
|
|
481
|
+
* and no alignment guide is drawn. Measuring a thing must not move it.
|
|
482
|
+
*/
|
|
483
|
+
function measureSelection() {
|
|
484
|
+
const group = chosen();
|
|
485
|
+
const where = group.at(-1)?.where ?? null;
|
|
486
|
+
const band = where ? bandBox(getLayout(), where.band) : null;
|
|
487
|
+
|
|
488
|
+
if (!band || !group.length) {
|
|
489
|
+
onGuides(null);
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const boxes = group.map(entry => itemBox(entry.item));
|
|
494
|
+
|
|
495
|
+
onGuides({
|
|
496
|
+
band: where.band,
|
|
497
|
+
lines: [],
|
|
498
|
+
gaps: gapsAround(unionBox(boxes), neighbours(where.band, group), band)
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Redraws whatever the gesture in progress is entitled to show. Called
|
|
505
|
+
* whenever alt changes, from either side of a drag: during one the group is
|
|
506
|
+
* what is being measured, and outside one the selection is.
|
|
507
|
+
*/
|
|
508
|
+
function refreshGuides() {
|
|
509
|
+
if (drag?.mode === 'move') {
|
|
510
|
+
showGuides(drag.group);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (measuring) {
|
|
515
|
+
measureSelection();
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
onGuides(null);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Records whether alt is down.
|
|
525
|
+
*
|
|
526
|
+
* It is read off the pointer as well as off the keyboard: a key pressed
|
|
527
|
+
* while the pointer is captured does not reliably arrive as a keystroke on
|
|
528
|
+
* the document, and every pointer event carries the modifier anyway - so
|
|
529
|
+
* the pointer is the better source during a drag, and the keyboard covers
|
|
530
|
+
* the case where the pointer is not moving.
|
|
531
|
+
*
|
|
532
|
+
* @param {boolean} held
|
|
533
|
+
* @returns {boolean} whether it changed
|
|
534
|
+
*/
|
|
535
|
+
function setMeasuring(held) {
|
|
536
|
+
if (held === measuring) return false;
|
|
537
|
+
|
|
538
|
+
measuring = held;
|
|
539
|
+
return true;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
function onPointerUp(event) {
|
|
544
|
+
if (!drag) return;
|
|
545
|
+
|
|
546
|
+
canvas.releasePointerCapture?.(event.pointerId);
|
|
547
|
+
canvas.classList.remove('is-dragging');
|
|
548
|
+
|
|
549
|
+
if (drag.mode === 'guide') {
|
|
550
|
+
const { axis, at, key } = drag;
|
|
551
|
+
drag = null;
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Off the printable area is how a guide is thrown away - dragged
|
|
555
|
+
* back over the ruler it came from, or past the far edge. A guide
|
|
556
|
+
* outside the page cannot line anything up with anything.
|
|
557
|
+
*/
|
|
558
|
+
const span = paperSpan(axis);
|
|
559
|
+
|
|
560
|
+
if (at === null || at < 0 || (span !== null && at > span)) {
|
|
561
|
+
removeGuide(getLayout(), axis, at);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
commit({ live: false, key });
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
const moved = drag.moved;
|
|
569
|
+
const landing = drag.target;
|
|
570
|
+
const collapseTo = drag.collapseTo;
|
|
571
|
+
drag = null;
|
|
572
|
+
|
|
573
|
+
onTarget(null);
|
|
574
|
+
setMeasuring(event.altKey);
|
|
575
|
+
|
|
576
|
+
if (moved) {
|
|
577
|
+
commit({ live: false, band: landing });
|
|
578
|
+
} else if (collapseTo) {
|
|
579
|
+
/** a click on a member of a group, that turned out not to be a drag */
|
|
580
|
+
select(collapseTo);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Last, because both of those redraw the sheet and the guides live in a
|
|
585
|
+
* layer of it. Alt still being down is still a question being asked -
|
|
586
|
+
* the mouse came up, the key did not - so the answer is drawn again for
|
|
587
|
+
* whatever the selection has become.
|
|
588
|
+
*/
|
|
589
|
+
refreshGuides();
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* How far the printable area runs on one axis, for deciding whether a guide
|
|
594
|
+
* has been dragged off it.
|
|
595
|
+
*
|
|
596
|
+
* @param {'x'|'y'} axis
|
|
597
|
+
* @returns {number|null} null when the page is not drawn
|
|
598
|
+
*/
|
|
599
|
+
function paperSpan(axis) {
|
|
600
|
+
const page = getLayout()?.page;
|
|
601
|
+
if (!page?.margin) return null;
|
|
602
|
+
|
|
603
|
+
return axis === 'x'
|
|
604
|
+
? page.width - page.margin.left - page.margin.right
|
|
605
|
+
: page.height - page.margin.top - page.margin.bottom;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Nudge. A designer without arrow keys feels broken the first time someone
|
|
611
|
+
* wants a box one pixel over, and the grid step is what the drag uses, so
|
|
612
|
+
* the two agree.
|
|
613
|
+
*/
|
|
614
|
+
function onKeyDown(event) {
|
|
615
|
+
if (!enabled()) return;
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* A keystroke aimed at the properties rail is not aimed at the canvas.
|
|
619
|
+
* Checked first, and for escape too: dropping the selection while
|
|
620
|
+
* someone is typing into it would take the rail out from under them.
|
|
621
|
+
*/
|
|
622
|
+
if (event.target.closest?.('input, textarea, select')) return;
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* Alt on its own asks to see the distances rather than to do anything,
|
|
626
|
+
* so the keystroke is swallowed - Windows would otherwise read it as a
|
|
627
|
+
* reach for the browser's menu bar and take the focus off the canvas.
|
|
628
|
+
*/
|
|
629
|
+
if (event.key === 'Alt') {
|
|
630
|
+
if (setMeasuring(true)) refreshGuides();
|
|
631
|
+
event.preventDefault();
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
if (event.key === 'Escape') {
|
|
636
|
+
select(null);
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
if (event.key === 'Delete' || event.key === 'Backspace') {
|
|
641
|
+
if (!chosen().length) return;
|
|
642
|
+
|
|
643
|
+
event.preventDefault();
|
|
644
|
+
remove();
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
const step = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }[event.key];
|
|
649
|
+
if (!step) return;
|
|
650
|
+
|
|
651
|
+
const group = chosen();
|
|
652
|
+
if (!group.length) return;
|
|
653
|
+
|
|
654
|
+
const scale = event.shiftKey ? GRID : 1;
|
|
655
|
+
|
|
656
|
+
for (const { item } of group) {
|
|
657
|
+
item.x = (item.x ?? 0) + step[0] * scale;
|
|
658
|
+
item.y = (item.y ?? 0) + step[1] * scale;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
event.preventDefault();
|
|
662
|
+
commit({ live: false });
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
function onKeyUp(event) {
|
|
666
|
+
if (event.key !== 'Alt') return;
|
|
667
|
+
|
|
668
|
+
if (setMeasuring(false)) refreshGuides();
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* A window that loses the focus never sees the key come up - alt-tab is the
|
|
674
|
+
* obvious way to lose it - and the measurements would be left on screen
|
|
675
|
+
* with nobody holding anything down.
|
|
676
|
+
*/
|
|
677
|
+
function onBlur() {
|
|
678
|
+
if (setMeasuring(false)) refreshGuides();
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
canvas.addEventListener('pointerdown', onPointerDown);
|
|
683
|
+
canvas.addEventListener('pointermove', onPointerMove);
|
|
684
|
+
canvas.addEventListener('pointerup', onPointerUp);
|
|
685
|
+
canvas.addEventListener('pointercancel', onPointerUp);
|
|
686
|
+
doc.addEventListener('keydown', onKeyDown);
|
|
687
|
+
doc.addEventListener('keyup', onKeyUp);
|
|
688
|
+
doc.defaultView?.addEventListener('blur', onBlur);
|
|
689
|
+
|
|
690
|
+
return function detach() {
|
|
691
|
+
canvas.removeEventListener('pointerdown', onPointerDown);
|
|
692
|
+
canvas.removeEventListener('pointermove', onPointerMove);
|
|
693
|
+
canvas.removeEventListener('pointerup', onPointerUp);
|
|
694
|
+
canvas.removeEventListener('pointercancel', onPointerUp);
|
|
695
|
+
doc.removeEventListener('keydown', onKeyDown);
|
|
696
|
+
doc.removeEventListener('keyup', onKeyUp);
|
|
697
|
+
doc.defaultView?.removeEventListener('blur', onBlur);
|
|
698
|
+
};
|
|
699
|
+
}
|