@mce/openxml 0.28.1 → 0.29.1

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 +49 -83
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,9 +1,31 @@
1
- import { definePlugin } from "mce";
1
+ import { base64ToBytes, definePlugin, matchSource } from "mce";
2
2
  //#region src/plugin.ts
3
+ var PPTX_MIME = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
4
+ var XLSX_MIME = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
5
+ var DOCX_MIME = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
3
6
  function plugin() {
4
7
  return definePlugin((editor) => {
5
8
  const { log, upload, to, fonts } = editor;
6
- const RE = /\.pptx$/i;
9
+ function makeExporter(name, mime, reverse, withFonts, run) {
10
+ return {
11
+ name,
12
+ saveAs: true,
13
+ handle: async (options) => {
14
+ const { [name]: specificOptions, ...jsonOptions } = options;
15
+ const doc = await to("json", jsonOptions);
16
+ if (reverse) doc.children?.reverse();
17
+ const bytes = await run({
18
+ ...doc,
19
+ ...withFonts ? { fonts } : {},
20
+ meta: {
21
+ ...doc.meta,
22
+ ...specificOptions
23
+ }
24
+ });
25
+ return new Blob([bytes], { type: mime });
26
+ }
27
+ };
28
+ }
7
29
  return {
8
30
  name: "mce:openxml",
9
31
  messages: {
@@ -22,15 +44,10 @@ function plugin() {
22
44
  {
23
45
  name: "pptx",
24
46
  accept: ".pptx",
25
- test: (source) => {
26
- if (source instanceof Blob) {
27
- if (source.type.startsWith("application/vnd.openxmlformats-officedocument.presentationml.presentation")) return true;
28
- }
29
- if (source instanceof File) {
30
- if (RE.test(source.name)) return true;
31
- }
32
- return false;
33
- },
47
+ test: matchSource({
48
+ ext: /\.pptx$/i,
49
+ mime: PPTX_MIME
50
+ }),
34
51
  load: async (source) => {
35
52
  const [{ pptxToDoc }, presetShapeDefinitions] = await Promise.all([import("modern-openxml"), import("modern-openxml/presetShapeDefinitions").then((rep) => rep.default)]);
36
53
  const doc = await pptxToDoc(await source.arrayBuffer(), {
@@ -38,7 +55,7 @@ function plugin() {
38
55
  upload: async (input, meta) => {
39
56
  const filename = meta.image;
40
57
  let mimeType = getMimeType(meta.image);
41
- const arrayBuffer = base64ToArrayBuffer(input);
58
+ const arrayBuffer = base64ToBytes(input).buffer;
42
59
  if (!mimeType) {
43
60
  const ext = detectImageExt(arrayBuffer);
44
61
  if (ext) mimeType = getMimeType(ext);
@@ -66,11 +83,10 @@ function plugin() {
66
83
  {
67
84
  name: "xlsx",
68
85
  accept: ".xlsx",
69
- test: (source) => {
70
- if (source instanceof Blob && source.type.includes("spreadsheetml.sheet")) return true;
71
- if (source instanceof File && /\.xlsx$/i.test(source.name)) return true;
72
- return false;
73
- },
86
+ test: matchSource({
87
+ ext: /\.xlsx$/i,
88
+ mime: XLSX_MIME
89
+ }),
74
90
  load: async (source) => {
75
91
  const { xlsxToDoc } = await import("modern-openxml");
76
92
  const doc = await xlsxToDoc(await source.arrayBuffer());
@@ -82,11 +98,10 @@ function plugin() {
82
98
  {
83
99
  name: "docx",
84
100
  accept: ".docx",
85
- test: (source) => {
86
- if (source instanceof Blob && source.type.includes("wordprocessingml.document")) return true;
87
- if (source instanceof File && /\.docx$/i.test(source.name)) return true;
88
- return false;
89
- },
101
+ test: matchSource({
102
+ ext: /\.docx$/i,
103
+ mime: DOCX_MIME
104
+ }),
90
105
  load: async (source) => {
91
106
  const { docxToDoc } = await import("modern-openxml");
92
107
  const doc = await docxToDoc(await source.arrayBuffer());
@@ -97,59 +112,18 @@ function plugin() {
97
112
  }
98
113
  ],
99
114
  exporters: [
100
- {
101
- name: "pptx",
102
- saveAs: true,
103
- handle: async (options) => {
104
- const { docToPptx } = await import("modern-openxml");
105
- const { pptx: pptxOptions, ...jsonOptions } = options;
106
- const doc = await to("json", jsonOptions);
107
- doc.children?.reverse();
108
- const pptx = await docToPptx({
109
- ...doc,
110
- fonts,
111
- meta: {
112
- ...doc.meta,
113
- ...pptxOptions
114
- }
115
- });
116
- return new Blob([pptx], { type: "application/vnd.openxmlformats-officedocument.presentationml.presentation" });
117
- }
118
- },
119
- {
120
- name: "xlsx",
121
- saveAs: true,
122
- handle: async (options) => {
123
- const { docToXlsx } = await import("modern-openxml");
124
- const { xlsx: xlsxOptions, ...jsonOptions } = options;
125
- const doc = await to("json", jsonOptions);
126
- const xlsx = await docToXlsx({
127
- ...doc,
128
- meta: {
129
- ...doc.meta,
130
- ...xlsxOptions
131
- }
132
- });
133
- return new Blob([xlsx], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
134
- }
135
- },
136
- {
137
- name: "docx",
138
- saveAs: true,
139
- handle: async (options) => {
140
- const { docToDocx } = await import("modern-openxml");
141
- const { docx: docxOptions, ...jsonOptions } = options;
142
- const doc = await to("json", jsonOptions);
143
- const docx = await docToDocx({
144
- ...doc,
145
- meta: {
146
- ...doc.meta,
147
- ...docxOptions
148
- }
149
- });
150
- return new Blob([docx], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" });
151
- }
152
- }
115
+ makeExporter("pptx", PPTX_MIME, true, true, async (payload) => {
116
+ const { docToPptx } = await import("modern-openxml");
117
+ return await docToPptx(payload);
118
+ }),
119
+ makeExporter("xlsx", XLSX_MIME, false, false, async (payload) => {
120
+ const { docToXlsx } = await import("modern-openxml");
121
+ return await docToXlsx(payload);
122
+ }),
123
+ makeExporter("docx", DOCX_MIME, false, false, async (payload) => {
124
+ const { docToDocx } = await import("modern-openxml");
125
+ return await docToDocx(payload);
126
+ })
153
127
  ]
154
128
  };
155
129
  });
@@ -170,14 +144,6 @@ function getMimeType(filename) {
170
144
  const ext = arr[arr.length - 1].toLowerCase();
171
145
  return ext in EXT_TO_MIMES ? EXT_TO_MIMES[ext] : null;
172
146
  }
173
- function base64ToArrayBuffer(base64) {
174
- const binaryString = atob(base64);
175
- const length = binaryString.length;
176
- const buffer = new ArrayBuffer(length);
177
- const uint8Array = new Uint8Array(buffer);
178
- for (let i = 0; i < length; i++) uint8Array[i] = binaryString.charCodeAt(i);
179
- return buffer;
180
- }
181
147
  function detectImageExt(buffer) {
182
148
  const view = new DataView(buffer);
183
149
  const len = view.byteLength;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mce/openxml",
3
3
  "type": "module",
4
- "version": "0.28.1",
4
+ "version": "0.29.1",
5
5
  "description": "Openxml plugin for mce",
6
6
  "author": "wxm",
7
7
  "license": "MIT",
@@ -49,7 +49,7 @@
49
49
  "modern-openxml": "^1.12.4"
50
50
  },
51
51
  "devDependencies": {
52
- "mce": "0.28.1"
52
+ "mce": "0.29.1"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "mce": "^0"