@8ux-co/eelzap 0.10.2 → 0.11.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.
package/dist/preview.js CHANGED
@@ -4,9 +4,12 @@ import {
4
4
  EDITOR_MESSAGE_TYPES,
5
5
  FIELD_KEY_RE,
6
6
  MAX_ANCHORS,
7
+ MAX_ANCHOR_NTH,
7
8
  MAX_DRAFT_ROUTE_LENGTH,
8
9
  MAX_EMAIL_LENGTH,
9
10
  MAX_LABELS,
11
+ MAX_MARKS,
12
+ MAX_MARK_LABEL,
10
13
  MAX_MESSAGE_BYTES,
11
14
  MAX_PINS,
12
15
  MAX_QUOTE_CONTEXT,
@@ -32,6 +35,7 @@ import {
32
35
  emptyDropStats,
33
36
  envelope,
34
37
  findAnchorElement,
38
+ isDiffMark,
35
39
  isDomAnchor,
36
40
  isDraftRoutePath,
37
41
  isFieldKey,
@@ -60,7 +64,7 @@ import {
60
64
  textQuoteFor,
61
65
  utf8Length,
62
66
  zapAttrs
63
- } from "./chunk-T6S3S5RH.js";
67
+ } from "./chunk-G2JAAZKS.js";
64
68
  import {
65
69
  NEST_PRODUCTION_ORIGIN,
66
70
  PENDING_STORAGE_KEY,
@@ -77,7 +81,7 @@ var isLocalHost = (hostname) => LOCAL_ORIGIN.test(`http://${hostname}`);
77
81
 
78
82
  // src/preview/site/load.ts
79
83
  function loadChunk(name, _base) {
80
- return name === "signin" ? import("./signin-XOGBCGBL.js") : import("./suggest-KLS6B7AM.js");
84
+ return name === "signin" ? import("./signin-XOGBCGBL.js") : import("./suggest-FDD4MBRP.js");
81
85
  }
82
86
 
83
87
  // src/preview/site/boot.ts
@@ -213,6 +217,121 @@ function createPageBridge(options) {
213
217
  };
214
218
  }
215
219
 
220
+ // src/preview/scroll.ts
221
+ var round = (value) => Math.round(value * 1e4) / 1e4;
222
+ function createScrollSync(win, doc, index, post) {
223
+ let enabled = false;
224
+ let frame = null;
225
+ let last = "";
226
+ let placed = null;
227
+ let target = null;
228
+ const at = () => `${Math.round(win.scrollX)},${Math.round(win.scrollY)}`;
229
+ const range = (size, view) => Math.max(0, size - view);
230
+ const nextFrame = (fn) => typeof win.requestAnimationFrame === "function" ? win.requestAnimationFrame(fn) : win.setTimeout(fn, 16);
231
+ function fieldRef(tagged) {
232
+ const { recordRef, fieldKey } = tagged;
233
+ return { recordRef, fieldKey, nth: index.byField(recordRef, fieldKey).indexOf(tagged) };
234
+ }
235
+ function topOf(ref) {
236
+ const element = ref && index.byField(ref.recordRef, ref.fieldKey)[ref.nth]?.element;
237
+ return element?.isConnected ? element.getBoundingClientRect().top : void 0;
238
+ }
239
+ function measure() {
240
+ const root = doc.documentElement;
241
+ const fraction = (pos, size, view) => {
242
+ const max = range(size, view);
243
+ return max > 0 ? round(Math.min(1, Math.max(0, pos / max))) : 0;
244
+ };
245
+ const payload = { y: fraction(win.scrollY, root.scrollHeight, win.innerHeight) };
246
+ if (range(root.scrollWidth, win.innerWidth) > 0) {
247
+ payload.x = fraction(win.scrollX, root.scrollWidth, win.innerWidth);
248
+ }
249
+ let above = null;
250
+ let below = null;
251
+ let up = -Infinity;
252
+ let down = Infinity;
253
+ for (const tagged of index.elements) {
254
+ const { top, height } = tagged.element.getBoundingClientRect();
255
+ if (height <= 0) continue;
256
+ if (top > 0) {
257
+ if (top < down) {
258
+ below = tagged;
259
+ down = top;
260
+ }
261
+ } else if (top > up) {
262
+ above = tagged;
263
+ up = top;
264
+ }
265
+ }
266
+ const anchor = above ?? (down < win.innerHeight ? below : null);
267
+ if (anchor) {
268
+ payload.anchor = { ...fieldRef(anchor), offset: Math.round(above ? up : down) };
269
+ if (above && below) {
270
+ payload.next = fieldRef(below);
271
+ payload.progress = round(-up / (down - up));
272
+ }
273
+ }
274
+ return payload;
275
+ }
276
+ function flush() {
277
+ frame = null;
278
+ if (!enabled) return;
279
+ if (at() === placed) return;
280
+ placed = null;
281
+ const payload = measure();
282
+ const key = JSON.stringify(payload);
283
+ if (key === last) return;
284
+ last = key;
285
+ post(payload);
286
+ }
287
+ function apply() {
288
+ const next = target;
289
+ target = null;
290
+ if (!next) return;
291
+ const root = doc.documentElement;
292
+ const { y, x, anchor, progress } = next;
293
+ let top = y * range(root.scrollHeight, win.innerHeight);
294
+ const from = topOf(anchor);
295
+ const to = topOf(next.next);
296
+ if (from !== void 0) {
297
+ top = win.scrollY + from + (to !== void 0 && progress !== void 0 ? progress * (to - from) : -anchor.offset);
298
+ }
299
+ const left = x === void 0 ? win.scrollX : x * range(root.scrollWidth, win.innerWidth);
300
+ try {
301
+ win.scrollTo({ top, left, behavior: "instant" });
302
+ } catch {
303
+ win.scrollTo(left, top);
304
+ }
305
+ placed = at();
306
+ last = JSON.stringify(measure());
307
+ }
308
+ const onScroll = () => {
309
+ if (!enabled || frame !== null) return;
310
+ frame = nextFrame(flush);
311
+ };
312
+ return {
313
+ setEnabled(next) {
314
+ if (next === enabled) return;
315
+ enabled = next;
316
+ if (next) {
317
+ win.addEventListener("scroll", onScroll, { passive: true });
318
+ last = "";
319
+ placed = null;
320
+ flush();
321
+ } else win.removeEventListener("scroll", onScroll);
322
+ },
323
+ scrollTo(next) {
324
+ if (!target) nextFrame(apply);
325
+ target = next;
326
+ },
327
+ destroy() {
328
+ enabled = false;
329
+ target = null;
330
+ win.removeEventListener("scroll", onScroll);
331
+ }
332
+ };
333
+ }
334
+
216
335
  // src/preview/sanitize.ts
217
336
  var RICH_TEXT_ALLOWED_TAGS = /* @__PURE__ */ new Set([
218
337
  "p",
@@ -383,6 +502,7 @@ var ValueApplier = class {
383
502
  }
384
503
  write(tagged, locale, value) {
385
504
  if (tagged.locale && tagged.locale !== locale) return false;
505
+ if (tagged.siteRenders) return false;
386
506
  const element = tagged.element;
387
507
  if (tagged.textNode) {
388
508
  const text2 = plainText(value);
@@ -419,7 +539,7 @@ var ValueApplier = class {
419
539
  function plainText(value) {
420
540
  if (value === null || typeof value !== "object") return value === null ? "" : String(value);
421
541
  const v = value;
422
- return v.text ?? v.url ?? v.email ?? null;
542
+ return v.text ?? v.label ?? v.url ?? v.email ?? null;
423
543
  }
424
544
  function writeLink(element, value, base) {
425
545
  const email = "email" in value;
@@ -472,7 +592,7 @@ function writeImage(element, image, base) {
472
592
  }
473
593
 
474
594
  // src/preview/runtime.ts
475
- var CLIENT_VERSION = "0.10.0";
595
+ var CLIENT_VERSION = "0.11.1";
476
596
  var MAX_PROBLEM_REPORTS = 20;
477
597
  function startRuntime(config, options = {}) {
478
598
  const win = options.win ?? window;
@@ -525,6 +645,13 @@ function startRuntime(config, options = {}) {
525
645
  config.onMessage?.(message);
526
646
  }
527
647
  });
648
+ const scroll = createScrollSync(win, doc, index, (payload) => bridge.post("zap:scroll", payload));
649
+ let marks = [];
650
+ const drawMarks = () => overlay.setMarks(
651
+ marks.flatMap(
652
+ ({ recordRef, fieldKey, tone, label, active }) => index.byField(recordRef, fieldKey).map(({ element }) => ({ element, tone, label, active: !!active }))
653
+ )
654
+ );
528
655
  function handle(message) {
529
656
  switch (message.type) {
530
657
  case "zap:hello": {
@@ -572,6 +699,16 @@ function startRuntime(config, options = {}) {
572
699
  overlay.setPins(pins);
573
700
  return;
574
701
  }
702
+ case "zap:scroll-sync":
703
+ scroll.setEnabled(message.payload.enabled);
704
+ return;
705
+ case "zap:scroll-to":
706
+ scroll.scrollTo(message.payload);
707
+ return;
708
+ case "zap:marks":
709
+ marks = message.payload.marks;
710
+ drawMarks();
711
+ return;
575
712
  }
576
713
  }
577
714
  function report(why, force = false) {
@@ -608,6 +745,7 @@ function startRuntime(config, options = {}) {
608
745
  function refresh(page = true) {
609
746
  if (!scan(page)) return;
610
747
  values.reapply();
748
+ if (marks.length) drawMarks();
611
749
  report(page ? "mutation" : "reapply");
612
750
  const url = config.pageUrl();
613
751
  if (url !== lastUrl) {
@@ -667,6 +805,7 @@ function startRuntime(config, options = {}) {
667
805
  destroy() {
668
806
  for (const id of retries) win.clearTimeout(id);
669
807
  stopObserving();
808
+ scroll.destroy();
670
809
  win.removeEventListener("pagehide", onPageHide);
671
810
  win.removeEventListener("pageshow", onPageShow);
672
811
  overlay.destroy();
@@ -796,8 +935,13 @@ function initZap(options, runtimeOptions = {}) {
796
935
  if (token) store?.setItem(PREVIEW_TOKEN_STORAGE_KEY, token);
797
936
  else forgetPreviewToken(win);
798
937
  if (token && last && last !== token && draftRoute) {
799
- const query = new URLSearchParams({ token, path: win.location.pathname });
800
- void win.fetch(`${draftRoute}?${query}`, { credentials: "same-origin", redirect: "manual" }).catch(() => {
938
+ const body = new URLSearchParams({ token, path: win.location.pathname });
939
+ void win.fetch(draftRoute, {
940
+ method: "POST",
941
+ body,
942
+ credentials: "same-origin",
943
+ redirect: "manual"
944
+ }).catch(() => {
801
945
  });
802
946
  }
803
947
  } catch {
@@ -867,9 +1011,12 @@ export {
867
1011
  EDITOR_MESSAGE_TYPES,
868
1012
  FIELD_KEY_RE,
869
1013
  MAX_ANCHORS,
1014
+ MAX_ANCHOR_NTH,
870
1015
  MAX_DRAFT_ROUTE_LENGTH,
871
1016
  MAX_EMAIL_LENGTH,
872
1017
  MAX_LABELS,
1018
+ MAX_MARKS,
1019
+ MAX_MARK_LABEL,
873
1020
  MAX_MESSAGE_BYTES,
874
1021
  MAX_PINS,
875
1022
  MAX_QUOTE_CONTEXT,
@@ -906,6 +1053,7 @@ export {
906
1053
  findAnchorElement,
907
1054
  getPreviewToken,
908
1055
  initZap,
1056
+ isDiffMark,
909
1057
  isDomAnchor,
910
1058
  isDraftRoutePath,
911
1059
  isFieldKey,