@kerebron/extension-odt 0.4.23 → 0.4.24
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/esm/ExtensionOdt.d.ts.map +1 -1
- package/esm/ExtensionOdt.js +71 -31
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExtensionOdt.d.ts","sourceRoot":"","sources":["../src/ExtensionOdt.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAQ,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,EACL,KAAK,SAAS,EACd,KAAK,UAAU,EACf,SAAS,EACT,KAAK,WAAW,EACjB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EAAa,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAE5D,OAAO,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"ExtensionOdt.d.ts","sourceRoot":"","sources":["../src/ExtensionOdt.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAQ,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,EACL,KAAK,SAAS,EACd,KAAK,UAAU,EACf,SAAS,EACT,KAAK,WAAW,EACjB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EAAa,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAE5D,OAAO,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AAGpD,MAAM,WAAW,SAAU,SAAQ,eAAe;IAChD,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,mBAAmB,CAAC,EAAE,OAAO,EAAE,CAAC;CACjC;AAED,qBAAa,YAAa,SAAQ,SAAS;IAIb,MAAM,EAAE,SAAS;IAH7C,IAAI,SAAS;IACN,eAAe,CAAC,EAAE,WAAW,CAAC;gBAET,MAAM,GAAE,SAAc;IAIzC,aAAa,CACpB,MAAM,EAAE,UAAU,EAClB,MAAM,EAAE,MAAM,GACb,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC;CAoK7B"}
|
package/esm/ExtensionOdt.js
CHANGED
|
@@ -2,6 +2,7 @@ import { Extension, } from '@kerebron/editor';
|
|
|
2
2
|
import { parse_content, parse_styles, unzip } from '@kerebron/odt-wasm';
|
|
3
3
|
import { OdtParser } from './OdtParser.js';
|
|
4
4
|
import { getDefaultsPostProcessFilters } from './postprocess/postProcess.js';
|
|
5
|
+
import { InputRulesPlugin } from '@kerebron/editor/plugins/input-rules';
|
|
5
6
|
export class ExtensionOdt extends Extension {
|
|
6
7
|
config;
|
|
7
8
|
name = 'odt';
|
|
@@ -17,22 +18,88 @@ export class ExtensionOdt extends Extension {
|
|
|
17
18
|
throw new Error('Not implemented');
|
|
18
19
|
},
|
|
19
20
|
toDoc: async (buffer) => {
|
|
20
|
-
const { doc, filesMap } =
|
|
21
|
+
const { doc, filesMap } = odtConverter.odtToJson(buffer);
|
|
21
22
|
const filterCommands = getDefaultsPostProcessFilters({
|
|
22
23
|
doc,
|
|
23
24
|
filesMap,
|
|
24
25
|
}).concat(this.config.postProcessCommands || []);
|
|
26
|
+
const plugin = editor.state.plugins.find((plugin) => plugin instanceof InputRulesPlugin);
|
|
27
|
+
if (plugin) {
|
|
28
|
+
// plugin.props.
|
|
29
|
+
}
|
|
30
|
+
const subEditor = editor.clone();
|
|
31
|
+
subEditor.setDocument(doc.toJSON());
|
|
32
|
+
let modified = false;
|
|
33
|
+
if (this.urlFromRewriter) {
|
|
34
|
+
const imageNodes = [];
|
|
35
|
+
subEditor.getDocument().descendants((node, pos) => {
|
|
36
|
+
if (node.type.name === 'image') {
|
|
37
|
+
imageNodes.push({ node, pos });
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
const linkNodes = [];
|
|
41
|
+
subEditor.getDocument().descendants((node, pos) => {
|
|
42
|
+
if (node.marks.find((mark) => mark.type.name === 'link')) {
|
|
43
|
+
linkNodes.push({ node, pos });
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
const tr = subEditor.state.tr;
|
|
47
|
+
for (const { node, pos } of linkNodes) {
|
|
48
|
+
const linkMark = node.marks.find((mark) => mark.type.name === 'link');
|
|
49
|
+
if (!linkMark) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
let href = linkMark.attrs.href || '';
|
|
53
|
+
href = await this.urlFromRewriter(href, {
|
|
54
|
+
type: 'A',
|
|
55
|
+
dest: 'kerebron',
|
|
56
|
+
});
|
|
57
|
+
if (href !== linkMark.attrs.href) {
|
|
58
|
+
const newMarks = node.marks.map((mark) => {
|
|
59
|
+
if (mark.type.name === 'link') {
|
|
60
|
+
const markType = this.editor.schema.marks['link'];
|
|
61
|
+
return markType.create({ ...mark.attrs, href });
|
|
62
|
+
}
|
|
63
|
+
return mark;
|
|
64
|
+
});
|
|
65
|
+
const nodeType = this.editor.schema.nodes[node.type.name];
|
|
66
|
+
let replaceNode;
|
|
67
|
+
if (nodeType.isText) {
|
|
68
|
+
replaceNode = this.editor.schema.text(node.text || '', newMarks);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
replaceNode = nodeType.create(node.attrs, node.content, newMarks);
|
|
72
|
+
}
|
|
73
|
+
tr.replaceWith(tr.mapping.map(pos), tr.mapping.map(pos + node.nodeSize), replaceNode);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
for (const { node, pos } of imageNodes) {
|
|
77
|
+
let src = node.attrs.src || '';
|
|
78
|
+
src = await this.urlFromRewriter(src, {
|
|
79
|
+
type: 'IMG',
|
|
80
|
+
dest: 'kerebron',
|
|
81
|
+
filesMap,
|
|
82
|
+
});
|
|
83
|
+
if (src !== node.attrs.src) {
|
|
84
|
+
const nodeType = this.editor.schema.nodes[node.type.name];
|
|
85
|
+
const replaceNode = nodeType.create({ ...node.attrs, src }, node.content, node.marks);
|
|
86
|
+
tr.replaceWith(tr.mapping.map(pos), tr.mapping.map(pos + node.nodeSize), replaceNode);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
subEditor.dispatchTransaction(tr), modified = true;
|
|
90
|
+
}
|
|
25
91
|
if (filterCommands.length > 0) {
|
|
26
|
-
const subEditor = editor.clone();
|
|
27
|
-
subEditor.setDocument(doc.toJSON());
|
|
28
92
|
for (const filter of filterCommands) {
|
|
29
93
|
filter(subEditor.state, (tr) => subEditor.dispatchTransaction(tr));
|
|
30
94
|
}
|
|
95
|
+
modified = true;
|
|
96
|
+
}
|
|
97
|
+
if (modified) {
|
|
31
98
|
return subEditor.getDocument();
|
|
32
99
|
}
|
|
33
100
|
return doc;
|
|
34
101
|
},
|
|
35
|
-
odtToJson:
|
|
102
|
+
odtToJson: (buffer) => {
|
|
36
103
|
const files = unzip(buffer);
|
|
37
104
|
const filesMap = {};
|
|
38
105
|
for (const k of files.keys()) {
|
|
@@ -53,33 +120,6 @@ export class ExtensionOdt extends Extension {
|
|
|
53
120
|
const parser = new OdtParser(editor.schema, this.config);
|
|
54
121
|
parser.filesMap = filesMap;
|
|
55
122
|
const doc = parser.parse({ ...filesMap, contentTree, stylesTree });
|
|
56
|
-
if (this.urlFromRewriter) {
|
|
57
|
-
const nodesToProcess = [];
|
|
58
|
-
doc.descendants((node, pos) => {
|
|
59
|
-
if (node.type.name === 'image') {
|
|
60
|
-
nodesToProcess.push({ node, pos });
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
// TODO
|
|
64
|
-
// if (ctx.urlRewriter) {
|
|
65
|
-
// href = ctx.urlRewriter(href, { type: 'A', dest: 'kerebron' });
|
|
66
|
-
// }
|
|
67
|
-
const tr = this.editor.state.tr;
|
|
68
|
-
for (const { node, pos } of nodesToProcess) {
|
|
69
|
-
let src = node.attrs.src || '';
|
|
70
|
-
src = await this.urlFromRewriter(src, {
|
|
71
|
-
type: 'IMG',
|
|
72
|
-
dest: 'kerebron',
|
|
73
|
-
filesMap,
|
|
74
|
-
});
|
|
75
|
-
if (src !== node.attrs.src) {
|
|
76
|
-
const nodeType = this.editor.schema.nodes[node.type.name];
|
|
77
|
-
const replaceNode = nodeType.create({ ...node.attrs, src }, node.content, node.marks);
|
|
78
|
-
tr.replaceWith(tr.mapping.map(pos), tr.mapping.map(pos + node.nodeSize), replaceNode);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
editor.view.dispatch(tr);
|
|
82
|
-
}
|
|
83
123
|
return { doc, stylesTree, contentTree, filesMap };
|
|
84
124
|
},
|
|
85
125
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kerebron/extension-odt",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.24",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"module": "./esm/ExtensionOdt.js",
|
|
6
6
|
"exports": {
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
},
|
|
11
11
|
"scripts": {},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@kerebron/editor": "0.4.
|
|
14
|
-
"@kerebron/odt-wasm": "0.4.
|
|
13
|
+
"@kerebron/editor": "0.4.24",
|
|
14
|
+
"@kerebron/odt-wasm": "0.4.24",
|
|
15
15
|
"prosemirror-model": "1.25.3",
|
|
16
16
|
"prosemirror-state": "1.4.3"
|
|
17
17
|
},
|