@websline/cms-view-utils 1.0.0-alpha.5 → 1.0.0
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
|
|
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 e={BLOCK_ADD:"BLOCK_ADD",BLOCK_DELETE:"BLOCK_DELETE",BLOCK_EDIT:"BLOCK_EDIT",BLOCK_POSITION:"BLOCK_POSITION",BLOCK_SHOW:"BLOCK_SHOW",BLOCK_HIDE:"BLOCK_HIDE",
|
|
1
|
+
(()=>{var e={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"},_=(r,a={})=>{r&&window.parent!==window&&window.parent.postMessage({type:r,payload:a},"*")};(function(){if(window.parent===window||window.__PREVIEW_BRIDGE_INITIALIZED__)return;window.__PREVIEW_BRIDGE_INITIALIZED__=!0;let r=t=>t.metaKey||t.ctrlKey||t.shiftKey,a=t=>{let n=t.target.closest("a[href]");if(!n||n.target==="_blank"||r(t))return;let o=new URL(n.href,window.location.origin);o.origin===window.location.origin&&(t.preventDefault(),t.stopPropagation(),_(e.PATH_CHANGED,{path:o.pathname}))};document.addEventListener("click",a,!0),_(e.PATH_ON_LOAD,{path:window.location.pathname}),window.addEventListener("load",()=>{_(e.IFRAME_DOM_LOADED)});let L=t=>{let n=`[data-cms-block-id="${t}"]`,o=0,i=10,O=setInterval(()=>{let E=document.querySelector(n);E&&(E.scrollIntoView({behavior:"smooth",block:"center"}),clearInterval(O)),++o>=i&&clearInterval(O)},100)};window.addEventListener("message",t=>{let{type:n,payload:o}=t.data||{};if(n&&n===e.SCROLL_TO_BLOCK){let{draftBlockUuid:i}=o||{};if(!i)return;L(i)}})})();})();
|
package/package.json
CHANGED
package/src/utils/editScroll.js
CHANGED
|
@@ -6,16 +6,16 @@
|
|
|
6
6
|
|
|
7
7
|
const script =
|
|
8
8
|
document.currentScript ||
|
|
9
|
-
[...document.querySelectorAll("script[data-
|
|
9
|
+
[...document.querySelectorAll("script[data-path]")].pop();
|
|
10
10
|
|
|
11
|
-
const
|
|
11
|
+
const path = script?.dataset?.path;
|
|
12
12
|
|
|
13
|
-
if (!
|
|
14
|
-
console.warn("[editScroll] Missing data-
|
|
13
|
+
if (!path) {
|
|
14
|
+
console.warn("[editScroll] Missing data-path");
|
|
15
15
|
return;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
const storageKey = `preview-scroll:${
|
|
18
|
+
const storageKey = `preview-scroll:${path}`;
|
|
19
19
|
|
|
20
20
|
let timeout = null;
|
|
21
21
|
let lastY = 0;
|
package/src/utils/fetchPage.js
CHANGED
|
@@ -4,20 +4,21 @@ import { buildCmsHeaders } from "./headers.js";
|
|
|
4
4
|
import { HttpError } from "./errors.js";
|
|
5
5
|
|
|
6
6
|
const fetchPage = async (context) => {
|
|
7
|
-
let {
|
|
7
|
+
let { path = "" } = context.params;
|
|
8
|
+
|
|
8
9
|
const request = context.request;
|
|
9
10
|
const url = new URL(request.url);
|
|
10
11
|
const isCMSPreviewRoute = url.pathname.startsWith("/preview_");
|
|
11
12
|
|
|
12
13
|
if (isCMSPreviewRoute) {
|
|
13
|
-
|
|
14
|
+
path = path.replace(/^preview_/, "");
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
const editorToken = url.searchParams.get("editorToken");
|
|
17
18
|
const draftUuid = resolveDraftUuidFromToken(editorToken);
|
|
18
19
|
const isCMSEditRoute = editorToken && draftUuid;
|
|
19
20
|
|
|
20
|
-
const cmsUrl = buildCmsPageUrl({
|
|
21
|
+
const cmsUrl = buildCmsPageUrl({ draftUuid, path });
|
|
21
22
|
|
|
22
23
|
const response = await fetch(cmsUrl, {
|
|
23
24
|
method: "GET",
|
package/src/utils/notify.js
CHANGED
|
@@ -5,8 +5,8 @@ const EVENTS = {
|
|
|
5
5
|
BLOCK_POSITION: "BLOCK_POSITION",
|
|
6
6
|
BLOCK_SHOW: "BLOCK_SHOW",
|
|
7
7
|
BLOCK_HIDE: "BLOCK_HIDE",
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
PATH_CHANGED: "PATH_CHANGED",
|
|
9
|
+
PATH_ON_LOAD: "PATH_ON_LOAD",
|
|
10
10
|
IFRAME_DOM_LOADED: "IFRAME_DOM_LOADED",
|
|
11
11
|
CURRENT_BLOCK_EDIT: "CURRENT_BLOCK_EDIT",
|
|
12
12
|
SCROLL_TO_BLOCK: "SCROLL_TO_BLOCK",
|
|
@@ -21,15 +21,15 @@ import { EVENTS, notifyCMS } from "./notify.js";
|
|
|
21
21
|
event.preventDefault();
|
|
22
22
|
event.stopPropagation();
|
|
23
23
|
|
|
24
|
-
notifyCMS(EVENTS.
|
|
25
|
-
|
|
24
|
+
notifyCMS(EVENTS.PATH_CHANGED, {
|
|
25
|
+
path: url.pathname,
|
|
26
26
|
});
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
document.addEventListener("click", handleClick, true);
|
|
30
30
|
|
|
31
|
-
notifyCMS(EVENTS.
|
|
32
|
-
|
|
31
|
+
notifyCMS(EVENTS.PATH_ON_LOAD, {
|
|
32
|
+
path: window.location.pathname,
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
window.addEventListener("load", () => {
|
package/src/utils/urlResolver.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
const buildCmsPageUrl = ({
|
|
1
|
+
const buildCmsPageUrl = ({ draftUuid, path }) => {
|
|
2
2
|
const base = import.meta.env.CMS_URL;
|
|
3
3
|
|
|
4
4
|
if (draftUuid) {
|
|
5
5
|
return `${base}/api/public/pages/preview/${draftUuid}`;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
const
|
|
8
|
+
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
9
9
|
|
|
10
|
-
return `${base}/api/public/pages${
|
|
10
|
+
return `${base}/api/public/pages${normalizedPath}`;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
export { buildCmsPageUrl };
|