@mce/openxml 0.17.10 → 0.17.12

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