@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.
- package/dist/index.js +107 -71
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,69 +1,92 @@
|
|
|
1
|
-
import { definePlugin
|
|
2
|
-
import {
|
|
3
|
-
function
|
|
4
|
-
return
|
|
1
|
+
import { definePlugin } from "mce";
|
|
2
|
+
import { docToPptx, pptxToDoc } from "modern-openxml";
|
|
3
|
+
function plugin() {
|
|
4
|
+
return definePlugin((editor) => {
|
|
5
5
|
const {
|
|
6
|
-
log
|
|
7
|
-
upload
|
|
8
|
-
config
|
|
9
|
-
to
|
|
10
|
-
fonts
|
|
11
|
-
} =
|
|
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: (
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
|
48
|
-
new File([
|
|
49
|
-
type:
|
|
46
|
+
return await upload(
|
|
47
|
+
new File([arrayBuffer], filename, {
|
|
48
|
+
type: mimeType ?? void 0
|
|
50
49
|
})
|
|
51
50
|
);
|
|
52
51
|
},
|
|
53
|
-
progress: (
|
|
54
|
-
|
|
52
|
+
progress: (current, total, cached) => {
|
|
53
|
+
log("load pptx progress", `${current}/${total}`, cached ? "cached" : "");
|
|
55
54
|
}
|
|
56
55
|
});
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
|
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
|
|
78
|
-
const
|
|
79
|
-
|
|
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
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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 (
|
|
96
|
-
const
|
|
97
|
-
if (
|
|
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 (
|
|
133
|
+
if (len >= 12 && view.getUint32(8, false) === 1464156752) {
|
|
103
134
|
return "webp";
|
|
135
|
+
}
|
|
104
136
|
break;
|
|
105
137
|
}
|
|
106
|
-
if (
|
|
138
|
+
if (view.getUint8(0) === 255 && view.getUint8(1) === 216 && view.getUint8(2) === 255) {
|
|
107
139
|
return "jpeg";
|
|
108
|
-
|
|
140
|
+
}
|
|
141
|
+
if (view.getUint8(0) === 66 && view.getUint8(1) === 77) {
|
|
109
142
|
return "bmp";
|
|
110
|
-
|
|
111
|
-
|
|
143
|
+
}
|
|
144
|
+
if (len >= 22) {
|
|
145
|
+
if (view.getUint32(0, true) === 1179469088) {
|
|
112
146
|
return "emf";
|
|
113
|
-
if (
|
|
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
|
-
|
|
119
|
-
|
|
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.
|
|
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.
|
|
52
|
+
"mce": "0.11.2"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"mce": "^0"
|