-
Notifications
You must be signed in to change notification settings - Fork 49
/
editor.ts
114 lines (104 loc) · 3.89 KB
/
editor.ts
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { history } from "prosemirror-history";
import { Node as ProseMirrorNode } from "prosemirror-model";
import { EditorState } from "prosemirror-state";
import { EditorView } from "prosemirror-view";
import { IExternalPluginProvider } from "../shared/editor-plugin";
import { CodeBlockHighlightPlugin } from "../shared/highlighting/highlight-plugin";
import { log } from "../shared/logger";
import { createMenuPlugin } from "../shared/menu";
import { commonmarkCodePasteHandler } from "../shared/prosemirror-plugins/code-paste-handler";
import {
commonmarkImageUpload,
defaultImageUploadHandler,
} from "../shared/prosemirror-plugins/image-upload";
import { interfaceManagerPlugin } from "../shared/prosemirror-plugins/interface-manager";
import { placeholderPlugin } from "../shared/prosemirror-plugins/placeholder";
import {
editableCheck,
readonlyPlugin,
} from "../shared/prosemirror-plugins/readonly";
import { CodeStringParser } from "../shared/schema";
import { deepMerge } from "../shared/utils";
import {
BaseView,
CommonViewOptions,
defaultParserFeatures,
EditorType,
} from "../shared/view";
import { createMenuEntries } from "./commands";
import { allKeymaps } from "./key-bindings";
import { commonmarkSchema } from "./schema";
export type CommonmarkOptions = CommonViewOptions;
export class CommonmarkEditor extends BaseView {
private options: CommonmarkOptions;
constructor(
target: Node,
content: string,
pluginProvider: IExternalPluginProvider,
options: CommonmarkOptions = {}
) {
super();
this.options = deepMerge(CommonmarkEditor.defaultOptions, options);
const menuEntries = pluginProvider.getFinalizedMenu(
createMenuEntries(this.options),
EditorType.Commonmark,
commonmarkSchema
);
const menu = createMenuPlugin(
menuEntries,
this.options.menuParentContainer
);
this.editorView = new EditorView(
(node: HTMLElement) => {
node.classList.add(...(this.options.classList || []));
target.appendChild(node);
},
{
editable: editableCheck,
state: EditorState.create({
doc: this.parseContent(content),
plugins: [
history(),
...allKeymaps(this.options.parserFeatures),
menu,
CodeBlockHighlightPlugin(null),
interfaceManagerPlugin(
this.options.pluginParentContainer
),
commonmarkImageUpload(
this.options.imageUpload,
this.options.parserFeatures.validateLink
),
placeholderPlugin(this.options.placeholderText),
readonlyPlugin(),
commonmarkCodePasteHandler,
...pluginProvider.plugins.commonmark,
],
}),
plugins: [],
}
);
log(
"prosemirror commonmark document",
this.editorView.state.doc.toJSON()
);
}
static get defaultOptions(): CommonmarkOptions {
return {
// set to null to disable by default
editorHelpLink: null,
menuParentContainer: null,
parserFeatures: defaultParserFeatures,
placeholderText: null,
imageUpload: {
handler: defaultImageUploadHandler,
},
};
}
parseContent(content: string): ProseMirrorNode {
return CodeStringParser.fromSchema(commonmarkSchema).parseCode(content);
}
serializeContent(): string {
return CodeStringParser.toString(this.editorView.state.doc);
}
}