cms-renderer 0.6.10 → 0.6.11

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.
@@ -453,7 +453,391 @@ var LanguageSchema = z9.object({
453
453
 
454
454
  // lib/block-renderer.tsx
455
455
  import React from "react";
456
- import { ClientEditableBlock } from "./client-editable-block.js";
456
+
457
+ // lib/cms-overlay-script.ts
458
+ function generateCmsOverlayScript(cmsParentOrigin) {
459
+ return `
460
+ (function() {
461
+ if (window.__cmsOverlayInitialized) return;
462
+ window.__cmsOverlayInitialized = true;
463
+
464
+ var CMS_PARENT_ORIGIN = ${JSON.stringify(cmsParentOrigin)};
465
+
466
+ var style = document.createElement('style');
467
+ style.setAttribute('data-cms-overlay', '');
468
+ style.textContent = \`
469
+ [data-cms-editable] {
470
+ cursor: pointer;
471
+ border-radius: 2px;
472
+ }
473
+ [data-cms-editable]:hover {
474
+ outline: 2px solid #3b82f6;
475
+ outline-offset: 2px;
476
+ }
477
+ #cms-overlay-root {
478
+ position: fixed;
479
+ top: 0;
480
+ left: 0;
481
+ width: 0;
482
+ height: 0;
483
+ pointer-events: none;
484
+ z-index: 99998;
485
+ }
486
+ #cms-overlay-root > * {
487
+ pointer-events: auto;
488
+ }
489
+ .cms-block-outline {
490
+ position: fixed;
491
+ border: 2px solid #3b82f6;
492
+ border-radius: 4px;
493
+ pointer-events: none;
494
+ z-index: 99997;
495
+ transition: all 0.15s ease;
496
+ }
497
+ .cms-block-outline.cms-outline-selected {
498
+ border-color: #2563eb;
499
+ border-width: 3px;
500
+ }
501
+ .cms-block-cursor {
502
+ position: fixed;
503
+ width: 22px;
504
+ height: 22px;
505
+ background: radial-gradient(circle, #fff 5px, #000 5px);
506
+ border-radius: 50%;
507
+ pointer-events: none;
508
+ z-index: 99999;
509
+ transform: translate(-50%, -50%);
510
+ opacity: 0;
511
+ transition: opacity 0.1s ease;
512
+ }
513
+ .cms-block-cursor.cms-cursor-visible {
514
+ opacity: 1;
515
+ }
516
+ .cms-block-toolbar {
517
+ position: fixed;
518
+ display: flex;
519
+ gap: 4px;
520
+ background: #1f2937;
521
+ border-radius: 6px;
522
+ padding: 4px;
523
+ box-shadow: 0 4px 12px rgba(0,0,0,0.25);
524
+ z-index: 99999;
525
+ pointer-events: none;
526
+ opacity: 0;
527
+ transform: scale(0.9) translateY(4px);
528
+ transition: opacity 0.15s ease, transform 0.15s ease;
529
+ }
530
+ .cms-block-toolbar.cms-toolbar-visible {
531
+ opacity: 1;
532
+ transform: scale(1) translateY(0);
533
+ pointer-events: auto;
534
+ }
535
+ .cms-block-toolbar button {
536
+ display: flex;
537
+ align-items: center;
538
+ justify-content: center;
539
+ width: 28px;
540
+ height: 28px;
541
+ border: none;
542
+ background: transparent;
543
+ color: #9ca3af;
544
+ border-radius: 4px;
545
+ cursor: pointer;
546
+ transition: background 0.15s ease, color 0.15s ease;
547
+ }
548
+ .cms-block-toolbar button:hover {
549
+ background: #374151;
550
+ color: #fff;
551
+ }
552
+ .cms-block-toolbar button.delete:hover {
553
+ background: #dc2626;
554
+ color: #fff;
555
+ }
556
+ .cms-block-toolbar button:disabled {
557
+ opacity: 0.4;
558
+ cursor: not-allowed;
559
+ }
560
+ .cms-block-toolbar button:disabled:hover {
561
+ background: transparent;
562
+ color: #9ca3af;
563
+ }
564
+ .cms-block-toolbar svg {
565
+ width: 16px;
566
+ height: 16px;
567
+ }
568
+ \`;
569
+ document.head.appendChild(style);
570
+
571
+ function stampBlockElements() {
572
+ var sentinels = document.querySelectorAll('[data-cms-sentinel]');
573
+ sentinels.forEach(function(sentinel) {
574
+ var blockEl = sentinel.nextElementSibling;
575
+ if (!blockEl) return;
576
+
577
+ var blockId = sentinel.getAttribute('data-block-id');
578
+ var blockType = sentinel.getAttribute('data-block-type');
579
+
580
+ blockEl.setAttribute('data-cms-block', '');
581
+ blockEl.setAttribute('data-block-id', blockId);
582
+ blockEl.setAttribute('data-block-type', blockType);
583
+ blockEl.setAttribute('data-cms-editable', '');
584
+ });
585
+ }
586
+
587
+ stampBlockElements();
588
+
589
+ var stampObserver = new MutationObserver(function(mutations) {
590
+ var needsStamp = mutations.some(function(m) {
591
+ return m.addedNodes.length > 0;
592
+ });
593
+ if (needsStamp) stampBlockElements();
594
+ });
595
+ stampObserver.observe(document.body, { childList: true, subtree: true });
596
+
597
+ var overlayRoot = document.createElement('div');
598
+ overlayRoot.id = 'cms-overlay-root';
599
+ document.body.appendChild(overlayRoot);
600
+
601
+ var cursor = document.createElement('div');
602
+ cursor.className = 'cms-block-cursor';
603
+ overlayRoot.appendChild(cursor);
604
+
605
+ var outline = document.createElement('div');
606
+ outline.className = 'cms-block-outline';
607
+ outline.style.display = 'none';
608
+ overlayRoot.appendChild(outline);
609
+
610
+ var selectedOutline = document.createElement('div');
611
+ selectedOutline.className = 'cms-block-outline cms-outline-selected';
612
+ selectedOutline.style.display = 'none';
613
+ overlayRoot.appendChild(selectedOutline);
614
+
615
+ var toolbar = document.createElement('div');
616
+ toolbar.className = 'cms-block-toolbar';
617
+ toolbar.setAttribute('data-cms-toolbar', '');
618
+ toolbar.innerHTML = \`
619
+ <button class="move-up" title="Move up">
620
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
621
+ <path d="M18 15l-6-6-6 6"/>
622
+ </svg>
623
+ </button>
624
+ <button class="move-down" title="Move down">
625
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
626
+ <path d="M6 9l6 6 6-6"/>
627
+ </svg>
628
+ </button>
629
+ <button class="delete" title="Delete">
630
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
631
+ <path d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
632
+ </svg>
633
+ </button>
634
+ \`;
635
+ overlayRoot.appendChild(toolbar);
636
+
637
+ var currentBlockId = null;
638
+ var toolbarVisible = false;
639
+ var selectedBlockId = null;
640
+
641
+ function postToParent(message) {
642
+ if (!CMS_PARENT_ORIGIN || !window.parent || window.parent === window) {
643
+ return;
644
+ }
645
+ window.parent.postMessage(message, CMS_PARENT_ORIGIN);
646
+ }
647
+
648
+ function sendReadySignal() {
649
+ postToParent({ type: 'cms-preview-ready' });
650
+ }
651
+ sendReadySignal();
652
+ // The preview iframe can execute before the admin listener is attached.
653
+ setTimeout(sendReadySignal, 500);
654
+ setTimeout(sendReadySignal, 1500);
655
+
656
+ function getBlockElement(blockId) {
657
+ return document.querySelector('[data-block-id="' + blockId + '"]');
658
+ }
659
+
660
+ function getAllBlocks() {
661
+ return Array.from(document.querySelectorAll('[data-cms-block]'));
662
+ }
663
+
664
+ function getBlockIndex(blockId) {
665
+ var blocks = getAllBlocks();
666
+ for (var i = 0; i < blocks.length; i++) {
667
+ if (blocks[i].getAttribute('data-block-id') === blockId) return i;
668
+ }
669
+ return -1;
670
+ }
671
+
672
+ function updateOutline(el, outlineEl) {
673
+ if (!el) {
674
+ outlineEl.style.display = 'none';
675
+ return;
676
+ }
677
+ var rect = el.getBoundingClientRect();
678
+ outlineEl.style.display = 'block';
679
+ outlineEl.style.top = rect.top + 'px';
680
+ outlineEl.style.left = rect.left + 'px';
681
+ outlineEl.style.width = rect.width + 'px';
682
+ outlineEl.style.height = rect.height + 'px';
683
+ }
684
+
685
+ function positionToolbar(x, y) {
686
+ var rect = toolbar.getBoundingClientRect();
687
+ var top = Math.max(4, y - rect.height - 12);
688
+ var left = Math.max(4, Math.min(x - rect.width / 2, window.innerWidth - rect.width - 4));
689
+ toolbar.style.top = top + 'px';
690
+ toolbar.style.left = left + 'px';
691
+ }
692
+
693
+ function showToolbar(x, y, blockId) {
694
+ currentBlockId = blockId;
695
+ toolbarVisible = true;
696
+ positionToolbar(x, y);
697
+ toolbar.classList.add('cms-toolbar-visible');
698
+
699
+ var index = getBlockIndex(blockId);
700
+ var total = getAllBlocks().length;
701
+ toolbar.querySelector('.move-up').disabled = index <= 0;
702
+ toolbar.querySelector('.move-down').disabled = index >= total - 1;
703
+ }
704
+
705
+ function hideToolbar() {
706
+ toolbarVisible = false;
707
+ toolbar.classList.remove('cms-toolbar-visible');
708
+ }
709
+
710
+ function showCursor(x, y) {
711
+ cursor.style.top = y + 'px';
712
+ cursor.style.left = x + 'px';
713
+ cursor.classList.add('cms-cursor-visible');
714
+ }
715
+
716
+ function hideCursor() {
717
+ cursor.classList.remove('cms-cursor-visible');
718
+ }
719
+
720
+ document.addEventListener('mouseover', function(e) {
721
+ if (toolbarVisible) return;
722
+ var block = e.target.closest('[data-cms-block]');
723
+ if (block) {
724
+ updateOutline(block, outline);
725
+ }
726
+ });
727
+
728
+ document.addEventListener('mouseout', function(e) {
729
+ var block = e.target.closest('[data-cms-block]');
730
+ var related = e.relatedTarget ? e.relatedTarget.closest('[data-cms-block]') : null;
731
+ if (block && block !== related) {
732
+ outline.style.display = 'none';
733
+ hideCursor();
734
+ }
735
+ });
736
+
737
+ document.addEventListener('mousemove', function(e) {
738
+ if (toolbarVisible) return;
739
+ var block = e.target.closest('[data-cms-block]');
740
+ if (block) {
741
+ showCursor(e.clientX, e.clientY);
742
+ }
743
+ });
744
+
745
+ document.addEventListener('contextmenu', function(e) {
746
+ if (e.target.closest('[data-cms-toolbar]')) return;
747
+ var block = e.target.closest('[data-cms-block]');
748
+ if (block) {
749
+ if (toolbarVisible) return;
750
+ e.preventDefault();
751
+ hideCursor();
752
+ outline.style.display = 'none';
753
+ var blockId = block.getAttribute('data-block-id');
754
+ showToolbar(e.clientX, e.clientY, blockId);
755
+ }
756
+ });
757
+
758
+ document.addEventListener('click', function(e) {
759
+ if (toolbarVisible && !e.target.closest('[data-cms-toolbar]')) {
760
+ var block = e.target.closest('[data-cms-block]');
761
+ if (!block || block.getAttribute('data-block-id') !== currentBlockId) {
762
+ hideToolbar();
763
+ }
764
+ }
765
+
766
+ if (e.target.closest('[data-cms-toolbar]')) return;
767
+
768
+ var editable = e.target.closest('[data-cms-editable]');
769
+ if (editable) {
770
+ postToParent({
771
+ type: 'cms-editable-click',
772
+ blockId: editable.getAttribute('data-block-id'),
773
+ blockType: editable.getAttribute('data-block-type'),
774
+ contentPath: editable.getAttribute('data-content-path')
775
+ });
776
+ return;
777
+ }
778
+
779
+ var block = e.target.closest('[data-cms-block]');
780
+ if (block) {
781
+ postToParent({
782
+ type: 'cms-editable-click',
783
+ blockId: block.getAttribute('data-block-id'),
784
+ blockType: block.getAttribute('data-block-type'),
785
+ contentPath: null
786
+ });
787
+ }
788
+ });
789
+
790
+ toolbar.querySelector('.move-up').addEventListener('click', function() {
791
+ if (!currentBlockId) return;
792
+ postToParent({ type: 'cms-block-move', blockId: currentBlockId, direction: 'up' });
793
+ hideToolbar();
794
+ });
795
+
796
+ toolbar.querySelector('.move-down').addEventListener('click', function() {
797
+ if (!currentBlockId) return;
798
+ postToParent({ type: 'cms-block-move', blockId: currentBlockId, direction: 'down' });
799
+ hideToolbar();
800
+ });
801
+
802
+ toolbar.querySelector('.delete').addEventListener('click', function() {
803
+ if (!currentBlockId) return;
804
+ postToParent({ type: 'cms-block-delete', blockId: currentBlockId });
805
+ hideToolbar();
806
+ });
807
+
808
+ window.addEventListener('scroll', function() {
809
+ hideCursor();
810
+ hideToolbar();
811
+ if (selectedBlockId) {
812
+ var el = getBlockElement(selectedBlockId);
813
+ updateOutline(el, selectedOutline);
814
+ }
815
+ }, { passive: true, capture: true });
816
+
817
+ window.addEventListener('resize', function() {
818
+ if (selectedBlockId) {
819
+ var el = getBlockElement(selectedBlockId);
820
+ updateOutline(el, selectedOutline);
821
+ }
822
+ }, { passive: true });
823
+
824
+ window.addEventListener('message', function(e) {
825
+ if (CMS_PARENT_ORIGIN && e.origin !== CMS_PARENT_ORIGIN) return;
826
+ if (e.source !== window.parent) return;
827
+
828
+ if (e.data && e.data.type === 'cms-select-block') {
829
+ selectedBlockId = e.data.blockId || null;
830
+ if (selectedBlockId) {
831
+ var el = getBlockElement(selectedBlockId);
832
+ updateOutline(el, selectedOutline);
833
+ } else {
834
+ selectedOutline.style.display = 'none';
835
+ }
836
+ }
837
+ });
838
+ })();
839
+ `;
840
+ }
457
841
 
458
842
  // lib/cms-post-message.ts
459
843
  var CMS_PARENT_ORIGIN_PARAM = "cms_parent_origin";
@@ -511,137 +895,19 @@ function getCmsParentTargetOrigin(preferredCmsUrl, explicitParentOrigin) {
511
895
 
512
896
  // lib/block-renderer.tsx
513
897
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
514
- function extractContentValues(content, basePath = []) {
515
- const map = /* @__PURE__ */ new Map();
516
- function walk(obj, path) {
517
- if (typeof obj === "string" && obj.trim() !== "") {
518
- const contentPath = path.join(".");
519
- const existing = map.get(obj) || [];
520
- existing.push({ contentPath, value: obj });
521
- map.set(obj, existing);
522
- } else if (Array.isArray(obj)) {
523
- for (let index = 0; index < obj.length; index++) {
524
- walk(obj[index], [...path, String(index)]);
525
- }
526
- } else if (obj && typeof obj === "object") {
527
- for (const [key, value] of Object.entries(obj)) {
528
- walk(value, [...path, key]);
529
- }
530
- }
531
- }
532
- walk(content, basePath);
533
- return map;
534
- }
535
898
  function CmsEditableInit({
536
899
  cmsUrl,
537
900
  cmsParentOrigin
538
901
  }) {
539
- const cmsParentOriginJson = JSON.stringify(
540
- getCmsParentTargetOrigin(cmsUrl, cmsParentOrigin) ?? ""
541
- );
542
- return /* @__PURE__ */ jsxs(Fragment, { children: [
543
- /* @__PURE__ */ jsx("style", { children: `
544
- [data-cms-editable] {
545
- cursor: pointer;
546
- border-radius: 2px;
547
- }
548
- [data-cms-editable]:hover {
549
- outline: 2px solid #3b82f6;
550
- outline-offset: 2px;
551
- }
552
- .cms-block-toolbar {
553
- position: fixed;
554
- display: flex;
555
- gap: 4px;
556
- background: #1f2937;
557
- border-radius: 6px;
558
- padding: 4px;
559
- box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
560
- transition: opacity 0.15s ease;
561
- z-index: 99999;
562
- pointer-events: auto;
563
- }
564
- .cms-block-toolbar button {
565
- display: flex;
566
- align-items: center;
567
- justify-content: center;
568
- width: 28px;
569
- height: 28px;
570
- border: none;
571
- background: transparent;
572
- color: #9ca3af;
573
- border-radius: 4px;
574
- cursor: pointer;
575
- transition: background 0.15s ease, color 0.15s ease;
576
- }
577
- .cms-block-toolbar button:hover {
578
- background: #374151;
579
- color: #fff;
580
- }
581
- .cms-block-toolbar button.delete:hover {
582
- background: #dc2626;
583
- color: #fff;
584
- }
585
- .cms-block-toolbar button:disabled {
586
- opacity: 0.4;
587
- cursor: not-allowed;
588
- }
589
- .cms-block-toolbar button:disabled:hover {
590
- background: transparent;
591
- color: #9ca3af;
592
- }
593
- .cms-block-toolbar svg {
594
- width: 16px;
595
- height: 16px;
596
- }
597
- ` }),
598
- /* @__PURE__ */ jsx(
599
- "script",
600
- {
601
- dangerouslySetInnerHTML: {
602
- __html: `
603
- (function() {
604
- var cmsParentOrigin = ${cmsParentOriginJson};
605
- if (!window.__cmsEditableInitialized) {
606
- window.__cmsEditableInitialized = true;
607
-
608
- document.addEventListener('click', function(e) {
609
- if (e.target.closest('.cms-block-toolbar')) return;
610
-
611
- var editableTarget = e.target.closest('[data-cms-editable]');
612
- if (editableTarget) {
613
- var message = {
614
- type: 'cms-editable-click',
615
- blockId: editableTarget.getAttribute('data-block-id'),
616
- blockType: editableTarget.getAttribute('data-block-type'),
617
- contentPath: editableTarget.getAttribute('data-content-path')
618
- };
619
- if (cmsParentOrigin && window.parent && window.parent !== window) {
620
- window.parent.postMessage(message, cmsParentOrigin);
621
- }
622
- return;
623
- }
624
-
625
- var blockTarget = e.target.closest('[data-cms-block]');
626
- if (blockTarget) {
627
- var message = {
628
- type: 'cms-editable-click',
629
- blockId: blockTarget.getAttribute('data-block-id'),
630
- blockType: blockTarget.getAttribute('data-block-type'),
631
- contentPath: null
632
- };
633
- if (cmsParentOrigin && window.parent && window.parent !== window) {
634
- window.parent.postMessage(message, cmsParentOrigin);
635
- }
636
- }
637
- });
638
- }
639
- })();
640
- `
641
- }
902
+ const targetOrigin = getCmsParentTargetOrigin(cmsUrl, cmsParentOrigin) ?? "";
903
+ return /* @__PURE__ */ jsx(
904
+ "script",
905
+ {
906
+ dangerouslySetInnerHTML: {
907
+ __html: generateCmsOverlayScript(targetOrigin)
642
908
  }
643
- )
644
- ] });
909
+ }
910
+ );
645
911
  }
646
912
  function pathMatchesPattern(path, pattern) {
647
913
  const pathSegs = path.split("/").filter(Boolean);
@@ -692,9 +958,19 @@ function BlockRenderer({
692
958
  if (disableEditable) {
693
959
  return component;
694
960
  }
695
- const contentValueMap = extractContentValues(block.content);
696
- const contentEntries = Array.from(contentValueMap.entries()).map(([value, matches]) => ({ v: value, p: matches[0]?.contentPath })).filter((e) => !!e.p);
697
- return /* @__PURE__ */ jsx(ClientEditableBlock, { blockId: block.id, blockType: block.type, contentEntries, children: component });
961
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
962
+ /* @__PURE__ */ jsx(
963
+ "span",
964
+ {
965
+ "data-cms-sentinel": "",
966
+ "data-block-id": block.id,
967
+ "data-block-type": block.type,
968
+ style: { display: "none" },
969
+ "aria-hidden": "true"
970
+ }
971
+ ),
972
+ component
973
+ ] });
698
974
  }
699
975
 
700
976
  // lib/parametric-route.tsx
@@ -724,11 +1000,11 @@ async function renderParametricRoute({
724
1000
  websiteId: providedWebsiteId
725
1001
  }) {
726
1002
  const websiteId = getWebsiteId(providedWebsiteId);
727
- const { slug } = await params;
728
- const resolvedSearchParams = await searchParams;
1003
+ const { slug } = "then" in params ? await params : params;
1004
+ const resolvedSearchParams = searchParams && "then" in searchParams ? await searchParams : searchParams;
729
1005
  let aiPreviewIndex = null;
730
1006
  const aiPreviewParam = resolvedSearchParams?.ai_preview;
731
- if (aiPreviewParam) {
1007
+ if (aiPreviewParam && typeof aiPreviewParam !== "boolean") {
732
1008
  const paramValue = Array.isArray(aiPreviewParam) ? aiPreviewParam[0] : aiPreviewParam;
733
1009
  if (paramValue) {
734
1010
  const parsed = parseInt(paramValue, 10);
@@ -738,9 +1014,9 @@ async function renderParametricRoute({
738
1014
  }
739
1015
  }
740
1016
  const editModeParam = resolvedSearchParams?.edit_mode;
741
- const editMode = editModeParam === "true" || editModeParam === "1";
1017
+ const editMode = editModeParam === true || editModeParam === "true" || editModeParam === "1";
742
1018
  const cmsParentOriginParam = resolvedSearchParams?.cms_parent_origin;
743
- const cmsParentOrigin = Array.isArray(cmsParentOriginParam) ? cmsParentOriginParam[0] : cmsParentOriginParam;
1019
+ const cmsParentOrigin = typeof cmsParentOriginParam === "boolean" ? void 0 : Array.isArray(cmsParentOriginParam) ? cmsParentOriginParam[0] : cmsParentOriginParam;
744
1020
  const rawPath = `/${slug.join("/")}`;
745
1021
  const path = normalizePath(rawPath);
746
1022
  if (/\.[a-zA-Z0-9]+$/.test(path)) {