@valbuild/shared 0.150.2 → 0.150.8

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 (41) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/client/dist/valbuild-shared-client.cjs.dev.js +8 -2
  3. package/client/dist/valbuild-shared-client.cjs.prod.js +8 -2
  4. package/client/dist/valbuild-shared-client.esm.js +2 -2
  5. package/dist/{acquireTypes-50960f2c.esm.js → acquireTypes-6e2f7961.esm.js} +2 -2
  6. package/dist/{acquireTypes-de10005d.cjs.dev.js → acquireTypes-84a4077a.cjs.dev.js} +6 -6
  7. package/dist/{acquireTypes-e9467a2a.cjs.prod.js → acquireTypes-dd976839.cjs.prod.js} +6 -6
  8. package/dist/{createForOfIteratorHelper-3db5d737.esm.js → createForOfIteratorHelper-666a6d17.esm.js} +31 -15
  9. package/dist/{createForOfIteratorHelper-90e6a8b6.cjs.prod.js → createForOfIteratorHelper-87a33eff.cjs.dev.js} +31 -15
  10. package/dist/{createForOfIteratorHelper-d86bb352.cjs.dev.js → createForOfIteratorHelper-fef4fa7d.cjs.prod.js} +31 -15
  11. package/dist/declarations/src/client/index.d.ts +1 -0
  12. package/dist/declarations/src/internal/ApiRoutes.d.ts +8 -0
  13. package/dist/declarations/src/internal/elementPicker.d.ts +96 -0
  14. package/dist/declarations/src/internal/index.d.ts +1 -0
  15. package/dist/declarations/src/internal/theme/accentRamp.d.ts +20 -0
  16. package/dist/declarations/src/internal/valCanvasProtocol.d.ts +36 -0
  17. package/dist/{getSitemapTree-667bcb3c.cjs.prod.js → getSitemapTree-873f7266.cjs.prod.js} +333 -2
  18. package/dist/{getSitemapTree-5afbc3ac.esm.js → getSitemapTree-b6161fd8.esm.js} +328 -3
  19. package/dist/{getSitemapTree-0fa0569c.cjs.dev.js → getSitemapTree-be70d9fc.cjs.dev.js} +333 -2
  20. package/dist/{proposalProject-a3d3ad76.esm.js → proposalProject-689c24cc.esm.js} +2 -2
  21. package/dist/{proposalProject-cdc53711.cjs.dev.js → proposalProject-926f2c68.cjs.dev.js} +2 -2
  22. package/dist/{proposalProject-28550146.cjs.prod.js → proposalProject-fab0c026.cjs.prod.js} +2 -2
  23. package/dist/{protocol-1c13de0d.cjs.prod.js → protocol-4e2a455b.cjs.prod.js} +1 -1
  24. package/dist/{protocol-3b9ef786.esm.js → protocol-7ddc1a71.esm.js} +1 -1
  25. package/dist/{protocol-d8a6efda.cjs.dev.js → protocol-ac1c3ec7.cjs.dev.js} +1 -1
  26. package/dist/{slicedToArray-6cd80315.esm.js → slicedToArray-22567815.esm.js} +2 -18
  27. package/dist/{slicedToArray-62e428df.cjs.dev.js → slicedToArray-c8a29f6b.cjs.dev.js} +1 -18
  28. package/dist/{slicedToArray-af50e684.cjs.prod.js → slicedToArray-d99b4e2e.cjs.prod.js} +1 -18
  29. package/internal/dist/valbuild-shared-internal.cjs.dev.js +71 -10
  30. package/internal/dist/valbuild-shared-internal.cjs.prod.js +71 -10
  31. package/internal/dist/valbuild-shared-internal.esm.js +63 -9
  32. package/package.json +2 -2
  33. package/publish/dist/valbuild-shared-publish.cjs.dev.js +6 -6
  34. package/publish/dist/valbuild-shared-publish.cjs.prod.js +6 -6
  35. package/publish/dist/valbuild-shared-publish.esm.js +4 -4
  36. package/typecheck/dist/valbuild-shared-typecheck.cjs.dev.js +7 -7
  37. package/typecheck/dist/valbuild-shared-typecheck.cjs.prod.js +7 -7
  38. package/typecheck/dist/valbuild-shared-typecheck.esm.js +5 -5
  39. package/typecheck/testing/dist/valbuild-shared-typecheck-testing.cjs.dev.js +4 -4
  40. package/typecheck/testing/dist/valbuild-shared-typecheck-testing.cjs.prod.js +4 -4
  41. package/typecheck/testing/dist/valbuild-shared-typecheck-testing.esm.js +3 -3
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var createForOfIteratorHelper = require('./createForOfIteratorHelper-90e6a8b6.cjs.prod.js');
3
+ var createForOfIteratorHelper = require('./createForOfIteratorHelper-fef4fa7d.cjs.prod.js');
4
4
  var core = require('@valbuild/core');
5
5
 
6
6
  var VAL_CONFIG_SESSION_STORAGE_KEY = "val-config";
@@ -75,8 +75,16 @@ function isValCanvasPageMessage(data) {
75
75
  if (createForOfIteratorHelper._typeof(data) !== "object" || data === null) return false;
76
76
  var message = data;
77
77
  if (message.val !== VAL_CANVAS_MESSAGE) return false;
78
- return message.type === "ready" || message.type === "elements" || message.type === "clicked" || message.type === "pinch" || message.type === "zoom" || message.type === "refreshing";
78
+ return message.type === "ready" || message.type === "elements" || message.type === "clicked" || message.type === "pinch" || message.type === "zoom" || message.type === "refreshing" || message.type === "elementPickingStopped" || message.type === "elementPicked" && isPickedElement(data.element);
79
79
  }
80
+ function isPickedElement(value) {
81
+ if (createForOfIteratorHelper._typeof(value) !== "object" || value === null) return false;
82
+ var element = value;
83
+ return typeof element.tag === "string" && typeof element.text === "string" && typeof element.selector === "string" && Array.isArray(element.classes) && element.classes.every(function (item) {
84
+ return typeof item === "string";
85
+ }) && (element.valPath === undefined || typeof element.valPath === "string");
86
+ }
87
+ var PICK_MODIFIERS = ["alt", "meta", "shift", "ctrl"];
80
88
 
81
89
  /**
82
90
  * Whether a value is a colour a `theme` message may carry.
@@ -108,6 +116,10 @@ function isValCanvasStudioMessage(data) {
108
116
  // at all, and there is one guard rather than one per framework binding.
109
117
  return isCanvasSelectionColor(message.selection) && isCanvasSelectionColor(message.selectionSoft);
110
118
  }
119
+ if (message.type === "pickElements") {
120
+ var pick = data;
121
+ return typeof pick.enabled === "boolean" && typeof pick.active === "boolean" && typeof pick.keepPicked === "boolean" && PICK_MODIFIERS.includes(pick.modifier);
122
+ }
111
123
  return message.type === "rescan" || message.type === "highlight" || message.type === "setPicking" || message.type === "sourceUpdate" || message.type === "sourcesSynced";
112
124
  }
113
125
 
@@ -150,6 +162,319 @@ function withValCanvasParam(url) {
150
162
  return "".concat(url).concat(separator).concat(VAL_CANVAS_PARAM, "=1");
151
163
  }
152
164
 
165
+ /**
166
+ * Pointing at anything on a page, as Lovable's picker does: hover outlines
167
+ * the element under the pointer, a click picks it, and the assistant is told
168
+ * which (valbuild/home `docs/design-tab.md`).
169
+ *
170
+ * ANY element, not only what Val marks. The design tab changes code, and the
171
+ * thing a person wants changed is as often a wrapper, a gap or a button as a
172
+ * field -- so a field's path is extra information when there is one
173
+ * (`valPath`), never the condition for picking.
174
+ *
175
+ * Picking is on while a MODIFIER is held, so the page stays a page: links
176
+ * follow, menus open, and nothing has to be switched back off. `active`
177
+ * turns it on without one, for a Select button or a touch screen.
178
+ *
179
+ * Plain DOM, nothing of React's or the Studio's, and in `shared` because
180
+ * both halves need it: the canvas bridge (`@valbuild/next`,
181
+ * `@valbuild/tanstack`) runs it inside the proposal's page, which is another
182
+ * origin than the design tab and the only place the page's elements can be
183
+ * reached; the Studio's stories run it on a page of their own. Only the
184
+ * `PickedElement` crosses the frame (`valCanvasProtocol.ts`).
185
+ */
186
+
187
+ var TEXT_LENGTH = 80;
188
+ var CLASSES = 6;
189
+ function describeElement(element, root) {
190
+ var _element$textContent, _element$closest$getA, _element$closest;
191
+ var text = ((_element$textContent = element.textContent) !== null && _element$textContent !== void 0 ? _element$textContent : "").replace(/\s+/g, " ").trim();
192
+ var valPath = (_element$closest$getA = (_element$closest = element.closest("[data-val-path]")) === null || _element$closest === void 0 ? void 0 : _element$closest.getAttribute("data-val-path")) !== null && _element$closest$getA !== void 0 ? _element$closest$getA : undefined;
193
+ return createForOfIteratorHelper._objectSpread2({
194
+ tag: element.tagName.toLowerCase(),
195
+ text: text.length > TEXT_LENGTH ? "".concat(text.slice(0, TEXT_LENGTH - 1), "\u2026") : text,
196
+ classes: Array.from(element.classList).slice(0, CLASSES),
197
+ selector: selectorOf(element, root)
198
+ }, valPath !== undefined ? {
199
+ valPath: valPath
200
+ } : {});
201
+ }
202
+ function selectorOf(element, root) {
203
+ var parts = [];
204
+ var at = element;
205
+ while (at !== null && at !== root) {
206
+ var tag = at.tagName.toLowerCase();
207
+ if (at.id !== "") {
208
+ parts.unshift("".concat(tag, "#").concat(at.id));
209
+ break;
210
+ }
211
+ var parent = at.parentElement;
212
+ var same = parent === null ? [] : Array.from(parent.children).filter(function (sibling) {
213
+ var _at;
214
+ return sibling.tagName === ((_at = at) === null || _at === void 0 ? void 0 : _at.tagName);
215
+ });
216
+ parts.unshift(same.length > 1 ? "".concat(tag, ":nth-of-type(").concat(same.indexOf(at) + 1, ")") : tag);
217
+ at = parent;
218
+ }
219
+ return parts.join(" > ");
220
+ }
221
+
222
+ /** What a person is told to hold, on their own keyboard. */
223
+ function modifierLabel(modifier, mac) {
224
+ switch (modifier) {
225
+ case "alt":
226
+ return mac ? "⌥" : "Alt";
227
+ case "meta":
228
+ return mac ? "⌘" : "Win";
229
+ case "shift":
230
+ return mac ? "⇧" : "Shift";
231
+ case "ctrl":
232
+ return mac ? "⌃" : "Ctrl";
233
+ }
234
+ }
235
+ function held(event, modifier) {
236
+ switch (modifier) {
237
+ case "alt":
238
+ return event.altKey;
239
+ case "meta":
240
+ return event.metaKey;
241
+ case "shift":
242
+ return event.shiftKey;
243
+ case "ctrl":
244
+ return event.ctrlKey;
245
+ }
246
+ }
247
+ /**
248
+ * Listens on `root` (capturing, so the page's own handlers never see a pick)
249
+ * and returns what stops it.
250
+ */
251
+ function attachElementPicker(root, options) {
252
+ var win = root.ownerDocument.defaultView;
253
+ var last = null;
254
+ var picking = function picking(event) {
255
+ return options.active() || held(event, options.modifier);
256
+ };
257
+ var target = function target(event) {
258
+ return event.target instanceof Element && root.contains(event.target) ? event.target : null;
259
+ };
260
+ var onMove = function onMove(event) {
261
+ last = {
262
+ x: event.clientX,
263
+ y: event.clientY
264
+ };
265
+ options.onHover(picking(event) ? target(event) : null);
266
+ };
267
+ var onClick = function onClick(event) {
268
+ if (!picking(event)) return;
269
+ var element = target(event);
270
+ if (element === null) return;
271
+ event.preventDefault();
272
+ event.stopPropagation();
273
+ options.onPick(element);
274
+ };
275
+ // Pressing the modifier with the pointer still: outline what is under it
276
+ // now, rather than after the next move.
277
+ var onKey = function onKey(event) {
278
+ if (!picking(event)) {
279
+ options.onHover(null);
280
+ return;
281
+ }
282
+ if (last === null) return;
283
+ var under = root.ownerDocument.elementFromPoint(last.x, last.y);
284
+ options.onHover(under !== null && root.contains(under) ? under : null);
285
+ };
286
+ var onLeave = function onLeave() {
287
+ last = null;
288
+ options.onHover(null);
289
+ };
290
+ root.addEventListener("mousemove", onMove, true);
291
+ root.addEventListener("click", onClick, true);
292
+ root.addEventListener("mouseleave", onLeave);
293
+ win === null || win === void 0 || win.addEventListener("keydown", onKey, true);
294
+ win === null || win === void 0 || win.addEventListener("keyup", onKey, true);
295
+ return function () {
296
+ root.removeEventListener("mousemove", onMove, true);
297
+ root.removeEventListener("click", onClick, true);
298
+ root.removeEventListener("mouseleave", onLeave);
299
+ win === null || win === void 0 || win.removeEventListener("keydown", onKey, true);
300
+ win === null || win === void 0 || win.removeEventListener("keyup", onKey, true);
301
+ };
302
+ }
303
+
304
+ /**
305
+ * The outlines over the page while picking: the element under the pointer,
306
+ * named, and the one picked.
307
+ *
308
+ * Boxes `position: fixed` on top of everything, with `pointer-events: none`,
309
+ * appended to the page's body -- not outlines on the elements themselves.
310
+ * The canvas's field outlines are a stylesheet so they cannot disturb the
311
+ * page's layout (`ValCanvasBridge`); a fixed box is out of the flow, so it
312
+ * cannot either, and it is the one way to put a NAME beside any element, which
313
+ * a selector cannot reach.
314
+ *
315
+ * In a shadow root of one host element: the bridge re-measures the page on
316
+ * every `style` change under `body`, and a box that follows the pointer
317
+ * changes its style every frame. Inside a shadow root those changes are not
318
+ * the page's, so the host's one insertion is all the bridge sees.
319
+ */
320
+
321
+ var TOP = "2147483647";
322
+ function createPickerOverlay(doc, initialColor) {
323
+ var color = initialColor;
324
+ var shown = {
325
+ hover: null,
326
+ picked: null
327
+ };
328
+ var host = doc.createElement("div");
329
+ host.setAttribute("data-val-picker", "");
330
+ var shadow = host.attachShadow({
331
+ mode: "open"
332
+ });
333
+ doc.body.appendChild(host);
334
+ var box = function box(kind) {
335
+ var el = doc.createElement("div");
336
+ el.setAttribute("data-kind", kind);
337
+ Object.assign(el.style, {
338
+ position: "fixed",
339
+ pointerEvents: "none",
340
+ zIndex: TOP,
341
+ boxSizing: "border-box",
342
+ display: "none"
343
+ });
344
+ shadow.appendChild(el);
345
+ return el;
346
+ };
347
+ var boxes = {
348
+ hover: box("hover"),
349
+ picked: box("picked")
350
+ };
351
+ var label = doc.createElement("span");
352
+ Object.assign(label.style, {
353
+ position: "absolute",
354
+ left: "-2px",
355
+ padding: "1px 4px",
356
+ borderRadius: "2px",
357
+ font: "500 10px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace",
358
+ color: "#fff",
359
+ whiteSpace: "nowrap"
360
+ });
361
+ boxes.hover.appendChild(label);
362
+ // The pointer says it is picking, and a link under it is not a link.
363
+ var cursor = doc.createElement("style");
364
+ cursor.textContent = "[data-val-picking] * { cursor: crosshair !important; }";
365
+ doc.head.appendChild(cursor);
366
+ var place = function place(kind) {
367
+ var element = shown[kind];
368
+ var el = boxes[kind];
369
+ if (element === null || !element.isConnected) {
370
+ el.style.display = "none";
371
+ return;
372
+ }
373
+ var rect = element.getBoundingClientRect();
374
+ Object.assign(el.style, {
375
+ display: "block",
376
+ top: "".concat(rect.top - 2, "px"),
377
+ left: "".concat(rect.left - 2, "px"),
378
+ width: "".concat(rect.width + 4, "px"),
379
+ height: "".concat(rect.height + 4, "px"),
380
+ border: "2px ".concat(kind === "hover" ? "solid" : "dashed", " ").concat(color)
381
+ });
382
+ if (kind === "hover") {
383
+ label.textContent = elementLabel(element);
384
+ label.style.background = color;
385
+ // Above the box, unless that is off the top of the window.
386
+ if (rect.top >= 18) {
387
+ label.style.top = "";
388
+ label.style.bottom = "100%";
389
+ } else {
390
+ label.style.bottom = "";
391
+ label.style.top = "100%";
392
+ }
393
+ }
394
+ };
395
+ var frame = null;
396
+ var win = doc.defaultView;
397
+ var reposition = function reposition() {
398
+ if (frame !== null || win === null) return;
399
+ frame = win.requestAnimationFrame(function () {
400
+ frame = null;
401
+ place("hover");
402
+ place("picked");
403
+ });
404
+ };
405
+ win === null || win === void 0 || win.addEventListener("scroll", reposition, true);
406
+ win === null || win === void 0 || win.addEventListener("resize", reposition);
407
+ return {
408
+ show: function show(kind, element) {
409
+ shown[kind] = element;
410
+ if (kind === "hover") {
411
+ if (element !== null) {
412
+ doc.documentElement.setAttribute("data-val-picking", "");
413
+ } else {
414
+ doc.documentElement.removeAttribute("data-val-picking");
415
+ }
416
+ }
417
+ place(kind);
418
+ },
419
+ setColor: function setColor(next) {
420
+ color = next;
421
+ place("hover");
422
+ place("picked");
423
+ },
424
+ destroy: function destroy() {
425
+ if (frame !== null) win === null || win === void 0 || win.cancelAnimationFrame(frame);
426
+ win === null || win === void 0 || win.removeEventListener("scroll", reposition, true);
427
+ win === null || win === void 0 || win.removeEventListener("resize", reposition);
428
+ host.remove();
429
+ cursor.remove();
430
+ doc.documentElement.removeAttribute("data-val-picking");
431
+ }
432
+ };
433
+ }
434
+
435
+ /** `h2.mb-6.text-2xl`: what the hover box is named. */
436
+ function elementLabel(element) {
437
+ var classes = Array.from(element.classList).slice(0, 2);
438
+ return [element.tagName.toLowerCase()].concat(createForOfIteratorHelper._toConsumableArray(classes)).join(".");
439
+ }
440
+ /**
441
+ * Picking on a page, whole: the listeners, the outlines, and Esc to stop
442
+ * picking without the modifier. What a canvas bridge mounts.
443
+ */
444
+ function startElementPicking(root, options) {
445
+ var _doc$defaultView;
446
+ var doc = root.ownerDocument;
447
+ var overlay = createPickerOverlay(doc, options.color);
448
+ var detach = attachElementPicker(root, {
449
+ modifier: options.modifier,
450
+ active: options.active,
451
+ onHover: function onHover(element) {
452
+ return overlay.show("hover", element);
453
+ },
454
+ onPick: function onPick(element) {
455
+ overlay.show("hover", null);
456
+ overlay.show("picked", element);
457
+ options.onPick(describeElement(element, root));
458
+ }
459
+ });
460
+ var onKey = function onKey(event) {
461
+ if (event.key === "Escape" && options.active()) options.onStop();
462
+ };
463
+ (_doc$defaultView = doc.defaultView) === null || _doc$defaultView === void 0 || _doc$defaultView.addEventListener("keydown", onKey);
464
+ return {
465
+ clearPicked: function clearPicked() {
466
+ return overlay.show("picked", null);
467
+ },
468
+ setColor: overlay.setColor,
469
+ stop: function stop() {
470
+ var _doc$defaultView2;
471
+ (_doc$defaultView2 = doc.defaultView) === null || _doc$defaultView2 === void 0 || _doc$defaultView2.removeEventListener("keydown", onKey);
472
+ detach();
473
+ overlay.destroy();
474
+ }
475
+ };
476
+ }
477
+
153
478
  function parseRoutePattern(pattern) {
154
479
  if (pattern === "" || pattern === "/") {
155
480
  return [];
@@ -398,6 +723,10 @@ exports.VAL_CANVAS_MESSAGE = VAL_CANVAS_MESSAGE;
398
723
  exports.VAL_CANVAS_PARAM = VAL_CANVAS_PARAM;
399
724
  exports.VAL_CONFIG_SESSION_STORAGE_KEY = VAL_CONFIG_SESSION_STORAGE_KEY;
400
725
  exports.VAL_THEME_SESSION_STORAGE_KEY = VAL_THEME_SESSION_STORAGE_KEY;
726
+ exports.attachElementPicker = attachElementPicker;
727
+ exports.createPickerOverlay = createPickerOverlay;
728
+ exports.describeElement = describeElement;
729
+ exports.elementLabel = elementLabel;
401
730
  exports.formatRoutePattern = formatRoutePattern;
402
731
  exports.getNextAppRouterSourceFolder = getNextAppRouterSourceFolder;
403
732
  exports.getPageRouterSitemapTree = getPageRouterSitemapTree;
@@ -408,5 +737,7 @@ exports.isTanStackRoutesFolder = isTanStackRoutesFolder;
408
737
  exports.isValCanvasFrame = isValCanvasFrame;
409
738
  exports.isValCanvasPageMessage = isValCanvasPageMessage;
410
739
  exports.isValCanvasStudioMessage = isValCanvasStudioMessage;
740
+ exports.modifierLabel = modifierLabel;
411
741
  exports.parseRoutePattern = parseRoutePattern;
742
+ exports.startElementPicking = startElementPicking;
412
743
  exports.withValCanvasParam = withValCanvasParam;