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