-
Notifications
You must be signed in to change notification settings - Fork 61
/
SimpleEditor.js
180 lines (159 loc) · 5.44 KB
/
SimpleEditor.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// @flow
import {baseKeymap} from 'prosemirror-commands';
import {dropCursor} from 'prosemirror-dropcursor';
import {gapCursor} from 'prosemirror-gapcursor';
import {history} from 'prosemirror-history';
import {keymap} from 'prosemirror-keymap';
import {Schema} from 'prosemirror-model';
import {EditorState} from 'prosemirror-state';
import {Transform} from 'prosemirror-transform';
import {EditorView} from 'prosemirror-view';
import React from 'react';
import DocNodeSpec from '../src//DocNodeSpec';
import * as EditorCommands from '../src/EditorCommands';
import HardBreakNodeSpec from '../src/HardBreakNodeSpec';
import LinkMarkSpec from '../src/LinkMarkSpec';
import LinkTooltipPlugin from '../src/LinkTooltipPlugin';
import * as MarkNames from '../src/MarkNames';
import * as NodeNames from '../src/NodeNames';
import ParagraphNodeSpec from '../src/ParagraphNodeSpec';
import SelectionPlaceholderPlugin from '../src/SelectionPlaceholderPlugin';
import StrongMarkSpec from '../src/StrongMarkSpec';
import TextNodeSpec from '../src/TextNodeSpec';
import buildInputRules from '../src/buildInputRules';
import convertFromHTML from '../src/convertFromHTML';
import convertFromJSON from '../src/convertFromJSON';
import createEditorKeyMap from '../src/createEditorKeyMap';
import createEmptyEditorState from '../src/createEmptyEditorState';
import CommandButton from '../src/ui/CommandButton';
import Editor from '../src/ui/Editor';
import EditorFrameset from '../src/ui/EditorFrameset';
import Icon from '../src/ui/Icon';
import './simple-editor.css';
// Schema Nodes that <SimpleEditor /> can use.
// See https://prosemirror.net/examples/schema/
const {DOC, HARD_BREAK, PARAGRAPH, TEXT} = NodeNames;
// Schema Marks that <SimpleEditor /> can use.
// See https://prosemirror.net/examples/schema/
const {MARK_LINK, MARK_STRONG} = MarkNames;
// Commands that <SimpleEditor /> can use.
const {HISTORY_REDO, HISTORY_UNDO, LINK_SET_URL, STRONG} = EditorCommands;
// Create the nodes mapping for schema. Please see `EditorNodes` for all the
// nodes available.
const NODES = {
[DOC]: DocNodeSpec,
[PARAGRAPH]: ParagraphNodeSpec,
[TEXT]: TextNodeSpec,
[HARD_BREAK]: HardBreakNodeSpec,
};
// Creates the marks mapping for schema. Please see `EditorMarks` for all the
// marks available.
const MARKS = {
[MARK_LINK]: LinkMarkSpec,
[MARK_STRONG]: StrongMarkSpec,
};
// Create the schema.
// See https://prosemirror.net/examples/schema/
const SCHEMA = new Schema({nodes: NODES, marks: MARKS});
// Define the plugins. Please see `EditorPlugins` for all the plugins
// available.
const PLUGINS = [
// Plugin to let user edit link's url inline.
new LinkTooltipPlugin(),
// Plugin to persist user's text selection visible when the is not focsued.
new SelectionPlaceholderPlugin(),
// Basic behaviors.
buildInputRules(SCHEMA),
dropCursor(),
gapCursor(),
history(),
// Basic keyboard shotcuts.
keymap(createEditorKeyMap()),
keymap(baseKeymap),
];
// This defines a simple editor that only supports plain-text, web link and bold
// style. You may use this as an example to customize editor with custom schema.
class SimpleEditor extends React.PureComponent<any, any, any> {
static convertFromJSON = (json: string | Object): EditorState => {
return convertFromJSON(json, SCHEMA, PLUGINS);
};
static convertFromHTML = (html: string): EditorState => {
return convertFromHTML(html, SCHEMA, PLUGINS);
};
static createEmptyEditorState = (): EditorState => {
return createEmptyEditorState(SCHEMA, PLUGINS);
};
state = {
editorView: null,
};
render(): React.Element<any> {
const {editorView} = this.state;
const {editorState} = this.props;
const toolbar = (
<div className="simple-editor-toolbar">
<CommandButton
command={HISTORY_UNDO}
dispatch={this._dispatchTransaction}
editorState={editorState}
editorView={editorView}
icon={Icon.get('undo')}
title="Undo"
/>
<CommandButton
command={HISTORY_REDO}
dispatch={this._dispatchTransaction}
editorState={editorState}
editorView={editorView}
icon={Icon.get('redo')}
title="Redo"
/>
<CommandButton
command={LINK_SET_URL}
dispatch={this._dispatchTransaction}
editorState={editorState}
editorView={editorView}
icon={Icon.get('link')}
title="link"
/>
<CommandButton
command={STRONG}
dispatch={this._dispatchTransaction}
editorState={editorState}
editorView={editorView}
icon={Icon.get('format_bold')}
title="link"
/>
</div>
);
const body = (
<Editor
dispatchTransaction={this._dispatchTransaction}
editorState={editorState}
onReady={this._onReady}
/>
);
return (
<EditorFrameset
body={body}
className="simple-editor"
height={400}
toolbar={toolbar}
width={500}
/>
);
}
_dispatchTransaction = (tr: Transform): void => {
const {onChange, editorState} = this.props;
if (onChange) {
const prevState = editorState || SimpleEditor.createEmptyEditorState();
const nextState = prevState.apply(tr);
onChange(nextState);
}
};
_onReady = (editorView: EditorView): void => {
if (editorView !== this.state.editorView) {
this.setState({editorView});
}
};
}
export default SimpleEditor;