cms-renderer 0.6.10 → 0.6.12

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.
@@ -381,7 +381,427 @@ var LanguageSchema = z9.object({
381
381
 
382
382
  // lib/block-renderer.tsx
383
383
  import React from "react";
384
- import { ClientEditableBlock } from "./client-editable-block.js";
384
+
385
+ // lib/cms-overlay-script.ts
386
+ function generateCmsOverlayScript(cmsParentOrigin) {
387
+ return `
388
+ (function() {
389
+ if (window.__cmsOverlayInitialized) return;
390
+ window.__cmsOverlayInitialized = true;
391
+
392
+ var CMS_PARENT_ORIGIN = ${JSON.stringify(cmsParentOrigin)};
393
+
394
+ var style = document.createElement('style');
395
+ style.setAttribute('data-cms-overlay', '');
396
+ style.textContent = \`
397
+ [data-cms-block],
398
+ [data-cms-editable] {
399
+ cursor: pointer;
400
+ }
401
+ [data-cms-editable] {
402
+ border-radius: 2px;
403
+ }
404
+ [data-cms-editable]:not([data-cms-block]):hover {
405
+ outline: 2px solid #3b82f6;
406
+ outline-offset: 2px;
407
+ }
408
+ #cms-overlay-root {
409
+ position: fixed;
410
+ top: 0;
411
+ left: 0;
412
+ width: 0;
413
+ height: 0;
414
+ pointer-events: none;
415
+ z-index: 99998;
416
+ }
417
+ #cms-overlay-root > * {
418
+ pointer-events: none;
419
+ }
420
+ .cms-block-outline {
421
+ position: fixed;
422
+ border: 2px solid #3b82f6;
423
+ border-radius: 4px;
424
+ pointer-events: none;
425
+ z-index: 99997;
426
+ transition: all 0.15s ease;
427
+ }
428
+ .cms-block-outline.cms-outline-selected {
429
+ border-color: #2563eb;
430
+ border-width: 3px;
431
+ }
432
+ .cms-block-cursor {
433
+ position: fixed;
434
+ width: 22px;
435
+ height: 22px;
436
+ background: radial-gradient(circle, #fff 5px, #000 5px);
437
+ border-radius: 50%;
438
+ pointer-events: none;
439
+ z-index: 99999;
440
+ transform: translate(-50%, -50%);
441
+ opacity: 0;
442
+ transition: opacity 0.1s ease;
443
+ }
444
+ .cms-block-cursor.cms-cursor-visible {
445
+ opacity: 1;
446
+ }
447
+ .cms-block-toolbar {
448
+ position: fixed;
449
+ display: flex;
450
+ gap: 4px;
451
+ background: #1f2937;
452
+ border-radius: 6px;
453
+ padding: 4px;
454
+ box-shadow: 0 4px 12px rgba(0,0,0,0.25);
455
+ z-index: 99999;
456
+ pointer-events: none;
457
+ opacity: 0;
458
+ transform: scale(0.9) translateY(4px);
459
+ transition: opacity 0.15s ease, transform 0.15s ease;
460
+ }
461
+ .cms-block-toolbar.cms-toolbar-visible {
462
+ opacity: 1;
463
+ transform: scale(1) translateY(0);
464
+ pointer-events: auto;
465
+ }
466
+ .cms-block-toolbar button {
467
+ display: flex;
468
+ align-items: center;
469
+ justify-content: center;
470
+ width: 28px;
471
+ height: 28px;
472
+ border: none;
473
+ background: transparent;
474
+ color: #9ca3af;
475
+ border-radius: 4px;
476
+ cursor: pointer;
477
+ transition: background 0.15s ease, color 0.15s ease;
478
+ }
479
+ .cms-block-toolbar button:hover {
480
+ background: #374151;
481
+ color: #fff;
482
+ }
483
+ .cms-block-toolbar button.delete:hover {
484
+ background: #dc2626;
485
+ color: #fff;
486
+ }
487
+ .cms-block-toolbar button:disabled {
488
+ opacity: 0.4;
489
+ cursor: not-allowed;
490
+ }
491
+ .cms-block-toolbar button:disabled:hover {
492
+ background: transparent;
493
+ color: #9ca3af;
494
+ }
495
+ .cms-block-toolbar svg {
496
+ width: 16px;
497
+ height: 16px;
498
+ }
499
+ \`;
500
+ document.head.appendChild(style);
501
+
502
+ function stampBlockElements() {
503
+ var sentinels = document.querySelectorAll('[data-cms-sentinel]');
504
+ sentinels.forEach(function(sentinel) {
505
+ var blockEl = sentinel.nextElementSibling;
506
+ if (!blockEl) return;
507
+
508
+ var blockId = sentinel.getAttribute('data-block-id');
509
+ var blockType = sentinel.getAttribute('data-block-type');
510
+
511
+ blockEl.setAttribute('data-cms-block', '');
512
+ blockEl.setAttribute('data-block-id', blockId);
513
+ blockEl.setAttribute('data-block-type', blockType);
514
+ });
515
+ }
516
+
517
+ stampBlockElements();
518
+
519
+ var stampObserver = new MutationObserver(function(mutations) {
520
+ var needsStamp = mutations.some(function(m) {
521
+ return m.addedNodes.length > 0;
522
+ });
523
+ if (needsStamp) stampBlockElements();
524
+ });
525
+ stampObserver.observe(document.body, { childList: true, subtree: true });
526
+
527
+ var overlayRoot = document.createElement('div');
528
+ overlayRoot.id = 'cms-overlay-root';
529
+ document.body.appendChild(overlayRoot);
530
+
531
+ var cursor = document.createElement('div');
532
+ cursor.className = 'cms-block-cursor';
533
+ overlayRoot.appendChild(cursor);
534
+
535
+ var outline = document.createElement('div');
536
+ outline.className = 'cms-block-outline';
537
+ outline.style.display = 'none';
538
+ overlayRoot.appendChild(outline);
539
+
540
+ var selectedOutline = document.createElement('div');
541
+ selectedOutline.className = 'cms-block-outline cms-outline-selected';
542
+ selectedOutline.style.display = 'none';
543
+ overlayRoot.appendChild(selectedOutline);
544
+
545
+ var toolbar = document.createElement('div');
546
+ toolbar.className = 'cms-block-toolbar';
547
+ toolbar.setAttribute('data-cms-toolbar', '');
548
+ toolbar.innerHTML = \`
549
+ <button class="move-up" title="Move up">
550
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
551
+ <path d="M18 15l-6-6-6 6"/>
552
+ </svg>
553
+ </button>
554
+ <button class="move-down" title="Move down">
555
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
556
+ <path d="M6 9l6 6 6-6"/>
557
+ </svg>
558
+ </button>
559
+ <button class="delete" title="Delete">
560
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
561
+ <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"/>
562
+ </svg>
563
+ </button>
564
+ \`;
565
+ overlayRoot.appendChild(toolbar);
566
+
567
+ var currentBlockId = null;
568
+ var toolbarVisible = false;
569
+ var selectedBlockId = null;
570
+
571
+ function decoratePreviewUrl(rawHref) {
572
+ if (!rawHref) return rawHref;
573
+ try {
574
+ var url = new URL(rawHref, window.location.href);
575
+ if (url.origin !== window.location.origin) return rawHref;
576
+ url.searchParams.set('edit_mode', 'true');
577
+ if (CMS_PARENT_ORIGIN) {
578
+ url.searchParams.set('cms_parent_origin', CMS_PARENT_ORIGIN);
579
+ }
580
+ return url.toString();
581
+ } catch (_) {
582
+ return rawHref;
583
+ }
584
+ }
585
+
586
+ function preserveEditParamsOnNavigation(e) {
587
+ if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
588
+ return;
589
+ }
590
+
591
+ var anchor = e.target.closest('a[href]');
592
+ if (!anchor || (anchor.target && anchor.target !== '_self') || anchor.hasAttribute('download')) {
593
+ return;
594
+ }
595
+
596
+ var href = anchor.getAttribute('href');
597
+ var decorated = decoratePreviewUrl(href);
598
+ if (decorated && decorated !== href) {
599
+ anchor.setAttribute('href', decorated);
600
+ }
601
+ }
602
+
603
+ function postToParent(message) {
604
+ if (!CMS_PARENT_ORIGIN || !window.parent || window.parent === window) {
605
+ return;
606
+ }
607
+ window.parent.postMessage(message, CMS_PARENT_ORIGIN);
608
+ }
609
+
610
+ function sendReadySignal() {
611
+ postToParent({ type: 'cms-preview-ready' });
612
+ }
613
+ sendReadySignal();
614
+ // The preview iframe can execute before the admin listener is attached.
615
+ setTimeout(sendReadySignal, 500);
616
+ setTimeout(sendReadySignal, 1500);
617
+
618
+ function getBlockElement(blockId) {
619
+ return document.querySelector('[data-block-id="' + blockId + '"]');
620
+ }
621
+
622
+ function getAllBlocks() {
623
+ return Array.from(document.querySelectorAll('[data-cms-block]'));
624
+ }
625
+
626
+ function getBlockIndex(blockId) {
627
+ var blocks = getAllBlocks();
628
+ for (var i = 0; i < blocks.length; i++) {
629
+ if (blocks[i].getAttribute('data-block-id') === blockId) return i;
630
+ }
631
+ return -1;
632
+ }
633
+
634
+ function updateOutline(el, outlineEl) {
635
+ if (!el) {
636
+ outlineEl.style.display = 'none';
637
+ return;
638
+ }
639
+ var rect = el.getBoundingClientRect();
640
+ outlineEl.style.display = 'block';
641
+ outlineEl.style.top = rect.top + 'px';
642
+ outlineEl.style.left = rect.left + 'px';
643
+ outlineEl.style.width = rect.width + 'px';
644
+ outlineEl.style.height = rect.height + 'px';
645
+ }
646
+
647
+ function positionToolbar(x, y) {
648
+ var rect = toolbar.getBoundingClientRect();
649
+ var top = Math.max(4, y - rect.height - 12);
650
+ var left = Math.max(4, Math.min(x - rect.width / 2, window.innerWidth - rect.width - 4));
651
+ toolbar.style.top = top + 'px';
652
+ toolbar.style.left = left + 'px';
653
+ }
654
+
655
+ function showToolbar(x, y, blockId) {
656
+ currentBlockId = blockId;
657
+ toolbarVisible = true;
658
+ positionToolbar(x, y);
659
+ toolbar.classList.add('cms-toolbar-visible');
660
+
661
+ var index = getBlockIndex(blockId);
662
+ var total = getAllBlocks().length;
663
+ toolbar.querySelector('.move-up').disabled = index <= 0;
664
+ toolbar.querySelector('.move-down').disabled = index >= total - 1;
665
+ }
666
+
667
+ function hideToolbar() {
668
+ toolbarVisible = false;
669
+ toolbar.classList.remove('cms-toolbar-visible');
670
+ }
671
+
672
+ function showCursor(x, y) {
673
+ cursor.style.top = y + 'px';
674
+ cursor.style.left = x + 'px';
675
+ cursor.classList.add('cms-cursor-visible');
676
+ }
677
+
678
+ function hideCursor() {
679
+ cursor.classList.remove('cms-cursor-visible');
680
+ }
681
+
682
+ document.addEventListener('mouseover', function(e) {
683
+ if (toolbarVisible) return;
684
+ var block = e.target.closest('[data-cms-block]');
685
+ if (block) {
686
+ updateOutline(block, outline);
687
+ }
688
+ });
689
+
690
+ document.addEventListener('mouseout', function(e) {
691
+ var block = e.target.closest('[data-cms-block]');
692
+ var related = e.relatedTarget ? e.relatedTarget.closest('[data-cms-block]') : null;
693
+ if (block && block !== related) {
694
+ outline.style.display = 'none';
695
+ hideCursor();
696
+ }
697
+ });
698
+
699
+ document.addEventListener('mousemove', function(e) {
700
+ if (toolbarVisible) return;
701
+ var block = e.target.closest('[data-cms-block]');
702
+ if (block) {
703
+ showCursor(e.clientX, e.clientY);
704
+ }
705
+ });
706
+
707
+ document.addEventListener('contextmenu', function(e) {
708
+ if (e.target.closest('[data-cms-toolbar]')) return;
709
+ var block = e.target.closest('[data-cms-block]');
710
+ if (block) {
711
+ if (toolbarVisible) return;
712
+ e.preventDefault();
713
+ hideCursor();
714
+ outline.style.display = 'none';
715
+ var blockId = block.getAttribute('data-block-id');
716
+ showToolbar(e.clientX, e.clientY, blockId);
717
+ }
718
+ });
719
+
720
+ document.addEventListener('click', function(e) {
721
+ preserveEditParamsOnNavigation(e);
722
+
723
+ if (toolbarVisible && !e.target.closest('[data-cms-toolbar]')) {
724
+ var block = e.target.closest('[data-cms-block]');
725
+ if (!block || block.getAttribute('data-block-id') !== currentBlockId) {
726
+ hideToolbar();
727
+ }
728
+ }
729
+
730
+ if (e.target.closest('[data-cms-toolbar]')) return;
731
+
732
+ var editable = e.target.closest('[data-cms-editable]');
733
+ if (editable) {
734
+ postToParent({
735
+ type: 'cms-editable-click',
736
+ blockId: editable.getAttribute('data-block-id'),
737
+ blockType: editable.getAttribute('data-block-type'),
738
+ contentPath: editable.getAttribute('data-content-path')
739
+ });
740
+ return;
741
+ }
742
+
743
+ var block = e.target.closest('[data-cms-block]');
744
+ if (block) {
745
+ postToParent({
746
+ type: 'cms-editable-click',
747
+ blockId: block.getAttribute('data-block-id'),
748
+ blockType: block.getAttribute('data-block-type'),
749
+ contentPath: null
750
+ });
751
+ }
752
+ }, true);
753
+
754
+ toolbar.querySelector('.move-up').addEventListener('click', function() {
755
+ if (!currentBlockId) return;
756
+ postToParent({ type: 'cms-block-action', action: 'move-up', blockId: currentBlockId });
757
+ hideToolbar();
758
+ });
759
+
760
+ toolbar.querySelector('.move-down').addEventListener('click', function() {
761
+ if (!currentBlockId) return;
762
+ postToParent({ type: 'cms-block-action', action: 'move-down', blockId: currentBlockId });
763
+ hideToolbar();
764
+ });
765
+
766
+ toolbar.querySelector('.delete').addEventListener('click', function() {
767
+ if (!currentBlockId) return;
768
+ postToParent({ type: 'cms-block-action', action: 'delete', blockId: currentBlockId });
769
+ hideToolbar();
770
+ });
771
+
772
+ window.addEventListener('scroll', function() {
773
+ hideCursor();
774
+ hideToolbar();
775
+ if (selectedBlockId) {
776
+ var el = getBlockElement(selectedBlockId);
777
+ updateOutline(el, selectedOutline);
778
+ }
779
+ }, { passive: true, capture: true });
780
+
781
+ window.addEventListener('resize', function() {
782
+ if (selectedBlockId) {
783
+ var el = getBlockElement(selectedBlockId);
784
+ updateOutline(el, selectedOutline);
785
+ }
786
+ }, { passive: true });
787
+
788
+ window.addEventListener('message', function(e) {
789
+ if (CMS_PARENT_ORIGIN && e.origin !== CMS_PARENT_ORIGIN) return;
790
+ if (e.source !== window.parent) return;
791
+
792
+ if (e.data && e.data.type === 'cms-select-block') {
793
+ selectedBlockId = e.data.blockId || null;
794
+ if (selectedBlockId) {
795
+ var el = getBlockElement(selectedBlockId);
796
+ updateOutline(el, selectedOutline);
797
+ } else {
798
+ selectedOutline.style.display = 'none';
799
+ }
800
+ }
801
+ });
802
+ })();
803
+ `;
804
+ }
385
805
 
386
806
  // lib/cms-post-message.ts
387
807
  var CMS_PARENT_ORIGIN_PARAM = "cms_parent_origin";
@@ -439,137 +859,19 @@ function getCmsParentTargetOrigin(preferredCmsUrl, explicitParentOrigin) {
439
859
 
440
860
  // lib/block-renderer.tsx
441
861
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
442
- function extractContentValues(content, basePath = []) {
443
- const map = /* @__PURE__ */ new Map();
444
- function walk(obj, path) {
445
- if (typeof obj === "string" && obj.trim() !== "") {
446
- const contentPath = path.join(".");
447
- const existing = map.get(obj) || [];
448
- existing.push({ contentPath, value: obj });
449
- map.set(obj, existing);
450
- } else if (Array.isArray(obj)) {
451
- for (let index = 0; index < obj.length; index++) {
452
- walk(obj[index], [...path, String(index)]);
453
- }
454
- } else if (obj && typeof obj === "object") {
455
- for (const [key, value] of Object.entries(obj)) {
456
- walk(value, [...path, key]);
457
- }
458
- }
459
- }
460
- walk(content, basePath);
461
- return map;
462
- }
463
862
  function CmsEditableInit({
464
863
  cmsUrl,
465
864
  cmsParentOrigin
466
865
  }) {
467
- const cmsParentOriginJson = JSON.stringify(
468
- getCmsParentTargetOrigin(cmsUrl, cmsParentOrigin) ?? ""
469
- );
470
- return /* @__PURE__ */ jsxs(Fragment, { children: [
471
- /* @__PURE__ */ jsx("style", { children: `
472
- [data-cms-editable] {
473
- cursor: pointer;
474
- border-radius: 2px;
475
- }
476
- [data-cms-editable]:hover {
477
- outline: 2px solid #3b82f6;
478
- outline-offset: 2px;
479
- }
480
- .cms-block-toolbar {
481
- position: fixed;
482
- display: flex;
483
- gap: 4px;
484
- background: #1f2937;
485
- border-radius: 6px;
486
- padding: 4px;
487
- box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
488
- transition: opacity 0.15s ease;
489
- z-index: 99999;
490
- pointer-events: auto;
491
- }
492
- .cms-block-toolbar button {
493
- display: flex;
494
- align-items: center;
495
- justify-content: center;
496
- width: 28px;
497
- height: 28px;
498
- border: none;
499
- background: transparent;
500
- color: #9ca3af;
501
- border-radius: 4px;
502
- cursor: pointer;
503
- transition: background 0.15s ease, color 0.15s ease;
504
- }
505
- .cms-block-toolbar button:hover {
506
- background: #374151;
507
- color: #fff;
508
- }
509
- .cms-block-toolbar button.delete:hover {
510
- background: #dc2626;
511
- color: #fff;
512
- }
513
- .cms-block-toolbar button:disabled {
514
- opacity: 0.4;
515
- cursor: not-allowed;
516
- }
517
- .cms-block-toolbar button:disabled:hover {
518
- background: transparent;
519
- color: #9ca3af;
520
- }
521
- .cms-block-toolbar svg {
522
- width: 16px;
523
- height: 16px;
524
- }
525
- ` }),
526
- /* @__PURE__ */ jsx(
527
- "script",
528
- {
529
- dangerouslySetInnerHTML: {
530
- __html: `
531
- (function() {
532
- var cmsParentOrigin = ${cmsParentOriginJson};
533
- if (!window.__cmsEditableInitialized) {
534
- window.__cmsEditableInitialized = true;
535
-
536
- document.addEventListener('click', function(e) {
537
- if (e.target.closest('.cms-block-toolbar')) return;
538
-
539
- var editableTarget = e.target.closest('[data-cms-editable]');
540
- if (editableTarget) {
541
- var message = {
542
- type: 'cms-editable-click',
543
- blockId: editableTarget.getAttribute('data-block-id'),
544
- blockType: editableTarget.getAttribute('data-block-type'),
545
- contentPath: editableTarget.getAttribute('data-content-path')
546
- };
547
- if (cmsParentOrigin && window.parent && window.parent !== window) {
548
- window.parent.postMessage(message, cmsParentOrigin);
549
- }
550
- return;
551
- }
552
-
553
- var blockTarget = e.target.closest('[data-cms-block]');
554
- if (blockTarget) {
555
- var message = {
556
- type: 'cms-editable-click',
557
- blockId: blockTarget.getAttribute('data-block-id'),
558
- blockType: blockTarget.getAttribute('data-block-type'),
559
- contentPath: null
560
- };
561
- if (cmsParentOrigin && window.parent && window.parent !== window) {
562
- window.parent.postMessage(message, cmsParentOrigin);
563
- }
564
- }
565
- });
566
- }
567
- })();
568
- `
569
- }
866
+ const targetOrigin = getCmsParentTargetOrigin(cmsUrl, cmsParentOrigin) ?? "";
867
+ return /* @__PURE__ */ jsx(
868
+ "script",
869
+ {
870
+ dangerouslySetInnerHTML: {
871
+ __html: generateCmsOverlayScript(targetOrigin)
570
872
  }
571
- )
572
- ] });
873
+ }
874
+ );
573
875
  }
574
876
  function pathMatchesPattern(path, pattern) {
575
877
  const pathSegs = path.split("/").filter(Boolean);
@@ -620,9 +922,19 @@ function BlockRenderer({
620
922
  if (disableEditable) {
621
923
  return component;
622
924
  }
623
- const contentValueMap = extractContentValues(block.content);
624
- const contentEntries = Array.from(contentValueMap.entries()).map(([value, matches]) => ({ v: value, p: matches[0]?.contentPath })).filter((e) => !!e.p);
625
- return /* @__PURE__ */ jsx(ClientEditableBlock, { blockId: block.id, blockType: block.type, contentEntries, children: component });
925
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
926
+ /* @__PURE__ */ jsx(
927
+ "span",
928
+ {
929
+ "data-cms-sentinel": "",
930
+ "data-block-id": block.id,
931
+ "data-block-type": block.type,
932
+ style: { display: "none" },
933
+ "aria-hidden": "true"
934
+ }
935
+ ),
936
+ component
937
+ ] });
626
938
  }
627
939
 
628
940
  // lib/cms-api.ts
@@ -700,11 +1012,11 @@ async function renderParametricRoute({
700
1012
  websiteId: providedWebsiteId
701
1013
  }) {
702
1014
  const websiteId = getWebsiteId(providedWebsiteId);
703
- const { slug } = await params;
704
- const resolvedSearchParams = await searchParams;
1015
+ const { slug } = "then" in params ? await params : params;
1016
+ const resolvedSearchParams = searchParams && "then" in searchParams ? await searchParams : searchParams;
705
1017
  let aiPreviewIndex = null;
706
1018
  const aiPreviewParam = resolvedSearchParams?.ai_preview;
707
- if (aiPreviewParam) {
1019
+ if (aiPreviewParam && typeof aiPreviewParam !== "boolean") {
708
1020
  const paramValue = Array.isArray(aiPreviewParam) ? aiPreviewParam[0] : aiPreviewParam;
709
1021
  if (paramValue) {
710
1022
  const parsed = parseInt(paramValue, 10);
@@ -714,9 +1026,9 @@ async function renderParametricRoute({
714
1026
  }
715
1027
  }
716
1028
  const editModeParam = resolvedSearchParams?.edit_mode;
717
- const editMode = editModeParam === "true" || editModeParam === "1";
1029
+ const editMode = editModeParam === true || editModeParam === "true" || editModeParam === "1";
718
1030
  const cmsParentOriginParam = resolvedSearchParams?.cms_parent_origin;
719
- const cmsParentOrigin = Array.isArray(cmsParentOriginParam) ? cmsParentOriginParam[0] : cmsParentOriginParam;
1031
+ const cmsParentOrigin = typeof cmsParentOriginParam === "boolean" ? void 0 : Array.isArray(cmsParentOriginParam) ? cmsParentOriginParam[0] : cmsParentOriginParam;
720
1032
  const rawPath = `/${slug.join("/")}`;
721
1033
  const path = normalizePath(rawPath);
722
1034
  if (/\.[a-zA-Z0-9]+$/.test(path)) {