@websline/cms-view-utils 1.5.2 → 1.5.3

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
- (()=>{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
+ (()=>{(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 +1 @@
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)}})})();})();
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)}})})();})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@websline/cms-view-utils",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src",
@@ -126,28 +126,13 @@
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
-
139
129
  const handler = (event) => {
140
130
  const { type, payload } = event.data || {};
141
131
  if (type !== EVENTS.CURRENT_BLOCK_EDIT) return;
142
132
 
143
- const { draftBlockUuid, scroll } = payload || {};
144
- const becameActive = draftBlockUuid === block.uuid;
145
-
146
- isActive = becameActive;
133
+ const { draftBlockUuid } = payload || {};
147
134
 
148
- if (becameActive && scroll && !isRefInViewport()) {
149
- ref.scrollIntoView({ behavior: "smooth", block: "start" });
150
- }
135
+ isActive = draftBlockUuid === block.uuid;
151
136
  };
152
137
 
153
138
  let isOutside = $derived(calcBarPosition === "outside");
@@ -1,9 +1,8 @@
1
- import { storageKeyForPath } from "./scrollStore.js";
2
-
3
1
  (function () {
4
2
  if (typeof window === "undefined") return;
5
3
 
6
4
  const DEBOUNCE_MS = 150;
5
+ const RESTORE_DELAY = 60;
7
6
 
8
7
  const script =
9
8
  document.currentScript ||
@@ -16,7 +15,7 @@ import { storageKeyForPath } from "./scrollStore.js";
16
15
  return;
17
16
  }
18
17
 
19
- const storageKey = storageKeyForPath(path);
18
+ const storageKey = `preview-scroll:${path}`;
20
19
 
21
20
  let timeout = null;
22
21
  let lastY = 0;
@@ -34,5 +33,23 @@ import { storageKeyForPath } from "./scrollStore.js";
34
33
  timeout = setTimeout(saveScroll, DEBOUNCE_MS);
35
34
  };
36
35
 
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
+
37
52
  window.addEventListener("scroll", onScroll, { passive: true });
53
+ window.addEventListener("beforeunload", saveScroll);
54
+ window.addEventListener("load", restoreScroll);
38
55
  })();
@@ -1,5 +1,4 @@
1
1
  import { EVENTS, notifyCMS } from "./notify.js";
2
- import { resolveScrollPath, storageKeyForPath } from "./scrollStore.js";
3
2
 
4
3
  (function () {
5
4
  if (window.parent === window) return;
@@ -33,18 +32,7 @@ import { resolveScrollPath, storageKeyForPath } from "./scrollStore.js";
33
32
  path: window.location.pathname,
34
33
  });
35
34
 
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
-
46
35
  window.addEventListener("load", () => {
47
- restoreScroll();
48
36
  notifyCMS(EVENTS.IFRAME_DOM_LOADED);
49
37
  });
50
38
 
@@ -1,7 +0,0 @@
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 };