@grapesjs/studio-sdk-plugins 1.0.20 → 1.0.21-rc.1
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.
- package/dist/canvasAbsoluteMode/index.cjs.js +1 -1
- package/dist/canvasAbsoluteMode/index.es.js +173 -149
- package/dist/canvasAbsoluteMode/index.umd.js +1 -1
- package/dist/canvasAbsoluteMode/types.d.ts +7 -1
- package/dist/canvasAbsoluteMode/typesSchema.d.ts +22 -3
- package/dist/canvasAbsoluteMode/utils.d.ts +5 -0
- package/dist/googleFontsAssetProvider/index.cjs.js +1 -1
- package/dist/googleFontsAssetProvider/index.es.js +124 -109
- package/dist/googleFontsAssetProvider/index.umd.js +1 -1
- package/dist/googleFontsAssetProvider/utils.d.ts +3 -2
- package/dist/index.cjs.js +53 -53
- package/dist/index.es.js +3178 -3142
- package/dist/index.umd.js +55 -55
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const
|
|
1
|
+
"use strict";const V="app.grapesjs.com",k="app-stage.grapesjs.com",I=[V,k,"localhost","127.0.0.1",".local-credentialless.webcontainer.io",".local.webcontainer.io","-sandpack.codesandbox.io"],X="license:check:start",j="license:check:end",F=()=>typeof window<"u",q=({isDev:e,isStage:a})=>`${e?"":`https://${a?k:V}`}/api`,J=()=>{const e=F()&&window.location.hostname;return!!e&&(I.includes(e)||I.some(a=>e.endsWith(a)))};function z(e){return typeof e=="function"}async function Q({path:e,baseApiUrl:a,method:u="GET",headers:l={},params:d,body:n}){const s=`${a||q({isDev:!1,isStage:!1})}${e}`,m={method:u,headers:{"Content-Type":"application/json",...l}};n&&(m.body=JSON.stringify(n));const h=d?new URLSearchParams(d).toString():"",t=h?`?${h}`:"",o=await fetch(`${s}${t}`,m);if(!o.ok)throw new Error(`HTTP error! status: ${o.status}`);return o.json()}var P=(e=>(e.free="free",e.startup="startup",e.business="business",e.enterprise="enterprise",e))(P||{});const W={[P.free]:0,[P.startup]:10,[P.business]:20,[P.enterprise]:30};function Z(e){const a=e;return a.init=u=>l=>e(l,u),a}const ee=e=>Z(e);async function te({editor:e,plan:a,pluginName:u,licenseKey:l,cleanup:d}){let n="",c=!1;const s=J(),m=t=>{console.warn("Cleanup plugin:",u,"Reason:",t),d()},h=(t={})=>{var E;const{error:o,sdkLicense:f}=t,i=(E=t.plan)==null?void 0:E.category;if(!(f||t.license)||o)m(o||"Invalid license");else if(i){const b=W[a],v=W[i];b>v&&m({pluginRequiredPlan:a,licensePlan:i})}};e.on(X,t=>{n=t==null?void 0:t.baseApiUrl,c=!0}),e.on(j,t=>{h(t)}),setTimeout(async()=>{if(!c){if(s)return;if(l){const t=await ne({licenseKey:l,pluginName:u,baseApiUrl:n});t&&h(t)}else m("The `licenseKey` option not provided")}},2e3)}async function ne(e){const{licenseKey:a,pluginName:u,baseApiUrl:l}=e;try{return(await Q({baseApiUrl:l,path:`/sdk/${a||"na"}`,method:"POST",params:{d:window.location.hostname,pn:u}})).result||{}}catch(d){return console.error("Error during SDK license check:",d),!1}}const se="canvasAbsoluteMode",oe=P.startup,ce=(e,a,u)=>{const l=u.enableAbsolute??(m=>m.hasAbsolute),n=(Array.isArray(a)?a:a?[a]:[...e.getSelectedAll()])[0];if(!n)return;const c=n.getEl();return{result:z(l)?l({component:n,hasAbsolute:!!c&&getComputedStyle(c).position==="absolute"}):!1,component:n,dmode:n.get("dmode")}};function B({guideMatched:e,canvasView:a}){var L;const u=e.guide.x===void 0?"y":"x";let l=0,d=0,n=0,c=0;const{guide:s,matched:m}=e,h=a.getElBoxRect(s.componentView.el),t=a.getElBoxRect(((L=m.componentView)==null?void 0:L.el)||m.componentEl),o=h.y,f=h.y+h.height,i=h.x,x=h.x+h.width,E=t.y,b=t.y+t.height,v=t.x,_=t.x+t.width,S=Math.max(0,o-b),p=Math.max(0,E-f),w=Math.max(0,i-_),A=Math.max(0,v-x),r=S>p,g=w>A,y=s.type;return u==="x"&&(y==="l"?(l=i,r?(d=b,n=S):(d=f,n=p)):y==="r"?(l=x,r?(d=b,n=S):(d=f,n=p)):y==="x"&&(l=i-(i-x)/2,r?(d=b,n=S):(d=f,n=p))),u==="y"&&(y==="t"?(d=o,g?(l=_,c=w):(l=x,c=A)):y==="b"?(d=f,g?(l=_,c=w):(l=x,c=A)):y==="y"&&(d=o-(o-f)/2,g?(l=_,c=w):(l=x,c=A))),{axis:u,x:l,y:d,width:c,height:n}}function ie({guideMatched:e,component:a,canvasView:u}){const l=e.guide.x===void 0?"x":"y",{guide:d,matched:n}=e,c=d.type,s=n.type,m=u.getElementPos(d.componentView.el).rect,h=u.getElementPos(n.componentView.el).rect;if(l==="x"){const t=m.height,o=h.top,f=h.top+h.height;let i=0;c==="t"?s==="t"?i=o-t:s==="b"?i=f-t:s==="y"&&(i=o-t-(o-f)/2):c==="b"?s==="t"?i=o-t*2:s==="b"?i=f-t*2:s==="y"&&(i=o-t*2-(o-f)/2):c==="y"&&(s==="t"?i=o-t*1.5:s==="b"?i=f-t*1.5:s==="y"&&(i=o-t*1.5-(o-f)/2)),a.addStyle({top:`${i-5}px`},{partial:!0})}if(l==="y"){const t=m.width,o=h.left,f=h.left+h.width;let i=0;c==="l"?s==="l"?i=o:s==="r"?i=f:s==="x"&&(i=o-(o-f)/2):c==="r"?s==="l"?i=o-t:s==="r"?i=f-t:s==="x"&&(i=o-t-(o-f)/2):c==="x"&&(s==="l"?i=o-t/2:s==="r"&&(i=f-t/2)),a.addStyle({left:`${i}px`},{partial:!0})}}function H({snapping:e,editor:a,props:u}){var R,O;const{command:l,guidesMatched:d}=u,n=u.component,c=n.getEl(),s=a.Canvas.getCanvasView(),{x:m,y:h}=e??{x:10,y:10},t=l.opts.event,o=t.clientX,f=t.clientY;if(!n.__lastSnappedPosition){const T=s.getElBoxRect(c),M=o-T.x,D=f-T.y;n.__lastSnappedPosition={left:T.x,top:T.y},n.__dragOffset={x:M,y:D}}const i=((R=n.__dragOffset)==null?void 0:R.x)||0,x=((O=n.__dragOffset)==null?void 0:O.y)||0,E=n.__lastSnappedPosition,b=o-E.left-i,v=f-E.top-x;let _=E.left,S=E.top;const p=d==null?void 0:d[0],w=(p==null?void 0:p.guide.type)==="l",A=(p==null?void 0:p.guide.type)==="x",r=(p==null?void 0:p.guide.type)==="r",g=(p==null?void 0:p.guide.type)==="t",y=(p==null?void 0:p.guide.type)==="b",L=(p==null?void 0:p.guide.type)==="y";if(p){const{x:T,y:M}=B({guideMatched:p,canvasView:s}),D=c.offsetWidth,G=c.offsetHeight;w?_=T:r?_=T-D:A?_=T-D/2:g?S=M:y?S=M-G:L&&(S=M-G/2),n.__lastSnappedGuideMatched=p}else Math.abs(b)>=m&&(_=Math.round((E.left+b)/m)*m,n.__lastSnappedGuideMatched=null),Math.abs(v)>=h&&(S=Math.round((E.top+v)/h)*h,n.__lastSnappedGuideMatched=null);return n.addStyle({left:`${_}px`,top:`${S}px`},{partial:!0}),n.__lastSnappedPosition={left:_,top:S},{x:_,y:S,guideMatched:n.__lastSnappedGuideMatched}}function ae({snapping:e,props:a,editor:u}){const{x:l,y:d}=H({snapping:e,props:a,editor:u}),n=a.component;n.addStyle({left:`${l}px`,top:`${d}px`}),delete n.__lastSnappedPosition,delete n.__lastSnappedGuideMatched}const le="core:component-drag",de="studio:setDragAbsolute",fe="dmode:start",Y="dmode:move",K="dmode:end",$="dragAbsolute",re="select",pe="hover",ue="spacing",he=function(e,a={}){const{Commands:u}=e,l=u.events,d=u.getConfig().defaultOptions??{},n=`${l.runBeforeCommand}tlb-move`;d[le]={run:r=>({...r,skipGuidesRender:!0})};const{snapping:c,locking:s,globalAbsolute:m=!0}=a;m&&e.setDragMode("absolute");const h=!!(c!=null&&c.x||c!=null&&c.y),t=!!(s!=null&&s.x||s!=null&&s.y),{Canvas:o}=e;let f=null;const i=r=>{const{component:g,guidesMatched:y,command:L}=r,R=g.view,O=e.Canvas.getCanvasView(),T=g.getEl();let M=y.map(C=>C.matched.componentEl),D=y.map(C=>B({guideMatched:C,canvasView:O}));if(h){const{guideMatched:C}=H({snapping:c,props:r,editor:e});C?(M=[C.matched.componentEl],D=[B({guideMatched:C,canvasView:O})]):(M=[],D=[])}if(t)if(L.opts.event.shiftKey)if(!f)f=y[0];else{const N=f,U=N.guide.x===void 0?"x":"y";(U==="x"&&(s==null?void 0:s.x)||U==="y"&&(s==null?void 0:s.y))&&(ie({guideMatched:N,component:g,canvasView:O}),M=[N.matched.componentEl],D=[B({guideMatched:N,canvasView:O})])}else f=null;const G={id:$,type:$,component:g,componentView:R,originComponent:T,matchedComponents:M,originMatchedDistances:D};o.addSpot(G)},x=({enabled:r})=>{e.runCommand(de,{enabled:r})},E=({type:r})=>{o.removeSpots({type:r})},b=()=>{E({type:re}),E({type:pe}),E({type:ue})},v=r=>{x({enabled:!0}),i(r),b()},_=r=>{i(r),b()},S=r=>{E({type:$}),h&&ae({snapping:c,props:r,editor:e}),x({enabled:!1})},A=[[fe,r=>{v(r),e.on(Y,_);const g=()=>{S(r),e.off(Y,_),e.off(K,g)};e.on(K,g)}],[n,r=>{if(m)return;const g=r.options.target,y=ce(e,g,a);y!=null&&y.result&&!y.dmode&&y.component.set({dmode:"absolute"})}]];A.forEach(([r,g])=>e.on(r,g)),te({editor:e,licenseKey:a.licenseKey,plan:oe,pluginName:se,cleanup:()=>{x({enabled:!1}),A.forEach(([r,g])=>e.off(r,g))}})},me=ee(he);module.exports=me;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const V = "app.grapesjs.com", k = "app-stage.grapesjs.com", I = [
|
|
2
|
+
V,
|
|
3
|
+
k,
|
|
4
4
|
"localhost",
|
|
5
5
|
"127.0.0.1",
|
|
6
6
|
".local-credentialless.webcontainer.io",
|
|
@@ -9,214 +9,238 @@ const W = "app.grapesjs.com", Y = "app-stage.grapesjs.com", B = [
|
|
|
9
9
|
// For stackblitz.com demos
|
|
10
10
|
"-sandpack.codesandbox.io"
|
|
11
11
|
// For Sandpack demos
|
|
12
|
-
],
|
|
13
|
-
const e =
|
|
14
|
-
return !!e && (
|
|
12
|
+
], X = "license:check:start", j = "license:check:end", F = () => typeof window < "u", q = ({ isDev: e, isStage: a }) => `${e ? "" : `https://${a ? k : V}`}/api`, J = () => {
|
|
13
|
+
const e = F() && window.location.hostname;
|
|
14
|
+
return !!e && (I.includes(e) || I.some((a) => e.endsWith(a)));
|
|
15
15
|
};
|
|
16
|
-
|
|
16
|
+
function z(e) {
|
|
17
|
+
return typeof e == "function";
|
|
18
|
+
}
|
|
19
|
+
async function Q({
|
|
17
20
|
path: e,
|
|
18
|
-
baseApiUrl:
|
|
21
|
+
baseApiUrl: a,
|
|
19
22
|
method: u = "GET",
|
|
20
|
-
headers:
|
|
21
|
-
params:
|
|
22
|
-
body:
|
|
23
|
+
headers: l = {},
|
|
24
|
+
params: d,
|
|
25
|
+
body: n
|
|
23
26
|
}) {
|
|
24
|
-
const
|
|
27
|
+
const o = `${a || q({ isDev: !1, isStage: !1 })}${e}`, m = {
|
|
25
28
|
method: u,
|
|
26
29
|
headers: {
|
|
27
30
|
"Content-Type": "application/json",
|
|
28
|
-
...
|
|
31
|
+
...l
|
|
29
32
|
}
|
|
30
33
|
};
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
if (!
|
|
34
|
-
throw new Error(`HTTP error! status: ${
|
|
35
|
-
return
|
|
34
|
+
n && (m.body = JSON.stringify(n));
|
|
35
|
+
const h = d ? new URLSearchParams(d).toString() : "", t = h ? `?${h}` : "", s = await fetch(`${o}${t}`, m);
|
|
36
|
+
if (!s.ok)
|
|
37
|
+
throw new Error(`HTTP error! status: ${s.status}`);
|
|
38
|
+
return s.json();
|
|
36
39
|
}
|
|
37
|
-
var
|
|
38
|
-
const
|
|
39
|
-
[
|
|
40
|
-
[
|
|
41
|
-
[
|
|
42
|
-
[
|
|
40
|
+
var P = /* @__PURE__ */ ((e) => (e.free = "free", e.startup = "startup", e.business = "business", e.enterprise = "enterprise", e))(P || {});
|
|
41
|
+
const W = {
|
|
42
|
+
[P.free]: 0,
|
|
43
|
+
[P.startup]: 10,
|
|
44
|
+
[P.business]: 20,
|
|
45
|
+
[P.enterprise]: 30
|
|
43
46
|
};
|
|
44
|
-
function
|
|
45
|
-
const
|
|
46
|
-
return
|
|
47
|
+
function Z(e) {
|
|
48
|
+
const a = e;
|
|
49
|
+
return a.init = (u) => (l) => e(l, u), a;
|
|
47
50
|
}
|
|
48
|
-
const
|
|
49
|
-
async function
|
|
51
|
+
const ee = (e) => /* @__PURE__ */ Z(e);
|
|
52
|
+
async function te({
|
|
50
53
|
editor: e,
|
|
51
|
-
plan:
|
|
54
|
+
plan: a,
|
|
52
55
|
pluginName: u,
|
|
53
|
-
licenseKey:
|
|
54
|
-
cleanup:
|
|
56
|
+
licenseKey: l,
|
|
57
|
+
cleanup: d
|
|
55
58
|
}) {
|
|
56
|
-
let
|
|
57
|
-
const
|
|
58
|
-
console.warn("Cleanup plugin:", u, "Reason:", t),
|
|
59
|
-
},
|
|
60
|
-
var
|
|
61
|
-
const { error:
|
|
62
|
-
if (!(f || t.license) ||
|
|
63
|
-
m(
|
|
64
|
-
else if (
|
|
65
|
-
const
|
|
66
|
-
|
|
59
|
+
let n = "", c = !1;
|
|
60
|
+
const o = J(), m = (t) => {
|
|
61
|
+
console.warn("Cleanup plugin:", u, "Reason:", t), d();
|
|
62
|
+
}, h = (t = {}) => {
|
|
63
|
+
var E;
|
|
64
|
+
const { error: s, sdkLicense: f } = t, i = (E = t.plan) == null ? void 0 : E.category;
|
|
65
|
+
if (!(f || t.license) || s)
|
|
66
|
+
m(s || "Invalid license");
|
|
67
|
+
else if (i) {
|
|
68
|
+
const b = W[a], v = W[i];
|
|
69
|
+
b > v && m({ pluginRequiredPlan: a, licensePlan: i });
|
|
67
70
|
}
|
|
68
71
|
};
|
|
69
|
-
e.on(
|
|
70
|
-
|
|
71
|
-
}), e.on(
|
|
72
|
-
|
|
72
|
+
e.on(X, (t) => {
|
|
73
|
+
n = t == null ? void 0 : t.baseApiUrl, c = !0;
|
|
74
|
+
}), e.on(j, (t) => {
|
|
75
|
+
h(t);
|
|
73
76
|
}), setTimeout(async () => {
|
|
74
|
-
if (!
|
|
75
|
-
if (
|
|
76
|
-
if (
|
|
77
|
-
const t = await
|
|
78
|
-
t &&
|
|
77
|
+
if (!c) {
|
|
78
|
+
if (o) return;
|
|
79
|
+
if (l) {
|
|
80
|
+
const t = await ne({ licenseKey: l, pluginName: u, baseApiUrl: n });
|
|
81
|
+
t && h(t);
|
|
79
82
|
} else
|
|
80
83
|
m("The `licenseKey` option not provided");
|
|
81
84
|
}
|
|
82
85
|
}, 2e3);
|
|
83
86
|
}
|
|
84
|
-
async function
|
|
85
|
-
const { licenseKey:
|
|
87
|
+
async function ne(e) {
|
|
88
|
+
const { licenseKey: a, pluginName: u, baseApiUrl: l } = e;
|
|
86
89
|
try {
|
|
87
|
-
return (await
|
|
88
|
-
baseApiUrl:
|
|
89
|
-
path: `/sdk/${
|
|
90
|
+
return (await Q({
|
|
91
|
+
baseApiUrl: l,
|
|
92
|
+
path: `/sdk/${a || "na"}`,
|
|
90
93
|
method: "POST",
|
|
91
94
|
params: {
|
|
92
95
|
d: window.location.hostname,
|
|
93
96
|
pn: u
|
|
94
97
|
}
|
|
95
98
|
})).result || {};
|
|
96
|
-
} catch (
|
|
97
|
-
return console.error("Error during SDK license check:",
|
|
99
|
+
} catch (d) {
|
|
100
|
+
return console.error("Error during SDK license check:", d), !1;
|
|
98
101
|
}
|
|
99
102
|
}
|
|
100
|
-
const
|
|
101
|
-
|
|
103
|
+
const oe = "canvasAbsoluteMode", se = P.startup, ce = (e, a, u) => {
|
|
104
|
+
const l = u.enableAbsolute ?? ((m) => m.hasAbsolute), n = (Array.isArray(a) ? a : a ? [a] : [...e.getSelectedAll()])[0];
|
|
105
|
+
if (!n) return;
|
|
106
|
+
const c = n.getEl();
|
|
107
|
+
return {
|
|
108
|
+
result: z(l) ? l({ component: n, hasAbsolute: !!c && getComputedStyle(c).position === "absolute" }) : !1,
|
|
109
|
+
component: n,
|
|
110
|
+
dmode: n.get("dmode")
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
function B({
|
|
102
114
|
guideMatched: e,
|
|
103
|
-
canvasView:
|
|
115
|
+
canvasView: a
|
|
104
116
|
}) {
|
|
117
|
+
var L;
|
|
105
118
|
const u = e.guide.x === void 0 ? "y" : "x";
|
|
106
|
-
let
|
|
107
|
-
const { guide:
|
|
108
|
-
return u === "x" && (
|
|
119
|
+
let l = 0, d = 0, n = 0, c = 0;
|
|
120
|
+
const { guide: o, matched: m } = e, h = a.getElBoxRect(o.componentView.el), t = a.getElBoxRect(((L = m.componentView) == null ? void 0 : L.el) || m.componentEl), s = h.y, f = h.y + h.height, i = h.x, x = h.x + h.width, E = t.y, b = t.y + t.height, v = t.x, _ = t.x + t.width, S = Math.max(0, s - b), p = Math.max(0, E - f), w = Math.max(0, i - _), A = Math.max(0, v - x), r = S > p, g = w > A, y = o.type;
|
|
121
|
+
return u === "x" && (y === "l" ? (l = i, r ? (d = b, n = S) : (d = f, n = p)) : y === "r" ? (l = x, r ? (d = b, n = S) : (d = f, n = p)) : y === "x" && (l = i - (i - x) / 2, r ? (d = b, n = S) : (d = f, n = p))), u === "y" && (y === "t" ? (d = s, g ? (l = _, c = w) : (l = x, c = A)) : y === "b" ? (d = f, g ? (l = _, c = w) : (l = x, c = A)) : y === "y" && (d = s - (s - f) / 2, g ? (l = _, c = w) : (l = x, c = A))), { axis: u, x: l, y: d, width: c, height: n };
|
|
109
122
|
}
|
|
110
|
-
function
|
|
123
|
+
function ie({
|
|
111
124
|
guideMatched: e,
|
|
112
|
-
component:
|
|
125
|
+
component: a,
|
|
113
126
|
canvasView: u
|
|
114
127
|
}) {
|
|
115
|
-
const
|
|
116
|
-
if (
|
|
117
|
-
const t = m.height,
|
|
118
|
-
let
|
|
119
|
-
|
|
128
|
+
const l = e.guide.x === void 0 ? "x" : "y", { guide: d, matched: n } = e, c = d.type, o = n.type, m = u.getElementPos(d.componentView.el).rect, h = u.getElementPos(n.componentView.el).rect;
|
|
129
|
+
if (l === "x") {
|
|
130
|
+
const t = m.height, s = h.top, f = h.top + h.height;
|
|
131
|
+
let i = 0;
|
|
132
|
+
c === "t" ? o === "t" ? i = s - t : o === "b" ? i = f - t : o === "y" && (i = s - t - (s - f) / 2) : c === "b" ? o === "t" ? i = s - t * 2 : o === "b" ? i = f - t * 2 : o === "y" && (i = s - t * 2 - (s - f) / 2) : c === "y" && (o === "t" ? i = s - t * 1.5 : o === "b" ? i = f - t * 1.5 : o === "y" && (i = s - t * 1.5 - (s - f) / 2)), a.addStyle({ top: `${i - 5}px` }, { partial: !0 });
|
|
120
133
|
}
|
|
121
|
-
if (
|
|
122
|
-
const t = m.width,
|
|
123
|
-
let
|
|
124
|
-
|
|
134
|
+
if (l === "y") {
|
|
135
|
+
const t = m.width, s = h.left, f = h.left + h.width;
|
|
136
|
+
let i = 0;
|
|
137
|
+
c === "l" ? o === "l" ? i = s : o === "r" ? i = f : o === "x" && (i = s - (s - f) / 2) : c === "r" ? o === "l" ? i = s - t : o === "r" ? i = f - t : o === "x" && (i = s - t - (s - f) / 2) : c === "x" && (o === "l" ? i = s - t / 2 : o === "r" && (i = f - t / 2)), a.addStyle({ left: `${i}px` }, { partial: !0 });
|
|
125
138
|
}
|
|
126
139
|
}
|
|
127
|
-
function
|
|
140
|
+
function H({
|
|
128
141
|
snapping: e,
|
|
129
|
-
editor:
|
|
142
|
+
editor: a,
|
|
130
143
|
props: u
|
|
131
144
|
}) {
|
|
132
|
-
var
|
|
133
|
-
const { command:
|
|
134
|
-
if (!
|
|
135
|
-
const
|
|
136
|
-
|
|
145
|
+
var R, O;
|
|
146
|
+
const { command: l, guidesMatched: d } = u, n = u.component, c = n.getEl(), o = a.Canvas.getCanvasView(), { x: m, y: h } = e ?? { x: 10, y: 10 }, t = l.opts.event, s = t.clientX, f = t.clientY;
|
|
147
|
+
if (!n.__lastSnappedPosition) {
|
|
148
|
+
const T = o.getElBoxRect(c), M = s - T.x, D = f - T.y;
|
|
149
|
+
n.__lastSnappedPosition = { left: T.x, top: T.y }, n.__dragOffset = { x: M, y: D };
|
|
137
150
|
}
|
|
138
|
-
const
|
|
139
|
-
let
|
|
140
|
-
const
|
|
141
|
-
if (
|
|
142
|
-
const { x:
|
|
143
|
-
|
|
151
|
+
const i = ((R = n.__dragOffset) == null ? void 0 : R.x) || 0, x = ((O = n.__dragOffset) == null ? void 0 : O.y) || 0, E = n.__lastSnappedPosition, b = s - E.left - i, v = f - E.top - x;
|
|
152
|
+
let _ = E.left, S = E.top;
|
|
153
|
+
const p = d == null ? void 0 : d[0], w = (p == null ? void 0 : p.guide.type) === "l", A = (p == null ? void 0 : p.guide.type) === "x", r = (p == null ? void 0 : p.guide.type) === "r", g = (p == null ? void 0 : p.guide.type) === "t", y = (p == null ? void 0 : p.guide.type) === "b", L = (p == null ? void 0 : p.guide.type) === "y";
|
|
154
|
+
if (p) {
|
|
155
|
+
const { x: T, y: M } = B({ guideMatched: p, canvasView: o }), D = c.offsetWidth, G = c.offsetHeight;
|
|
156
|
+
w ? _ = T : r ? _ = T - D : A ? _ = T - D / 2 : g ? S = M : y ? S = M - G : L && (S = M - G / 2), n.__lastSnappedGuideMatched = p;
|
|
144
157
|
} else
|
|
145
|
-
Math.abs(
|
|
146
|
-
return
|
|
158
|
+
Math.abs(b) >= m && (_ = Math.round((E.left + b) / m) * m, n.__lastSnappedGuideMatched = null), Math.abs(v) >= h && (S = Math.round((E.top + v) / h) * h, n.__lastSnappedGuideMatched = null);
|
|
159
|
+
return n.addStyle({ left: `${_}px`, top: `${S}px` }, { partial: !0 }), n.__lastSnappedPosition = { left: _, top: S }, { x: _, y: S, guideMatched: n.__lastSnappedGuideMatched };
|
|
147
160
|
}
|
|
148
|
-
function
|
|
161
|
+
function ae({
|
|
149
162
|
snapping: e,
|
|
150
|
-
props:
|
|
163
|
+
props: a,
|
|
151
164
|
editor: u
|
|
152
165
|
}) {
|
|
153
|
-
const { x:
|
|
154
|
-
|
|
166
|
+
const { x: l, y: d } = H({ snapping: e, props: a, editor: u }), n = a.component;
|
|
167
|
+
n.addStyle({ left: `${l}px`, top: `${d}px` }), delete n.__lastSnappedPosition, delete n.__lastSnappedGuideMatched;
|
|
155
168
|
}
|
|
156
|
-
const
|
|
157
|
-
const u = e.
|
|
158
|
-
|
|
159
|
-
run: (
|
|
160
|
-
...
|
|
169
|
+
const le = "core:component-drag", de = "studio:setDragAbsolute", fe = "dmode:start", Y = "dmode:move", K = "dmode:end", $ = "dragAbsolute", re = "select", pe = "hover", ue = "spacing", he = function(e, a = {}) {
|
|
170
|
+
const { Commands: u } = e, l = u.events, d = u.getConfig().defaultOptions ?? {}, n = `${l.runBeforeCommand}tlb-move`;
|
|
171
|
+
d[le] = {
|
|
172
|
+
run: (r) => ({
|
|
173
|
+
...r,
|
|
161
174
|
skipGuidesRender: !0
|
|
162
175
|
})
|
|
163
176
|
};
|
|
164
|
-
const { snapping:
|
|
165
|
-
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
177
|
+
const { snapping: c, locking: o, globalAbsolute: m = !0 } = a;
|
|
178
|
+
m && e.setDragMode("absolute");
|
|
179
|
+
const h = !!(c != null && c.x || c != null && c.y), t = !!(o != null && o.x || o != null && o.y), { Canvas: s } = e;
|
|
180
|
+
let f = null;
|
|
181
|
+
const i = (r) => {
|
|
182
|
+
const { component: g, guidesMatched: y, command: L } = r, R = g.view, O = e.Canvas.getCanvasView(), T = g.getEl();
|
|
183
|
+
let M = y.map((C) => C.matched.componentEl), D = y.map((C) => B({ guideMatched: C, canvasView: O }));
|
|
184
|
+
if (h) {
|
|
185
|
+
const { guideMatched: C } = H({ snapping: c, props: r, editor: e });
|
|
186
|
+
C ? (M = [C.matched.componentEl], D = [B({ guideMatched: C, canvasView: O })]) : (M = [], D = []);
|
|
172
187
|
}
|
|
173
|
-
if (
|
|
174
|
-
if (
|
|
175
|
-
if (!
|
|
176
|
-
|
|
188
|
+
if (t)
|
|
189
|
+
if (L.opts.event.shiftKey)
|
|
190
|
+
if (!f)
|
|
191
|
+
f = y[0];
|
|
177
192
|
else {
|
|
178
|
-
const
|
|
179
|
-
(
|
|
193
|
+
const N = f, U = N.guide.x === void 0 ? "x" : "y";
|
|
194
|
+
(U === "x" && (o == null ? void 0 : o.x) || U === "y" && (o == null ? void 0 : o.y)) && (ie({ guideMatched: N, component: g, canvasView: O }), M = [N.matched.componentEl], D = [B({ guideMatched: N, canvasView: O })]);
|
|
180
195
|
}
|
|
181
196
|
else
|
|
182
|
-
|
|
183
|
-
const
|
|
184
|
-
id:
|
|
185
|
-
type:
|
|
197
|
+
f = null;
|
|
198
|
+
const G = {
|
|
199
|
+
id: $,
|
|
200
|
+
type: $,
|
|
186
201
|
component: g,
|
|
187
|
-
componentView:
|
|
188
|
-
originComponent:
|
|
189
|
-
matchedComponents:
|
|
190
|
-
originMatchedDistances:
|
|
202
|
+
componentView: R,
|
|
203
|
+
originComponent: T,
|
|
204
|
+
matchedComponents: M,
|
|
205
|
+
originMatchedDistances: D
|
|
191
206
|
};
|
|
192
|
-
|
|
193
|
-
},
|
|
194
|
-
e.runCommand(
|
|
195
|
-
},
|
|
196
|
-
|
|
197
|
-
},
|
|
198
|
-
|
|
199
|
-
},
|
|
200
|
-
|
|
201
|
-
}, _ = (
|
|
202
|
-
r
|
|
203
|
-
},
|
|
204
|
-
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
207
|
+
s.addSpot(G);
|
|
208
|
+
}, x = ({ enabled: r }) => {
|
|
209
|
+
e.runCommand(de, { enabled: r });
|
|
210
|
+
}, E = ({ type: r }) => {
|
|
211
|
+
s.removeSpots({ type: r });
|
|
212
|
+
}, b = () => {
|
|
213
|
+
E({ type: re }), E({ type: pe }), E({ type: ue });
|
|
214
|
+
}, v = (r) => {
|
|
215
|
+
x({ enabled: !0 }), i(r), b();
|
|
216
|
+
}, _ = (r) => {
|
|
217
|
+
i(r), b();
|
|
218
|
+
}, S = (r) => {
|
|
219
|
+
E({ type: $ }), h && ae({ snapping: c, props: r, editor: e }), x({ enabled: !1 });
|
|
220
|
+
}, A = [
|
|
221
|
+
[fe, (r) => {
|
|
222
|
+
v(r), e.on(Y, _);
|
|
223
|
+
const g = () => {
|
|
224
|
+
S(r), e.off(Y, _), e.off(K, g);
|
|
225
|
+
};
|
|
226
|
+
e.on(K, g);
|
|
227
|
+
}],
|
|
228
|
+
[n, (r) => {
|
|
229
|
+
if (m) return;
|
|
230
|
+
const g = r.options.target, y = ce(e, g, a);
|
|
231
|
+
y != null && y.result && !y.dmode && y.component.set({ dmode: "absolute" });
|
|
232
|
+
}]
|
|
233
|
+
];
|
|
234
|
+
A.forEach(([r, g]) => e.on(r, g)), te({
|
|
213
235
|
editor: e,
|
|
214
|
-
licenseKey:
|
|
215
|
-
plan:
|
|
216
|
-
pluginName:
|
|
217
|
-
cleanup: () =>
|
|
236
|
+
licenseKey: a.licenseKey,
|
|
237
|
+
plan: se,
|
|
238
|
+
pluginName: oe,
|
|
239
|
+
cleanup: () => {
|
|
240
|
+
x({ enabled: !1 }), A.forEach(([r, g]) => e.off(r, g));
|
|
241
|
+
}
|
|
218
242
|
});
|
|
219
|
-
},
|
|
243
|
+
}, ge = ee(he);
|
|
220
244
|
export {
|
|
221
|
-
|
|
245
|
+
ge as default
|
|
222
246
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(P,
|
|
1
|
+
(function(P,L){typeof exports=="object"&&typeof module<"u"?module.exports=L():typeof define=="function"&&define.amd?define(L):(P=typeof globalThis<"u"?globalThis:P||self,P.StudioSdkPlugins_canvasAbsoluteMode=L())})(this,function(){"use strict";const P="app.grapesjs.com",L="app-stage.grapesjs.com",W=[P,L,"localhost","127.0.0.1",".local-credentialless.webcontainer.io",".local.webcontainer.io","-sandpack.codesandbox.io"],X="license:check:start",j="license:check:end",F=()=>typeof window<"u",q=({isDev:e,isStage:l})=>`${e?"":`https://${l?L:P}`}/api`,J=()=>{const e=F()&&window.location.hostname;return!!e&&(W.includes(e)||W.some(l=>e.endsWith(l)))};function z(e){return typeof e=="function"}async function Q({path:e,baseApiUrl:l,method:u="GET",headers:a={},params:d,body:n}){const o=`${l||q({isDev:!1,isStage:!1})}${e}`,m={method:u,headers:{"Content-Type":"application/json",...a}};n&&(m.body=JSON.stringify(n));const h=d?new URLSearchParams(d).toString():"",t=h?`?${h}`:"",s=await fetch(`${o}${t}`,m);if(!s.ok)throw new Error(`HTTP error! status: ${s.status}`);return s.json()}var R=(e=>(e.free="free",e.startup="startup",e.business="business",e.enterprise="enterprise",e))(R||{});const Y={[R.free]:0,[R.startup]:10,[R.business]:20,[R.enterprise]:30};function Z(e){const l=e;return l.init=u=>a=>e(a,u),l}const ee=e=>Z(e);async function te({editor:e,plan:l,pluginName:u,licenseKey:a,cleanup:d}){let n="",i=!1;const o=J(),m=t=>{console.warn("Cleanup plugin:",u,"Reason:",t),d()},h=(t={})=>{var E;const{error:s,sdkLicense:f}=t,c=(E=t.plan)==null?void 0:E.category;if(!(f||t.license)||s)m(s||"Invalid license");else if(c){const b=Y[l],v=Y[c];b>v&&m({pluginRequiredPlan:l,licensePlan:c})}};e.on(X,t=>{n=t==null?void 0:t.baseApiUrl,i=!0}),e.on(j,t=>{h(t)}),setTimeout(async()=>{if(!i){if(o)return;if(a){const t=await ne({licenseKey:a,pluginName:u,baseApiUrl:n});t&&h(t)}else m("The `licenseKey` option not provided")}},2e3)}async function ne(e){const{licenseKey:l,pluginName:u,baseApiUrl:a}=e;try{return(await Q({baseApiUrl:a,path:`/sdk/${l||"na"}`,method:"POST",params:{d:window.location.hostname,pn:u}})).result||{}}catch(d){return console.error("Error during SDK license check:",d),!1}}const oe="canvasAbsoluteMode",se=R.startup,ie=(e,l,u)=>{const a=u.enableAbsolute??(m=>m.hasAbsolute),n=(Array.isArray(l)?l:l?[l]:[...e.getSelectedAll()])[0];if(!n)return;const i=n.getEl();return{result:z(a)?a({component:n,hasAbsolute:!!i&&getComputedStyle(i).position==="absolute"}):!1,component:n,dmode:n.get("dmode")}};function N({guideMatched:e,canvasView:l}){var G;const u=e.guide.x===void 0?"y":"x";let a=0,d=0,n=0,i=0;const{guide:o,matched:m}=e,h=l.getElBoxRect(o.componentView.el),t=l.getElBoxRect(((G=m.componentView)==null?void 0:G.el)||m.componentEl),s=h.y,f=h.y+h.height,c=h.x,S=h.x+h.width,E=t.y,b=t.y+t.height,v=t.x,x=t.x+t.width,_=Math.max(0,s-b),p=Math.max(0,E-f),w=Math.max(0,c-x),A=Math.max(0,v-S),r=_>p,g=w>A,y=o.type;return u==="x"&&(y==="l"?(a=c,r?(d=b,n=_):(d=f,n=p)):y==="r"?(a=S,r?(d=b,n=_):(d=f,n=p)):y==="x"&&(a=c-(c-S)/2,r?(d=b,n=_):(d=f,n=p))),u==="y"&&(y==="t"?(d=s,g?(a=x,i=w):(a=S,i=A)):y==="b"?(d=f,g?(a=x,i=w):(a=S,i=A)):y==="y"&&(d=s-(s-f)/2,g?(a=x,i=w):(a=S,i=A))),{axis:u,x:a,y:d,width:i,height:n}}function ce({guideMatched:e,component:l,canvasView:u}){const a=e.guide.x===void 0?"x":"y",{guide:d,matched:n}=e,i=d.type,o=n.type,m=u.getElementPos(d.componentView.el).rect,h=u.getElementPos(n.componentView.el).rect;if(a==="x"){const t=m.height,s=h.top,f=h.top+h.height;let c=0;i==="t"?o==="t"?c=s-t:o==="b"?c=f-t:o==="y"&&(c=s-t-(s-f)/2):i==="b"?o==="t"?c=s-t*2:o==="b"?c=f-t*2:o==="y"&&(c=s-t*2-(s-f)/2):i==="y"&&(o==="t"?c=s-t*1.5:o==="b"?c=f-t*1.5:o==="y"&&(c=s-t*1.5-(s-f)/2)),l.addStyle({top:`${c-5}px`},{partial:!0})}if(a==="y"){const t=m.width,s=h.left,f=h.left+h.width;let c=0;i==="l"?o==="l"?c=s:o==="r"?c=f:o==="x"&&(c=s-(s-f)/2):i==="r"?o==="l"?c=s-t:o==="r"?c=f-t:o==="x"&&(c=s-t-(s-f)/2):i==="x"&&(o==="l"?c=s-t/2:o==="r"&&(c=f-t/2)),l.addStyle({left:`${c}px`},{partial:!0})}}function k({snapping:e,editor:l,props:u}){var B,O;const{command:a,guidesMatched:d}=u,n=u.component,i=n.getEl(),o=l.Canvas.getCanvasView(),{x:m,y:h}=e??{x:10,y:10},t=a.opts.event,s=t.clientX,f=t.clientY;if(!n.__lastSnappedPosition){const T=o.getElBoxRect(i),M=s-T.x,D=f-T.y;n.__lastSnappedPosition={left:T.x,top:T.y},n.__dragOffset={x:M,y:D}}const c=((B=n.__dragOffset)==null?void 0:B.x)||0,S=((O=n.__dragOffset)==null?void 0:O.y)||0,E=n.__lastSnappedPosition,b=s-E.left-c,v=f-E.top-S;let x=E.left,_=E.top;const p=d==null?void 0:d[0],w=(p==null?void 0:p.guide.type)==="l",A=(p==null?void 0:p.guide.type)==="x",r=(p==null?void 0:p.guide.type)==="r",g=(p==null?void 0:p.guide.type)==="t",y=(p==null?void 0:p.guide.type)==="b",G=(p==null?void 0:p.guide.type)==="y";if(p){const{x:T,y:M}=N({guideMatched:p,canvasView:o}),D=i.offsetWidth,$=i.offsetHeight;w?x=T:r?x=T-D:A?x=T-D/2:g?_=M:y?_=M-$:G&&(_=M-$/2),n.__lastSnappedGuideMatched=p}else Math.abs(b)>=m&&(x=Math.round((E.left+b)/m)*m,n.__lastSnappedGuideMatched=null),Math.abs(v)>=h&&(_=Math.round((E.top+v)/h)*h,n.__lastSnappedGuideMatched=null);return n.addStyle({left:`${x}px`,top:`${_}px`},{partial:!0}),n.__lastSnappedPosition={left:x,top:_},{x,y:_,guideMatched:n.__lastSnappedGuideMatched}}function le({snapping:e,props:l,editor:u}){const{x:a,y:d}=k({snapping:e,props:l,editor:u}),n=l.component;n.addStyle({left:`${a}px`,top:`${d}px`}),delete n.__lastSnappedPosition,delete n.__lastSnappedGuideMatched}const ae="core:component-drag",de="studio:setDragAbsolute",fe="dmode:start",K="dmode:move",V="dmode:end",I="dragAbsolute",re="select",pe="hover",ue="spacing";return ee(function(e,l={}){const{Commands:u}=e,a=u.events,d=u.getConfig().defaultOptions??{},n=`${a.runBeforeCommand}tlb-move`;d[ae]={run:r=>({...r,skipGuidesRender:!0})};const{snapping:i,locking:o,globalAbsolute:m=!0}=l;m&&e.setDragMode("absolute");const h=!!(i!=null&&i.x||i!=null&&i.y),t=!!(o!=null&&o.x||o!=null&&o.y),{Canvas:s}=e;let f=null;const c=r=>{const{component:g,guidesMatched:y,command:G}=r,B=g.view,O=e.Canvas.getCanvasView(),T=g.getEl();let M=y.map(C=>C.matched.componentEl),D=y.map(C=>N({guideMatched:C,canvasView:O}));if(h){const{guideMatched:C}=k({snapping:i,props:r,editor:e});C?(M=[C.matched.componentEl],D=[N({guideMatched:C,canvasView:O})]):(M=[],D=[])}if(t)if(G.opts.event.shiftKey)if(!f)f=y[0];else{const U=f,H=U.guide.x===void 0?"x":"y";(H==="x"&&(o==null?void 0:o.x)||H==="y"&&(o==null?void 0:o.y))&&(ce({guideMatched:U,component:g,canvasView:O}),M=[U.matched.componentEl],D=[N({guideMatched:U,canvasView:O})])}else f=null;const $={id:I,type:I,component:g,componentView:B,originComponent:T,matchedComponents:M,originMatchedDistances:D};s.addSpot($)},S=({enabled:r})=>{e.runCommand(de,{enabled:r})},E=({type:r})=>{s.removeSpots({type:r})},b=()=>{E({type:re}),E({type:pe}),E({type:ue})},v=r=>{S({enabled:!0}),c(r),b()},x=r=>{c(r),b()},_=r=>{E({type:I}),h&&le({snapping:i,props:r,editor:e}),S({enabled:!1})},A=[[fe,r=>{v(r),e.on(K,x);const g=()=>{_(r),e.off(K,x),e.off(V,g)};e.on(V,g)}],[n,r=>{if(m)return;const g=r.options.target,y=ie(e,g,l);y!=null&&y.result&&!y.dmode&&y.component.set({dmode:"absolute"})}]];A.forEach(([r,g])=>e.on(r,g)),te({editor:e,licenseKey:l.licenseKey,plan:se,pluginName:oe,cleanup:()=>{S({enabled:!1}),A.forEach(([r,g])=>e.off(r,g))}})})});
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import { Component } from 'grapesjs';
|
|
1
2
|
import { CanvasAbsoluteModeOptions as CanvasAbsoluteModeOptionsSchema } from './typesSchema';
|
|
2
|
-
export interface CanvasAbsoluteModeOptions extends CanvasAbsoluteModeOptionsSchema {
|
|
3
|
+
export interface CanvasAbsoluteModeOptions extends Omit<CanvasAbsoluteModeOptionsSchema, 'enableAbsolute'> {
|
|
4
|
+
enableAbsolute?: EnableAbsoluteFn;
|
|
3
5
|
}
|
|
6
|
+
export type EnableAbsoluteFn = (props: {
|
|
7
|
+
component: Component;
|
|
8
|
+
hasAbsolute: boolean;
|
|
9
|
+
}) => boolean;
|
|
@@ -1,16 +1,35 @@
|
|
|
1
1
|
import { SDKPluginOptions } from '../utils';
|
|
2
2
|
export interface CanvasAbsoluteModeOptions extends SDKPluginOptions {
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Enable absolute mode globally. This will activate absolute drag mode for all components.
|
|
5
|
+
* @default true
|
|
6
|
+
*/
|
|
7
|
+
globalAbsolute?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Custom function to determine whether an element should use absolute positioning mode.
|
|
10
|
+
* This allows to define your own logic for enabling absolute mode, such as checking if
|
|
11
|
+
* the element has `position: absolute` or matches a specific condition.
|
|
12
|
+
*
|
|
13
|
+
* This check is skipped if the `globalAbsolute` option is set to `true`.
|
|
14
|
+
* @examples
|
|
15
|
+
* enableAbsolute: ({ component }) => getComputedStyle(component.getEl()).position === 'absolute'
|
|
16
|
+
*/
|
|
17
|
+
enableAbsolute?: '__fn__';
|
|
18
|
+
/**
|
|
19
|
+
* Enables grid-based snapping for absolute positioning.
|
|
20
|
+
* Defines the snapping intervals in pixels along the x and y axes. When set, dragged elements
|
|
21
|
+
* will align to the closest grid point.
|
|
5
22
|
* @example
|
|
6
|
-
* { x: 100, y: 50 }
|
|
23
|
+
* { x: 100, y: 50 } // will snap elements every 100px horizontally and 50px vertically.
|
|
7
24
|
*/
|
|
8
25
|
snapping?: {
|
|
9
26
|
x: number;
|
|
10
27
|
y: number;
|
|
11
28
|
};
|
|
12
29
|
/**
|
|
13
|
-
*
|
|
30
|
+
* Enables axis locking during element dragging.
|
|
31
|
+
* When enabled, holding the Shift key while snapping to a nearby component locks movement to the matched axis (horizontal or vertical),
|
|
32
|
+
* allowing for precise alignment based on distance indicators.
|
|
14
33
|
* @example
|
|
15
34
|
* { x: true, y: false }
|
|
16
35
|
*/
|
|
@@ -2,6 +2,11 @@ import { ComponentDragEventProps, Canvas, Component, Editor } from 'grapesjs';
|
|
|
2
2
|
import { CanvasAbsoluteModeOptions } from './types';
|
|
3
3
|
export type GuideMatched = ComponentDragEventProps['guidesMatched'][number];
|
|
4
4
|
type CanvasView = ReturnType<Canvas['module']['getCanvasView']>;
|
|
5
|
+
export declare const isAbsolute: (editor: Editor, target: Component | Component[], opts: CanvasAbsoluteModeOptions) => {
|
|
6
|
+
result: boolean;
|
|
7
|
+
component: Component;
|
|
8
|
+
dmode: import('grapesjs').DragMode | undefined;
|
|
9
|
+
} | undefined;
|
|
5
10
|
/**
|
|
6
11
|
* This function calculates the distances between the origin and matched components.
|
|
7
12
|
* - It returns the coordinates and dimensions of the bounding box for the matched components.
|