@mce/openxml 0.11.1 → 0.11.2

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