@websline/cms-view-utils 1.5.0 → 1.5.2

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.
@@ -1 +1 @@
1
- (()=>{(function(){var c;if(typeof window>"u")return;let a=150,d=60,e=document.currentScript||[...document.querySelectorAll("script[data-path]")].pop(),n=(c=e==null?void 0:e.dataset)==null?void 0:c.path;if(!n){console.warn("[editScroll] Missing data-path");return}let r=`preview-scroll:${n}`,s=null,i=0,l=()=>{let t=window.scrollY;t!==i&&(i=t,sessionStorage.setItem(r,String(t)))},u=()=>{clearTimeout(s),s=setTimeout(l,a)},w=()=>{let t=sessionStorage.getItem(r);if(!t)return;let o=Number(t);Number.isNaN(o)||requestAnimationFrame(()=>{window.scrollTo(0,o),setTimeout(()=>{window.scrollTo(0,o)},d)})};window.addEventListener("scroll",u,{passive:!0}),window.addEventListener("beforeunload",l),window.addEventListener("load",w)})();})();
1
+ (()=>{var l=t=>`preview-scroll:${t}`;(function(){var s;if(typeof window>"u")return;let t=150,e=document.currentScript||[...document.querySelectorAll("script[data-path]")].pop(),r=(s=e==null?void 0:e.dataset)==null?void 0:s.path;if(!r){console.warn("[editScroll] Missing data-path");return}let c=l(r),n=null,a=0,i=()=>{let o=window.scrollY;o!==a&&(a=o,sessionStorage.setItem(c,String(o)))},p=()=>{clearTimeout(n),n=setTimeout(i,t)};window.addEventListener("scroll",p,{passive:!0})})();})();
@@ -1 +1 @@
1
- (()=>{var r={BLOCK_ADD:"BLOCK_ADD",BLOCK_DELETE:"BLOCK_DELETE",BLOCK_EDIT:"BLOCK_EDIT",BLOCK_POSITION:"BLOCK_POSITION",BLOCK_SHOW:"BLOCK_SHOW",BLOCK_HIDE:"BLOCK_HIDE",PATH_CHANGED:"PATH_CHANGED",PATH_ON_LOAD:"PATH_ON_LOAD",IFRAME_DOM_LOADED:"IFRAME_DOM_LOADED",CURRENT_BLOCK_EDIT:"CURRENT_BLOCK_EDIT",SCROLL_TO_BLOCK:"SCROLL_TO_BLOCK"},_=(i,a={})=>{i&&window.parent!==window&&window.parent.postMessage({type:i,payload:a},"*")};(function(){if(window.parent===window||window.__PREVIEW_BRIDGE_INITIALIZED__)return;window.__PREVIEW_BRIDGE_INITIALIZED__=!0;let i=t=>t.metaKey||t.ctrlKey||t.shiftKey,a=t=>{let n=t.target.closest("a[href]");if(!n||n.target==="_blank"||i(t))return;let o=new URL(n.href,window.location.origin);o.origin===window.location.origin&&(t.preventDefault(),t.stopPropagation(),_(r.PATH_CHANGED,{path:o.pathname}))};document.addEventListener("click",a,!0),_(r.PATH_ON_LOAD,{path:window.location.pathname}),window.addEventListener("load",()=>{_(r.IFRAME_DOM_LOADED)});let L=t=>{let n=`[data-cms-block-id="${t}"]`,o=0,e=10,O=setInterval(()=>{let E=document.querySelector(n);E&&(E.scrollIntoView({behavior:"smooth",block:"start"}),clearInterval(O)),++o>=e&&clearInterval(O)},100)};window.addEventListener("message",t=>{let{type:n,payload:o}=t.data||{};if(n&&n===r.SCROLL_TO_BLOCK){let{draftBlockUuid:e}=o||{};if(!e)return;L(e)}})})();})();
1
+ (()=>{var i={BLOCK_ADD:"BLOCK_ADD",BLOCK_DELETE:"BLOCK_DELETE",BLOCK_EDIT:"BLOCK_EDIT",BLOCK_POSITION:"BLOCK_POSITION",BLOCK_SHOW:"BLOCK_SHOW",BLOCK_HIDE:"BLOCK_HIDE",PATH_CHANGED:"PATH_CHANGED",PATH_ON_LOAD:"PATH_ON_LOAD",IFRAME_DOM_LOADED:"IFRAME_DOM_LOADED",CURRENT_BLOCK_EDIT:"CURRENT_BLOCK_EDIT",SCROLL_TO_BLOCK:"SCROLL_TO_BLOCK"},s=(r,e={})=>{r&&window.parent!==window&&window.parent.postMessage({type:r,payload:e},"*")};var l=r=>`preview-scroll:${r}`,O=()=>{var r,e;return((e=(r=[...document.querySelectorAll("script[data-path]")].pop())==null?void 0:r.dataset)==null?void 0:e.path)||window.location.pathname};(function(){if(window.parent===window||window.__PREVIEW_BRIDGE_INITIALIZED__)return;window.__PREVIEW_BRIDGE_INITIALIZED__=!0;let r=t=>t.metaKey||t.ctrlKey||t.shiftKey,e=t=>{let o=t.target.closest("a[href]");if(!o||o.target==="_blank"||r(t))return;let n=new URL(o.href,window.location.origin);n.origin===window.location.origin&&(t.preventDefault(),t.stopPropagation(),s(i.PATH_CHANGED,{path:n.pathname}))};document.addEventListener("click",e,!0),s(i.PATH_ON_LOAD,{path:window.location.pathname});let E=()=>{let t=sessionStorage.getItem(l(O()));if(!t)return;let o=Number(t);Number.isNaN(o)||window.scrollTo({top:o,left:0,behavior:"instant"})};window.addEventListener("load",()=>{E(),s(i.IFRAME_DOM_LOADED)});let L=t=>{let o=`[data-cms-block-id="${t}"]`,n=0,a=10,_=setInterval(()=>{let c=document.querySelector(o);c&&(c.scrollIntoView({behavior:"smooth",block:"start"}),clearInterval(_)),++n>=a&&clearInterval(_)},100)};window.addEventListener("message",t=>{let{type:o,payload:n}=t.data||{};if(o&&o===i.SCROLL_TO_BLOCK){let{draftBlockUuid:a}=n||{};if(!a)return;L(a)}})})();})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@websline/cms-view-utils",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src",
@@ -40,6 +40,7 @@ const fetchPage = async (context) => {
40
40
  context.locals.page = data?.page ?? {};
41
41
  context.locals.seo = data?.seo ?? {};
42
42
  context.locals.languageSwitcher = data?.languageSwitcher ?? [];
43
+ context.locals.tenant = data?.tenant ?? null;
43
44
  context.locals._cmsRaw = data;
44
45
 
45
46
  if (isCMSPreviewRoute) {
@@ -126,13 +126,28 @@
126
126
  isHovered = true;
127
127
  }, 50);
128
128
 
129
+ const isRefInViewport = () => {
130
+ if (!ref) return true;
131
+
132
+ const { top, bottom } = ref.getBoundingClientRect();
133
+ const viewportHeight =
134
+ window.innerHeight || document.documentElement.clientHeight;
135
+
136
+ return bottom > 0 && top < viewportHeight;
137
+ };
138
+
129
139
  const handler = (event) => {
130
140
  const { type, payload } = event.data || {};
131
141
  if (type !== EVENTS.CURRENT_BLOCK_EDIT) return;
132
142
 
133
- const { draftBlockUuid } = payload || {};
143
+ const { draftBlockUuid, scroll } = payload || {};
144
+ const becameActive = draftBlockUuid === block.uuid;
145
+
146
+ isActive = becameActive;
134
147
 
135
- isActive = draftBlockUuid === block.uuid;
148
+ if (becameActive && scroll && !isRefInViewport()) {
149
+ ref.scrollIntoView({ behavior: "smooth", block: "start" });
150
+ }
136
151
  };
137
152
 
138
153
  let isOutside = $derived(calcBarPosition === "outside");
@@ -1,8 +1,9 @@
1
+ import { storageKeyForPath } from "./scrollStore.js";
2
+
1
3
  (function () {
2
4
  if (typeof window === "undefined") return;
3
5
 
4
6
  const DEBOUNCE_MS = 150;
5
- const RESTORE_DELAY = 60;
6
7
 
7
8
  const script =
8
9
  document.currentScript ||
@@ -15,7 +16,7 @@
15
16
  return;
16
17
  }
17
18
 
18
- const storageKey = `preview-scroll:${path}`;
19
+ const storageKey = storageKeyForPath(path);
19
20
 
20
21
  let timeout = null;
21
22
  let lastY = 0;
@@ -33,23 +34,5 @@
33
34
  timeout = setTimeout(saveScroll, DEBOUNCE_MS);
34
35
  };
35
36
 
36
- const restoreScroll = () => {
37
- const raw = sessionStorage.getItem(storageKey);
38
- if (!raw) return;
39
-
40
- const y = Number(raw);
41
- if (Number.isNaN(y)) return;
42
-
43
- requestAnimationFrame(() => {
44
- window.scrollTo(0, y);
45
-
46
- setTimeout(() => {
47
- window.scrollTo(0, y);
48
- }, RESTORE_DELAY);
49
- });
50
- };
51
-
52
37
  window.addEventListener("scroll", onScroll, { passive: true });
53
- window.addEventListener("beforeunload", saveScroll);
54
- window.addEventListener("load", restoreScroll);
55
38
  })();
@@ -1,4 +1,5 @@
1
1
  import { EVENTS, notifyCMS } from "./notify.js";
2
+ import { resolveScrollPath, storageKeyForPath } from "./scrollStore.js";
2
3
 
3
4
  (function () {
4
5
  if (window.parent === window) return;
@@ -32,7 +33,18 @@ import { EVENTS, notifyCMS } from "./notify.js";
32
33
  path: window.location.pathname,
33
34
  });
34
35
 
36
+ const restoreScroll = () => {
37
+ const raw = sessionStorage.getItem(storageKeyForPath(resolveScrollPath()));
38
+ if (!raw) return;
39
+
40
+ const y = Number(raw);
41
+ if (Number.isNaN(y)) return;
42
+
43
+ window.scrollTo({ top: y, left: 0, behavior: "instant" });
44
+ };
45
+
35
46
  window.addEventListener("load", () => {
47
+ restoreScroll();
36
48
  notifyCMS(EVENTS.IFRAME_DOM_LOADED);
37
49
  });
38
50
 
@@ -0,0 +1,7 @@
1
+ const storageKeyForPath = (path) => `preview-scroll:${path}`;
2
+
3
+ const resolveScrollPath = () =>
4
+ [...document.querySelectorAll("script[data-path]")].pop()?.dataset?.path ||
5
+ window.location.pathname;
6
+
7
+ export { storageKeyForPath, resolveScrollPath };