@ohhwells/bridge 0.1.180-next.482 → 0.1.180-next.483

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
@@ -126,6 +126,7 @@ __export(index_exports, {
126
126
  ensureFooterTree: () => ensureFooterTree,
127
127
  ensureSectionButtonCollections: () => ensureSectionButtonCollections,
128
128
  filterAvailablePages: () => filterAvailablePages,
129
+ fitButtonCollection: () => fitButtonCollection,
129
130
  foldAlignIntoTrees: () => foldAlignIntoTrees,
130
131
  getEditModeInitialState: () => getEditModeInitialState,
131
132
  getLogoVersions: () => getLogoVersions,
@@ -3006,19 +3007,19 @@ function brandTrackedChrome(doc, chrome) {
3006
3007
  borderColor: brandTrackedColor(doc, chrome.borderColor, BRAND_TRACK_PRECEDENCE.borderColor)
3007
3008
  };
3008
3009
  }
3009
- function firstFamily(stack) {
3010
- const first = stack.split(",")[0]?.trim() ?? "";
3010
+ function firstFamily(stack2) {
3011
+ const first = stack2.split(",")[0]?.trim() ?? "";
3011
3012
  return first.replace(/^['"]|['"]$/g, "").toLowerCase();
3012
3013
  }
3013
- function brandTrackedFontFamily(doc, stack) {
3014
- if (!stack || stack.includes("var(")) return stack;
3015
- const family = firstFamily(stack);
3016
- if (!family) return stack;
3014
+ function brandTrackedFontFamily(doc, stack2) {
3015
+ if (!stack2 || stack2.includes("var(")) return stack2;
3016
+ const family = firstFamily(stack2);
3017
+ if (!family) return stack2;
3017
3018
  for (const name of FONT_TRACK_VARS) {
3018
3019
  const raw = readRootVar(doc, name);
3019
- if (raw && firstFamily(raw) === family) return `var(${name}, ${stack})`;
3020
+ if (raw && firstFamily(raw) === family) return `var(${name}, ${stack2})`;
3020
3021
  }
3021
- return stack;
3022
+ return stack2;
3022
3023
  }
3023
3024
  var paintLockByEl = /* @__PURE__ */ new WeakMap();
3024
3025
  var paintObserverByEl = /* @__PURE__ */ new WeakMap();
@@ -3550,6 +3551,80 @@ function applyCollectionPositionsFromContent(doc, content) {
3550
3551
  unwrapEmptyColumns(doc);
3551
3552
  }
3552
3553
 
3554
+ // src/lib/button-collection-fit.ts
3555
+ var STACKED_ATTR = "data-ohw-collection-stacked";
3556
+ var MEDIA_SELECTOR = 'img, picture, video, svg, figure, [data-ohw-editable="image"], [data-ohw-editable="bg-image"]';
3557
+ var COPY_SELECTOR = '[data-ohw-href-key*="-copy-"]';
3558
+ var BUTTON_SELECTOR2 = '[data-ohw-role="button"]';
3559
+ function overlaps(a, b) {
3560
+ return a.left < b.right - 1 && b.left < a.right - 1 && a.top < b.bottom - 1 && b.top < a.bottom - 1;
3561
+ }
3562
+ function visibleButtons(collection) {
3563
+ return Array.from(collection.querySelectorAll(BUTTON_SELECTOR2)).filter(
3564
+ (el) => el.getClientRects().length > 0 && el.style.display !== "none"
3565
+ );
3566
+ }
3567
+ function covers2(outer, inner) {
3568
+ return outer.left <= inner.left + 1 && outer.top <= inner.top + 1 && outer.right >= inner.right - 1 && outer.bottom >= inner.bottom - 1;
3569
+ }
3570
+ function sectionMedia(collection) {
3571
+ const section = collection.closest("[data-ohw-section], section") ?? collection.ownerDocument.body;
3572
+ const own = collection.getBoundingClientRect();
3573
+ const boxes = [];
3574
+ for (const el of Array.from(section.querySelectorAll(MEDIA_SELECTOR))) {
3575
+ if (collection.contains(el) || el.contains(collection)) continue;
3576
+ if (el.closest(BUTTON_SELECTOR2)) continue;
3577
+ const r2 = el.getBoundingClientRect();
3578
+ if (r2.width < 1 || r2.height < 1) continue;
3579
+ if (covers2(r2, own)) continue;
3580
+ boxes.push(r2);
3581
+ }
3582
+ return boxes;
3583
+ }
3584
+ function hitsMedia(collection) {
3585
+ const media = sectionMedia(collection);
3586
+ if (media.length === 0) return false;
3587
+ return visibleButtons(collection).some((button) => {
3588
+ const box = button.getBoundingClientRect();
3589
+ return media.some((m) => overlaps(box, m));
3590
+ });
3591
+ }
3592
+ function stack(collection, on) {
3593
+ if (on) {
3594
+ collection.setAttribute(STACKED_ATTR, "");
3595
+ collection.style.flexDirection = "column";
3596
+ collection.style.alignItems = "flex-start";
3597
+ } else if (collection.hasAttribute(STACKED_ATTR)) {
3598
+ collection.removeAttribute(STACKED_ATTR);
3599
+ collection.style.flexDirection = "";
3600
+ collection.style.alignItems = "";
3601
+ }
3602
+ }
3603
+ function fitButtonCollection(collection) {
3604
+ if (!collection.querySelector(COPY_SELECTOR)) {
3605
+ stack(collection, false);
3606
+ return;
3607
+ }
3608
+ if (!/flex/.test(getComputedStyle(collection).display)) return;
3609
+ stack(collection, false);
3610
+ if (visibleButtons(collection).length < 2) return;
3611
+ if (hitsMedia(collection)) stack(collection, true);
3612
+ }
3613
+ var watched = /* @__PURE__ */ new WeakSet();
3614
+ function fitButtonCollections(doc, collectionAttr) {
3615
+ for (const collection of Array.from(doc.querySelectorAll(`[${collectionAttr}]`))) {
3616
+ fitButtonCollection(collection);
3617
+ }
3618
+ const win = doc.defaultView;
3619
+ if (!win || watched.has(doc)) return;
3620
+ watched.add(doc);
3621
+ let frame = 0;
3622
+ win.addEventListener("resize", () => {
3623
+ win.cancelAnimationFrame(frame);
3624
+ frame = win.requestAnimationFrame(() => fitButtonCollections(doc, collectionAttr));
3625
+ });
3626
+ }
3627
+
3553
3628
  // src/lib/button-collections.ts
3554
3629
  var BUTTON_COLLECTIONS_KEY = "__ohw_button_collections";
3555
3630
  var BUTTON_SNAPSHOTS_KEY = "__ohw_button_snapshots";
@@ -3698,8 +3773,21 @@ function addButtonNextTo(button, content) {
3698
3773
  const collection = buttonCollectionOf(button) ?? (isSectionCta(button) ? ensureSectionButtonCollection(button, content) : null);
3699
3774
  return collection ? addButtonToCollection(collection, content) : duplicateButton(button, content, "new");
3700
3775
  }
3776
+ var LABEL_EDITABLE_SELECTOR = '[data-ohw-editable="text"], [data-ohw-editable="plain"]';
3701
3777
  function labelElement2(el) {
3702
- return el.querySelector('[data-ohw-editable="text"], [data-ohw-editable="plain"]') ?? el.querySelector("[data-ohw-key]");
3778
+ return el.querySelector(LABEL_EDITABLE_SELECTOR) ?? (el.matches(LABEL_EDITABLE_SELECTOR) ? el : null) ?? el.querySelector("[data-ohw-key]") ?? (el.hasAttribute("data-ohw-key") ? el : null);
3779
+ }
3780
+ function keepOnlyLabel(copy, label) {
3781
+ if (!label || label === copy) return;
3782
+ let node = label;
3783
+ while (node && node !== copy) {
3784
+ const parent = node.parentElement;
3785
+ if (!parent) break;
3786
+ for (const sibling of Array.from(parent.children)) {
3787
+ if (sibling !== node) sibling.remove();
3788
+ }
3789
+ node = parent;
3790
+ }
3703
3791
  }
3704
3792
  function buttonLabel(el) {
3705
3793
  const label = labelElement2(el);
@@ -3884,6 +3972,17 @@ function stampVariantClass(el, variant) {
3884
3972
  const without = raw.replace(/\bbtn--(?:primary|secondary|ghost)\b/g, "").replace(/\s+/g, " ").trim();
3885
3973
  el.className = without ? `${without} btn--${variant}` : `btn btn--${variant}`;
3886
3974
  }
3975
+ function keepSourceWidth(copy, source) {
3976
+ const row = copy.parentElement;
3977
+ if (!row || !source.isConnected) return;
3978
+ const rowWidth = row.getBoundingClientRect().width;
3979
+ const copyWidth = copy.getBoundingClientRect().width;
3980
+ const sourceWidth = source.getBoundingClientRect().width;
3981
+ if (rowWidth <= 0) return;
3982
+ const copyFills = copyWidth >= rowWidth - 1;
3983
+ const sourceFills = sourceWidth >= rowWidth - 1;
3984
+ if (copyFills && !sourceFills) copy.style.width = "auto";
3985
+ }
3887
3986
  function snapshotOf(el) {
3888
3987
  const id = buttonId(el) ?? "";
3889
3988
  const label = labelElement2(el);
@@ -3918,10 +4017,10 @@ function duplicateButton(source, content, mode = "duplicate") {
3918
4017
  if (href) copy.setAttribute("href", href);
3919
4018
  const sourceAssignment = readAssignments(content)[sourceId] ?? assignmentFor(source, content, tokens);
3920
4019
  const copyAssignment = mode === "new" ? { ...sourceAssignment, variant: "secondary" } : { ...sourceAssignment };
3921
- stampVariantClass(copy, copyAssignment.variant);
3922
- applyButtonStyle(copy, copyAssignment, paintedTokensOf(source) ?? tokens);
3923
4020
  const copyLabelEl = labelElement2(copy);
4021
+ if (mode === "new") keepOnlyLabel(copy, copyLabelEl);
3924
4022
  if (copyLabelEl) copyLabelEl.innerHTML = label;
4023
+ else if (mode === "new") copy.textContent = label;
3925
4024
  let collection = buttonCollectionOf(source) ?? (isSectionCta(source) ? ensureSectionButtonCollection(source, content) : null);
3926
4025
  const collections = readCollections(content);
3927
4026
  let collectionId = collection?.getAttribute(COLLECTION_ATTR) ?? "";
@@ -3943,6 +4042,10 @@ function duplicateButton(source, content, mode = "duplicate") {
3943
4042
  const at = members.indexOf(sourceId);
3944
4043
  members.splice(at >= 0 ? at + 1 : members.length, 0, copyId);
3945
4044
  }
4045
+ stampVariantClass(copy, copyAssignment.variant);
4046
+ keepSourceWidth(copy, source);
4047
+ applyButtonStyle(copy, copyAssignment, paintedTokensOf(source) ?? tokens);
4048
+ fitButtonCollection(collection);
3946
4049
  const sourceLabelKey = labelElement2(source)?.getAttribute("data-ohw-key");
3947
4050
  const snapshots = {
3948
4051
  ...readSnapshots(content),
@@ -4251,6 +4354,7 @@ function syncButtonsFromContent(doc, content) {
4251
4354
  applyCollectionPositionsFromContent(doc, content);
4252
4355
  applyButtonContentFromContent(doc, content);
4253
4356
  applyButtonStylesFromContent(doc, content);
4357
+ fitButtonCollections(doc, COLLECTION_ATTR);
4254
4358
  }
4255
4359
  function applyButtonContentFromContent(doc, content) {
4256
4360
  const snapshots = readSnapshots(content);
@@ -4291,7 +4395,7 @@ function reconcileButtonCollections(doc, content) {
4291
4395
  members.forEach((id) => claimed.add(id));
4292
4396
  const live = members.map((id) => findButton(doc, id) ?? restoreSnapshot(doc, snapshots[id], content)).filter((el) => Boolean(el && !hidden.has(buttonId(el) ?? "")));
4293
4397
  if (live.length === 0) continue;
4294
- const anchor = live.find((el) => el.isConnected) ?? live[0];
4398
+ const anchor = live.find((el) => el.isConnected) ?? copiesHome(doc, live) ?? live[0];
4295
4399
  const section = isSectionCta(anchor);
4296
4400
  if (live.length === 1 && !section) {
4297
4401
  const existing = buttonCollectionOf(live[0]);
@@ -4315,6 +4419,15 @@ function reconcileButtonCollections(doc, content) {
4315
4419
  if (section) syncCollectionEmpty(collection);
4316
4420
  }
4317
4421
  }
4422
+ function copiesHome(doc, copies) {
4423
+ for (const copy of copies) {
4424
+ const id = buttonId(copy);
4425
+ if (!id) continue;
4426
+ const origin = findButton(doc, `${hrefKeyBase(id)}-href`);
4427
+ if (origin?.isConnected && origin.parentElement) return origin;
4428
+ }
4429
+ return null;
4430
+ }
4318
4431
  function dropUnsavedCopies(doc, snapshots, collections) {
4319
4432
  const stale = Array.from(doc.querySelectorAll('[data-ohw-href-key*="-copy-"]')).filter((el) => {
4320
4433
  const id = buttonId(el);
@@ -9246,7 +9359,7 @@ function presetOf(node) {
9246
9359
  }
9247
9360
  function maxButtonsIn(parent) {
9248
9361
  if (parent.type === "card") return presetOf(parent) === "pricing" ? 2 : 1;
9249
- return 2;
9362
+ return Infinity;
9250
9363
  }
9251
9364
  function hasOnePrimaryIfPricing(parent) {
9252
9365
  if (parent.type !== "card" || presetOf(parent) !== "pricing" || parent.children.length !== 2) return true;
@@ -10010,8 +10123,8 @@ function parseBrandKit(raw) {
10010
10123
  return null;
10011
10124
  }
10012
10125
  }
10013
- function familyOf(stack) {
10014
- const first = stack.split(",")[0]?.trim() ?? "";
10126
+ function familyOf(stack2) {
10127
+ const first = stack2.split(",")[0]?.trim() ?? "";
10015
10128
  return first.replace(/^['"]|['"]$/g, "");
10016
10129
  }
10017
10130
  function quoteFamily(family) {
@@ -14278,8 +14391,8 @@ var GENERIC_FONT_KEYWORDS = /* @__PURE__ */ new Set([
14278
14391
  "emoji",
14279
14392
  "fangsong"
14280
14393
  ]);
14281
- function primaryFontFamily(stack) {
14282
- const first = stack.split(",").map((part) => part.trim().replace(/^["']|["']$/g, "")).find((part) => part && !GENERIC_FONT_KEYWORDS.has(part.toLowerCase()));
14394
+ function primaryFontFamily(stack2) {
14395
+ const first = stack2.split(",").map((part) => part.trim().replace(/^["']|["']$/g, "")).find((part) => part && !GENERIC_FONT_KEYWORDS.has(part.toLowerCase()));
14283
14396
  return first ?? "";
14284
14397
  }
14285
14398
  function humanizeFontFamily(name) {
@@ -22427,7 +22540,7 @@ function apiBase() {
22427
22540
  function bridgeVersion() {
22428
22541
  try {
22429
22542
  if (true) {
22430
- return "0.1.180-next.482";
22543
+ return "0.1.180-next.483";
22431
22544
  }
22432
22545
  } catch {
22433
22546
  }
@@ -22441,7 +22554,7 @@ function stampBridgeVersionOnDocument() {
22441
22554
  return;
22442
22555
  }
22443
22556
  try {
22444
- const v = true ? "0.1.180-next.482" : "";
22557
+ const v = true ? "0.1.180-next.483" : "";
22445
22558
  if (v) {
22446
22559
  document.documentElement.setAttribute("data-ohw-bridge-version", v);
22447
22560
  window.__OHW_BRIDGE_VERSION__ = v;
@@ -24346,11 +24459,11 @@ function MediaStripToolbar({
24346
24459
  ) });
24347
24460
  }
24348
24461
  function findMediaStripSlots(tree) {
24349
- const stack = tree.rows.flatMap((row) => row.blocks);
24350
- while (stack.length) {
24351
- const node = stack.shift();
24462
+ const stack2 = tree.rows.flatMap((row) => row.blocks);
24463
+ while (stack2.length) {
24464
+ const node = stack2.shift();
24352
24465
  if (node.type === "media-strip") return node.slots ?? {};
24353
- if (node.children) stack.push(...node.children);
24466
+ if (node.children) stack2.push(...node.children);
24354
24467
  }
24355
24468
  return null;
24356
24469
  }
@@ -24894,13 +25007,13 @@ function PricingCardToolbar({
24894
25007
  ) });
24895
25008
  }
24896
25009
  function findPricingCardNode(tree, index) {
24897
- const stack = tree.rows.flatMap((row) => row.blocks);
24898
- while (stack.length) {
24899
- const node = stack.shift();
25010
+ const stack2 = tree.rows.flatMap((row) => row.blocks);
25011
+ while (stack2.length) {
25012
+ const node = stack2.shift();
24900
25013
  if (node.type === "collection" && Array.isArray(node.children) && node.children[0]?.type === "card" && node.children[0].slots?.preset === "pricing") {
24901
25014
  return node.children[index] ?? null;
24902
25015
  }
24903
- if (node.children) stack.push(...node.children);
25016
+ if (node.children) stack2.push(...node.children);
24904
25017
  }
24905
25018
  return null;
24906
25019
  }
@@ -29686,7 +29799,7 @@ function isIconMarkupValue(value) {
29686
29799
  function isIconEditable(el) {
29687
29800
  return el.dataset.ohwEditable === "icon";
29688
29801
  }
29689
- var MEDIA_SELECTOR = '[data-ohw-editable="image"], [data-ohw-editable="bg-image"], [data-ohw-editable="video"]';
29802
+ var MEDIA_SELECTOR2 = '[data-ohw-editable="image"], [data-ohw-editable="bg-image"], [data-ohw-editable="video"]';
29690
29803
  var EDITOR_CHROME_SELECTOR = '[data-ohw-toolbar], [data-ohw-form-toolbar], [data-ohw-field-toolbar], [data-ohw-dropdown-option-toolbar], [data-ohw-item-toolbar], [data-ohw-more-menu], [data-ohw-state-toggle], [data-ohw-max-badge], [data-slot="dropdown-menu-content"], [data-slot="dropdown-menu-item"]';
29691
29804
  function isOverEditorChrome(x, y) {
29692
29805
  return document.elementsFromPoint(x, y).some((el) => el instanceof HTMLElement && el.matches(EDITOR_CHROME_SELECTOR));
@@ -30035,8 +30148,8 @@ function resolveNavContainerSelectionTarget(target, clientX, clientY) {
30035
30148
  function getNavigationSelectionParent(el) {
30036
30149
  const socialsRow = findSocialsRow(el);
30037
30150
  if (socialsRow) return socialsRow;
30038
- const buttonCollection = canAddButton(el) ? buttonCollectionOf(el) : null;
30039
- if (buttonCollection) return buttonCollection;
30151
+ const buttonCollection = buttonCollectionOf(el.parentElement);
30152
+ if (buttonCollection && isSectionButtonCollection(buttonCollection)) return buttonCollection;
30040
30153
  if (isNavigationItem2(el)) {
30041
30154
  const childrenRoot = el.closest("[data-ohw-nav-children]");
30042
30155
  if (childrenRoot) {
@@ -34909,7 +35022,7 @@ function OhhwellsBridge() {
34909
35022
  };
34910
35023
  const findImageAtPoint = (clientX, clientY, fromParentViewport) => {
34911
35024
  const { x, y } = toProbeCoords(clientX, clientY, fromParentViewport);
34912
- const images = Array.from(document.querySelectorAll(MEDIA_SELECTOR));
35025
+ const images = Array.from(document.querySelectorAll(MEDIA_SELECTOR2));
34913
35026
  const matches = [];
34914
35027
  for (let i = images.length - 1; i >= 0; i--) {
34915
35028
  const el = images[i];
@@ -37243,7 +37356,7 @@ function OhhwellsBridge() {
37243
37356
  if (e.data?.type !== "ow:click-at") return;
37244
37357
  const { clientX, clientY } = e.data;
37245
37358
  const allImages = Array.from(
37246
- document.querySelectorAll(MEDIA_SELECTOR)
37359
+ document.querySelectorAll(MEDIA_SELECTOR2)
37247
37360
  ).filter((el) => !!el.closest("[data-ohw-editable-state]"));
37248
37361
  const stateCardImage = allImages.find((el) => {
37249
37362
  const ownerCard = el.closest("[data-ohw-editable-state]");
@@ -37758,7 +37871,7 @@ function OhhwellsBridge() {
37758
37871
  postToParent2({
37759
37872
  type: "ow:ready",
37760
37873
  version: "1",
37761
- bridgeVersion: "0.1.180-next.482",
37874
+ bridgeVersion: "0.1.180-next.483",
37762
37875
  path: pathname,
37763
37876
  nodes: collectEditableNodes(editContentRef.current),
37764
37877
  sections
@@ -38461,7 +38574,7 @@ function OhhwellsBridge() {
38461
38574
  const currentSections = sectionsByPath[pathname] ?? [];
38462
38575
  linkPopoverOpenRef.current = linkPopover !== null;
38463
38576
  const handleMediaSelect = (0, import_react29.useCallback)((key) => {
38464
- const el = hoveredImageRef.current?.dataset.ohwKey === key ? hoveredImageRef.current : Array.from(document.querySelectorAll(MEDIA_SELECTOR)).find(
38577
+ const el = hoveredImageRef.current?.dataset.ohwKey === key ? hoveredImageRef.current : Array.from(document.querySelectorAll(MEDIA_SELECTOR2)).find(
38465
38578
  (m) => (m.dataset.ohwKey ?? "") === key
38466
38579
  ) ?? null;
38467
38580
  if (!el) return;
@@ -39340,6 +39453,7 @@ function EmptySection({ title, homeHref = "/", eyebrowKey, titleKey, subtitleKey
39340
39453
  ensureFooterTree,
39341
39454
  ensureSectionButtonCollections,
39342
39455
  filterAvailablePages,
39456
+ fitButtonCollection,
39343
39457
  foldAlignIntoTrees,
39344
39458
  getEditModeInitialState,
39345
39459
  getLogoVersions,