1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
/* eslint-disable no-underscore-dangle */
export class ContentEditor {
constructor({ tiptapEditor, serializer, deserializer, assetResolver, eventHub, drawioEnabled }) {
this._tiptapEditor = tiptapEditor;
this._serializer = serializer;
this._deserializer = deserializer;
this._eventHub = eventHub;
this._assetResolver = assetResolver;
this._pristineDoc = null;
this.drawioEnabled = drawioEnabled;
}
get tiptapEditor() {
return this._tiptapEditor;
}
get eventHub() {
return this._eventHub;
}
get changed() {
if (!this._pristineDoc) {
return !this.empty;
}
return !this._pristineDoc.eq(this.tiptapEditor.state.doc);
}
get empty() {
return this.tiptapEditor.isEmpty;
}
get editable() {
return this.tiptapEditor.isEditable;
}
dispose() {
this.tiptapEditor.destroy();
}
disposeAllEvents() {
this._eventHub.dispose();
}
deserialize(markdown) {
const { _tiptapEditor: editor, _deserializer: deserializer } = this;
return deserializer.deserialize({
schema: editor.schema,
markdown,
});
}
resolveUrl(canonicalSrc) {
return this._assetResolver.resolveUrl(canonicalSrc);
}
resolveReference(originalText) {
return this._assetResolver.resolveReference(originalText);
}
renderDiagram(code, language) {
return this._assetResolver.renderDiagram(code, language);
}
setEditable(editable = true) {
this._tiptapEditor.setOptions({
editable,
});
}
async setSerializedContent(serializedContent) {
const { _tiptapEditor: editor } = this;
const { doc, tr } = editor.state;
const { document } = await this.deserialize(serializedContent);
if (document) {
this._pristineDoc = document;
tr.replaceWith(0, doc.content.size, document).setMeta('preventUpdate', true);
editor.view.dispatch(tr);
}
}
getSerializedContent() {
const { _tiptapEditor: editor, _serializer: serializer, _pristineDoc: pristineDoc } = this;
const { doc } = editor.state;
return serializer.serialize({ doc, pristineDoc });
}
}
|