@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.js CHANGED
@@ -189,9 +189,23 @@ function parseSectionsFromHtml(html) {
189
189
  return parseSectionsFromRoot(doc);
190
190
  }
191
191
 
192
+ // src/lib/soft-hide.ts
193
+ var PREV_DISPLAY_ATTR = "data-ohw-prev-display";
194
+ function softHide(el) {
195
+ if (!el.hasAttribute(PREV_DISPLAY_ATTR)) el.setAttribute(PREV_DISPLAY_ATTR, el.style.display);
196
+ el.style.display = "none";
197
+ }
198
+ function softUnhide(el) {
199
+ const prev = el.getAttribute(PREV_DISPLAY_ATTR);
200
+ el.removeAttribute(PREV_DISPLAY_ATTR);
201
+ if (prev) el.style.display = prev;
202
+ else el.style.removeProperty("display");
203
+ }
204
+
192
205
  // src/lib/section-instances.ts
193
206
  var SECTION_ORDER_KEY = "__ohw_section_order";
194
207
  var REMOVED_ATTR = "data-ohw-section-removed";
208
+ var CLONE_ATTR = "data-ohw-section-clone";
195
209
  function isRemovedSection(el) {
196
210
  return el.hasAttribute(REMOVED_ATTR);
197
211
  }
@@ -260,14 +274,14 @@ function syncRemovedFlags(entries) {
260
274
  const removedIds = new Set(entries.filter((e) => e.removed).map((e) => e.instanceId));
261
275
  document.querySelectorAll(`[${REMOVED_ATTR}]`).forEach((el) => {
262
276
  if (!removedIds.has(instanceIdOf(el))) {
263
- el.style.removeProperty("display");
277
+ softUnhide(el);
264
278
  el.removeAttribute(REMOVED_ATTR);
265
279
  }
266
280
  });
267
281
  for (const id of removedIds) {
268
282
  const el = findByInstanceId(id);
269
283
  if (el) {
270
- el.style.display = "none";
284
+ softHide(el);
271
285
  el.setAttribute(REMOVED_ATTR, "");
272
286
  }
273
287
  }
@@ -320,6 +334,7 @@ function duplicateSectionInstance(instanceId, newId, currentPath, existingEntrie
320
334
  if (!original) return null;
321
335
  const clone = original.cloneNode(true);
322
336
  clone.setAttribute("data-ohw-instance", newId);
337
+ clone.setAttribute(CLONE_ATTR, "");
323
338
  const keyRekeys = rekeySectionSubtree(clone, newId);
324
339
  original.insertAdjacentElement("afterend", clone);
325
340
  const byId = new Map(existingEntries.map((e) => [e.instanceId, e]));
@@ -339,6 +354,15 @@ function duplicateSectionInstance(instanceId, newId, currentPath, existingEntrie
339
354
  function newInstanceId() {
340
355
  return typeof crypto !== "undefined" && "randomUUID" in crypto ? crypto.randomUUID() : `instance-${Date.now()}-${Math.random().toString(36).slice(2)}`;
341
356
  }
357
+ function orderBeforeMove(raw, currentPath) {
358
+ const entries = topLevelSections().map((el, order) => ({
359
+ instanceId: instanceIdOf(el),
360
+ type: sectionTypeOf(el),
361
+ order,
362
+ pagePath: currentPath
363
+ }));
364
+ return JSON.stringify(mergePageSectionOrder(raw, currentPath, entries));
365
+ }
342
366
  function getPageSectionOrderEntries(raw, currentPath) {
343
367
  if (!raw) return [];
344
368
  try {
@@ -390,6 +414,10 @@ function initSectionInstancesFromContent(content, currentPath) {
390
414
  if (type) el.setAttribute("data-ohw-instance", type);
391
415
  });
392
416
  const entries = getPageSectionOrderEntries(content[SECTION_ORDER_KEY], currentPath);
417
+ const listed = new Set(entries.map((entry) => entry.instanceId));
418
+ document.querySelectorAll(`[${CLONE_ATTR}]`).forEach((clone) => {
419
+ if (!listed.has(instanceIdOf(clone))) clone.remove();
420
+ });
393
421
  for (const entry of entries) {
394
422
  if (entry.instanceId === entry.type) continue;
395
423
  if (document.querySelector(`[data-ohw-instance="${CSS.escape(entry.instanceId)}"]`)) continue;
@@ -399,6 +427,7 @@ function initSectionInstancesFromContent(content, currentPath) {
399
427
  if (!original) continue;
400
428
  const clone = original.cloneNode(true);
401
429
  clone.setAttribute("data-ohw-instance", entry.instanceId);
430
+ clone.setAttribute(CLONE_ATTR, "");
402
431
  rekeySectionSubtree(clone, entry.instanceId);
403
432
  original.insertAdjacentElement("afterend", clone);
404
433
  }
@@ -1556,6 +1585,8 @@ function brandTrackedFontFamily(doc, stack) {
1556
1585
  }
1557
1586
  var paintLockByEl = /* @__PURE__ */ new WeakMap();
1558
1587
  var paintObserverByEl = /* @__PURE__ */ new WeakMap();
1588
+ var prePaintByEl = /* @__PURE__ */ new WeakMap();
1589
+ var paintedEls = /* @__PURE__ */ new Set();
1559
1590
  var PAINT_GUARD_ATTRS = {
1560
1591
  attributes: true,
1561
1592
  attributeFilter: ["style", "class"]
@@ -1619,6 +1650,18 @@ function paintedTokensOf(el) {
1619
1650
  }
1620
1651
  function applyButtonStyle(el, assignment, tokens) {
1621
1652
  const doc = el.ownerDocument;
1653
+ if (!prePaintByEl.has(el)) {
1654
+ const label = labelElement(el);
1655
+ const ownLabel = label && label !== el ? label : null;
1656
+ prePaintByEl.set(el, {
1657
+ style: el.getAttribute("style"),
1658
+ variant: el.getAttribute("data-ohw-btn-variant"),
1659
+ size: el.getAttribute("data-ohw-btn-size"),
1660
+ label: ownLabel,
1661
+ labelStyle: ownLabel?.getAttribute("style") ?? null
1662
+ });
1663
+ }
1664
+ paintedEls.add(el);
1622
1665
  const tracked = {
1623
1666
  ...tokens,
1624
1667
  variants: {
@@ -1638,6 +1681,31 @@ function applyButtonStylesFromContent(doc, content) {
1638
1681
  if (!el) continue;
1639
1682
  applyButtonStyle(el, assignment, tokensFromButton(el));
1640
1683
  }
1684
+ for (const el of paintedEls) {
1685
+ if (!el.isConnected) {
1686
+ paintedEls.delete(el);
1687
+ continue;
1688
+ }
1689
+ const id = buttonId(el);
1690
+ if (el.ownerDocument === doc && (!id || !assignments[id])) unpaintButton(el);
1691
+ }
1692
+ }
1693
+ function unpaintButton(el) {
1694
+ paintObserverByEl.get(el)?.disconnect();
1695
+ paintObserverByEl.delete(el);
1696
+ paintLockByEl.delete(el);
1697
+ paintedEls.delete(el);
1698
+ const before = prePaintByEl.get(el);
1699
+ prePaintByEl.delete(el);
1700
+ if (!before) return;
1701
+ const restore = (target, name, value) => {
1702
+ if (value === null) target.removeAttribute(name);
1703
+ else target.setAttribute(name, value);
1704
+ };
1705
+ restore(el, "style", before.style);
1706
+ restore(el, "data-ohw-btn-variant", before.variant);
1707
+ restore(el, "data-ohw-btn-size", before.size);
1708
+ if (before.label?.isConnected) restore(before.label, "style", before.labelStyle);
1641
1709
  }
1642
1710
  function withAssignment(content, id, assignment) {
1643
1711
  return { ...readAssignments(content), [id]: assignment };
@@ -2188,6 +2256,7 @@ function reconcileButtonCollections(doc, content) {
2188
2256
  }
2189
2257
  const snapshots = readSnapshots(content);
2190
2258
  const collections = readCollections(content);
2259
+ dropUnsavedCopies(doc, snapshots, collections);
2191
2260
  for (const collectionState of Object.values(collections)) {
2192
2261
  const live = collectionState.members.map((id) => findButton(doc, id) ?? restoreSnapshot(doc, snapshots[id], content)).filter((el) => Boolean(el && !hidden.has(buttonId(el) ?? "")));
2193
2262
  if (live.length === 0) continue;
@@ -2212,6 +2281,18 @@ function reconcileButtonCollections(doc, content) {
2212
2281
  for (const member of live) setButtonDragAttr(member);
2213
2282
  }
2214
2283
  }
2284
+ function dropUnsavedCopies(doc, snapshots, collections) {
2285
+ const stale = Array.from(doc.querySelectorAll('[data-ohw-href-key*="-copy-"]')).filter((el) => {
2286
+ const id = buttonId(el);
2287
+ return Boolean(id && /-copy-\d+-href$/.test(id) && !snapshots[id]);
2288
+ });
2289
+ for (const el of stale) {
2290
+ const collection = buttonCollectionOf(el);
2291
+ el.remove();
2292
+ const id = collection?.getAttribute(COLLECTION_ATTR) ?? "";
2293
+ if (collection && !collections[id] && listCollectionMembers(collection).length <= 1) unwrapCollection(collection);
2294
+ }
2295
+ }
2215
2296
  function restoreSnapshot(doc, snapshot, content) {
2216
2297
  if (!snapshot) return null;
2217
2298
  const existing = findButton(doc, snapshot.id);
@@ -4746,6 +4827,9 @@ function reconcileNavbarItemsFromContent(content) {
4746
4827
  }
4747
4828
  }
4748
4829
  pruneNavbarItemsNotInOrder(allowed);
4830
+ for (const node of orderForest) {
4831
+ if (node.children.length === 0) collapseEmptyNavDropdownForParent(node.id);
4832
+ }
4749
4833
  applyNavForest(orderForest);
4750
4834
  for (const parentKey of openParentKeys) {
4751
4835
  const trigger = document.querySelector(
@@ -5665,10 +5749,10 @@ function rekeyItemClone(clone, source, sibling, group, newIndex) {
5665
5749
  function setRemoved(el, removed) {
5666
5750
  if (removed) {
5667
5751
  el.setAttribute(ITEM_REMOVED_ATTR, "");
5668
- el.style.display = "none";
5752
+ softHide(el);
5669
5753
  } else if (el.hasAttribute(ITEM_REMOVED_ATTR)) {
5670
5754
  el.removeAttribute(ITEM_REMOVED_ATTR);
5671
- el.style.removeProperty("display");
5755
+ softUnhide(el);
5672
5756
  }
5673
5757
  }
5674
5758
  function seedState(group) {
@@ -17490,7 +17574,7 @@ function apiBase() {
17490
17574
  function bridgeVersion() {
17491
17575
  try {
17492
17576
  if (true) {
17493
- return "0.1.169";
17577
+ return "0.1.170";
17494
17578
  }
17495
17579
  } catch {
17496
17580
  }
@@ -17504,7 +17588,7 @@ function stampBridgeVersionOnDocument() {
17504
17588
  return;
17505
17589
  }
17506
17590
  try {
17507
- const v = true ? "0.1.169" : "";
17591
+ const v = true ? "0.1.170" : "";
17508
17592
  if (v) {
17509
17593
  document.documentElement.setAttribute("data-ohw-bridge-version", v);
17510
17594
  window.__OHW_BRIDGE_VERSION__ = v;
@@ -18492,7 +18576,7 @@ function highlightedRect() {
18492
18576
  const rect = first.getBoundingClientRect();
18493
18577
  return { top: rect.top + window.scrollY, bottom: rect.bottom + window.scrollY };
18494
18578
  }
18495
- function installAiChangeHighlight() {
18579
+ function installAiChangeHighlight(select) {
18496
18580
  const onMessage = (e) => {
18497
18581
  const data = e.data;
18498
18582
  if (data?.type !== "ow:ai-highlight") return;
@@ -18501,6 +18585,8 @@ function installAiChangeHighlight() {
18501
18585
  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;
18502
18586
  const sectionId = typeof data.sectionId === "string" ? data.sectionId : void 0;
18503
18587
  const found = highlightChangedElements(keys, item, sectionId);
18588
+ const first = data.select === true ? document.querySelector(`[${MARK_ATTR}]`) : null;
18589
+ if (first) select?.(first);
18504
18590
  const rect = data.reveal === true && found > 0 ? highlightedRect() : null;
18505
18591
  window.parent?.postMessage({ type: "ow:ai-highlight-done", found, ...rect ? { rect } : {} }, "*");
18506
18592
  };
@@ -18508,6 +18594,28 @@ function installAiChangeHighlight() {
18508
18594
  return () => window.removeEventListener("message", onMessage);
18509
18595
  }
18510
18596
 
18597
+ // src/lib/history-key-relay.ts
18598
+ var isMac = () => typeof navigator !== "undefined" && /Mac|iPhone|iPad/i.test(navigator.platform || navigator.userAgent);
18599
+ function historyActionFor(e, mac = isMac()) {
18600
+ const mod = mac ? e.metaKey : e.ctrlKey;
18601
+ if (!mod || e.altKey) return null;
18602
+ const key = e.key.toLowerCase();
18603
+ if (key === "z") return e.shiftKey ? "redo" : "undo";
18604
+ if (key === "y" && !mac) return "redo";
18605
+ return null;
18606
+ }
18607
+ var isEditingText = (el) => !!el && (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement || el instanceof HTMLElement && el.isContentEditable);
18608
+ function installHistoryKeyRelay() {
18609
+ const onKeyDown = (e) => {
18610
+ const action = historyActionFor(e);
18611
+ if (!action || isEditingText(document.activeElement)) return;
18612
+ e.preventDefault();
18613
+ window.parent?.postMessage({ type: "ow:history-key", action }, "*");
18614
+ };
18615
+ window.addEventListener("keydown", onKeyDown);
18616
+ return () => window.removeEventListener("keydown", onKeyDown);
18617
+ }
18618
+
18511
18619
  // src/ui/ai-section/AiSectionOverlay.tsx
18512
18620
  import { Fragment as Fragment6, jsx as jsx19, jsxs as jsxs10 } from "react/jsx-runtime";
18513
18621
  var findSectionElement = findByInstanceId;
@@ -18760,7 +18868,8 @@ function AiSectionOverlay({
18760
18868
  window.addEventListener("message", onMessage);
18761
18869
  return () => window.removeEventListener("message", onMessage);
18762
18870
  }, [postToParent2]);
18763
- useEffect8(() => installAiChangeHighlight(), []);
18871
+ useEffect8(() => installAiChangeHighlight((el) => selectFromElement(el)), [selectFromElement]);
18872
+ useEffect8(() => installHistoryKeyRelay(), []);
18764
18873
  const [hoveredId, setHoveredId] = useState7(null);
18765
18874
  useEffect8(() => {
18766
18875
  const onMove = (e) => {
@@ -23885,12 +23994,19 @@ function useSectionDrag({
23885
23994
  clearSectionDragVisuals();
23886
23995
  return;
23887
23996
  }
23997
+ const previous = orderBeforeMove(editContentRef.current[SECTION_ORDER_KEY], window.location.pathname);
23888
23998
  const orderJson = JSON.stringify(
23889
23999
  mergePageSectionOrder(editContentRef.current[SECTION_ORDER_KEY], window.location.pathname, entries)
23890
24000
  );
23891
24001
  editContentRef.current = { ...editContentRef.current, [SECTION_ORDER_KEY]: orderJson };
23892
24002
  setAiSectionOrder(orderJson, window.location.pathname);
23893
- postToParentRef.current({ type: "ow:change", nodes: [{ key: SECTION_ORDER_KEY, text: orderJson }] });
24003
+ postToParentRef.current({
24004
+ type: "ow:change",
24005
+ nodes: [{ key: SECTION_ORDER_KEY, text: orderJson }],
24006
+ previous: { [SECTION_ORDER_KEY]: previous },
24007
+ // Undo scrolls to this section; a one-place move swaps two, so the order alone can't say which.
24008
+ moved: session.instanceId
24009
+ });
23894
24010
  applyPersistedOrder(getPageSectionOrderEntries(orderJson, window.location.pathname));
23895
24011
  clearSectionDragVisuals();
23896
24012
  requestAnimationFrame(() => {
@@ -30949,6 +31065,7 @@ function OhhwellsBridge() {
30949
31065
  reconcileNavbarItemsFromContent(editContentRef.current);
30950
31066
  reconcileFooterOrderFromContent(editContentRef.current);
30951
31067
  syncButtonsFromContent(document, editContentRef.current);
31068
+ if (selectedElRef.current && !selectedElRef.current.isConnected) deselectRef.current();
30952
31069
  syncNavigationDragCursorAttrs();
30953
31070
  if (typeof content[STYLE_STORE_KEY] === "string") {
30954
31071
  applyStylesToDom(parseStyleStore(content[STYLE_STORE_KEY]));
@@ -31380,6 +31497,7 @@ function OhhwellsBridge() {
31380
31497
  const instanceId = typeof e.data.instanceId === "string" ? e.data.instanceId : "";
31381
31498
  const direction = e.data.direction === "up" || e.data.direction === "down" ? e.data.direction : null;
31382
31499
  if (!instanceId || !direction) return;
31500
+ const previous = orderBeforeMove(editContentRef.current[SECTION_ORDER_KEY], window.location.pathname);
31383
31501
  const entries = moveSectionInstance(instanceId, direction, window.location.pathname);
31384
31502
  if (!entries) return;
31385
31503
  const orderJson = JSON.stringify(
@@ -31388,7 +31506,12 @@ function OhhwellsBridge() {
31388
31506
  editContentRef.current = { ...editContentRef.current, [SECTION_ORDER_KEY]: orderJson };
31389
31507
  applyPersistedOrder(getPageSectionOrderEntries(orderJson, window.location.pathname));
31390
31508
  setAiSectionOrder(orderJson, window.location.pathname);
31391
- postToParentRef.current({ type: "ow:change", nodes: [{ key: SECTION_ORDER_KEY, text: orderJson }] });
31509
+ postToParentRef.current({
31510
+ type: "ow:change",
31511
+ nodes: [{ key: SECTION_ORDER_KEY, text: orderJson }],
31512
+ previous: { [SECTION_ORDER_KEY]: previous },
31513
+ moved: instanceId
31514
+ });
31392
31515
  window.dispatchEvent(new Event("resize"));
31393
31516
  };
31394
31517
  window.addEventListener("message", handleMoveSection);
@@ -31751,12 +31874,6 @@ function OhhwellsBridge() {
31751
31874
  return;
31752
31875
  }
31753
31876
  }
31754
- if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "z" && !activeElRef.current) {
31755
- if (runPendingDeleteUndoRef.current()) {
31756
- e.preventDefault();
31757
- return;
31758
- }
31759
- }
31760
31877
  if (e.key !== "Escape") return;
31761
31878
  const el = activeElRef.current;
31762
31879
  if (el && originalContentRef.current !== null) {
@@ -32700,7 +32817,7 @@ function OhhwellsBridge() {
32700
32817
  postToParent2({
32701
32818
  type: "ow:ready",
32702
32819
  version: "1",
32703
- bridgeVersion: "0.1.169",
32820
+ bridgeVersion: "0.1.170",
32704
32821
  path: pathname,
32705
32822
  nodes: collectEditableNodes(editContentRef.current),
32706
32823
  sections
@@ -32901,7 +33018,7 @@ function OhhwellsBridge() {
32901
33018
  const copyHref = result.copy.getAttribute("data-ohw-href-key");
32902
33019
  const copyHrefVal = copyHref ? result.content[copyHref] : void 0;
32903
33020
  if (copyHref && copyHrefVal !== void 0) applyLinkByKey(copyHref, copyHrefVal);
32904
- postToParent2({ type: "ow:change", nodes: result.nodes });
33021
+ postToParent2({ type: "ow:change", nodes: result.nodes, label: "Button duplicated" });
32905
33022
  enforceLinkHrefs();
32906
33023
  requestAnimationFrame(() => {
32907
33024
  selectRef.current(result.copy);
@@ -32950,7 +33067,7 @@ function OhhwellsBridge() {
32950
33067
  ...editContentRef.current,
32951
33068
  ...Object.fromEntries(nodes.map((node) => [node.key, node.text]))
32952
33069
  };
32953
- postToParent2({ type: "ow:change", nodes });
33070
+ postToParent2({ type: "ow:change", nodes, label: "Social icon duplicated" });
32954
33071
  enforceLinkHrefs();
32955
33072
  const copyRow = findSocialsRow(result.item);
32956
33073
  if (copyRow) applySocialsDisplayToRow(copyRow, socialsDisplayFor(copyRow, editContentRef.current));
@@ -33000,7 +33117,7 @@ function OhhwellsBridge() {
33000
33117
  [NAV_COUNT_KEY]: navCount,
33001
33118
  [NAV_ORDER_KEY]: result.orderJson
33002
33119
  };
33003
- postToParent2({ type: "ow:change", nodes });
33120
+ postToParent2({ type: "ow:change", nodes, label: "Link duplicated" });
33004
33121
  enforceLinkHrefs();
33005
33122
  if (isNestedNavChild(selected) || navItemOwnsDropdownPanel(selected)) {
33006
33123
  setNavGroupForceOpen(selected, true);
@@ -33040,7 +33157,8 @@ function OhhwellsBridge() {
33040
33157
  { key: result.hrefKey, text: result.href },
33041
33158
  { key: result.labelKey, text: result.label },
33042
33159
  { key: FOOTER_ORDER_KEY, text: orderJson }
33043
- ]
33160
+ ],
33161
+ label: "Link duplicated"
33044
33162
  });
33045
33163
  enforceLinkHrefs();
33046
33164
  requestAnimationFrame(() => {