@mce/openxml 0.11.1 → 0.11.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.
Files changed (2) hide show
  1. package/dist/index.js +106 -71
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,69 +1,91 @@
1
- import { definePlugin as b } from "mce";
2
- import { pptxToDoc as x, docToPptx as U } from "modern-openxml";
3
- function j() {
4
- return b((r) => {
1
+ import { definePlugin } from "mce";
2
+ import { docToPptx, pptxToDoc } from "modern-openxml";
3
+ function plugin() {
4
+ return definePlugin((editor) => {
5
5
  const {
6
- log: e,
7
- upload: t,
8
- config: f,
9
- to: o,
10
- fonts: s
11
- } = r, y = /\.pptx$/i;
6
+ log,
7
+ upload,
8
+ to,
9
+ fonts
10
+ } = editor;
11
+ const RE = /\.pptx$/i;
12
12
  return {
13
13
  name: "mce:openxml",
14
- exporters: [
15
- {
16
- name: "pptx",
17
- saveAs: !0,
18
- handle: async (i) => {
19
- const m = await o("json", i);
20
- return new Blob([
21
- await U({
22
- ...m,
23
- fonts: s
24
- })
25
- ], {
26
- type: "application/vnd.openxmlformats-officedocument.presentationml.presentation"
27
- });
28
- }
29
- }
30
- ],
31
14
  loaders: [
32
15
  {
33
16
  name: "pptx",
34
17
  accept: ".pptx",
35
- test: (i) => !!(i instanceof Blob && i.type.startsWith("application/vnd.openxmlformats-officedocument.presentationml.presentation") || i instanceof File && y.test(i.name)),
36
- load: async (i) => {
37
- const m = await import("modern-openxml/presetShapeDefinitions").then((n) => n.default), p = await x(await i.arrayBuffer(), {
38
- presetShapeDefinitions: m,
39
- upload: async (n, a) => {
40
- const g = a.image;
41
- let c = d(a.image);
42
- const u = v(n);
43
- if (!c) {
44
- const l = h(u);
45
- l && (c = d(l));
18
+ test: (source) => {
19
+ if (source instanceof Blob) {
20
+ if (source.type.startsWith("application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
21
+ return true;
22
+ }
23
+ }
24
+ if (source instanceof File) {
25
+ if (RE.test(source.name)) {
26
+ return true;
27
+ }
28
+ }
29
+ return false;
30
+ },
31
+ load: async (source) => {
32
+ const presetShapeDefinitions = await import("modern-openxml/presetShapeDefinitions").then((rep) => rep.default);
33
+ const doc = await pptxToDoc(await source.arrayBuffer(), {
34
+ presetShapeDefinitions,
35
+ upload: async (input, meta) => {
36
+ const filename = meta.image;
37
+ let mimeType = getMimeType(meta.image);
38
+ const arrayBuffer = base64ToArrayBuffer(input);
39
+ if (!mimeType) {
40
+ const ext = detectImageExt(arrayBuffer);
41
+ if (ext) {
42
+ mimeType = getMimeType(ext);
43
+ }
46
44
  }
47
- return await t(
48
- new File([u], g, {
49
- type: c ?? void 0
45
+ return await upload(
46
+ new File([arrayBuffer], filename, {
47
+ type: mimeType ?? void 0
50
48
  })
51
49
  );
52
50
  },
53
- progress: (n, a, g) => {
54
- e("load pptx progress", `${n}/${a}`, g ? "cached" : "");
51
+ progress: (current, total, cached) => {
52
+ log("load pptx progress", `${current}/${total}`, cached ? "cached" : "");
55
53
  }
56
54
  });
57
- return p.children?.forEach((n, a) => {
58
- n.name = `Slide ${a + 1}`, n.style ??= {}, n.style.left = 0, n.style.top = Number(n.style.top) + a * f.value.frameGap, n.meta.inEditorIs = "Frame";
59
- }), p.name = i.name, p.meta.inEditorIs = "Doc", p;
55
+ doc.children?.forEach((child, index) => {
56
+ child.name = `Slide ${index + 1}`;
57
+ child.style ??= {};
58
+ child.style.left = 0;
59
+ child.style.top = Number(child.style.top);
60
+ child.meta.inEditorIs = "Frame";
61
+ });
62
+ doc.name = source.name;
63
+ doc.meta.inEditorIs = "Doc";
64
+ return doc;
65
+ }
66
+ }
67
+ ],
68
+ exporters: [
69
+ {
70
+ name: "pptx",
71
+ saveAs: true,
72
+ handle: async (options) => {
73
+ const doc = await to("json", options);
74
+ return new Blob([
75
+ await docToPptx({
76
+ ...doc,
77
+ fonts
78
+ })
79
+ ], {
80
+ type: "application/vnd.openxmlformats-officedocument.presentationml.presentation"
81
+ });
60
82
  }
61
83
  }
62
84
  ]
63
85
  };
64
86
  });
65
87
  }
66
- const w = {
88
+ const EXT_TO_MIMES = {
67
89
  jpeg: "image/jpeg",
68
90
  jpg: "image/jpeg",
69
91
  png: "image/png",
@@ -74,47 +96,60 @@ const w = {
74
96
  emf: "image/emf",
75
97
  wmf: "image/wmf"
76
98
  };
77
- function d(r) {
78
- const e = r.split("."), t = e[e.length - 1].toLowerCase();
79
- return t in w ? w[t] : null;
99
+ function getMimeType(filename) {
100
+ const arr = filename.split(".");
101
+ const ext = arr[arr.length - 1].toLowerCase();
102
+ return ext in EXT_TO_MIMES ? EXT_TO_MIMES[ext] : null;
80
103
  }
81
- function v(r) {
82
- const e = atob(r), t = e.length, f = new ArrayBuffer(t), o = new Uint8Array(f);
83
- for (let s = 0; s < t; s++)
84
- o[s] = e.charCodeAt(s);
85
- return f;
104
+ function base64ToArrayBuffer(base64) {
105
+ const binaryString = atob(base64);
106
+ const length = binaryString.length;
107
+ const buffer = new ArrayBuffer(length);
108
+ const uint8Array = new Uint8Array(buffer);
109
+ for (let i = 0; i < length; i++) {
110
+ uint8Array[i] = binaryString.charCodeAt(i);
111
+ }
112
+ return buffer;
86
113
  }
87
- function h(r) {
88
- const e = new DataView(r), t = e.byteLength;
89
- if (t < 4)
90
- return;
91
- switch (e.getUint32(0, !1)) {
114
+ function detectImageExt(buffer) {
115
+ const view = new DataView(buffer);
116
+ const len = view.byteLength;
117
+ if (len < 4)
118
+ return void 0;
119
+ const magic = view.getUint32(0, false);
120
+ switch (magic) {
92
121
  case 2303741511:
93
122
  return "png";
94
123
  case 1195984440:
95
- if (t >= 6) {
96
- const o = e.getUint16(4, !1);
97
- if (o === 14177 || o === 14689)
124
+ if (len >= 6) {
125
+ const version = view.getUint16(4, false);
126
+ if (version === 14177 || version === 14689) {
98
127
  return "gif";
128
+ }
99
129
  }
100
130
  break;
101
131
  case 1380533830:
102
- if (t >= 12 && e.getUint32(8, !1) === 1464156752)
132
+ if (len >= 12 && view.getUint32(8, false) === 1464156752) {
103
133
  return "webp";
134
+ }
104
135
  break;
105
136
  }
106
- if (e.getUint8(0) === 255 && e.getUint8(1) === 216 && e.getUint8(2) === 255)
137
+ if (view.getUint8(0) === 255 && view.getUint8(1) === 216 && view.getUint8(2) === 255) {
107
138
  return "jpeg";
108
- if (e.getUint8(0) === 66 && e.getUint8(1) === 77)
139
+ }
140
+ if (view.getUint8(0) === 66 && view.getUint8(1) === 77) {
109
141
  return "bmp";
110
- if (t >= 22) {
111
- if (e.getUint32(0, !0) === 1179469088)
142
+ }
143
+ if (len >= 22) {
144
+ if (view.getUint32(0, true) === 1179469088) {
112
145
  return "emf";
113
- if (e.getUint16(0, !0) === 55245 || e.getUint16(6, !0) === 55245)
146
+ } else if (view.getUint16(0, true) === 55245 || view.getUint16(6, true) === 55245) {
114
147
  return "wmf";
148
+ }
115
149
  }
150
+ return void 0;
116
151
  }
117
152
  export {
118
- j as default,
119
- j as plugin
153
+ plugin as default,
154
+ plugin
120
155
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mce/openxml",
3
3
  "type": "module",
4
- "version": "0.11.1",
4
+ "version": "0.11.3",
5
5
  "description": "Openxml plugin for mce",
6
6
  "author": "wxm",
7
7
  "license": "MIT",
@@ -49,7 +49,7 @@
49
49
  "modern-openxml": "^1.10.0"
50
50
  },
51
51
  "devDependencies": {
52
- "mce": "0.11.1"
52
+ "mce": "0.11.3"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "mce": "^0"