@valbuild/react 0.154.0 → 0.154.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.
Files changed (55) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/analytics/dist/valbuild-react-analytics.browser.esm.js +2 -1
  3. package/analytics/dist/valbuild-react-analytics.cjs.dev.js +6 -5
  4. package/analytics/dist/valbuild-react-analytics.cjs.prod.js +6 -5
  5. package/analytics/dist/valbuild-react-analytics.esm.js +2 -1
  6. package/analytics/dist/valbuild-react-analytics.worker.esm.js +2 -1
  7. package/dist/ValComponentEditorChrome-440d086a.cjs.dev.js +966 -0
  8. package/dist/ValComponentEditorChrome-4ac9dc79.worker.esm.js +952 -0
  9. package/dist/ValComponentEditorChrome-93321513.esm.js +952 -0
  10. package/dist/ValComponentEditorChrome-bf2fefc6.browser.esm.js +952 -0
  11. package/dist/ValComponentEditorChrome-c0b30556.cjs.js +7 -0
  12. package/dist/ValComponentEditorChrome-c0b30556.cjs.prod.js +966 -0
  13. package/dist/ValPreviewBridgeCore-00015d00.esm.js +157 -0
  14. package/dist/ValPreviewBridgeCore-023ffffb.browser.esm.js +157 -0
  15. package/dist/ValPreviewBridgeCore-9b984cb0.cjs.js +7 -0
  16. package/dist/ValPreviewBridgeCore-9b984cb0.cjs.prod.js +161 -0
  17. package/dist/ValPreviewBridgeCore-bbedf957.cjs.dev.js +161 -0
  18. package/dist/ValPreviewBridgeCore-e0d5a840.worker.esm.js +157 -0
  19. package/dist/{createForOfIteratorHelper-598a2f85.cjs.prod.js → createForOfIteratorHelper-6f9dc6fa.cjs.prod.js} +0 -17
  20. package/dist/{createForOfIteratorHelper-522d616d.cjs.dev.js → createForOfIteratorHelper-8a871dc3.cjs.dev.js} +0 -17
  21. package/dist/{createForOfIteratorHelper-595d57b7.esm.js → createForOfIteratorHelper-8bd0009a.browser.esm.js} +2 -18
  22. package/dist/{createForOfIteratorHelper-548f1578.worker.esm.js → createForOfIteratorHelper-93215c75.worker.esm.js} +2 -18
  23. package/dist/{createForOfIteratorHelper-3316a633.browser.esm.js → createForOfIteratorHelper-9b22d0cd.esm.js} +2 -18
  24. package/dist/declarations/src/internal/ValComponentEditor.d.ts +54 -0
  25. package/dist/declarations/src/internal/ValPreviewBridgeCore.d.ts +22 -0
  26. package/dist/declarations/src/internal/componentPreview.d.ts +39 -0
  27. package/dist/declarations/src/internal/index.d.ts +3 -0
  28. package/dist/declarations/src/read/jsonValues.d.ts +2 -0
  29. package/dist/declarations/src/stega/stegaEncode.d.ts +9 -218
  30. package/dist/objectSpread2-3c87fb4f.cjs.prod.js +52 -0
  31. package/dist/objectSpread2-69569a13.worker.esm.js +48 -0
  32. package/dist/objectSpread2-792eb2c2.cjs.dev.js +52 -0
  33. package/dist/objectSpread2-af6b2a44.browser.esm.js +48 -0
  34. package/dist/objectSpread2-be6bfc61.esm.js +48 -0
  35. package/dist/{raw-15ce77ae.browser.esm.js → raw-0561be8e.browser.esm.js} +81 -90
  36. package/dist/{raw-d504f4fd.cjs.prod.js → raw-a69a8d13.cjs.prod.js} +80 -92
  37. package/dist/{raw-534fcde0.esm.js → raw-aa7a3ac4.esm.js} +81 -90
  38. package/dist/{raw-67d24dd6.cjs.dev.js → raw-de032c12.cjs.dev.js} +80 -92
  39. package/dist/{raw-645a854a.worker.esm.js → raw-f32dca84.worker.esm.js} +81 -90
  40. package/dist/toConsumableArray-203e636a.cjs.prod.js +21 -0
  41. package/dist/toConsumableArray-257d6741.esm.js +19 -0
  42. package/dist/toConsumableArray-2fc46f85.browser.esm.js +19 -0
  43. package/dist/toConsumableArray-d7cf1601.cjs.dev.js +21 -0
  44. package/dist/toConsumableArray-e4f48095.worker.esm.js +19 -0
  45. package/internal/dist/valbuild-react-internal.browser.esm.js +405 -7
  46. package/internal/dist/valbuild-react-internal.cjs.dev.js +430 -24
  47. package/internal/dist/valbuild-react-internal.cjs.prod.js +430 -24
  48. package/internal/dist/valbuild-react-internal.esm.js +405 -7
  49. package/internal/dist/valbuild-react-internal.worker.esm.js +405 -7
  50. package/package.json +4 -4
  51. package/stega/dist/valbuild-react-stega.browser.esm.js +5 -3
  52. package/stega/dist/valbuild-react-stega.cjs.dev.js +11 -9
  53. package/stega/dist/valbuild-react-stega.cjs.prod.js +11 -9
  54. package/stega/dist/valbuild-react-stega.esm.js +5 -3
  55. package/stega/dist/valbuild-react-stega.worker.esm.js +5 -3
@@ -0,0 +1,966 @@
1
+ 'use client';
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var objectSpread2 = require('./objectSpread2-792eb2c2.cjs.dev.js');
7
+ var createForOfIteratorHelper = require('./createForOfIteratorHelper-8a871dc3.cjs.dev.js');
8
+ var slicedToArray = require('./slicedToArray-387314c6.cjs.dev.js');
9
+ var core = require('@valbuild/core');
10
+ var client = require('@valbuild/shared/client');
11
+ var React = require('react');
12
+ var ReactJSXRuntime = require('react/jsx-runtime');
13
+
14
+ /** On the element around a list, with the list's path once there is one. */
15
+ var VAL_COMPONENT_LIST_ATTRIBUTE = "data-val-component-list";
16
+
17
+ /**
18
+ * `true` where a list must not be arranged although the document is inside
19
+ * the Studio: the preview of ONE component beside its fields
20
+ * (`ValPreviewBridgeCore`). That frame is in the Studio too, so it looks like
21
+ * the canvas from the inside, but nothing there is listening for an edit to
22
+ * a list - the controls would be buttons that do nothing.
23
+ */
24
+ var ValComponentControlsOff = /*#__PURE__*/React.createContext(false);
25
+
26
+ /** A rectangle in the viewport: what `getBoundingClientRect` gives. */
27
+
28
+ /**
29
+ * Whether a list runs across rather than down: its first two items sit side
30
+ * by side. A list of one has no direction, and down is the answer that costs
31
+ * nothing when it is wrong.
32
+ */
33
+ function isHorizontal(rects) {
34
+ var _rects = slicedToArray._slicedToArray(rects, 2),
35
+ first = _rects[0],
36
+ second = _rects[1];
37
+ if (first === undefined || second === undefined) {
38
+ return false;
39
+ }
40
+ return second.left >= first.left + first.width / 2;
41
+ }
42
+
43
+ /**
44
+ * How far in from an item's edge its outline can be grabbed to drag it. Wider
45
+ * than the 2px outline that is drawn, so it can be hit, and narrow enough to
46
+ * leave what the component has near its edge clickable.
47
+ */
48
+ var BORDER_GRAB = 8;
49
+
50
+ /** Marks the strips along an item's outline that start a drag. */
51
+ var VAL_COMPONENT_BORDER_ATTRIBUTE = "data-val-component-border";
52
+
53
+ /**
54
+ * The four strips along the inside of an item's outline. A side shorter than
55
+ * twice the grab width is all strip, which is still only the item itself.
56
+ */
57
+ function borderStrips(rect) {
58
+ var grabX = Math.min(BORDER_GRAB, rect.width / 2);
59
+ var grabY = Math.min(BORDER_GRAB, rect.height / 2);
60
+ return [{
61
+ edge: "top",
62
+ style: {
63
+ top: rect.top,
64
+ left: rect.left,
65
+ width: rect.width,
66
+ height: grabY
67
+ }
68
+ }, {
69
+ edge: "bottom",
70
+ style: {
71
+ top: rect.top + rect.height - grabY,
72
+ left: rect.left,
73
+ width: rect.width,
74
+ height: grabY
75
+ }
76
+ }, {
77
+ edge: "left",
78
+ style: {
79
+ top: rect.top + grabY,
80
+ left: rect.left,
81
+ width: grabX,
82
+ height: Math.max(rect.height - 2 * grabY, 0)
83
+ }
84
+ }, {
85
+ edge: "right",
86
+ style: {
87
+ top: rect.top + grabY,
88
+ left: rect.left + rect.width - grabX,
89
+ width: grabX,
90
+ height: Math.max(rect.height - 2 * grabY, 0)
91
+ }
92
+ }];
93
+ }
94
+
95
+ /**
96
+ * Where a dragged item would land if it were let go at `point`: the number of
97
+ * items that come before that place, so `0` is the top of the list and
98
+ * `rects.length` is the end of it.
99
+ *
100
+ * An item comes before the pointer once the pointer has passed its middle,
101
+ * which is what makes a section give way when it is half covered instead of
102
+ * at its very edge.
103
+ */
104
+ function slotAt(rects, point) {
105
+ var horizontal = isHorizontal(rects);
106
+ var slot = 0;
107
+ var _iterator = createForOfIteratorHelper._createForOfIteratorHelper(rects),
108
+ _step;
109
+ try {
110
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
111
+ var rect = _step.value;
112
+ var middle = horizontal ? rect.left + rect.width / 2 : rect.top + rect.height / 2;
113
+ if ((horizontal ? point.x : point.y) < middle) {
114
+ break;
115
+ }
116
+ slot++;
117
+ }
118
+ } catch (err) {
119
+ _iterator.e(err);
120
+ } finally {
121
+ _iterator.f();
122
+ }
123
+ return slot;
124
+ }
125
+
126
+ /**
127
+ * The index an item ends up at when it is dropped in `slot`, or `null` when
128
+ * that is where it already is. The two slots on either side of an item are
129
+ * both its own place; past it, taking the item out moves everything up one.
130
+ */
131
+ function moveTarget(from, slot) {
132
+ if (slot === from || slot === from + 1) {
133
+ return null;
134
+ }
135
+ return slot > from ? slot - 1 : slot;
136
+ }
137
+
138
+ /**
139
+ * The element around a list of components, and - in the Studio's canvas - the
140
+ * controls to arrange it. See `ValComponentEditor`.
141
+ *
142
+ * The controls exist only when three things are true: the page is in the
143
+ * canvas frame, so there is a Studio to hear what was asked for; the list
144
+ * has a path, which it has when the page is showing draft content; and the
145
+ * Studio's Select is on. Anywhere else this is one element and its children.
146
+ */
147
+ function ValComponentList(_ref) {
148
+ var path = _ref.path,
149
+ className = _ref.className,
150
+ count = _ref.count,
151
+ children = _ref.children;
152
+ var ref = React.useRef(null);
153
+ var inCanvas = useIsCanvasFrame();
154
+ var off = React.useContext(ValComponentControlsOff);
155
+ var editMode = useEditMode();
156
+ var editing = inCanvas && editMode && !off && path !== undefined;
157
+ return /*#__PURE__*/ReactJSXRuntime.jsxs("div", objectSpread2._objectSpread2(objectSpread2._objectSpread2({
158
+ ref: ref,
159
+ className: className
160
+ }, objectSpread2._defineProperty({}, VAL_COMPONENT_LIST_ATTRIBUTE, path !== null && path !== void 0 ? path : "")), {}, {
161
+ children: [children, editing && count === 0 && /*#__PURE__*/ReactJSXRuntime.jsx(EmptyList, {
162
+ path: path
163
+ }), editing && /*#__PURE__*/ReactJSXRuntime.jsx(ListControls, {
164
+ listRef: ref,
165
+ path: path
166
+ })]
167
+ }));
168
+ }
169
+ function subscribeToNothing() {
170
+ return function () {};
171
+ }
172
+
173
+ /**
174
+ * Whether this document is the Studio's canvas. `false` on the server and
175
+ * for the first render in the browser, so what is hydrated is what was sent.
176
+ */
177
+ function useIsCanvasFrame() {
178
+ return React.useSyncExternalStore(subscribeToNothing, function () {
179
+ return client.isValCanvasFrame(window.location.search, readParentPathname());
180
+ }, function () {
181
+ return false;
182
+ });
183
+ }
184
+ function readParentPathname() {
185
+ try {
186
+ if (window.parent === window) {
187
+ return null;
188
+ }
189
+ return window.parent.location.pathname;
190
+ } catch (_unused) {
191
+ // A parent on another origin: not the Studio, whose canvas is the same app
192
+ return null;
193
+ }
194
+ }
195
+ function send(message) {
196
+ // To the Studio, which is the same app: nothing on another origin is told
197
+ // what an editor did.
198
+ window.parent.postMessage(message, window.location.origin);
199
+ }
200
+ function sendEdit(edit) {
201
+ send({
202
+ val: client.VAL_CANVAS_MESSAGE,
203
+ type: "componentEdit",
204
+ edit: edit
205
+ });
206
+ }
207
+
208
+ /**
209
+ * Whether the Studio's Select is on, which the canvas bridge says on `<html>`
210
+ * ({@link VAL_CANVAS_EDITING_ATTRIBUTE}). Off, the page in the canvas is a
211
+ * page - read and clicked as a visitor would - and nothing is drawn on it.
212
+ */
213
+ function subscribeToEditMode(onChange) {
214
+ var observer = new MutationObserver(onChange);
215
+ observer.observe(document.documentElement, {
216
+ attributes: true,
217
+ attributeFilter: [client.VAL_CANVAS_EDITING_ATTRIBUTE]
218
+ });
219
+ return function () {
220
+ return observer.disconnect();
221
+ };
222
+ }
223
+ function useEditMode() {
224
+ return React.useSyncExternalStore(subscribeToEditMode, function () {
225
+ return document.documentElement.hasAttribute(client.VAL_CANVAS_EDITING_ATTRIBUTE);
226
+ }, function () {
227
+ return false;
228
+ });
229
+ }
230
+
231
+ /**
232
+ * The Studio's selection colour, which the canvas bridge sets on the page's
233
+ * root ({@link VAL_CANVAS_SELECTION_PROPERTY}), or Val's green. A CSS value
234
+ * rather than a colour read from a message: a list rendered after the Studio
235
+ * said it - one inside a section just added, or on a page navigated to - was
236
+ * not there to hear it.
237
+ */
238
+ var SELECTION_COLOR = "var(".concat(client.VAL_CANVAS_SELECTION_PROPERTY, ", ").concat(client.DEFAULT_CANVAS_SELECTION, ")");
239
+
240
+ /** A list with nothing in it has nothing to hover: the way in is a button. */
241
+ function EmptyList(_ref3) {
242
+ var path = _ref3.path;
243
+ var color = SELECTION_COLOR;
244
+ return /*#__PURE__*/ReactJSXRuntime.jsxs("button", {
245
+ type: "button",
246
+ onClick: function onClick() {
247
+ return sendEdit({
248
+ op: "insert",
249
+ list: path,
250
+ index: 0
251
+ });
252
+ },
253
+ style: {
254
+ all: "unset",
255
+ boxSizing: "border-box",
256
+ display: "flex",
257
+ alignItems: "center",
258
+ justifyContent: "center",
259
+ gap: 8,
260
+ width: "100%",
261
+ minHeight: 96,
262
+ border: "1px dashed ".concat(color),
263
+ borderRadius: 8,
264
+ color: color,
265
+ font: CONTROL_FONT,
266
+ cursor: "pointer"
267
+ },
268
+ children: [/*#__PURE__*/ReactJSXRuntime.jsx(Icon, {
269
+ name: "plus"
270
+ }), "Add a section"]
271
+ });
272
+ }
273
+ var CONTROL_FONT = '500 13px/1 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif';
274
+
275
+ /** The toolbar's height: its buttons, and the padding around them. */
276
+ var TOOLBAR_HEIGHT = 32;
277
+
278
+ /** Above the page, below nothing: the controls must not be covered. */
279
+ var CONTROL_Z_INDEX = 2147483000;
280
+
281
+ /** How near the edge of the window a drag starts scrolling, and how fast. */
282
+ var SCROLL_EDGE = 48;
283
+ var SCROLL_STEP = 14;
284
+ function itemsOf(list) {
285
+ if (list === null) {
286
+ return [];
287
+ }
288
+ var items = [];
289
+ for (var _i = 0, _Array$from = Array.from(list.children); _i < _Array$from.length; _i++) {
290
+ var child = _Array$from[_i];
291
+ if (child instanceof HTMLElement && child.hasAttribute(client.VAL_COMPONENT_ATTRIBUTE)) {
292
+ items.push(child);
293
+ }
294
+ }
295
+ return items;
296
+ }
297
+
298
+ /**
299
+ * A rectangle measured in the window, as offsets from `origin`: where the
300
+ * controls' layer actually is, which is the window's corner unless an
301
+ * ancestor made itself the containing block of fixed elements.
302
+ */
303
+ function relativeTo(rect, origin) {
304
+ return {
305
+ top: rect.top - origin.y,
306
+ left: rect.left - origin.x,
307
+ width: rect.width,
308
+ height: rect.height
309
+ };
310
+ }
311
+ function rectOf(element) {
312
+ var _element$getBoundingC = element.getBoundingClientRect(),
313
+ top = _element$getBoundingC.top,
314
+ left = _element$getBoundingC.left,
315
+ width = _element$getBoundingC.width,
316
+ height = _element$getBoundingC.height;
317
+ return {
318
+ top: top,
319
+ left: left,
320
+ width: width,
321
+ height: height
322
+ };
323
+ }
324
+
325
+ /**
326
+ * The controls on a list, drawn over the page rather than in it: a layer of
327
+ * its own, positioned from where the items are, so nothing about the page's
328
+ * own layout has to make room for them.
329
+ *
330
+ * The layer is the list's last child and not a portal on `<body>`, which
331
+ * would need `react-dom`: this file is reached from `val.config.ts` (through
332
+ * the framework package's entry), which Val also evaluates where there is no
333
+ * DOM to import. It is `position: fixed`, so it takes no room in the list;
334
+ * and it is measured rather than assumed to sit at the window's corner,
335
+ * because an ancestor with a `transform` or a `filter` moves a fixed
336
+ * element's origin to itself ({@link relativeTo}).
337
+ */
338
+ function ListControls(_ref4) {
339
+ var listRef = _ref4.listRef,
340
+ path = _ref4.path;
341
+ var color = SELECTION_COLOR;
342
+ var layerRef = React.useRef(null);
343
+ var _useState = React.useState(null),
344
+ _useState2 = slicedToArray._slicedToArray(_useState, 2),
345
+ hover = _useState2[0],
346
+ setHover = _useState2[1];
347
+ // The item being dragged, and the place it would land in if let go now
348
+ var _useState3 = React.useState(null),
349
+ _useState4 = slicedToArray._slicedToArray(_useState3, 2),
350
+ dragFrom = _useState4[0],
351
+ setDragFrom = _useState4[1];
352
+ var _useState5 = React.useState(0),
353
+ _useState6 = slicedToArray._slicedToArray(_useState5, 2),
354
+ dragSlot = _useState6[0],
355
+ setDragSlot = _useState6[1];
356
+ // The item whose removal is waiting for a second click
357
+ var _useState7 = React.useState(null),
358
+ _useState8 = slicedToArray._slicedToArray(_useState7, 2),
359
+ confirming = _useState8[0],
360
+ setConfirming = _useState8[1];
361
+ // Where the items are, in the window. Measured when something could have
362
+ // moved them - the pointer reaching another item, a scroll, a resize, a
363
+ // drag - and never while rendering.
364
+ var _useState9 = React.useState([]),
365
+ _useState0 = slicedToArray._slicedToArray(_useState9, 2),
366
+ rects = _useState0[0],
367
+ setRects = _useState0[1];
368
+ var measureItems = React.useCallback(function () {
369
+ var _layerRef$current, _layer$left, _layer$top;
370
+ var layer = (_layerRef$current = layerRef.current) === null || _layerRef$current === void 0 ? void 0 : _layerRef$current.getBoundingClientRect();
371
+ var origin = {
372
+ x: (_layer$left = layer === null || layer === void 0 ? void 0 : layer.left) !== null && _layer$left !== void 0 ? _layer$left : 0,
373
+ y: (_layer$top = layer === null || layer === void 0 ? void 0 : layer.top) !== null && _layer$top !== void 0 ? _layer$top : 0
374
+ };
375
+ return itemsOf(listRef.current).map(function (item) {
376
+ return relativeTo(rectOf(item), origin);
377
+ });
378
+ }, [listRef]);
379
+ var measure = React.useCallback(function () {
380
+ setRects(measureItems());
381
+ }, [measureItems]);
382
+ // What `hover` is, for the handlers: they compare against it on every move
383
+ // of the pointer, and must not wait for a render to know it.
384
+ var hovered = React.useRef(null);
385
+ var hoverOn = React.useCallback(function (next) {
386
+ if (hovered.current === next) return;
387
+ hovered.current = next;
388
+ setHover(next);
389
+ // A confirmation is for the item it was asked on, and no other
390
+ setConfirming(null);
391
+ if (next !== null) {
392
+ measure();
393
+ }
394
+ }, [measure]);
395
+ var dragging = dragFrom !== null;
396
+
397
+ // Which item the pointer is over.
398
+ React.useEffect(function () {
399
+ if (dragging) return;
400
+ var onMove = function onMove(event) {
401
+ var _layerRef$current2;
402
+ var target = event.target;
403
+ if (!(target instanceof Element)) return;
404
+ // On the controls themselves: they belong to the item they are on.
405
+ if ((_layerRef$current2 = layerRef.current) !== null && _layerRef$current2 !== void 0 && _layerRef$current2.contains(target)) return;
406
+ // The NEAREST component only, so a list inside a component of another
407
+ // list answers for its own items and the outer one stays out of it.
408
+ var item = target.closest("[".concat(client.VAL_COMPONENT_ATTRIBUTE, "]"));
409
+ var index = item instanceof HTMLElement && item.parentElement === listRef.current ? itemsOf(listRef.current).indexOf(item) : -1;
410
+ hoverOn(index === -1 ? null : index);
411
+ };
412
+ var onLeave = function onLeave() {
413
+ return hoverOn(null);
414
+ };
415
+ document.addEventListener("pointermove", onMove);
416
+ document.documentElement.addEventListener("pointerleave", onLeave);
417
+ return function () {
418
+ document.removeEventListener("pointermove", onMove);
419
+ document.documentElement.removeEventListener("pointerleave", onLeave);
420
+ };
421
+ }, [dragging, listRef, hoverOn]);
422
+
423
+ // The page scrolls and resizes under the controls.
424
+ var active = hover !== null || dragging;
425
+ React.useEffect(function () {
426
+ if (!active) return;
427
+ window.addEventListener("scroll", measure, {
428
+ passive: true,
429
+ capture: true
430
+ });
431
+ window.addEventListener("resize", measure);
432
+ return function () {
433
+ window.removeEventListener("scroll", measure, {
434
+ capture: true
435
+ });
436
+ window.removeEventListener("resize", measure);
437
+ };
438
+ }, [active, measure]);
439
+
440
+ // A drag, once started, is the whole document's: the pointer leaves the
441
+ // handle at once, and has to be followed wherever it goes.
442
+ React.useEffect(function () {
443
+ if (dragFrom === null) return;
444
+ // Kept here as well as in state: letting go has to know the slot the
445
+ // pointer is over NOW, and must send its edit exactly once - from the
446
+ // handler, never from a state updater, which React may run twice.
447
+ var slot = dragFrom;
448
+ var pointer = null;
449
+ var place = function place() {
450
+ if (pointer === null) return;
451
+ setRects(measureItems());
452
+ // In the window's own coordinates, which is what the pointer is in
453
+ slot = slotAt(itemsOf(listRef.current).map(rectOf), pointer);
454
+ setDragSlot(slot);
455
+ };
456
+ // Scrolling while the pointer rests near an edge: a pointer that is held
457
+ // still sends no events, so the page is moved on every frame instead.
458
+ var frame = requestAnimationFrame(function scroll() {
459
+ frame = requestAnimationFrame(scroll);
460
+ if (pointer === null) return;
461
+ var step = pointer.y < SCROLL_EDGE ? -SCROLL_STEP : pointer.y > window.innerHeight - SCROLL_EDGE ? SCROLL_STEP : 0;
462
+ if (step !== 0) {
463
+ window.scrollBy(0, step);
464
+ place();
465
+ }
466
+ });
467
+ var end = function end() {
468
+ setDragFrom(null);
469
+ hoverOn(null);
470
+ };
471
+ var onMove = function onMove(event) {
472
+ pointer = {
473
+ x: event.clientX,
474
+ y: event.clientY
475
+ };
476
+ place();
477
+ };
478
+ var onUp = function onUp() {
479
+ var to = moveTarget(dragFrom, slot);
480
+ if (to !== null) {
481
+ sendEdit({
482
+ op: "move",
483
+ list: path,
484
+ from: dragFrom,
485
+ to: to
486
+ });
487
+ }
488
+ end();
489
+ };
490
+ var onKey = function onKey(event) {
491
+ if (event.key === "Escape") {
492
+ end();
493
+ }
494
+ };
495
+ var selectable = document.body.style.userSelect;
496
+ document.body.style.userSelect = "none";
497
+ document.documentElement.setAttribute(client.VAL_COMPONENT_DRAGGING_ATTRIBUTE, "");
498
+ document.addEventListener("pointermove", onMove);
499
+ document.addEventListener("pointerup", onUp);
500
+ document.addEventListener("pointercancel", end);
501
+ document.addEventListener("keydown", onKey);
502
+ return function () {
503
+ cancelAnimationFrame(frame);
504
+ document.body.style.userSelect = selectable;
505
+ document.documentElement.removeAttribute(client.VAL_COMPONENT_DRAGGING_ATTRIBUTE);
506
+ document.removeEventListener("pointermove", onMove);
507
+ document.removeEventListener("pointerup", onUp);
508
+ document.removeEventListener("pointercancel", end);
509
+ document.removeEventListener("keydown", onKey);
510
+ };
511
+ }, [dragFrom, listRef, path, measureItems, hoverOn]);
512
+ var open = React.useCallback(function (index) {
513
+ var itemPath = core.Internal.createValPathOfItem(path, index);
514
+ if (itemPath !== undefined) {
515
+ send({
516
+ val: client.VAL_CANVAS_MESSAGE,
517
+ type: "clicked",
518
+ paths: [itemPath]
519
+ });
520
+ }
521
+ }, [path]);
522
+ var horizontal = isHorizontal(rects);
523
+ var shown = dragFrom !== null ? dragFrom : hover;
524
+ var rect = shown !== null ? rects[shown] : undefined;
525
+ var controls = null;
526
+ if (dragFrom !== null) {
527
+ // The line at the start of slot `k`: before item `k`, or - past the
528
+ // last item - after it.
529
+ var lineAt = function lineAt(k) {
530
+ var before = rects[k];
531
+ var last = rects[rects.length - 1];
532
+ if (before !== undefined) {
533
+ return horizontal ? {
534
+ top: before.top,
535
+ left: before.left - 2,
536
+ height: before.height
537
+ } : {
538
+ top: before.top - 2,
539
+ left: before.left,
540
+ width: before.width
541
+ };
542
+ }
543
+ if (last !== undefined) {
544
+ return horizontal ? {
545
+ top: last.top,
546
+ left: last.left + last.width - 2,
547
+ height: last.height
548
+ } : {
549
+ top: last.top + last.height - 2,
550
+ left: last.left,
551
+ width: last.width
552
+ };
553
+ }
554
+ return null;
555
+ };
556
+ var places = [];
557
+ for (var k = 0; k <= rects.length; k++) {
558
+ // The two places next to the item are where it already is
559
+ if (moveTarget(dragFrom, k) === null) continue;
560
+ var line = lineAt(k);
561
+ if (line === null) continue;
562
+ var chosen = k === dragSlot;
563
+ places.push(/*#__PURE__*/ReactJSXRuntime.jsx(DropMarker, {
564
+ chosen: chosen,
565
+ horizontal: horizontal,
566
+ color: color,
567
+ line: line
568
+ }, k));
569
+ }
570
+ controls = /*#__PURE__*/ReactJSXRuntime.jsxs(ReactJSXRuntime.Fragment, {
571
+ children: [rect !== undefined && /*#__PURE__*/ReactJSXRuntime.jsx("div", {
572
+ style: objectSpread2._objectSpread2(objectSpread2._objectSpread2({
573
+ position: "absolute"
574
+ }, rect), {}, {
575
+ background: color,
576
+ opacity: 0.12,
577
+ pointerEvents: "none"
578
+ })
579
+ }), places]
580
+ });
581
+ } else if (hover !== null && rect !== undefined) {
582
+ var count = rects.length;
583
+ var startDrag = function startDrag(event) {
584
+ if (event.button !== 0) return;
585
+ event.preventDefault();
586
+ setDragSlot(hover);
587
+ setDragFrom(hover);
588
+ };
589
+ controls = /*#__PURE__*/ReactJSXRuntime.jsxs(ReactJSXRuntime.Fragment, {
590
+ children: [/*#__PURE__*/ReactJSXRuntime.jsx("div", {
591
+ style: objectSpread2._objectSpread2(objectSpread2._objectSpread2({
592
+ position: "absolute"
593
+ }, rect), {}, {
594
+ outline: "2px solid ".concat(color),
595
+ outlineOffset: -2,
596
+ pointerEvents: "none"
597
+ })
598
+ }), borderStrips(rect).map(function (strip) {
599
+ return /*#__PURE__*/ReactJSXRuntime.jsx("div", objectSpread2._objectSpread2(objectSpread2._objectSpread2({}, objectSpread2._defineProperty({}, VAL_COMPONENT_BORDER_ATTRIBUTE, strip.edge)), {}, {
600
+ onPointerDown: startDrag,
601
+ style: objectSpread2._objectSpread2(objectSpread2._objectSpread2({
602
+ position: "absolute"
603
+ }, strip.style), {}, {
604
+ cursor: "grab",
605
+ touchAction: "none"
606
+ })
607
+ }), strip.edge);
608
+ }), /*#__PURE__*/ReactJSXRuntime.jsx(AddButton, {
609
+ title: "Add a section before this one",
610
+ color: color,
611
+ style: horizontal ? {
612
+ top: rect.top + rect.height / 2 - 12,
613
+ left: rect.left - 12
614
+ } : {
615
+ top: rect.top - 12,
616
+ left: rect.left + rect.width / 2 - 12
617
+ },
618
+ onClick: function onClick() {
619
+ return sendEdit({
620
+ op: "insert",
621
+ list: path,
622
+ index: hover
623
+ });
624
+ }
625
+ }), /*#__PURE__*/ReactJSXRuntime.jsx(AddButton, {
626
+ title: "Add a section after this one",
627
+ color: color,
628
+ style: horizontal ? {
629
+ top: rect.top + rect.height / 2 - 12,
630
+ left: rect.left + rect.width - 12
631
+ } : {
632
+ top: rect.top + rect.height - 12,
633
+ left: rect.left + rect.width / 2 - 12
634
+ },
635
+ onClick: function onClick() {
636
+ return sendEdit({
637
+ op: "insert",
638
+ list: path,
639
+ index: hover + 1
640
+ });
641
+ }
642
+ }), /*#__PURE__*/ReactJSXRuntime.jsxs("div", {
643
+ "data-val-component-toolbar": "",
644
+ style: {
645
+ position: "absolute",
646
+ // On top of the item's corner rather than in it, so a small
647
+ // component is not covered by its own controls - and touching
648
+ // it, so the pointer reaches them without crossing anything
649
+ // else. Never off the top of the window: a section that starts
650
+ // above it keeps its controls in reach, inside its corner.
651
+ top: Math.max(rect.top - TOOLBAR_HEIGHT, 8),
652
+ left: Math.max(rect.left + rect.width, TOOLBAR_HEIGHT * 5),
653
+ transform: "translateX(-100%)",
654
+ display: "flex",
655
+ alignItems: "center",
656
+ gap: 2,
657
+ padding: 3,
658
+ borderRadius: 8,
659
+ background: "#18181b",
660
+ color: "#fafafa",
661
+ boxShadow: "0 4px 14px rgba(0, 0, 0, 0.28)",
662
+ font: CONTROL_FONT,
663
+ whiteSpace: "nowrap"
664
+ },
665
+ children: [/*#__PURE__*/ReactJSXRuntime.jsx(ToolbarButton, {
666
+ title: "Drag to move",
667
+ style: {
668
+ cursor: "grab",
669
+ touchAction: "none"
670
+ },
671
+ onPointerDown: startDrag,
672
+ children: /*#__PURE__*/ReactJSXRuntime.jsx(Icon, {
673
+ name: "grip"
674
+ })
675
+ }), /*#__PURE__*/ReactJSXRuntime.jsx(ToolbarButton, {
676
+ title: "Move up",
677
+ disabled: hover === 0,
678
+ onClick: function onClick() {
679
+ sendEdit({
680
+ op: "move",
681
+ list: path,
682
+ from: hover,
683
+ to: hover - 1
684
+ });
685
+ hoverOn(null);
686
+ },
687
+ children: /*#__PURE__*/ReactJSXRuntime.jsx(Icon, {
688
+ name: horizontal ? "left" : "up"
689
+ })
690
+ }), /*#__PURE__*/ReactJSXRuntime.jsx(ToolbarButton, {
691
+ title: "Move down",
692
+ disabled: hover === count - 1,
693
+ onClick: function onClick() {
694
+ sendEdit({
695
+ op: "move",
696
+ list: path,
697
+ from: hover,
698
+ to: hover + 1
699
+ });
700
+ hoverOn(null);
701
+ },
702
+ children: /*#__PURE__*/ReactJSXRuntime.jsx(Icon, {
703
+ name: horizontal ? "right" : "down"
704
+ })
705
+ }), /*#__PURE__*/ReactJSXRuntime.jsx(ToolbarButton, {
706
+ title: "Edit the content",
707
+ onClick: function onClick() {
708
+ return open(hover);
709
+ },
710
+ children: /*#__PURE__*/ReactJSXRuntime.jsx(Icon, {
711
+ name: "pencil"
712
+ })
713
+ }), confirming === hover ? /*#__PURE__*/ReactJSXRuntime.jsx(ToolbarButton, {
714
+ title: "Remove this section",
715
+ style: {
716
+ background: "#dc2626",
717
+ padding: "0 8px"
718
+ },
719
+ onClick: function onClick() {
720
+ sendEdit({
721
+ op: "remove",
722
+ list: path,
723
+ index: hover
724
+ });
725
+ hoverOn(null);
726
+ },
727
+ children: "Remove?"
728
+ }) : /*#__PURE__*/ReactJSXRuntime.jsx(ToolbarButton, {
729
+ title: "Remove this section",
730
+ onClick: function onClick() {
731
+ return setConfirming(hover);
732
+ },
733
+ children: /*#__PURE__*/ReactJSXRuntime.jsx(Icon, {
734
+ name: "trash"
735
+ })
736
+ })]
737
+ })]
738
+ });
739
+ }
740
+ return /*#__PURE__*/ReactJSXRuntime.jsx("div", objectSpread2._objectSpread2(objectSpread2._objectSpread2({
741
+ ref: layerRef
742
+ }, objectSpread2._defineProperty({}, client.VAL_CANVAS_CONTROLS_ATTRIBUTE, "")), {}, {
743
+ style: {
744
+ position: "fixed",
745
+ top: 0,
746
+ left: 0,
747
+ zIndex: CONTROL_Z_INDEX
748
+ },
749
+ children: controls
750
+ }));
751
+ }
752
+ function ToolbarButton(_ref7) {
753
+ var title = _ref7.title,
754
+ disabled = _ref7.disabled,
755
+ style = _ref7.style,
756
+ onClick = _ref7.onClick,
757
+ onPointerDown = _ref7.onPointerDown,
758
+ children = _ref7.children;
759
+ return /*#__PURE__*/ReactJSXRuntime.jsx("button", {
760
+ type: "button",
761
+ title: title,
762
+ "aria-label": title,
763
+ disabled: disabled,
764
+ onClick: onClick,
765
+ onPointerDown: onPointerDown,
766
+ style: objectSpread2._objectSpread2({
767
+ all: "unset",
768
+ boxSizing: "border-box",
769
+ display: "inline-flex",
770
+ alignItems: "center",
771
+ justifyContent: "center",
772
+ minWidth: 26,
773
+ height: 26,
774
+ borderRadius: 5,
775
+ cursor: disabled ? "default" : "pointer",
776
+ opacity: disabled ? 0.35 : 1
777
+ }, style),
778
+ children: children
779
+ });
780
+ }
781
+
782
+ /** How long a place a section could go to takes to fade from full to faint. */
783
+ var MARKER_FADE_MS = 700;
784
+ var MARKER_FAINT = 0.35;
785
+
786
+ /**
787
+ * One place the dragged section could land. All of them are drawn at full
788
+ * strength when the drag starts, so the editor sees where they are, and then
789
+ * settle to faint so the page can be read. The place it would land in now
790
+ * stays solid and carries `data-val-component-drop`.
791
+ *
792
+ * The fade runs once, when the marker appears: `animate` is the Web
793
+ * Animations API, so it needs no stylesheet in the customer's page and does
794
+ * not restart when the marker is re-rendered as the pointer moves.
795
+ */
796
+ function DropMarker(_ref8) {
797
+ var chosen = _ref8.chosen,
798
+ horizontal = _ref8.horizontal,
799
+ color = _ref8.color,
800
+ line = _ref8.line;
801
+ var ref = React.useRef(null);
802
+ React.useEffect(function () {
803
+ var element = ref.current;
804
+ // Not every environment has it (jsdom); the marker is simply faint then
805
+ if (!element || typeof element.animate !== "function") return;
806
+ var fade = element.animate([{
807
+ opacity: 1
808
+ }, {
809
+ opacity: MARKER_FAINT
810
+ }], {
811
+ duration: MARKER_FADE_MS,
812
+ easing: "ease-out"
813
+ });
814
+ return function () {
815
+ return fade.cancel();
816
+ };
817
+ }, []);
818
+ return /*#__PURE__*/ReactJSXRuntime.jsx("div", objectSpread2._objectSpread2(objectSpread2._objectSpread2({
819
+ ref: ref
820
+ // Only the place it would land in is THE drop line; the rest say where
821
+ // else it could go.
822
+ }, chosen ? {
823
+ "data-val-component-drop": ""
824
+ } : {
825
+ "data-val-component-slot": ""
826
+ }), {}, {
827
+ style: objectSpread2._objectSpread2(objectSpread2._objectSpread2(objectSpread2._objectSpread2({
828
+ position: "absolute"
829
+ }, horizontal ? {
830
+ width: chosen ? 4 : 2
831
+ } : {
832
+ height: chosen ? 4 : 2
833
+ }), line), {}, {
834
+ background: color,
835
+ opacity: chosen ? 1 : MARKER_FAINT,
836
+ transition: "opacity 150ms",
837
+ borderRadius: 2,
838
+ pointerEvents: "none"
839
+ })
840
+ }));
841
+ }
842
+ function AddButton(_ref9) {
843
+ var title = _ref9.title,
844
+ color = _ref9.color,
845
+ style = _ref9.style,
846
+ onClick = _ref9.onClick;
847
+ return /*#__PURE__*/ReactJSXRuntime.jsx("button", {
848
+ type: "button",
849
+ title: title,
850
+ "aria-label": title,
851
+ "data-val-component-add": "",
852
+ onClick: onClick,
853
+ style: objectSpread2._objectSpread2({
854
+ all: "unset",
855
+ boxSizing: "border-box",
856
+ position: "absolute",
857
+ display: "flex",
858
+ alignItems: "center",
859
+ justifyContent: "center",
860
+ width: 24,
861
+ height: 24,
862
+ borderRadius: 12,
863
+ background: color,
864
+ color: "#ffffff",
865
+ boxShadow: "0 2px 8px rgba(0, 0, 0, 0.25)",
866
+ cursor: "pointer"
867
+ }, style),
868
+ children: /*#__PURE__*/ReactJSXRuntime.jsx(Icon, {
869
+ name: "plus"
870
+ })
871
+ });
872
+ }
873
+ /**
874
+ * A function, and not a table of elements: a table is built when this file
875
+ * is imported, and this file is imported wherever `val.config.ts` is - the
876
+ * CLI and the language server included, which evaluate it with no React to
877
+ * build an element with. Nothing here may run before a component renders.
878
+ */
879
+ function iconPaths(name) {
880
+ switch (name) {
881
+ case "grip":
882
+ return /*#__PURE__*/ReactJSXRuntime.jsxs(ReactJSXRuntime.Fragment, {
883
+ children: [/*#__PURE__*/ReactJSXRuntime.jsx("circle", {
884
+ cx: "9",
885
+ cy: "6",
886
+ r: "1"
887
+ }), /*#__PURE__*/ReactJSXRuntime.jsx("circle", {
888
+ cx: "9",
889
+ cy: "12",
890
+ r: "1"
891
+ }), /*#__PURE__*/ReactJSXRuntime.jsx("circle", {
892
+ cx: "9",
893
+ cy: "18",
894
+ r: "1"
895
+ }), /*#__PURE__*/ReactJSXRuntime.jsx("circle", {
896
+ cx: "15",
897
+ cy: "6",
898
+ r: "1"
899
+ }), /*#__PURE__*/ReactJSXRuntime.jsx("circle", {
900
+ cx: "15",
901
+ cy: "12",
902
+ r: "1"
903
+ }), /*#__PURE__*/ReactJSXRuntime.jsx("circle", {
904
+ cx: "15",
905
+ cy: "18",
906
+ r: "1"
907
+ })]
908
+ });
909
+ case "up":
910
+ return /*#__PURE__*/ReactJSXRuntime.jsx("path", {
911
+ d: "M12 19V5M5 12l7-7 7 7"
912
+ });
913
+ case "down":
914
+ return /*#__PURE__*/ReactJSXRuntime.jsx("path", {
915
+ d: "M12 5v14M19 12l-7 7-7-7"
916
+ });
917
+ case "left":
918
+ return /*#__PURE__*/ReactJSXRuntime.jsx("path", {
919
+ d: "M19 12H5M12 19l-7-7 7-7"
920
+ });
921
+ case "right":
922
+ return /*#__PURE__*/ReactJSXRuntime.jsx("path", {
923
+ d: "M5 12h14M12 5l7 7-7 7"
924
+ });
925
+ case "pencil":
926
+ return /*#__PURE__*/ReactJSXRuntime.jsx("path", {
927
+ d: "M17 3a2.85 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5ZM15 5l4 4"
928
+ });
929
+ case "trash":
930
+ return /*#__PURE__*/ReactJSXRuntime.jsx("path", {
931
+ d: "M3 6h18M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2M10 11v6M14 11v6"
932
+ });
933
+ case "plus":
934
+ return /*#__PURE__*/ReactJSXRuntime.jsx("path", {
935
+ d: "M5 12h14M12 5v14"
936
+ });
937
+ }
938
+ }
939
+ function Icon(_ref0) {
940
+ var name = _ref0.name;
941
+ return /*#__PURE__*/ReactJSXRuntime.jsx("svg", {
942
+ width: "15",
943
+ height: "15",
944
+ viewBox: "0 0 24 24",
945
+ fill: "none",
946
+ stroke: "currentColor",
947
+ strokeWidth: "2",
948
+ strokeLinecap: "round",
949
+ strokeLinejoin: "round",
950
+ "aria-hidden": "true",
951
+ children: iconPaths(name)
952
+ });
953
+ }
954
+
955
+ Object.defineProperty(exports, 'VAL_COMPONENT_ATTRIBUTE', {
956
+ enumerable: true,
957
+ get: function () { return client.VAL_COMPONENT_ATTRIBUTE; }
958
+ });
959
+ exports.VAL_COMPONENT_LIST_ATTRIBUTE = VAL_COMPONENT_LIST_ATTRIBUTE;
960
+ exports.ValComponentControlsOff = ValComponentControlsOff;
961
+ exports.ValComponentList = ValComponentList;
962
+ exports.borderStrips = borderStrips;
963
+ exports.isHorizontal = isHorizontal;
964
+ exports.moveTarget = moveTarget;
965
+ exports.relativeTo = relativeTo;
966
+ exports.slotAt = slotAt;