@valbuild/tanstack 0.153.6 → 0.154.1

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.
@@ -6,7 +6,7 @@ var asyncToGenerator = require('./asyncToGenerator-8e5c36c8.cjs.prod.js');
6
6
  var _typeof = require('./typeof-56656438.cjs.prod.js');
7
7
  var core = require('@valbuild/core');
8
8
  var stega = require('@valbuild/react/stega');
9
- var version = require('./version-c72b9e12.cjs.prod.js');
9
+ var version = require('./version-04b9b5af.cjs.prod.js');
10
10
  var internal = require('@valbuild/react/internal');
11
11
  var ui = require('@valbuild/ui');
12
12
  var reactRouter = require('@tanstack/react-router');
@@ -263,6 +263,13 @@ var DEFAULT_SELECTION = {
263
263
  */
264
264
  var RESCAN_INTERVAL_MS = 5000;
265
265
 
266
+ /**
267
+ * This document, among the ones the frame loads: sent with every `ready`
268
+ * (see `document` there). Made when the module is evaluated, which is once
269
+ * per document. Not `crypto.randomUUID`, which an insecure context lacks.
270
+ */
271
+ var DOCUMENT_ID = "".concat(Date.now().toString(36), "-").concat(Math.random().toString(36).slice(2));
272
+
266
273
  /**
267
274
  * The page's half of the canvas protocol.
268
275
  *
@@ -327,6 +334,50 @@ function ValCanvasBridge(_ref) {
327
334
  return (_stopRestoring$curren = stopRestoring.current) === null || _stopRestoring$curren === void 0 ? void 0 : _stopRestoring$curren.call(stopRestoring);
328
335
  };
329
336
  }, []);
337
+ /**
338
+ * The field being edited on the page itself (`editInline`), or the last
339
+ * one that was, while its copy waits for the page to catch up. See
340
+ * `inlineEditing.ts` in `@valbuild/shared`, which both bindings run.
341
+ */
342
+ var inlineEditing = React__default["default"].useRef(null);
343
+ React__default["default"].useEffect(function () {
344
+ return function () {
345
+ var _inlineEditing$curren;
346
+ return (_inlineEditing$curren = inlineEditing.current) === null || _inlineEditing$curren === void 0 ? void 0 : _inlineEditing$curren.release();
347
+ };
348
+ }, []);
349
+ /**
350
+ * The last pick: the element and where on it. `editInline` is the Studio's
351
+ * answer to a `clicked`, and the caret belongs where that click was.
352
+ */
353
+ var lastPick = React__default["default"].useRef(null);
354
+ /**
355
+ * The page's texts as the Studio last said (`textFields`), and the origin
356
+ * that said it, which is the only one an edit started from them answers.
357
+ * `null` until it says: a Studio that predates it never does, and every
358
+ * click then asks, as before.
359
+ */
360
+ var textFields = React__default["default"].useRef(null);
361
+ /**
362
+ * Whether the edit in progress was started here, in a click, rather than
363
+ * by `editInline`: an `editInline` that arrives after it answers a click
364
+ * from before it, and must not take the caret away.
365
+ */
366
+ var startedOnClick = React__default["default"].useRef(false);
367
+ /**
368
+ * The keys pressed since a click the Studio was asked about, until it
369
+ * answers: see `inlineEditKeys.ts`.
370
+ */
371
+ var heldKeys = React__default["default"].useRef(null);
372
+ React__default["default"].useEffect(function () {
373
+ return function () {
374
+ var _heldKeys$current;
375
+ (_heldKeys$current = heldKeys.current) === null || _heldKeys$current === void 0 || _heldKeys$current.release();
376
+ };
377
+ }, []);
378
+ // Read when an edit starts, long after the render that set it.
379
+ var selectionColor = React__default["default"].useRef(selectionColors.selection);
380
+ selectionColor.current = selectionColors.selection;
330
381
  var post = React__default["default"].useCallback(function (message) {
331
382
  // `*` as the target origin: the studio and the page share an origin today,
332
383
  // but the frame does not know the parent's origin for certain and naming
@@ -348,6 +399,10 @@ function ValCanvasBridge(_ref) {
348
399
 
349
400
  /** Every element Val tagged, with where it is on the page. */
350
401
  var scan = React__default["default"].useCallback(function () {
402
+ var _textFields$current$e, _textFields$current;
403
+ // Before measuring: an emptied text measures with the room it is given
404
+ // while Select is on, so the Studio's outline of it has a size too.
405
+ client.markEmptyTextFields(document, pickingRef.current ? (_textFields$current$e = (_textFields$current = textFields.current) === null || _textFields$current === void 0 ? void 0 : _textFields$current.empty) !== null && _textFields$current$e !== void 0 ? _textFields$current$e : NO_PATHS : NO_PATHS, core.Internal.splitJoinedSourcePaths);
351
406
  var elements = [];
352
407
  var scrollX = window.scrollX;
353
408
  var scrollY = window.scrollY;
@@ -438,6 +493,7 @@ function ValCanvasBridge(_ref) {
438
493
  // What this bridge answers, so the Studio never waits on a request
439
494
  // it would not: see `capabilities` in the protocol.
440
495
  capabilities: _toConsumableArray(client.BRIDGE_CAPABILITIES),
496
+ document: DOCUMENT_ID,
441
497
  bridge: {
442
498
  "package": "@valbuild/tanstack",
443
499
  version: version.VERSION
@@ -555,6 +611,22 @@ function ValCanvasBridge(_ref) {
555
611
  setPicking(message.picking);
556
612
  return;
557
613
  }
614
+ if (message.type === "textFields") {
615
+ // What starts an edit on a click, so as `editInline`: only from the
616
+ // frame's parent, when that is the Studio here.
617
+ if (event.source !== window.parent) return;
618
+ if (event.origin !== window.location.origin) return;
619
+ textFields.current = {
620
+ editing: message.editing,
621
+ editable: new Map(message.editable.map(function (candidate) {
622
+ return [candidate.path, candidate];
623
+ })),
624
+ empty: new Set(message.empty),
625
+ origin: event.origin
626
+ };
627
+ scan();
628
+ return;
629
+ }
558
630
  if (message.type === "pickElements") {
559
631
  var _message$color;
560
632
  setElementPicking({
@@ -597,15 +669,50 @@ function ValCanvasBridge(_ref) {
597
669
  window.parent.postMessage(readPageLogReply(message), _asker);
598
670
  return;
599
671
  }
672
+ if (message.type === "interact") {
673
+ // The assistant's `interact_page`: do something on the page and
674
+ // read it once it settled (`pageInteraction.ts`) -- a click or
675
+ // Enter/Space only once the person allowed it. As
676
+ // `readPage`: only for the frame's parent, when that is the Studio
677
+ // here, and answered to it alone.
678
+ if (event.source !== window.parent) return;
679
+ var _asker2 = event.origin;
680
+ if (_asker2 !== window.location.origin) return;
681
+ var requestId = message.requestId;
682
+ void client.interactWithPage(window, _typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2({
683
+ action: message.action
684
+ }, message.maxItems !== undefined ? {
685
+ maxItems: message.maxItems
686
+ } : {}), message.timeoutMs !== undefined ? {
687
+ timeoutMs: message.timeoutMs
688
+ } : {}), message.logFrom !== undefined ? {
689
+ logFrom: message.logFrom
690
+ } : {}), message.approved !== undefined ? {
691
+ approved: message.approved
692
+ } : {}), {}, {
693
+ document: DOCUMENT_ID
694
+ }), pageLog()).then(function (outcome) {
695
+ var answer = {
696
+ val: client.VAL_CANVAS_MESSAGE,
697
+ type: "interacted",
698
+ requestId: requestId,
699
+ outcome: outcome,
700
+ // Which document answered: see `interacted` in the protocol.
701
+ document: DOCUMENT_ID
702
+ };
703
+ window.parent.postMessage(answer, _asker2);
704
+ });
705
+ return;
706
+ }
600
707
  if (message.type === "screenshotPage") {
601
708
  // The assistant's `screenshot_page`: a picture of the page, drawn
602
709
  // here. As `readPage`: only for the frame's parent, when that is the
603
710
  // Studio here, and answered to it alone.
604
711
  if (event.source !== window.parent) return;
605
- var _asker2 = event.origin;
606
- if (_asker2 !== window.location.origin) return;
712
+ var _asker3 = event.origin;
713
+ if (_asker3 !== window.location.origin) return;
607
714
  void screenshotReply(message).then(function (reply) {
608
- return window.parent.postMessage(reply, _asker2);
715
+ return window.parent.postMessage(reply, _asker3);
609
716
  });
610
717
  return;
611
718
  }
@@ -656,6 +763,98 @@ function ValCanvasBridge(_ref) {
656
763
  }))();
657
764
  return;
658
765
  }
766
+ if (message.type === "stopInlineEdit") {
767
+ var _inlineEditing$curren2;
768
+ (_inlineEditing$curren2 = inlineEditing.current) === null || _inlineEditing$curren2 === void 0 || _inlineEditing$curren2.stop();
769
+ return;
770
+ }
771
+ if (message.type === "focusInlineEdit") {
772
+ var _inlineEditing$curren3;
773
+ (_inlineEditing$curren3 = inlineEditing.current) === null || _inlineEditing$curren3 === void 0 || _inlineEditing$curren3.focus();
774
+ return;
775
+ }
776
+ if (message.type === "applyInlineEdit") {
777
+ // What one of the Studio's controls made of the text being typed
778
+ // in. It replaces what is on the page, so as `editInline`: only
779
+ // from the frame's parent, when that is the Studio here.
780
+ if (event.source !== window.parent) return;
781
+ if (event.origin !== window.location.origin) return;
782
+ var editing = inlineEditing.current;
783
+ if (editing === null || !editing.isActive()) return;
784
+ if (editing.path !== message.path) return;
785
+ editing.apply(message.value, message.selection);
786
+ return;
787
+ }
788
+ if (message.type === "markInlineEdit") {
789
+ // Bold pressed with only a caret: for what is typed next. From the
790
+ // Studio only, as `applyInlineEdit`.
791
+ if (event.source !== window.parent) return;
792
+ if (event.origin !== window.location.origin) return;
793
+ var _editing = inlineEditing.current;
794
+ if (_editing === null || !_editing.isActive()) return;
795
+ if (_editing.path !== message.path) return;
796
+ _editing.mark(message.styles, message.selection);
797
+ return;
798
+ }
799
+ if (message.type === "editInline") {
800
+ var _held$release, _inlineEditing$curren4, _inlineEditing$curren5;
801
+ // A field's words go back as they are typed, unpublished ones
802
+ // included. As `readPage`: only for the frame's parent, when that
803
+ // is the Studio here, and answered to it alone.
804
+ if (event.source !== window.parent) return;
805
+ var _asker4 = event.origin;
806
+ if (_asker4 !== window.location.origin) return;
807
+ var reply = function reply(answer) {
808
+ return window.parent.postMessage(answer, _asker4);
809
+ };
810
+ var pick = lastPick.current;
811
+ lastPick.current = null;
812
+ var held = heldKeys.current;
813
+ heldKeys.current = null;
814
+ var keys = (_held$release = held === null || held === void 0 ? void 0 : held.release()) !== null && _held$release !== void 0 ? _held$release : [];
815
+ // The answer to a click from before the one that started the edit
816
+ // in progress, by itself: that click is what the person did last.
817
+ if (pick === null && startedOnClick.current && ((_inlineEditing$curren4 = inlineEditing.current) === null || _inlineEditing$curren4 === void 0 ? void 0 : _inlineEditing$curren4.isActive()) === true) {
818
+ reply({
819
+ val: client.VAL_CANVAS_MESSAGE,
820
+ type: "inlineEditUnavailable",
821
+ requestId: message.requestId
822
+ });
823
+ return;
824
+ }
825
+ // One edit at a time, and the element of the last one back as the
826
+ // page has it: it may be the very one asked for again.
827
+ (_inlineEditing$curren5 = inlineEditing.current) === null || _inlineEditing$curren5 === void 0 || _inlineEditing$curren5.release();
828
+ inlineEditing.current = null;
829
+ startedOnClick.current = false;
830
+ var started = startFirst(message.candidates, pick, {
831
+ color: selectionColor.current,
832
+ reply: reply
833
+ });
834
+ if (started !== null) {
835
+ inlineEditing.current = started.editing;
836
+ reply({
837
+ val: client.VAL_CANVAS_MESSAGE,
838
+ type: "inlineEditStarted",
839
+ requestId: message.requestId,
840
+ path: started.path
841
+ });
842
+ // What was typed while the Studio was being asked, now that
843
+ // there is somewhere for it to go: after `inlineEditStarted`,
844
+ // so the Studio is listening for what it types.
845
+ client.replayKeys(document, keys);
846
+ return;
847
+ }
848
+ // Nothing on the page shows one of them and nothing else: the
849
+ // Studio opens the field in its own column, and the keys pressed
850
+ // meanwhile are dropped, as on a page that is not being typed in.
851
+ reply({
852
+ val: client.VAL_CANVAS_MESSAGE,
853
+ type: "inlineEditUnavailable",
854
+ requestId: message.requestId
855
+ });
856
+ return;
857
+ }
659
858
  if (message.type === "theme") {
660
859
  setSelectionColors({
661
860
  selection: message.selection,
@@ -771,18 +970,85 @@ function ValCanvasBridge(_ref) {
771
970
  */
772
971
  React__default["default"].useEffect(function () {
773
972
  var onClick = function onClick(event) {
774
- if (!pickingRef.current) return;
973
+ var _heldKeys$current2;
775
974
  var target = event.target;
776
975
  if (!(target instanceof Element)) return;
976
+ // A click in the field being typed in places the caret, and that is
977
+ // all it does: it is neither a pick nor a click on the link or button
978
+ // the text happens to be in. (The caret is the mousedown's, so
979
+ // cancelling the click leaves it where it was put.)
980
+ var editing = inlineEditing.current;
981
+ if (editing !== null && editing.isActive() && editing.contains(target)) {
982
+ event.preventDefault();
983
+ event.stopPropagation();
984
+ return;
985
+ }
986
+ if (!pickingRef.current) return;
777
987
  var tagged = target.closest("[data-val-path]");
778
988
  var attribute = tagged === null || tagged === void 0 ? void 0 : tagged.getAttribute("data-val-path");
779
- if (!attribute) return;
989
+ if (!tagged || !attribute) return;
780
990
  event.preventDefault();
781
991
  event.stopPropagation();
992
+ var paths = core.Internal.splitJoinedSourcePaths(attribute);
993
+ var pick = {
994
+ element: tagged,
995
+ x: event.clientX,
996
+ y: event.clientY
997
+ };
998
+ (_heldKeys$current2 = heldKeys.current) === null || _heldKeys$current2 === void 0 || _heldKeys$current2.release();
999
+ heldKeys.current = null;
1000
+ /*
1001
+ * Select & edit, and the Studio has said which of this element's
1002
+ * fields are texts and what they hold: the edit starts HERE, in the
1003
+ * click, rather than a round trip across the frame later. Keys typed
1004
+ * straight after the click then go into the text -- in that round
1005
+ * trip they went to the page's body, which drops them -- and a phone
1006
+ * opens its keyboard, which it does only for a focus given in the tap.
1007
+ * Which element shows a field is still decided as `editInline` decides
1008
+ * it (`canEditInline`), from the values the Studio sent.
1009
+ */
1010
+ var known = textFields.current;
1011
+ var offered = known !== null && known.editing ? paths.flatMap(function (path) {
1012
+ var _known$editable$get;
1013
+ return (_known$editable$get = known.editable.get(path)) !== null && _known$editable$get !== void 0 ? _known$editable$get : [];
1014
+ }) : [];
1015
+ if (known !== null && offered.length > 0) {
1016
+ var _inlineEditing$curren6;
1017
+ var reply = function reply(answer) {
1018
+ return window.parent.postMessage(answer, known.origin);
1019
+ };
1020
+ (_inlineEditing$curren6 = inlineEditing.current) === null || _inlineEditing$curren6 === void 0 || _inlineEditing$curren6.release();
1021
+ inlineEditing.current = null;
1022
+ startedOnClick.current = false;
1023
+ var started = startFirst(offered, pick, {
1024
+ color: selectionColor.current,
1025
+ reply: reply
1026
+ });
1027
+ if (started !== null) {
1028
+ inlineEditing.current = started.editing;
1029
+ startedOnClick.current = true;
1030
+ lastPick.current = null;
1031
+ reply({
1032
+ val: client.VAL_CANVAS_MESSAGE,
1033
+ type: "inlineEditStarted",
1034
+ requestId: null,
1035
+ path: started.path
1036
+ });
1037
+ return;
1038
+ }
1039
+ /*
1040
+ * A text, but not as the Studio last said: its value changed since
1041
+ * (an edit a moment ago, still on its way). The Studio is asked, as
1042
+ * for any pick, and the keys pressed until it answers are held
1043
+ * rather than lost.
1044
+ */
1045
+ heldKeys.current = client.holdKeys(window);
1046
+ }
1047
+ lastPick.current = pick;
782
1048
  post({
783
1049
  val: client.VAL_CANVAS_MESSAGE,
784
1050
  type: "clicked",
785
- paths: core.Internal.splitJoinedSourcePaths(attribute)
1051
+ paths: paths
786
1052
  });
787
1053
  };
788
1054
  document.addEventListener("click", onClick, {
@@ -795,6 +1061,17 @@ function ValCanvasBridge(_ref) {
795
1061
  };
796
1062
  }, [post]);
797
1063
 
1064
+ // Select off: an emptied text has its room taken away, and keys held for
1065
+ // an answer to a pick are dropped. Select on: it has its room again.
1066
+ React__default["default"].useEffect(function () {
1067
+ if (!picking) {
1068
+ var _heldKeys$current3;
1069
+ (_heldKeys$current3 = heldKeys.current) === null || _heldKeys$current3 === void 0 || _heldKeys$current3.release();
1070
+ heldKeys.current = null;
1071
+ }
1072
+ scan();
1073
+ }, [picking, scan]);
1074
+
798
1075
  // Esc while picking fields: the key goes to the page, which has the focus
799
1076
  // once it has been clicked, so the studio hears of it only from here.
800
1077
  React__default["default"].useEffect(function () {
@@ -1002,7 +1279,11 @@ function ValCanvasBridge(_ref) {
1002
1279
  // CSS string. A path contains quotes (`?p="/"`) and would otherwise
1003
1280
  // close the selector early.
1004
1281
  dangerouslySetInnerHTML: {
1005
- __html: [picking ? "[data-val-path] { outline: 1px solid ".concat(selectionColors.selectionSoft, "; outline-offset: 1px; cursor: pointer; }\n[data-val-path]:hover { outline: 2px solid ").concat(selectionColors.selection, "; }") : "", highlighted ? highlightRule(highlighted, selectionColors.selection) : ""].filter(Boolean).join("\n")
1282
+ __html: [picking ? "[data-val-path] { outline: 1px solid ".concat(selectionColors.selectionSoft, "; outline-offset: 1px; cursor: pointer; }\n[data-val-path]:hover { outline: 2px solid ").concat(selectionColors.selection, "; }") : "",
1283
+ // The one rule here that moves the page, and only while Select is
1284
+ // on: an emptied text's line of room, without which it cannot be
1285
+ // pointed at. See `emptyTextFields.ts` for why, and how narrowly.
1286
+ picking ? client.EMPTY_TEXT_RULES : "", highlighted ? highlightRule(highlighted, selectionColors.selection) : ""].filter(Boolean).join("\n")
1006
1287
  }
1007
1288
  });
1008
1289
  }
@@ -1099,23 +1380,108 @@ function isScrollable(el) {
1099
1380
  };
1100
1381
  return scrolls(style.overflowY) && el.scrollHeight > el.clientHeight || scrolls(style.overflowX) && el.scrollWidth > el.clientWidth;
1101
1382
  }
1383
+ var NO_PATHS = new Set();
1384
+
1385
+ /**
1386
+ * Start editing the first of `candidates` an element on the page shows and
1387
+ * nothing else: the picked element when it carries the candidate's path,
1388
+ * else the first element that does. `null` when none can be.
1389
+ *
1390
+ * What `editInline` does, and what a click does by itself when the Studio has
1391
+ * said ahead of it what the element's texts hold (`textFields`): one way to
1392
+ * start, whichever asked.
1393
+ */
1394
+ function startFirst(candidates, pick, options) {
1395
+ var reply = options.reply;
1396
+ var _iterator = _typeof._createForOfIteratorHelper(candidates),
1397
+ _step;
1398
+ try {
1399
+ var _loop = function _loop() {
1400
+ var candidate = _step.value;
1401
+ var picked = pick !== null && pick.element.isConnected && pathOn(pick.element, candidate.path) !== null ? pick.element : null;
1402
+ var element = picked !== null && picked !== void 0 ? picked : findByPath(candidate.path);
1403
+ if (element === null) return 0; // continue
1404
+ var path = pathOn(element, candidate.path);
1405
+ if (path === null) return 0; // continue
1406
+ var editing = client.startInlineEditing(element, candidate, {
1407
+ // Where the click was, when it was on this element.
1408
+ at: picked !== null && pick !== null ? {
1409
+ x: pick.x,
1410
+ y: pick.y
1411
+ } : null,
1412
+ color: options.color,
1413
+ onChange: function onChange(value) {
1414
+ return reply({
1415
+ val: client.VAL_CANVAS_MESSAGE,
1416
+ type: "inlineEdit",
1417
+ path: path,
1418
+ value: value
1419
+ });
1420
+ },
1421
+ onEnd: function onEnd() {
1422
+ return reply({
1423
+ val: client.VAL_CANVAS_MESSAGE,
1424
+ type: "inlineEditEnded",
1425
+ path: path
1426
+ });
1427
+ },
1428
+ onSelection: function onSelection(selection, rect, caret) {
1429
+ return reply({
1430
+ val: client.VAL_CANVAS_MESSAGE,
1431
+ type: "inlineEditSelection",
1432
+ path: path,
1433
+ selection: selection,
1434
+ rect: rect,
1435
+ caret: caret
1436
+ });
1437
+ }
1438
+ });
1439
+ if (editing !== null) return {
1440
+ v: {
1441
+ editing: editing,
1442
+ path: path
1443
+ }
1444
+ };
1445
+ },
1446
+ _ret;
1447
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
1448
+ _ret = _loop();
1449
+ if (_ret === 0) continue;
1450
+ if (_ret) return _ret.v;
1451
+ }
1452
+ } catch (err) {
1453
+ _iterator.e(err);
1454
+ } finally {
1455
+ _iterator.f();
1456
+ }
1457
+ return null;
1458
+ }
1102
1459
  function findByPath(path) {
1103
1460
  var elements = document.querySelectorAll("[data-val-path]");
1104
1461
  for (var _i = 0, _Array$from = Array.from(elements); _i < _Array$from.length; _i++) {
1105
1462
  var el = _Array$from[_i];
1106
- var attribute = el.getAttribute("data-val-path");
1107
- if (!attribute) continue;
1108
- // Compared as strings: the split returns branded `SourcePath`s and what
1109
- // came back over `postMessage` is a plain string, which is the same value.
1110
- if (core.Internal.splitJoinedSourcePaths(attribute).some(function (candidate) {
1111
- return candidate === path;
1112
- })) {
1113
- return el;
1114
- }
1463
+ if (pathOn(el, path) !== null) return el;
1115
1464
  }
1116
1465
  return null;
1117
1466
  }
1118
1467
 
1468
+ /**
1469
+ * `path` as `element` carries it, or null when it does not.
1470
+ *
1471
+ * Compared as strings: the split returns branded `SourcePath`s and what came
1472
+ * back over `postMessage` is a plain string, which is the same value. What is
1473
+ * returned is the element's own, so it is a `SourcePath` without anyone
1474
+ * having to say so.
1475
+ */
1476
+ function pathOn(element, path) {
1477
+ var _Internal$splitJoined;
1478
+ var attribute = element.getAttribute("data-val-path");
1479
+ if (!attribute) return null;
1480
+ return (_Internal$splitJoined = core.Internal.splitJoinedSourcePaths(attribute).find(function (candidate) {
1481
+ return candidate === path;
1482
+ })) !== null && _Internal$splitJoined !== void 0 ? _Internal$splitJoined : null;
1483
+ }
1484
+
1119
1485
  /** What `screenshotPage` answers: the picture, or why it could not be taken. */
1120
1486
  function screenshotReply(_x) {
1121
1487
  return _screenshotReply.apply(this, arguments);