@kerebron/extension-odt 0.0.8

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 (49) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +57 -0
  3. package/esm/editor/src/CoreEditor.d.ts +28 -0
  4. package/esm/editor/src/CoreEditor.d.ts.map +1 -0
  5. package/esm/editor/src/CoreEditor.js +170 -0
  6. package/esm/editor/src/Extension.d.ts +26 -0
  7. package/esm/editor/src/Extension.d.ts.map +1 -0
  8. package/esm/editor/src/Extension.js +33 -0
  9. package/esm/editor/src/ExtensionManager.d.ts +32 -0
  10. package/esm/editor/src/ExtensionManager.d.ts.map +1 -0
  11. package/esm/editor/src/ExtensionManager.js +253 -0
  12. package/esm/editor/src/Mark.d.ts +18 -0
  13. package/esm/editor/src/Mark.d.ts.map +1 -0
  14. package/esm/editor/src/Mark.js +34 -0
  15. package/esm/editor/src/Node.d.ts +27 -0
  16. package/esm/editor/src/Node.d.ts.map +1 -0
  17. package/esm/editor/src/Node.js +43 -0
  18. package/esm/editor/src/commands/CommandManager.d.ts +20 -0
  19. package/esm/editor/src/commands/CommandManager.d.ts.map +1 -0
  20. package/esm/editor/src/commands/CommandManager.js +60 -0
  21. package/esm/editor/src/commands/createChainableState.d.ts +3 -0
  22. package/esm/editor/src/commands/createChainableState.d.ts.map +1 -0
  23. package/esm/editor/src/commands/createChainableState.js +29 -0
  24. package/esm/editor/src/commands/mod.d.ts +49 -0
  25. package/esm/editor/src/commands/mod.d.ts.map +1 -0
  26. package/esm/editor/src/commands/mod.js +928 -0
  27. package/esm/editor/src/mod.d.ts +5 -0
  28. package/esm/editor/src/mod.d.ts.map +1 -0
  29. package/esm/editor/src/mod.js +4 -0
  30. package/esm/editor/src/plugins/input-rules/InputRulesPlugin.d.ts +23 -0
  31. package/esm/editor/src/plugins/input-rules/InputRulesPlugin.d.ts.map +1 -0
  32. package/esm/editor/src/plugins/input-rules/InputRulesPlugin.js +163 -0
  33. package/esm/editor/src/types.d.ts +29 -0
  34. package/esm/editor/src/types.d.ts.map +1 -0
  35. package/esm/editor/src/types.js +1 -0
  36. package/esm/editor/src/utilities/createNodeFromContent.d.ts +12 -0
  37. package/esm/editor/src/utilities/createNodeFromContent.d.ts.map +1 -0
  38. package/esm/editor/src/utilities/createNodeFromContent.js +118 -0
  39. package/esm/editor/src/utilities/getHtmlAttributes.d.ts +4 -0
  40. package/esm/editor/src/utilities/getHtmlAttributes.d.ts.map +1 -0
  41. package/esm/editor/src/utilities/getHtmlAttributes.js +47 -0
  42. package/esm/extension-odt/src/ExtensionOdt.d.ts +7 -0
  43. package/esm/extension-odt/src/ExtensionOdt.d.ts.map +1 -0
  44. package/esm/extension-odt/src/ExtensionOdt.js +32 -0
  45. package/esm/extension-odt/src/OdtParser.d.ts +23 -0
  46. package/esm/extension-odt/src/OdtParser.d.ts.map +1 -0
  47. package/esm/extension-odt/src/OdtParser.js +315 -0
  48. package/esm/package.json +3 -0
  49. package/package.json +23 -0
@@ -0,0 +1,315 @@
1
+ import { Mark, } from 'prosemirror-model';
2
+ function attrs(spec, token, style) {
3
+ if (spec.getAttrs)
4
+ return spec.getAttrs(token, style);
5
+ // For backwards compatibility when `attrs` is a Function
6
+ else if (spec.attrs instanceof Function)
7
+ return spec.attrs(token);
8
+ else
9
+ return spec.attrs;
10
+ }
11
+ function resolveStyle(stylesTree, automaticStyles, name) {
12
+ let style;
13
+ if (!style) {
14
+ style = stylesTree.styles['list-style'].find((item) => item['@name'] === name);
15
+ }
16
+ if (!style) {
17
+ style = stylesTree.styles['style'].find((item) => item['@name'] === name);
18
+ }
19
+ if (!style) {
20
+ style = automaticStyles.style.find((item) => item['@name'] === name);
21
+ }
22
+ if (!style) {
23
+ style = {};
24
+ }
25
+ style['styles'] = [name];
26
+ if (style['@parent-style-name']) {
27
+ const parenStyle = resolveStyle(stylesTree, automaticStyles, style['@parent-style-name']);
28
+ if (parenStyle) {
29
+ const styles = [...style['styles'], ...parenStyle['styles']];
30
+ style = {
31
+ ...parenStyle,
32
+ ...style,
33
+ styles,
34
+ };
35
+ }
36
+ }
37
+ return style;
38
+ }
39
+ class OdtParseState {
40
+ constructor(schema, stylesTree, automaticStyles) {
41
+ Object.defineProperty(this, "schema", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: schema
46
+ });
47
+ Object.defineProperty(this, "stylesTree", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: stylesTree
52
+ });
53
+ Object.defineProperty(this, "automaticStyles", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: automaticStyles
58
+ });
59
+ Object.defineProperty(this, "stack", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: void 0
64
+ });
65
+ this.stack = [{
66
+ type: schema.topNodeType,
67
+ attrs: null,
68
+ content: [],
69
+ marks: Mark.none,
70
+ }];
71
+ }
72
+ top() {
73
+ return this.stack[this.stack.length - 1];
74
+ }
75
+ push(elt) {
76
+ if (this.stack.length)
77
+ this.top().content.push(elt);
78
+ }
79
+ // Adds the given text to the current position in the document,
80
+ // using the current marks as styling.
81
+ addText(text) {
82
+ if (!text)
83
+ return;
84
+ let top = this.top(), nodes = top.content, last = nodes[nodes.length - 1];
85
+ let node = this.schema.text(text, top.marks), merged;
86
+ // if (last && (merged = maybeMerge(last, node))) {
87
+ // nodes[nodes.length - 1] = merged;
88
+ // } else
89
+ // console.log('addtext', node);
90
+ nodes.push(node);
91
+ }
92
+ // Adds the given mark to the set of active marks.
93
+ openMark(mark) {
94
+ let top = this.top();
95
+ top.marks = mark.addToSet(top.marks);
96
+ }
97
+ // Removes the given mark from the set of active marks.
98
+ closeMark(mark) {
99
+ let top = this.top();
100
+ top.marks = mark.removeFromSet(top.marks);
101
+ }
102
+ // Add a node at the current position.
103
+ addNode(type, attrs, content) {
104
+ let top = this.top();
105
+ let node = type.createAndFill(attrs, content, top ? top.marks : []);
106
+ if (!node)
107
+ return null;
108
+ this.push(node);
109
+ return node;
110
+ }
111
+ // Wrap subsequent content in a node of the given type.
112
+ openNode(type, attrs) {
113
+ this.stack.push({
114
+ type: type,
115
+ attrs: attrs,
116
+ content: [],
117
+ marks: Mark.none,
118
+ });
119
+ }
120
+ // Close and return the node that is currently on top of the stack.
121
+ closeNode() {
122
+ let info = this.stack.pop();
123
+ // console.log('closeNode', info.type.name, info.attrs);
124
+ return this.addNode(info.type, info.attrs, info.content);
125
+ }
126
+ handleElement(nodeType, element) {
127
+ const spec = tokens[nodeType];
128
+ if (!spec) {
129
+ console.warn('No spec for:', nodeType, element, this.stack.map((item) => item.type.name));
130
+ return;
131
+ }
132
+ // console.log('handleElement', nodeType, this.stack.length);
133
+ const children = spec.children ? spec.children(element) : [];
134
+ // console.log('ccc', nodeType, children);
135
+ let style;
136
+ if ('object' === typeof element && element['@style-name']) {
137
+ style = resolveStyle(this.stylesTree, this.automaticStyles, element['@style-name']);
138
+ }
139
+ if (spec.block) {
140
+ let block = spec.block;
141
+ if ('string' !== typeof block) {
142
+ block = block(element, style);
143
+ }
144
+ let nodeType = this.schema.nodeType(block);
145
+ this.openNode(nodeType, attrs(spec, element, style));
146
+ if (children) {
147
+ iterateChildren(children, (nodeType, node) => {
148
+ this.handleElement(nodeType, node);
149
+ });
150
+ }
151
+ // this.addText(withoutTrailingNewline(tok.content));
152
+ this.closeNode();
153
+ }
154
+ else if (spec.node) {
155
+ }
156
+ else if (spec.mark) {
157
+ let markType = this.schema.marks[spec.mark];
158
+ this.openMark(markType.create(attrs(spec, element, style)));
159
+ if (children) {
160
+ iterateChildren(children, (nodeType, node) => {
161
+ this.handleElement(nodeType, node);
162
+ });
163
+ }
164
+ this.closeMark(markType);
165
+ }
166
+ else if (spec.text) {
167
+ // console.log('aaaaaaaaaaaaaaaaaaaa', element);
168
+ this.addText(spec.text(element));
169
+ }
170
+ else {
171
+ if (children) {
172
+ iterateChildren(children, (nodeType, node) => {
173
+ this.handleElement(nodeType, node);
174
+ });
175
+ }
176
+ }
177
+ }
178
+ }
179
+ function iterateChildren(nodes, callback) {
180
+ for (const child of nodes) {
181
+ if ('Unknown' === child) {
182
+ continue;
183
+ }
184
+ let key = '';
185
+ let value = null;
186
+ if (typeof child === 'string') {
187
+ key = '$text';
188
+ value = child;
189
+ }
190
+ else {
191
+ const keys = Object.keys(child);
192
+ key = keys[0];
193
+ value = child[key];
194
+ }
195
+ if (!key) {
196
+ return;
197
+ }
198
+ callback(key, value);
199
+ }
200
+ }
201
+ function iterateEnum($value) {
202
+ if (!$value) {
203
+ return [];
204
+ }
205
+ return $value.map((item) => {
206
+ if ('string' === typeof item) {
207
+ return {
208
+ [item]: true,
209
+ };
210
+ }
211
+ return item;
212
+ });
213
+ }
214
+ const tokens = {
215
+ 'body': {
216
+ children: (odtElement) => iterateEnum(odtElement.text?.$value),
217
+ },
218
+ 'p': {
219
+ block: (odtElement, style) => {
220
+ if (style.styles.find((item) => item.startsWith('Heading_20_'))) {
221
+ return 'heading';
222
+ }
223
+ return 'paragraph';
224
+ },
225
+ getAttrs: (odtElement, style) => {
226
+ const heading = style.styles.find((item) => item.startsWith('Heading_20_'));
227
+ if (heading) {
228
+ return {
229
+ level: parseInt(heading.substring('Heading_20_'.length)),
230
+ };
231
+ }
232
+ },
233
+ children: (odtElement) => iterateEnum(odtElement.$value),
234
+ },
235
+ 'span': {
236
+ children: (odtElement) => iterateEnum(odtElement.$value),
237
+ },
238
+ 'list': {
239
+ block: 'ordered_list',
240
+ children: (odtElement) => odtElement['list-item'].map((item) => ({ 'list-item': item })),
241
+ },
242
+ 'list-item': {
243
+ block: 'list_item',
244
+ children: (odtElement) => iterateEnum(odtElement.$value),
245
+ },
246
+ 'table': {
247
+ block: 'table',
248
+ children: (odtElement) => odtElement['table-row'].map((item) => ({ 'table-row': item })),
249
+ },
250
+ 'table-row': {
251
+ block: 'table_row',
252
+ children: (odtElement) => odtElement['table-cell'].map((item) => ({ 'table-cell': item })),
253
+ },
254
+ 'table-cell': {
255
+ block: 'table_cell',
256
+ children: (odtElement) => iterateEnum(odtElement.$value),
257
+ },
258
+ 'a': {
259
+ mark: 'link',
260
+ getAttrs: (tok) => ({
261
+ href: tok['@href'],
262
+ // title: tok.attrGet('title') || null,
263
+ }),
264
+ children: (odtElement) => odtElement['span'],
265
+ },
266
+ '$value': {
267
+ children: (odtElement) => iterateEnum(odtElement),
268
+ },
269
+ '$text': {
270
+ text: (odtElement) => String(odtElement || ''),
271
+ },
272
+ 's': {
273
+ text: (odtElement) => {
274
+ const chars = odtElement['@c'] || 1;
275
+ return ' '.substring(0, chars);
276
+ },
277
+ },
278
+ 'tab': {
279
+ text: (odtElement) => '\t',
280
+ },
281
+ 'table-of-content': {
282
+ children: (odtElement) => odtElement['index-body']['p'] || [],
283
+ },
284
+ 'frame': {
285
+ ignore: true,
286
+ },
287
+ 'rect': {
288
+ ignore: true,
289
+ },
290
+ 'annotation': {
291
+ ignore: true,
292
+ },
293
+ };
294
+ export class OdtParser {
295
+ constructor(schema) {
296
+ Object.defineProperty(this, "schema", {
297
+ enumerable: true,
298
+ configurable: true,
299
+ writable: true,
300
+ value: schema
301
+ });
302
+ // this.tokenHandlers = tokenHandlers(schema, tokens);
303
+ }
304
+ parse(files) {
305
+ const contentTree = files.contentTree;
306
+ const stylesTree = files.stylesTree;
307
+ const state = new OdtParseState(this.schema, stylesTree, contentTree['automatic-styles']);
308
+ state.handleElement('body', contentTree.body);
309
+ let doc;
310
+ do {
311
+ doc = state.closeNode();
312
+ } while (state.stack.length);
313
+ return doc || null; //this.schema.topNodeType.createAndFill()!;
314
+ }
315
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@kerebron/extension-odt",
3
+ "version": "0.0.8",
4
+ "license": "MIT",
5
+ "module": "./esm/extension-odt/src/ExtensionOdt.js",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./esm/extension-odt/src/ExtensionOdt.js"
9
+ }
10
+ },
11
+ "scripts": {},
12
+ "dependencies": {
13
+ "prosemirror-keymap": "1.2.2",
14
+ "prosemirror-model": "1.24.1",
15
+ "prosemirror-state": "1.4.3",
16
+ "prosemirror-transform": "1.10.3",
17
+ "prosemirror-view": "1.33.6"
18
+ },
19
+ "devDependencies": {
20
+ "@types/node": "^20.9.0"
21
+ },
22
+ "_generatedBy": "dnt@dev"
23
+ }