@ohhwells/bridge 0.1.169 → 0.1.170

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/index.cjs CHANGED
@@ -307,9 +307,23 @@ function parseSectionsFromHtml(html) {
307
307
  return parseSectionsFromRoot(doc);
308
308
  }
309
309
 
310
+ // src/lib/soft-hide.ts
311
+ var PREV_DISPLAY_ATTR = "data-ohw-prev-display";
312
+ function softHide(el) {
313
+ if (!el.hasAttribute(PREV_DISPLAY_ATTR)) el.setAttribute(PREV_DISPLAY_ATTR, el.style.display);
314
+ el.style.display = "none";
315
+ }
316
+ function softUnhide(el) {
317
+ const prev = el.getAttribute(PREV_DISPLAY_ATTR);
318
+ el.removeAttribute(PREV_DISPLAY_ATTR);
319
+ if (prev) el.style.display = prev;
320
+ else el.style.removeProperty("display");
321
+ }
322
+
310
323
  // src/lib/section-instances.ts
311
324
  var SECTION_ORDER_KEY = "__ohw_section_order";
312
325
  var REMOVED_ATTR = "data-ohw-section-removed";
326
+ var CLONE_ATTR = "data-ohw-section-clone";
313
327
  function isRemovedSection(el) {
314
328
  return el.hasAttribute(REMOVED_ATTR);
315
329
  }
@@ -378,14 +392,14 @@ function syncRemovedFlags(entries) {
378
392
  const removedIds = new Set(entries.filter((e) => e.removed).map((e) => e.instanceId));
379
393
  document.querySelectorAll(`[${REMOVED_ATTR}]`).forEach((el) => {
380
394
  if (!removedIds.has(instanceIdOf(el))) {
381
- el.style.removeProperty("display");
395
+ softUnhide(el);
382
396
  el.removeAttribute(REMOVED_ATTR);
383
397
  }
384
398
  });
385
399
  for (const id of removedIds) {
386
400
  const el = findByInstanceId(id);
387
401
  if (el) {
388
- el.style.display = "none";
402
+ softHide(el);
389
403
  el.setAttribute(REMOVED_ATTR, "");
390
404
  }
391
405
  }
@@ -438,6 +452,7 @@ function duplicateSectionInstance(instanceId, newId, currentPath, existingEntrie
438
452
  if (!original) return null;
439
453
  const clone = original.cloneNode(true);
440
454
  clone.setAttribute("data-ohw-instance", newId);
455
+ clone.setAttribute(CLONE_ATTR, "");
441
456
  const keyRekeys = rekeySectionSubtree(clone, newId);
442
457
  original.insertAdjacentElement("afterend", clone);
443
458
  const byId = new Map(existingEntries.map((e) => [e.instanceId, e]));
@@ -457,6 +472,15 @@ function duplicateSectionInstance(instanceId, newId, currentPath, existingEntrie
457
472
  function newInstanceId() {
458
473
  return typeof crypto !== "undefined" && "randomUUID" in crypto ? crypto.randomUUID() : `instance-${Date.now()}-${Math.random().toString(36).slice(2)}`;
459
474
  }
475
+ function orderBeforeMove(raw, currentPath) {
476
+ const entries = topLevelSections().map((el, order) => ({
477
+ instanceId: instanceIdOf(el),
478
+ type: sectionTypeOf(el),
479
+ order,
480
+ pagePath: currentPath
481
+ }));
482
+ return JSON.stringify(mergePageSectionOrder(raw, currentPath, entries));
483
+ }
460
484
  function getPageSectionOrderEntries(raw, currentPath) {
461
485
  if (!raw) return [];
462
486
  try {
@@ -508,6 +532,10 @@ function initSectionInstancesFromContent(content, currentPath) {
508
532
  if (type) el.setAttribute("data-ohw-instance", type);
509
533
  });
510
534
  const entries = getPageSectionOrderEntries(content[SECTION_ORDER_KEY], currentPath);
535
+ const listed = new Set(entries.map((entry) => entry.instanceId));
536
+ document.querySelectorAll(`[${CLONE_ATTR}]`).forEach((clone) => {
537
+ if (!listed.has(instanceIdOf(clone))) clone.remove();
538
+ });
511
539
  for (const entry of entries) {
512
540
  if (entry.instanceId === entry.type) continue;
513
541
  if (document.querySelector(`[data-ohw-instance="${CSS.escape(entry.instanceId)}"]`)) continue;
@@ -517,6 +545,7 @@ function initSectionInstancesFromContent(content, currentPath) {
517
545
  if (!original) continue;
518
546
  const clone = original.cloneNode(true);
519
547
  clone.setAttribute("data-ohw-instance", entry.instanceId);
548
+ clone.setAttribute(CLONE_ATTR, "");
520
549
  rekeySectionSubtree(clone, entry.instanceId);
521
550
  original.insertAdjacentElement("afterend", clone);
522
551
  }
@@ -1674,6 +1703,8 @@ function brandTrackedFontFamily(doc, stack) {
1674
1703
  }
1675
1704
  var paintLockByEl = /* @__PURE__ */ new WeakMap();
1676
1705
  var paintObserverByEl = /* @__PURE__ */ new WeakMap();
1706
+ var prePaintByEl = /* @__PURE__ */ new WeakMap();
1707
+ var paintedEls = /* @__PURE__ */ new Set();
1677
1708
  var PAINT_GUARD_ATTRS = {
1678
1709
  attributes: true,
1679
1710
  attributeFilter: ["style", "class"]
@@ -1737,6 +1768,18 @@ function paintedTokensOf(el) {
1737
1768
  }
1738
1769
  function applyButtonStyle(el, assignment, tokens) {
1739
1770
  const doc = el.ownerDocument;
1771
+ if (!prePaintByEl.has(el)) {
1772
+ const label = labelElement(el);
1773
+ const ownLabel = label && label !== el ? label : null;
1774
+ prePaintByEl.set(el, {
1775
+ style: el.getAttribute("style"),
1776
+ variant: el.getAttribute("data-ohw-btn-variant"),
1777
+ size: el.getAttribute("data-ohw-btn-size"),
1778
+ label: ownLabel,
1779
+ labelStyle: ownLabel?.getAttribute("style") ?? null
1780
+ });
1781
+ }
1782
+ paintedEls.add(el);
1740
1783
  const tracked = {
1741
1784
  ...tokens,
1742
1785
  variants: {
@@ -1756,6 +1799,31 @@ function applyButtonStylesFromContent(doc, content) {
1756
1799
  if (!el) continue;
1757
1800
  applyButtonStyle(el, assignment, tokensFromButton(el));
1758
1801
  }
1802
+ for (const el of paintedEls) {
1803
+ if (!el.isConnected) {
1804
+ paintedEls.delete(el);
1805
+ continue;
1806
+ }
1807
+ const id = buttonId(el);
1808
+ if (el.ownerDocument === doc && (!id || !assignments[id])) unpaintButton(el);
1809
+ }
1810
+ }
1811
+ function unpaintButton(el) {
1812
+ paintObserverByEl.get(el)?.disconnect();
1813
+ paintObserverByEl.delete(el);
1814
+ paintLockByEl.delete(el);
1815
+ paintedEls.delete(el);
1816
+ const before = prePaintByEl.get(el);
1817
+ prePaintByEl.delete(el);
1818
+ if (!before) return;
1819
+ const restore = (target, name, value) => {
1820
+ if (value === null) target.removeAttribute(name);
1821
+ else target.setAttribute(name, value);
1822
+ };
1823
+ restore(el, "style", before.style);
1824
+ restore(el, "data-ohw-btn-variant", before.variant);
1825
+ restore(el, "data-ohw-btn-size", before.size);
1826
+ if (before.label?.isConnected) restore(before.label, "style", before.labelStyle);
1759
1827
  }
1760
1828
  function withAssignment(content, id, assignment) {
1761
1829
  return { ...readAssignments(content), [id]: assignment };
@@ -2306,6 +2374,7 @@ function reconcileButtonCollections(doc, content) {
2306
2374
  }
2307
2375
  const snapshots = readSnapshots(content);
2308
2376
  const collections = readCollections(content);
2377
+ dropUnsavedCopies(doc, snapshots, collections);
2309
2378
  for (const collectionState of Object.values(collections)) {
2310
2379
  const live = collectionState.members.map((id) => findButton(doc, id) ?? restoreSnapshot(doc, snapshots[id], content)).filter((el) => Boolean(el && !hidden.has(buttonId(el) ?? "")));
2311
2380
  if (live.length === 0) continue;
@@ -2330,6 +2399,18 @@ function reconcileButtonCollections(doc, content) {
2330
2399
  for (const member of live) setButtonDragAttr(member);
2331
2400
  }
2332
2401
  }
2402
+ function dropUnsavedCopies(doc, snapshots, collections) {
2403
+ const stale = Array.from(doc.querySelectorAll('[data-ohw-href-key*="-copy-"]')).filter((el) => {
2404
+ const id = buttonId(el);
2405
+ return Boolean(id && /-copy-\d+-href$/.test(id) && !snapshots[id]);
2406
+ });
2407
+ for (const el of stale) {
2408
+ const collection = buttonCollectionOf(el);
2409
+ el.remove();
2410
+ const id = collection?.getAttribute(COLLECTION_ATTR) ?? "";
2411
+ if (collection && !collections[id] && listCollectionMembers(collection).length <= 1) unwrapCollection(collection);
2412
+ }
2413
+ }
2333
2414
  function restoreSnapshot(doc, snapshot, content) {
2334
2415
  if (!snapshot) return null;
2335
2416
  const existing = findButton(doc, snapshot.id);
@@ -4864,6 +4945,9 @@ function reconcileNavbarItemsFromContent(content) {
4864
4945
  }
4865
4946
  }
4866
4947
  pruneNavbarItemsNotInOrder(allowed);
4948
+ for (const node of orderForest) {
4949
+ if (node.children.length === 0) collapseEmptyNavDropdownForParent(node.id);
4950
+ }
4867
4951
  applyNavForest(orderForest);
4868
4952
  for (const parentKey of openParentKeys) {
4869
4953
  const trigger = document.querySelector(
@@ -5783,10 +5867,10 @@ function rekeyItemClone(clone, source, sibling, group, newIndex) {
5783
5867
  function setRemoved(el, removed) {
5784
5868
  if (removed) {
5785
5869
  el.setAttribute(ITEM_REMOVED_ATTR, "");
5786
- el.style.display = "none";
5870
+ softHide(el);
5787
5871
  } else if (el.hasAttribute(ITEM_REMOVED_ATTR)) {
5788
5872
  el.removeAttribute(ITEM_REMOVED_ATTR);
5789
- el.style.removeProperty("display");
5873
+ softUnhide(el);
5790
5874
  }
5791
5875
  }
5792
5876
  function seedState(group) {
@@ -17608,7 +17692,7 @@ function apiBase() {
17608
17692
  function bridgeVersion() {
17609
17693
  try {
17610
17694
  if (true) {
17611
- return "0.1.169";
17695
+ return "0.1.170";
17612
17696
  }
17613
17697
  } catch {
17614
17698
  }
@@ -17622,7 +17706,7 @@ function stampBridgeVersionOnDocument() {
17622
17706
  return;
17623
17707
  }
17624
17708
  try {
17625
- const v = true ? "0.1.169" : "";
17709
+ const v = true ? "0.1.170" : "";
17626
17710
  if (v) {
17627
17711
  document.documentElement.setAttribute("data-ohw-bridge-version", v);
17628
17712
  window.__OHW_BRIDGE_VERSION__ = v;
@@ -18610,7 +18694,7 @@ function highlightedRect() {
18610
18694
  const rect = first.getBoundingClientRect();
18611
18695
  return { top: rect.top + window.scrollY, bottom: rect.bottom + window.scrollY };
18612
18696
  }
18613
- function installAiChangeHighlight() {
18697
+ function installAiChangeHighlight(select) {
18614
18698
  const onMessage = (e) => {
18615
18699
  const data = e.data;
18616
18700
  if (data?.type !== "ow:ai-highlight") return;
@@ -18619,6 +18703,8 @@ function installAiChangeHighlight() {
18619
18703
  const item = raw && typeof raw.sectionId === "string" && typeof raw.group === "string" && typeof raw.index === "number" ? { sectionId: raw.sectionId, group: raw.group, index: raw.index } : void 0;
18620
18704
  const sectionId = typeof data.sectionId === "string" ? data.sectionId : void 0;
18621
18705
  const found = highlightChangedElements(keys, item, sectionId);
18706
+ const first = data.select === true ? document.querySelector(`[${MARK_ATTR}]`) : null;
18707
+ if (first) select?.(first);
18622
18708
  const rect = data.reveal === true && found > 0 ? highlightedRect() : null;
18623
18709
  window.parent?.postMessage({ type: "ow:ai-highlight-done", found, ...rect ? { rect } : {} }, "*");
18624
18710
  };
@@ -18626,6 +18712,28 @@ function installAiChangeHighlight() {
18626
18712
  return () => window.removeEventListener("message", onMessage);
18627
18713
  }
18628
18714
 
18715
+ // src/lib/history-key-relay.ts
18716
+ var isMac = () => typeof navigator !== "undefined" && /Mac|iPhone|iPad/i.test(navigator.platform || navigator.userAgent);
18717
+ function historyActionFor(e, mac = isMac()) {
18718
+ const mod = mac ? e.metaKey : e.ctrlKey;
18719
+ if (!mod || e.altKey) return null;
18720
+ const key = e.key.toLowerCase();
18721
+ if (key === "z") return e.shiftKey ? "redo" : "undo";
18722
+ if (key === "y" && !mac) return "redo";
18723
+ return null;
18724
+ }
18725
+ var isEditingText = (el) => !!el && (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement || el instanceof HTMLElement && el.isContentEditable);
18726
+ function installHistoryKeyRelay() {
18727
+ const onKeyDown = (e) => {
18728
+ const action = historyActionFor(e);
18729
+ if (!action || isEditingText(document.activeElement)) return;
18730
+ e.preventDefault();
18731
+ window.parent?.postMessage({ type: "ow:history-key", action }, "*");
18732
+ };
18733
+ window.addEventListener("keydown", onKeyDown);
18734
+ return () => window.removeEventListener("keydown", onKeyDown);
18735
+ }
18736
+
18629
18737
  // src/ui/ai-section/AiSectionOverlay.tsx
18630
18738
  var import_jsx_runtime19 = require("react/jsx-runtime");
18631
18739
  var findSectionElement = findByInstanceId;
@@ -18878,7 +18986,8 @@ function AiSectionOverlay({
18878
18986
  window.addEventListener("message", onMessage);
18879
18987
  return () => window.removeEventListener("message", onMessage);
18880
18988
  }, [postToParent2]);
18881
- (0, import_react12.useEffect)(() => installAiChangeHighlight(), []);
18989
+ (0, import_react12.useEffect)(() => installAiChangeHighlight((el) => selectFromElement(el)), [selectFromElement]);
18990
+ (0, import_react12.useEffect)(() => installHistoryKeyRelay(), []);
18882
18991
  const [hoveredId, setHoveredId] = (0, import_react12.useState)(null);
18883
18992
  (0, import_react12.useEffect)(() => {
18884
18993
  const onMove = (e) => {
@@ -23997,12 +24106,19 @@ function useSectionDrag({
23997
24106
  clearSectionDragVisuals();
23998
24107
  return;
23999
24108
  }
24109
+ const previous = orderBeforeMove(editContentRef.current[SECTION_ORDER_KEY], window.location.pathname);
24000
24110
  const orderJson = JSON.stringify(
24001
24111
  mergePageSectionOrder(editContentRef.current[SECTION_ORDER_KEY], window.location.pathname, entries)
24002
24112
  );
24003
24113
  editContentRef.current = { ...editContentRef.current, [SECTION_ORDER_KEY]: orderJson };
24004
24114
  setAiSectionOrder(orderJson, window.location.pathname);
24005
- postToParentRef.current({ type: "ow:change", nodes: [{ key: SECTION_ORDER_KEY, text: orderJson }] });
24115
+ postToParentRef.current({
24116
+ type: "ow:change",
24117
+ nodes: [{ key: SECTION_ORDER_KEY, text: orderJson }],
24118
+ previous: { [SECTION_ORDER_KEY]: previous },
24119
+ // Undo scrolls to this section; a one-place move swaps two, so the order alone can't say which.
24120
+ moved: session.instanceId
24121
+ });
24006
24122
  applyPersistedOrder(getPageSectionOrderEntries(orderJson, window.location.pathname));
24007
24123
  clearSectionDragVisuals();
24008
24124
  requestAnimationFrame(() => {
@@ -31061,6 +31177,7 @@ function OhhwellsBridge() {
31061
31177
  reconcileNavbarItemsFromContent(editContentRef.current);
31062
31178
  reconcileFooterOrderFromContent(editContentRef.current);
31063
31179
  syncButtonsFromContent(document, editContentRef.current);
31180
+ if (selectedElRef.current && !selectedElRef.current.isConnected) deselectRef.current();
31064
31181
  syncNavigationDragCursorAttrs();
31065
31182
  if (typeof content[STYLE_STORE_KEY] === "string") {
31066
31183
  applyStylesToDom(parseStyleStore(content[STYLE_STORE_KEY]));
@@ -31492,6 +31609,7 @@ function OhhwellsBridge() {
31492
31609
  const instanceId = typeof e.data.instanceId === "string" ? e.data.instanceId : "";
31493
31610
  const direction = e.data.direction === "up" || e.data.direction === "down" ? e.data.direction : null;
31494
31611
  if (!instanceId || !direction) return;
31612
+ const previous = orderBeforeMove(editContentRef.current[SECTION_ORDER_KEY], window.location.pathname);
31495
31613
  const entries = moveSectionInstance(instanceId, direction, window.location.pathname);
31496
31614
  if (!entries) return;
31497
31615
  const orderJson = JSON.stringify(
@@ -31500,7 +31618,12 @@ function OhhwellsBridge() {
31500
31618
  editContentRef.current = { ...editContentRef.current, [SECTION_ORDER_KEY]: orderJson };
31501
31619
  applyPersistedOrder(getPageSectionOrderEntries(orderJson, window.location.pathname));
31502
31620
  setAiSectionOrder(orderJson, window.location.pathname);
31503
- postToParentRef.current({ type: "ow:change", nodes: [{ key: SECTION_ORDER_KEY, text: orderJson }] });
31621
+ postToParentRef.current({
31622
+ type: "ow:change",
31623
+ nodes: [{ key: SECTION_ORDER_KEY, text: orderJson }],
31624
+ previous: { [SECTION_ORDER_KEY]: previous },
31625
+ moved: instanceId
31626
+ });
31504
31627
  window.dispatchEvent(new Event("resize"));
31505
31628
  };
31506
31629
  window.addEventListener("message", handleMoveSection);
@@ -31863,12 +31986,6 @@ function OhhwellsBridge() {
31863
31986
  return;
31864
31987
  }
31865
31988
  }
31866
- if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "z" && !activeElRef.current) {
31867
- if (runPendingDeleteUndoRef.current()) {
31868
- e.preventDefault();
31869
- return;
31870
- }
31871
- }
31872
31989
  if (e.key !== "Escape") return;
31873
31990
  const el = activeElRef.current;
31874
31991
  if (el && originalContentRef.current !== null) {
@@ -32812,7 +32929,7 @@ function OhhwellsBridge() {
32812
32929
  postToParent2({
32813
32930
  type: "ow:ready",
32814
32931
  version: "1",
32815
- bridgeVersion: "0.1.169",
32932
+ bridgeVersion: "0.1.170",
32816
32933
  path: pathname,
32817
32934
  nodes: collectEditableNodes(editContentRef.current),
32818
32935
  sections
@@ -33013,7 +33130,7 @@ function OhhwellsBridge() {
33013
33130
  const copyHref = result.copy.getAttribute("data-ohw-href-key");
33014
33131
  const copyHrefVal = copyHref ? result.content[copyHref] : void 0;
33015
33132
  if (copyHref && copyHrefVal !== void 0) applyLinkByKey(copyHref, copyHrefVal);
33016
- postToParent2({ type: "ow:change", nodes: result.nodes });
33133
+ postToParent2({ type: "ow:change", nodes: result.nodes, label: "Button duplicated" });
33017
33134
  enforceLinkHrefs();
33018
33135
  requestAnimationFrame(() => {
33019
33136
  selectRef.current(result.copy);
@@ -33062,7 +33179,7 @@ function OhhwellsBridge() {
33062
33179
  ...editContentRef.current,
33063
33180
  ...Object.fromEntries(nodes.map((node) => [node.key, node.text]))
33064
33181
  };
33065
- postToParent2({ type: "ow:change", nodes });
33182
+ postToParent2({ type: "ow:change", nodes, label: "Social icon duplicated" });
33066
33183
  enforceLinkHrefs();
33067
33184
  const copyRow = findSocialsRow(result.item);
33068
33185
  if (copyRow) applySocialsDisplayToRow(copyRow, socialsDisplayFor(copyRow, editContentRef.current));
@@ -33112,7 +33229,7 @@ function OhhwellsBridge() {
33112
33229
  [NAV_COUNT_KEY]: navCount,
33113
33230
  [NAV_ORDER_KEY]: result.orderJson
33114
33231
  };
33115
- postToParent2({ type: "ow:change", nodes });
33232
+ postToParent2({ type: "ow:change", nodes, label: "Link duplicated" });
33116
33233
  enforceLinkHrefs();
33117
33234
  if (isNestedNavChild(selected) || navItemOwnsDropdownPanel(selected)) {
33118
33235
  setNavGroupForceOpen(selected, true);
@@ -33152,7 +33269,8 @@ function OhhwellsBridge() {
33152
33269
  { key: result.hrefKey, text: result.href },
33153
33270
  { key: result.labelKey, text: result.label },
33154
33271
  { key: FOOTER_ORDER_KEY, text: orderJson }
33155
- ]
33272
+ ],
33273
+ label: "Link duplicated"
33156
33274
  });
33157
33275
  enforceLinkHrefs();
33158
33276
  requestAnimationFrame(() => {