livelocale 0.12.1 → 1.0.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.
@@ -268,15 +268,18 @@ function followNavigation(loc, href) {
268
268
  if (href === loc.href) loc.reload();
269
269
  else loc.assign(href);
270
270
  }
271
- var DEFAULT_STUDIO_ORIGIN, STUDIO_ORIGIN_STORAGE_KEY, MAX_VISIBLE_TEXTS, MAX_VISIBLE_TEXT_LENGTH, KEY_PATTERN, MAX_LOCATION_LENGTH, LOCATION_CONTROL_RE, LOCATION_PROBE_ORIGIN, IframeBridge;
271
+ var CLIENT_FEATURES, DEFAULT_STUDIO_ORIGIN, STUDIO_ORIGIN_STORAGE_KEY, MAX_VISIBLE_TEXTS, MAX_VISIBLE_TEXT_LENGTH, KEY_PATTERN, FEATURE_PATTERN, MAX_FEATURES, MAX_LOCATION_LENGTH, LOCATION_CONTROL_RE, LOCATION_PROBE_ORIGIN, IframeBridge;
272
272
  var init_iframe_bridge = __esm({
273
273
  "src/client/iframe-bridge.ts"() {
274
274
  "use strict";
275
+ CLIENT_FEATURES = ["edit-mode"];
275
276
  DEFAULT_STUDIO_ORIGIN = "http://localhost:3010";
276
277
  STUDIO_ORIGIN_STORAGE_KEY = "livelocale-studio-origin";
277
278
  MAX_VISIBLE_TEXTS = 100;
278
279
  MAX_VISIBLE_TEXT_LENGTH = 200;
279
280
  KEY_PATTERN = /^[a-zA-Z0-9._-]+$/;
281
+ FEATURE_PATTERN = /^[a-z0-9-]+$/;
282
+ MAX_FEATURES = 16;
280
283
  MAX_LOCATION_LENGTH = 2048;
281
284
  LOCATION_CONTROL_RE = /[\u0000-\u001f\u007f-\u009f]/;
282
285
  LOCATION_PROBE_ORIGIN = "http://livelocale.invalid";
@@ -298,7 +301,7 @@ var init_iframe_bridge = __esm({
298
301
  return;
299
302
  }
300
303
  }
301
- if (type === "VISIBLE_KEYS_CHANGED") {
304
+ if (type === "VISIBLE_KEYS_CHANGED" || type === "RENDERED_KEYS_CHANGED") {
302
305
  if (!Array.isArray(payload)) return;
303
306
  const validKeys = payload.filter(
304
307
  (k) => typeof k === "string" && /^[a-zA-Z0-9._-]+$/.test(k)
@@ -321,7 +324,15 @@ var init_iframe_bridge = __esm({
321
324
  }
322
325
  if (type === "READY" || type === "LOCATION_CHANGED") {
323
326
  const url = toLocationUrl(payload);
324
- payload = url === null ? {} : { url };
327
+ const next = url === null ? {} : { url };
328
+ const features = payload?.features;
329
+ if (type === "READY" && Array.isArray(features)) {
330
+ next.features = features.filter((f) => typeof f === "string" && FEATURE_PATTERN.test(f)).slice(0, MAX_FEATURES);
331
+ }
332
+ payload = next;
333
+ }
334
+ if (type === "TAP_UNKEYED") {
335
+ payload = void 0;
325
336
  }
326
337
  const message = {
327
338
  source: "livelocale-client",
@@ -375,16 +386,34 @@ function debounce(fn, wait) {
375
386
  };
376
387
  return debounced;
377
388
  }
378
- var DOMInterceptor;
389
+ function isRendered(el) {
390
+ const visible = typeof el.checkVisibility === "function" ? el.checkVisibility({ checkOpacity: true, checkVisibilityCSS: true }) : el.getClientRects().length > 0;
391
+ if (!visible) return false;
392
+ const rect = el.getBoundingClientRect();
393
+ return rect.width > 0 && rect.height > 0;
394
+ }
395
+ function hasOwnText(el) {
396
+ for (const child of el.childNodes) {
397
+ if (child.nodeType === Node.TEXT_NODE && (child.nodeValue ?? "").trim() !== "") return true;
398
+ }
399
+ return false;
400
+ }
401
+ var EDITABLE_SELECTOR, EDIT_STYLE_ID, EDIT_ATTRIBUTE, PICKED_ATTRIBUTE, REVEAL_MARGIN, DOMInterceptor;
379
402
  var init_dom_interceptor = __esm({
380
403
  "src/client/dom-interceptor.ts"() {
381
404
  "use strict";
382
405
  init_watermark();
383
406
  init_env();
384
407
  init_iframe_bridge();
408
+ EDITABLE_SELECTOR = 'input, textarea, select, [contenteditable]:not([contenteditable="false"])';
409
+ EDIT_STYLE_ID = "livelocale-edit-style";
410
+ EDIT_ATTRIBUTE = "data-livelocale-edit";
411
+ PICKED_ATTRIBUTE = "data-livelocale-picked";
412
+ REVEAL_MARGIN = 16;
385
413
  DOMInterceptor = class {
386
414
  onElementSelected;
387
415
  onVisibleKeysChanged;
416
+ onUnkeyedTap;
388
417
  hoveredElement = null;
389
418
  observer = null;
390
419
  inputSanitizersAttached = false;
@@ -393,11 +422,19 @@ var init_dom_interceptor = __esm({
393
422
  overlayBadge = null;
394
423
  // ─── Alt key state ───────────────────────────────────────────────────────
395
424
  isAltHeld = false;
425
+ // ─── Edit mode (touch) ───────────────────────────────────────────────────
426
+ // Kept across destroy()/init() so the Studio's CLEAR_SELECTION, which
427
+ // re-inits the interceptor, never unfreezes the site.
428
+ editMode = false;
429
+ active = false;
430
+ /** The element carrying `PICKED_ATTRIBUTE`, if any. */
431
+ pickedElement = null;
396
432
  // Debounced key-broadcast to avoid flooding the parent on rapid DOM mutations
397
433
  broadcastVisibleKeys;
398
- constructor(onElementSelected, onVisibleKeysChanged) {
434
+ constructor(onElementSelected, onVisibleKeysChanged, onUnkeyedTap) {
399
435
  this.onElementSelected = onElementSelected;
400
436
  this.onVisibleKeysChanged = onVisibleKeysChanged;
437
+ this.onUnkeyedTap = onUnkeyedTap;
401
438
  this.broadcastVisibleKeys = debounce(() => {
402
439
  if (this.onVisibleKeysChanged) {
403
440
  this.onVisibleKeysChanged(this.getActiveKeys());
@@ -445,6 +482,75 @@ var init_dom_interceptor = __esm({
445
482
  }
446
483
  return texts;
447
484
  }
485
+ /**
486
+ * Returns the unique keys that have at least one rendered, visible element
487
+ * (see `isRendered`), in document order. Keys only inside closed menus,
488
+ * hidden slides or a layout hidden at this width are left out.
489
+ */
490
+ getRenderedKeys() {
491
+ const keys = /* @__PURE__ */ new Set();
492
+ document.querySelectorAll("[data-i18n-key]").forEach((el) => {
493
+ const key = el.getAttribute("data-i18n-key");
494
+ if (key && !keys.has(key) && isRendered(el)) keys.add(key);
495
+ });
496
+ return Array.from(keys);
497
+ }
498
+ /** The first rendered, visible element carrying `key`, else `null`. */
499
+ findRenderedElement(key) {
500
+ for (const el of document.querySelectorAll("[data-i18n-key]")) {
501
+ if (el.getAttribute("data-i18n-key") === key && isRendered(el)) return el;
502
+ }
503
+ return null;
504
+ }
505
+ /**
506
+ * Scrolls the first visible element with `key` to the centre and selects
507
+ * it as a tap would. Returns false, selecting nothing, when no element with
508
+ * that key is visible or the interceptor is not running. A key that is no
509
+ * longer visible (a carousel moved on with no DOM change the observer sees)
510
+ * reports the keys again, so the Studio's next try uses a fresh list.
511
+ */
512
+ selectKey(key) {
513
+ if (!this.active) return false;
514
+ const el = this.findRenderedElement(key);
515
+ if (!el) {
516
+ this.broadcastVisibleKeys();
517
+ return false;
518
+ }
519
+ if (typeof el.scrollIntoView === "function") el.scrollIntoView({ block: "center" });
520
+ this.select(el, key);
521
+ return true;
522
+ }
523
+ /**
524
+ * Scrolls so the first visible element with `key` sits fully between the
525
+ * frame's top and `bottomInset` CSS px above its bottom (where the Studio's
526
+ * sheet starts), without selecting it. Does nothing when it already does,
527
+ * or when no element with that key is visible. A tall element keeps its top
528
+ * in view rather than its bottom.
529
+ */
530
+ revealKey(key, bottomInset) {
531
+ if (!this.active) return;
532
+ const el = this.findRenderedElement(key);
533
+ if (!el) return;
534
+ const rect = el.getBoundingClientRect();
535
+ const limit = window.innerHeight - bottomInset;
536
+ if (rect.top >= 0 && rect.bottom <= limit) return;
537
+ const delta = Math.min(rect.bottom - limit + REVEAL_MARGIN, rect.top - REVEAL_MARGIN);
538
+ window.scrollBy(0, delta);
539
+ }
540
+ /**
541
+ * Turns Edit mode on or off. In Edit mode no tap reaches the site: a tap on
542
+ * keyed text selects it, a tap on other text calls `onUnkeyedTap`, and
543
+ * anything else does nothing. Scrolling still works. The mode is kept on
544
+ * the instance and re-applied by `init()`.
545
+ */
546
+ setEditMode(on) {
547
+ const changed = this.editMode !== on;
548
+ this.editMode = on;
549
+ if (!this.active) return;
550
+ if (on) this.attachEditMode();
551
+ else this.detachEditMode();
552
+ if (changed) this.broadcastVisibleKeys();
553
+ }
448
554
  /**
449
555
  * Initializes the DOM event interceptors in development mode.
450
556
  * Also launches the watermark scanner and MutationObserver.
@@ -469,6 +575,8 @@ var init_dom_interceptor = __esm({
469
575
  this.scanSubtree(document.body);
470
576
  this.attachMutationObserver();
471
577
  this.attachInputSanitizers();
578
+ this.active = true;
579
+ if (this.editMode) this.attachEditMode();
472
580
  this.broadcastVisibleKeys();
473
581
  }
474
582
  /**
@@ -489,6 +597,8 @@ var init_dom_interceptor = __esm({
489
597
  window.removeEventListener("keydown", this.handleKeyDown, true);
490
598
  window.removeEventListener("keyup", this.handleKeyUp, true);
491
599
  window.removeEventListener("beforeinput", this.handleBeforeInput, true);
600
+ this.detachEditMode();
601
+ this.active = false;
492
602
  document.removeEventListener("submit", this.handleFormSubmit, true);
493
603
  if (this.observer) {
494
604
  this.observer.disconnect();
@@ -503,6 +613,87 @@ var init_dom_interceptor = __esm({
503
613
  this.hoveredElement = null;
504
614
  this.broadcastVisibleKeys.cancel();
505
615
  }
616
+ // ─── Edit Mode ───────────────────────────────────────────────────────────
617
+ attachEditMode() {
618
+ window.addEventListener("pointerdown", this.stopFromSite, true);
619
+ window.addEventListener("pointerup", this.stopFromSite, true);
620
+ window.addEventListener("mousedown", this.stopFromSite, true);
621
+ window.addEventListener("mouseup", this.stopFromSite, true);
622
+ window.addEventListener("touchstart", this.stopFromSite, { capture: true, passive: true });
623
+ window.addEventListener("touchend", this.stopFromSite, { capture: true, passive: true });
624
+ window.addEventListener("focusin", this.handleEditFocus, true);
625
+ if (!document.getElementById(EDIT_STYLE_ID)) {
626
+ const style = document.createElement("style");
627
+ style.id = EDIT_STYLE_ID;
628
+ style.textContent = `html[${EDIT_ATTRIBUTE}] [data-i18n-key]{outline:1px dashed rgba(79,195,220,.7);outline-offset:2px}html[${EDIT_ATTRIBUTE}] [data-i18n-key][${PICKED_ATTRIBUTE}]{outline:2px solid #FFB224;outline-offset:3px}`;
629
+ document.head.appendChild(style);
630
+ }
631
+ document.documentElement.setAttribute(EDIT_ATTRIBUTE, "");
632
+ this.hideOverlay();
633
+ this.hoveredElement = null;
634
+ }
635
+ detachEditMode() {
636
+ window.removeEventListener("pointerdown", this.stopFromSite, true);
637
+ window.removeEventListener("pointerup", this.stopFromSite, true);
638
+ window.removeEventListener("mousedown", this.stopFromSite, true);
639
+ window.removeEventListener("mouseup", this.stopFromSite, true);
640
+ window.removeEventListener("touchstart", this.stopFromSite, true);
641
+ window.removeEventListener("touchend", this.stopFromSite, true);
642
+ window.removeEventListener("focusin", this.handleEditFocus, true);
643
+ document.documentElement.removeAttribute(EDIT_ATTRIBUTE);
644
+ document.getElementById(EDIT_STYLE_ID)?.remove();
645
+ this.clearPicked();
646
+ }
647
+ /** Moves the picked-text marker to `element`. */
648
+ markPicked(element) {
649
+ this.clearPicked();
650
+ element.setAttribute(PICKED_ATTRIBUTE, "");
651
+ this.pickedElement = element;
652
+ }
653
+ clearPicked() {
654
+ this.pickedElement?.removeAttribute(PICKED_ATTRIBUTE);
655
+ this.pickedElement = null;
656
+ }
657
+ // Sites open menus on press events; in Edit mode none of them reaches the
658
+ // site. Never preventDefault here: on touchstart it would stop scrolling.
659
+ stopFromSite = (event) => {
660
+ event.stopImmediatePropagation();
661
+ };
662
+ handleEditFocus = (event) => {
663
+ const target = event.target;
664
+ if (!target || typeof target.matches !== "function") return;
665
+ if (target.matches(EDITABLE_SELECTOR) || target.isContentEditable) target.blur();
666
+ };
667
+ /** Edit-mode click: cancelled whatever the target, then a selection or the notice. */
668
+ handleEditClick(event) {
669
+ event.preventDefault();
670
+ event.stopImmediatePropagation();
671
+ const target = event.target;
672
+ if (!target || typeof target.closest !== "function") return;
673
+ const keyedElement = target.closest("[data-i18n-key]");
674
+ const key = keyedElement?.getAttribute("data-i18n-key");
675
+ if (keyedElement && key) {
676
+ this.select(keyedElement, key);
677
+ } else if (hasOwnText(target)) {
678
+ this.onUnkeyedTap?.();
679
+ }
680
+ }
681
+ /**
682
+ * Reports `element`, which carries `key`, as selected with its clean text. In Edit mode it
683
+ * also gets the picked-text marker; an Alt+Click pick on a desktop does not.
684
+ */
685
+ select(element, key) {
686
+ if (this.editMode) this.markPicked(element);
687
+ const rawText = element.innerText || "";
688
+ const cleanText = stripWatermark(rawText);
689
+ const fallbackValue = element.getAttribute("data-i18n-template") || cleanText;
690
+ const currentValue = cleanText;
691
+ this.onElementSelected({
692
+ key,
693
+ fallbackValue,
694
+ currentValue
695
+ });
696
+ }
506
697
  // ─── Floating Overlay ────────────────────────────────────────────────────
507
698
  /**
508
699
  * Creates and injects a single floating overlay div into the document body.
@@ -746,6 +937,7 @@ var init_dom_interceptor = __esm({
746
937
  };
747
938
  // ─── Mouse / Click Handlers ───────────────────────────────────────────────
748
939
  handleMouseOver = (event) => {
940
+ if (this.editMode) return;
749
941
  const target = event.target;
750
942
  if (!target || typeof target.closest !== "function") return;
751
943
  const keyedElement = target.closest("[data-i18n-key]");
@@ -774,6 +966,10 @@ var init_dom_interceptor = __esm({
774
966
  this.hoveredElement = null;
775
967
  };
776
968
  handleClick = (event) => {
969
+ if (this.editMode) {
970
+ this.handleEditClick(event);
971
+ return;
972
+ }
777
973
  if (!event.altKey) return;
778
974
  const target = event.target;
779
975
  if (!target || typeof target.closest !== "function") return;
@@ -783,15 +979,7 @@ var init_dom_interceptor = __esm({
783
979
  if (key === null) return;
784
980
  event.preventDefault();
785
981
  event.stopPropagation();
786
- const rawText = keyedElement.innerText || "";
787
- const cleanText = stripWatermark(rawText);
788
- const fallbackValue = keyedElement.getAttribute("data-i18n-template") || cleanText;
789
- const currentValue = cleanText;
790
- this.onElementSelected({
791
- key,
792
- fallbackValue,
793
- currentValue
794
- });
982
+ this.select(keyedElement, key);
795
983
  };
796
984
  handleCopy = (event) => {
797
985
  const selection = window.getSelection()?.toString() || "";
@@ -1095,16 +1283,22 @@ var init_client = __esm({
1095
1283
  },
1096
1284
  (keys) => {
1097
1285
  bridge.sendToParent("VISIBLE_KEYS_CHANGED", keys);
1286
+ bridge.sendToParent("RENDERED_KEYS_CHANGED", interceptor.getRenderedKeys());
1098
1287
  bridge.sendToParent("VISIBLE_TEXTS_CHANGED", interceptor.getVisibleTexts());
1288
+ },
1289
+ () => {
1290
+ bridge.sendToParent("TAP_UNKEYED");
1099
1291
  }
1100
1292
  );
1101
1293
  interceptor.init();
1102
1294
  const locationUrl = () => window.location.pathname + window.location.search + window.location.hash;
1103
- bridge.sendToParent("READY", { url: locationUrl() });
1295
+ bridge.sendToParent("READY", { url: locationUrl(), features: CLIENT_FEATURES });
1104
1296
  watchLocation(window, (url) => bridge.sendToParent("LOCATION_CHANGED", { url }));
1105
1297
  window.addEventListener("pagehide", () => bridge.sendToParent("LEAVING"));
1106
1298
  window.addEventListener("pageshow", (event) => {
1107
- if (event.persisted) bridge.sendToParent("READY", { url: locationUrl() });
1299
+ if (event.persisted) {
1300
+ bridge.sendToParent("READY", { url: locationUrl(), features: CLIENT_FEATURES });
1301
+ }
1108
1302
  });
1109
1303
  bridge.listenToParent((type, payload) => {
1110
1304
  if (type === "APPLY_PREVIEW") {
@@ -1122,6 +1316,18 @@ var init_client = __esm({
1122
1316
  } else if (type === "NAVIGATE") {
1123
1317
  const href = navigationHref(payload, window.location.origin);
1124
1318
  if (href !== null) followNavigation(window.location, href);
1319
+ } else if (type === "SET_EDIT_MODE") {
1320
+ if (payload && typeof payload.on === "boolean") {
1321
+ interceptor.setEditMode(payload.on);
1322
+ }
1323
+ } else if (type === "SELECT_KEY") {
1324
+ if (payload && typeof payload.key === "string") {
1325
+ interceptor.selectKey(payload.key);
1326
+ }
1327
+ } else if (type === "REVEAL_KEY") {
1328
+ if (payload && typeof payload.key === "string" && typeof payload.bottomInset === "number" && Number.isFinite(payload.bottomInset) && payload.bottomInset >= 0) {
1329
+ interceptor.revealKey(payload.key, payload.bottomInset);
1330
+ }
1125
1331
  }
1126
1332
  });
1127
1333
  window.__LIVELOCALE_CLIENT__ = {