@getdraft/plugin 1.13.0 → 1.15.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.
package/dist/index.es.js CHANGED
@@ -1,46 +1,55 @@
1
- var v = Object.defineProperty;
2
- var k = (n, e, t) => e in n ? v(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
- var a = (n, e, t) => k(n, typeof e != "symbol" ? e + "" : e, t);
4
- const P = "http://localhost:5174", h = "https://seplugin.sendsay.ru", b = async (n) => {
1
+ var O = Object.defineProperty;
2
+ var T = (o, t, e) => t in o ? O(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
3
+ var a = (o, t, e) => T(o, typeof t != "symbol" ? t + "" : t, e);
4
+ const b = "http://localhost:5174", D = "https://seplugin.sendsay.ru";
5
+ class k extends Error {
6
+ constructor(e, i) {
7
+ super(i);
8
+ a(this, "code");
9
+ this.name = "DraftError", this.code = e;
10
+ }
11
+ }
12
+ var H = /* @__PURE__ */ ((o) => (o.Audit = "audit", o.Guide = "guide", o.Style = "style", o.Blocks = "blocks", o))(H || {}), C = /* @__PURE__ */ ((o) => (o.Desktop = "desktop", o.Mobile = "mobile", o))(C || {});
13
+ const x = async (o) => {
5
14
  try {
6
- const e = await fetch(n, { method: "HEAD" });
7
- if (!e.ok)
15
+ const t = await fetch(o, { method: "HEAD" });
16
+ if (!t.ok)
8
17
  return null;
9
- const t = e.headers.get("Content-Length");
10
- return t ? parseInt(t, 10) : null;
18
+ const e = t.headers.get("Content-Length");
19
+ return e ? parseInt(e, 10) : null;
11
20
  } catch {
12
21
  return null;
13
22
  }
14
- }, g = (n) => new Promise((e, t) => {
15
- const o = new Image();
16
- o.onload = async () => {
17
- const i = await b(n) ?? 0;
18
- e({
19
- originalWidth: o.width,
20
- originalHeight: o.height,
21
- ratio: o.width / o.height,
22
- fileSize: i
23
+ }, P = (o) => new Promise((t, e) => {
24
+ const i = new Image();
25
+ i.onload = async () => {
26
+ const n = await x(o) ?? 0;
27
+ t({
28
+ originalWidth: i.width,
29
+ originalHeight: i.height,
30
+ ratio: i.width / i.height,
31
+ fileSize: n
23
32
  });
24
- }, o.onerror = (i) => {
25
- t(i);
26
- }, o.src = n;
27
- }), u = (n, e) => {
28
- if (n.length !== 1)
29
- return n;
30
- if (n.charCodeAt(0) >= 880) {
31
- if (e.indexOf("Key") === 0 && e.length === 4)
32
- return e.charAt(3);
33
- if (e.indexOf("Digit") === 0 && e.length === 6)
34
- return e.charAt(5);
33
+ }, i.onerror = (n) => {
34
+ e(n);
35
+ }, i.src = o;
36
+ }), A = (o, t) => {
37
+ if (o.length !== 1)
38
+ return o;
39
+ if (o.charCodeAt(0) >= 880) {
40
+ if (t.indexOf("Key") === 0 && t.length === 4)
41
+ return t.charAt(3);
42
+ if (t.indexOf("Digit") === 0 && t.length === 6)
43
+ return t.charAt(5);
35
44
  }
36
- return n;
37
- }, L = (n) => {
38
- let e = `${u(n.key, n.code)}`.toLowerCase() || n.key;
39
- const t = navigator.userAgent.includes("Mac OS");
40
- e === "backspace" && t && (e = "delete");
41
- const o = [];
42
- return (t && n.metaKey || !t && n.ctrlKey) && o.push("mod"), n.shiftKey && o.push("shift"), o.length > 0 && (e = `${o.join("-")}-${u(e, n.code)}`), e;
43
- }, I = /* @__PURE__ */ new Set([
45
+ return o;
46
+ }, z = (o) => {
47
+ let t = `${A(o.key, o.code)}`.toLowerCase() || o.key;
48
+ const e = navigator.userAgent.includes("Mac OS");
49
+ t === "backspace" && e && (t = "delete");
50
+ const i = [];
51
+ return (e && o.metaKey || !e && o.ctrlKey) && i.push("mod"), o.shiftKey && i.push("shift"), i.length > 0 && (t = `${i.join("-")}-${A(t, o.code)}`), t;
52
+ }, U = /* @__PURE__ */ new Set([
44
53
  "escape",
45
54
  "mod-c",
46
55
  "mod-d",
@@ -52,225 +61,234 @@ const P = "http://localhost:5174", h = "https://seplugin.sendsay.ru", b = async
52
61
  "mod-g",
53
62
  "mod-p",
54
63
  "mod-s"
55
- ]), x = "@getdraft/plugin", A = "1.13.0", D = "dist/index.cjs.js", C = "dist/index.es.js", H = "src", O = {
56
- prebuild: "rm -rf dist",
57
- build: "vite build",
58
- "dev-tsc": "npx tsc --noEmit",
59
- eslint: "npx eslint src",
60
- "eslint:fix": "pnpm eslint --fix",
61
- prettify: "npx prettier -w ./",
62
- prepublish: "pnpm build",
63
- watch: "NODE_ENV=development vite build --watch",
64
- start: "NODE_ENV=development npx vite"
65
- }, N = {}, z = {
66
- "@types/letty": "workspace:^",
67
- "@vitejs/plugin-legacy": "5.4.1",
68
- rollup: "4.22.4",
69
- terser: "5.31.3",
70
- "rollup-plugin-dts": "6.1.1",
71
- "@letty/eslint-config-ts": "workspace:^",
72
- "@letty/prettier": "workspace:^"
73
- }, T = "MIT", K = {
74
- name: x,
75
- version: A,
76
- main: D,
77
- module: C,
78
- types: H,
79
- scripts: O,
80
- dependencies: N,
81
- devDependencies: z,
82
- license: T
83
- }, M = () => {
84
- var s, r;
85
- const n = new RegExp("(^| )seplugin-dev=([^;]+)"), e = document.cookie.match(n), t = (r = e == null ? void 0 : (s = e[2]).toLowerCase) == null ? void 0 : r.call(s);
86
- if (t === "true")
87
- return P;
88
- if (t === "stage" || t != null && t.includes("https://") || window.location.pathname.includes("netlify"))
89
- return t != null && t.includes("https://") ? t : `${h}/stage`;
90
- const [o, i] = K.version.split(".");
91
- return `${h}/v${o}.${i}`;
92
- }, U = ({ token: n, pluginId: e, apiUrl: t }) => {
93
- if (!n)
64
+ ]), L = "1.15.0", M = {
65
+ version: L
66
+ }, K = () => {
67
+ var r, s;
68
+ const o = new RegExp("(^| )seplugin-dev=([^;]+)"), t = document.cookie.match(o), e = (s = t == null ? void 0 : (r = t[2]).toLowerCase) == null ? void 0 : s.call(r);
69
+ if (e === "true")
70
+ return b;
71
+ if (e != null && e.startsWith("https://"))
72
+ return e;
73
+ const [i, n] = M.version.split(".");
74
+ return `${D}/v${i}.${n}`;
75
+ }, N = ({ token: o, pluginId: t, apiUrl: e }) => {
76
+ if (!o)
94
77
  throw new Error("No [token] was provided");
95
- if (!e)
96
- throw new Error("No [pluginId] was provided");
97
78
  if (!t)
79
+ throw new Error("No [pluginId] was provided");
80
+ if (!e)
98
81
  throw new Error("No [apiUrl] was provided");
99
- }, j = () => {
100
- const n = document.createElement("iframe");
101
- return n.src = M(), n.setAttribute(
82
+ }, W = (o) => {
83
+ const t = document.createElement("iframe"), e = new URL(K());
84
+ return e.searchParams.set("parentOrigin", o), t.src = e.toString(), t.setAttribute(
102
85
  "style",
103
- "height:100%;width:100%;min-width:960px;border:0px"
104
- ), n.setAttribute("allow", "clipboard-read; clipboard-write"), n.setAttribute(
86
+ "height:100%;width:100%;min-width:960px;border:0px;"
87
+ ), t.setAttribute("allow", "clipboard-read; clipboard-write"), t.setAttribute(
105
88
  "sandbox",
106
- "allow-scripts allow-same-origin allow-forms allow-popups"
107
- ), n;
108
- };
109
- class G {
110
- constructor(e) {
89
+ "allow-scripts allow-same-origin allow-forms allow-popups allow-downloads"
90
+ ), t;
91
+ }, f = class f {
92
+ constructor(t) {
111
93
  a(this, "iframe", null);
112
94
  a(this, "hotkeysAttached", !1);
113
- a(this, "onHostHotkey", (e) => {
114
- var c;
115
- if (!this.captureHotkeysEnabled() || !((c = this.iframe) != null && c.contentWindow))
95
+ a(this, "iframeOrigin", null);
96
+ a(this, "onHostHotkey", (t) => {
97
+ var l;
98
+ if (!this.captureHotkeysEnabled() || !((l = this.iframe) != null && l.contentWindow))
116
99
  return;
117
- const t = L(e);
118
- if (!I.has(t))
100
+ const e = z(t);
101
+ if (!U.has(e))
119
102
  return;
120
- const o = e.target;
121
- if (o && (o.isContentEditable || ["INPUT", "TEXTAREA", "SELECT"].includes(o.tagName)))
103
+ const i = t.target;
104
+ if (i && (i.isContentEditable || ["INPUT", "TEXTAREA", "SELECT"].includes(i.tagName)))
122
105
  return;
123
- e.preventDefault();
124
- const { key: i, ctrlKey: s, metaKey: r, shiftKey: l, altKey: d } = e;
106
+ t.preventDefault();
107
+ const { key: n, ctrlKey: r, metaKey: s, shiftKey: c, altKey: h } = t;
125
108
  this.postEvent("hostHotkey", {
126
- key: i,
127
- code: e.code,
128
- metaKey: r,
129
- ctrlKey: s,
130
- shiftKey: l,
131
- altKey: d,
132
- keyCode: t
109
+ key: n,
110
+ code: t.code,
111
+ metaKey: s,
112
+ ctrlKey: r,
113
+ shiftKey: c,
114
+ altKey: h,
115
+ keyCode: e
133
116
  });
134
117
  });
135
118
  a(this, "onImageUpload", async ({
136
- id: e,
137
- ...t
119
+ id: t,
120
+ ...e
138
121
  }) => {
139
- var o, i;
122
+ var i, n;
140
123
  try {
141
- const s = await ((i = (o = this.config.on).uploadImage) == null ? void 0 : i.call(o, t));
142
- let r = {};
143
- s && (r = await g(s)), this.postEvent("imageUploaded", { id: e, img: { ...r, url: s } });
124
+ const r = await ((n = (i = this.config.on).uploadImage) == null ? void 0 : n.call(i, e));
125
+ let s = {};
126
+ r && (s = await P(r)), this.postEvent("imageUploaded", { id: t, img: { ...s, url: r } });
144
127
  } catch {
145
- this.postEvent("imageUploaded", { id: e });
128
+ this.postEvent("imageUploaded", { id: t });
146
129
  }
147
130
  });
148
131
  a(this, "onLoadPersonalization", async () => {
149
132
  try {
150
133
  if (!this.config.on.loadPersonalization)
151
134
  throw new Error("No personalization loader provided");
152
- const e = await this.config.on.loadPersonalization();
135
+ const t = await this.config.on.loadPersonalization();
153
136
  this.postEvent("loadPersonalization", {
154
- items: e
137
+ items: t
155
138
  });
156
139
  } catch {
157
140
  this.postEvent("loadPersonalization", {});
158
141
  }
159
142
  });
160
- a(this, "onGalleryOpen", async ({ id: e }) => {
161
- var o, i;
162
- const t = async (s) => {
163
- const r = { url: s };
164
- if (s) {
165
- const l = await g(s);
166
- Object.assign(r, l);
143
+ a(this, "onGalleryOpen", async ({ id: t }) => {
144
+ var i, n;
145
+ const e = async (r) => {
146
+ const s = { url: r };
147
+ if (r) {
148
+ const c = await P(r);
149
+ Object.assign(s, c);
167
150
  }
168
151
  this.postEvent("imageUploaded", {
169
- id: e,
170
- img: r
152
+ id: t,
153
+ img: s
171
154
  });
172
155
  };
173
- (i = (o = this.config.on).openGallery) == null || i.call(o, t);
156
+ (n = (i = this.config.on).openGallery) == null || n.call(i, e);
174
157
  });
175
- a(this, "onMessage", (e) => {
158
+ a(this, "onMessage", (t) => {
159
+ var r;
160
+ if (t.source !== ((r = this.iframe) == null ? void 0 : r.contentWindow))
161
+ return;
176
162
  const {
177
- data: { source: t, event: o, ...i }
178
- } = e;
179
- t === "DraftPlugin" && (o === "openGallery" ? this.onGalleryOpen(i) : o === "uploadImage" ? this.onImageUpload(i) : o === "loadPersonalization" ? this.onLoadPersonalization() : this.triggerEvent(
180
- this.config.on[o],
181
- i
163
+ data: { source: e, event: i, ...n }
164
+ } = t;
165
+ e === "DraftPlugin" && (i === "openGallery" ? this.onGalleryOpen(n) : i === "uploadImage" ? this.onImageUpload(n) : i === "loadPersonalization" ? this.onLoadPersonalization() : this.triggerEvent(
166
+ this.config.on[i],
167
+ n
182
168
  ));
183
169
  });
184
170
  a(this, "destroy", () => {
185
- var e;
186
- (e = this.iframe) == null || e.remove(), this.iframe = null, window.removeEventListener("message", this.onMessage), this.hotkeysAttached && (window.removeEventListener("keydown", this.onHostHotkey), this.hotkeysAttached = !1);
171
+ var t;
172
+ (t = this.iframe) == null || t.remove(), this.iframe = null, window.removeEventListener("message", this.onMessage), this.hotkeysAttached && (window.removeEventListener("keydown", this.onHostHotkey), this.hotkeysAttached = !1);
187
173
  });
188
- this.config = e;
174
+ this.config = t;
189
175
  }
190
176
  captureHotkeysEnabled() {
191
177
  return this.config.disableHotkeysPassing !== !0;
192
178
  }
193
- promisedIframeMethod(e) {
194
- let t = null;
195
- const o = new Promise((i, s) => {
196
- const r = (l) => {
197
- const {
198
- data: { source: d, event: c, ...p }
199
- } = l;
200
- d === "DraftPlugin" && c === e && (l.stopPropagation(), t && (clearTimeout(t), t = null), i(p), window.removeEventListener("message", r, !0));
179
+ promisedIframeMethod(t) {
180
+ let e = null, i = null;
181
+ const n = () => {
182
+ e && (clearTimeout(e), e = null), i && (window.removeEventListener("message", i, !0), i = null);
183
+ }, r = new Promise((s, c) => {
184
+ const h = () => {
185
+ e && clearTimeout(e), e = setTimeout(() => {
186
+ n(), c(
187
+ new k("timeout_error", `Action "${t}" timed out`)
188
+ );
189
+ }, f.HEARTBEAT_TIMEOUT);
201
190
  };
202
- window.addEventListener("message", r, !0), t = setTimeout(() => {
203
- window.removeEventListener("message", r, !0), s("Request timed out"), t = null;
204
- }, 2e3);
191
+ i = (l) => {
192
+ var g;
193
+ if (l.source !== ((g = this.iframe) == null ? void 0 : g.contentWindow)) return;
194
+ const { data: d } = l;
195
+ if (d.source === "DraftPlugin") {
196
+ if (d.event === "actionPing" && d.action === t) {
197
+ l.stopPropagation(), h();
198
+ return;
199
+ }
200
+ if (d.event === t) {
201
+ l.stopPropagation(), n();
202
+ const { source: y, event: v, ok: u, code: p, message: m, ...w } = d;
203
+ u === !1 ? c(
204
+ new k(
205
+ p || "unknown_error",
206
+ m || "Unknown error"
207
+ )
208
+ ) : s(w);
209
+ }
210
+ }
211
+ }, window.addEventListener("message", i, !0), h();
205
212
  });
206
- return this.postEvent(e), o;
213
+ return this.postEvent(t), r;
207
214
  }
208
- postEvent(e, t = {}) {
209
- this.iframe && this.iframe.contentWindow && this.iframe.contentWindow.postMessage(
210
- {
211
- ...t,
212
- event: e,
213
- source: "DraftPlugin"
214
- },
215
- "*"
216
- );
215
+ postEvent(t, e = {}) {
216
+ if (this.iframe && this.iframe.contentWindow && this.iframeOrigin)
217
+ this.iframe.contentWindow.postMessage(
218
+ {
219
+ ...e,
220
+ event: t,
221
+ source: "DraftPlugin"
222
+ },
223
+ this.iframeOrigin
224
+ );
225
+ else if (!this.iframeOrigin)
226
+ throw new Error("Iframe origin is not initialized");
217
227
  }
218
- triggerEvent(e, t) {
219
- if (e)
220
- return e(t);
228
+ triggerEvent(t, e) {
229
+ if (t)
230
+ return t(e);
221
231
  }
222
232
  start({
223
- template: e,
224
- templateName: t,
225
- uid: o
233
+ template: t,
234
+ templateName: e,
235
+ uid: i
226
236
  }) {
227
- const i = document.querySelector(this.config.container);
228
- if (i) {
229
- const s = ({ data: { source: r, event: l } }) => {
230
- if (r === "DraftPlugin" && l === "loaded") {
237
+ const n = document.querySelector(this.config.container);
238
+ if (n) {
239
+ const r = (s) => {
240
+ var l;
241
+ const {
242
+ data: { source: c, event: h }
243
+ } = s;
244
+ if (c === "DraftPlugin" && h === "loaded") {
245
+ if (s.source !== ((l = this.iframe) == null ? void 0 : l.contentWindow)) return;
246
+ this.iframeOrigin = s.origin;
231
247
  const {
232
248
  on: d,
233
- container: c,
234
- locale: p,
235
- autosave: m,
236
- token: f,
237
- apiUrl: w,
238
- pluginId: y,
239
- __config: E
249
+ container: g,
250
+ locale: y,
251
+ autosave: v,
252
+ token: u,
253
+ apiUrl: p,
254
+ pluginId: m,
255
+ authData: w,
256
+ __config: I
240
257
  } = this.config;
241
- U(this.config), this.iframe && this.iframe.contentWindow && this.postEvent("init", {
242
- container: c,
243
- locale: p,
244
- uid: o,
245
- autosave: m,
246
- token: f,
247
- pluginId: y,
248
- apiUrl: w,
249
- __config: E,
258
+ N(this.config), this.iframe && this.iframe.contentWindow && this.postEvent("init", {
259
+ container: g,
260
+ locale: y,
261
+ uid: i,
262
+ autosave: v,
263
+ token: u,
264
+ pluginId: m,
265
+ authData: w,
266
+ apiUrl: p,
267
+ __config: I,
250
268
  enabledListeners: Object.keys(d),
251
- template: e,
252
- templateName: t
253
- }), window.addEventListener("message", this.onMessage), window.removeEventListener("message", s);
269
+ template: t,
270
+ templateName: e
271
+ }), window.addEventListener("message", this.onMessage), window.removeEventListener("message", r);
254
272
  }
255
273
  };
256
- this.iframe = j(), window.addEventListener("message", s), i.appendChild(this.iframe), this.captureHotkeysEnabled() && !this.hotkeysAttached && (window.addEventListener("keydown", this.onHostHotkey), this.hotkeysAttached = !0);
274
+ this.iframe = W(window.location.origin), window.addEventListener("message", r), n.appendChild(this.iframe), this.captureHotkeysEnabled() && !this.hotkeysAttached && (window.addEventListener("keydown", this.onHostHotkey), this.hotkeysAttached = !0);
257
275
  } else
258
276
  throw new Error("Specified container not found");
259
277
  }
260
- changeTemplate(e, t = {}) {
261
- this.postEvent("changeTemplate", { ...t, template: e });
278
+ changeTemplate(t, e = {}) {
279
+ this.postEvent("changeTemplate", { ...e, template: t });
262
280
  }
263
- toggleGrid(e) {
264
- this.postEvent("toggleGrid", { state: e });
281
+ toggleGrid(t) {
282
+ this.postEvent("toggleGrid", { state: t });
265
283
  }
266
- togglePreview(e) {
267
- this.postEvent("togglePreview", { state: e });
284
+ togglePreview(t) {
285
+ this.postEvent("togglePreview", { state: t });
268
286
  }
269
- toggleMenu(e = null) {
270
- this.postEvent("toggleMenu", { menu: e });
287
+ toggleMenu(t = null) {
288
+ this.postEvent("toggleMenu", { menu: t });
271
289
  }
272
- toggleViewMode(e) {
273
- this.postEvent("toggleViewMode", { viewMode: e });
290
+ toggleViewMode(t) {
291
+ this.postEvent("toggleViewMode", { viewMode: t });
274
292
  }
275
293
  undo() {
276
294
  this.postEvent("undo");
@@ -287,27 +305,35 @@ class G {
287
305
  exportContent() {
288
306
  return this.promisedIframeMethod("exportContent");
289
307
  }
290
- }
291
- var S = /* @__PURE__ */ ((n) => (n.Audit = "audit", n.Guide = "guide", n.Style = "style", n.Blocks = "blocks", n))(S || {}), _ = /* @__PURE__ */ ((n) => (n.Desktop = "desktop", n.Mobile = "mobile", n))(_ || {});
292
- const $ = {
308
+ exportAsFile() {
309
+ return this.promisedIframeMethod("exportAsFile");
310
+ }
311
+ };
312
+ a(f, "HEARTBEAT_TIMEOUT", 4e3);
313
+ let E = f;
314
+ const _ = {
293
315
  container: "#draft-plugin-container",
294
316
  disableHotkeysPassing: !1,
295
317
  locale: "ru",
296
318
  on: {}
297
319
  };
298
- class W {
299
- create(e) {
300
- return new G({
301
- ...$,
302
- ...e
320
+ class S {
321
+ constructor() {
322
+ a(this, "version", L);
323
+ }
324
+ create(t) {
325
+ return new E({
326
+ ..._,
327
+ ...t
303
328
  });
304
329
  }
305
330
  }
306
- const F = new W();
307
- window.DraftPlugin = F;
331
+ const G = new S();
332
+ window.DraftPlugin = G;
308
333
  export {
309
- S as EditorMenu,
310
- _ as ViewMode,
311
- F as default
334
+ k as DraftError,
335
+ H as EditorMenu,
336
+ C as ViewMode,
337
+ G as default
312
338
  };
313
339
  //# sourceMappingURL=index.es.js.map