@mce/gaoding 0.17.3 → 0.17.4

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.
@@ -0,0 +1,2 @@
1
+ import type { NormalizedElement } from 'modern-idoc';
2
+ export declare function convertDoc(doc: any): Promise<NormalizedElement[]>;
@@ -0,0 +1,2 @@
1
+ import type { NormalizedElement } from 'modern-idoc';
2
+ export declare function convertElement(el: any, _parent?: any): Promise<NormalizedElement>;
@@ -0,0 +1,2 @@
1
+ export * from './doc';
2
+ export * from './element';
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { plugin } from './plugin';
2
- export * from './clipboard';
2
+ export * from './loaders';
3
3
  export * from './plugin';
4
4
  export default plugin;
package/dist/index.js CHANGED
@@ -1,28 +1,151 @@
1
1
  import { definePlugin } from "mce";
2
+ import { idGenerator } from "modern-idoc";
3
+ function pick(obj, keys) {
4
+ const result = {};
5
+ for (const key of keys) {
6
+ if (key in obj) {
7
+ result[key] = obj[key];
8
+ }
9
+ }
10
+ return result;
11
+ }
12
+ async function convertElement(el, _parent) {
13
+ const { elements, ...raw } = el;
14
+ const style = pick(el, [
15
+ "left",
16
+ "top",
17
+ "width",
18
+ "height",
19
+ "opacity",
20
+ "borderRadius",
21
+ // text
22
+ "color",
23
+ "fontFamily",
24
+ "fontStyle",
25
+ "fontWeight",
26
+ "fontSize",
27
+ "lineHeight",
28
+ "letterSpacing",
29
+ "textDecoration",
30
+ "writingMode",
31
+ "textAlign",
32
+ "verticalAlign",
33
+ "textIndent"
34
+ ]);
35
+ const meta = {
36
+ raw,
37
+ inPptIs: "Shape",
38
+ inEditorIs: "Element"
39
+ };
40
+ if (el.editable === false || el.hidden === true) {
41
+ style.visibility = "hidden";
42
+ }
43
+ if (el.lock === true) {
44
+ meta.lock = true;
45
+ }
46
+ const element = {
47
+ id: idGenerator(),
48
+ name: el.title,
49
+ style,
50
+ meta,
51
+ children: []
52
+ };
53
+ switch (el.type) {
54
+ case "text":
55
+ meta.inCanvasIs = "Element2D";
56
+ meta.inPptIs = "Shape";
57
+ element.text = {
58
+ content: el.content
59
+ };
60
+ break;
61
+ case "image":
62
+ meta.inCanvasIs = "Element2D";
63
+ meta.inPptIs = "Picture";
64
+ meta.lockAspectRatio = true;
65
+ element.foreground = {
66
+ image: el.url,
67
+ fillWithShape: true
68
+ };
69
+ break;
70
+ case "group":
71
+ meta.inCanvasIs = "Element2D";
72
+ meta.inPptIs = "GroupShape";
73
+ element.children.push(
74
+ ...await Promise.all(
75
+ elements.map(async (child) => {
76
+ try {
77
+ return await convertElement(child, el);
78
+ } catch (e) {
79
+ console.warn(e);
80
+ return void 0;
81
+ }
82
+ })
83
+ )
84
+ );
85
+ break;
86
+ case "path":
87
+ meta.inCanvasIs = "Element2D";
88
+ meta.inPptIs = "Picture";
89
+ meta.lockAspectRatio = true;
90
+ element.foreground = {
91
+ image: el.imageUrl,
92
+ fillWithShape: true
93
+ };
94
+ break;
95
+ }
96
+ return element;
97
+ }
98
+ async function convertDoc(doc) {
99
+ const { version: _version, elements } = doc;
100
+ return (await Promise.all(
101
+ elements.map(async (element) => {
102
+ try {
103
+ return await convertElement(element, void 0);
104
+ } catch (e) {
105
+ console.warn(e);
106
+ return void 0;
107
+ }
108
+ })
109
+ )).filter(Boolean);
110
+ }
111
+ function clipboardLoader() {
112
+ return {
113
+ name: "gaoding-clipboard",
114
+ test: (doc) => {
115
+ return doc instanceof Document && Boolean(doc.querySelector('span[data-app="editor-next"]'));
116
+ },
117
+ load: async (doc) => {
118
+ const encoded = doc.querySelector('span[data-app="editor-next"]');
119
+ if (encoded) {
120
+ const doc2 = JSON.parse(
121
+ decodeURIComponent(
122
+ new TextDecoder("utf-8", { fatal: false }).decode(
123
+ new Uint8Array(
124
+ atob(
125
+ encoded.getAttribute("data-clipboard")?.replace(/\s+/g, "") ?? ""
126
+ ).split("").map((c) => c.charCodeAt(0))
127
+ )
128
+ )
129
+ )
130
+ );
131
+ return await convertDoc(doc2);
132
+ }
133
+ return [];
134
+ }
135
+ };
136
+ }
2
137
  function plugin() {
3
138
  return definePlugin((_editor) => {
4
139
  return {
5
- name: "gaoding"
140
+ name: "gaoding",
141
+ loaders: [
142
+ clipboardLoader()
143
+ ]
6
144
  };
7
145
  });
8
146
  }
9
- function parseClipboard(dom) {
10
- const data = dom.querySelector('span[data-app="editor-next"]');
11
- if (data) {
12
- return decodeURIComponent(
13
- new TextDecoder("utf-8", { fatal: false }).decode(
14
- new Uint8Array(
15
- atob(
16
- data.getAttribute("data-clipboard")?.replace(/\s+/g, "") ?? ""
17
- ).split("").map((c) => c.charCodeAt(0))
18
- )
19
- )
20
- );
21
- }
22
- return void 0;
23
- }
24
147
  export {
148
+ clipboardLoader,
25
149
  plugin as default,
26
- parseClipboard,
27
150
  plugin
28
151
  };
@@ -0,0 +1 @@
1
+ export declare function clipboardLoader(): Mce.Loader;
@@ -0,0 +1 @@
1
+ export * from './clipboard';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mce/gaoding",
3
3
  "type": "module",
4
- "version": "0.17.3",
4
+ "version": "0.17.4",
5
5
  "description": "Plugin for mce",
6
6
  "author": "wxm",
7
7
  "license": "MIT",
@@ -45,7 +45,7 @@
45
45
  "dist"
46
46
  ],
47
47
  "devDependencies": {
48
- "mce": "0.17.3"
48
+ "mce": "0.17.4"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "mce": "^0"
@@ -1 +0,0 @@
1
- export declare function parseClipboard(dom: HTMLElement): string | undefined;