/
Editor.tsx
156 lines (136 loc) · 5.08 KB
/
Editor.tsx
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
import * as React from 'react';
import BuildInPluginState, { UrlPlaceholder } from '../BuildInPluginState';
import SampleColorPickerPluginDataProvider from '../samplepicker/SampleColorPickerPluginDataProvider';
import { EditorInstanceToggleablePlugins } from './EditorInstanceToggleablePlugins';
import { ImageResize } from 'roosterjs-plugin-image-resize';
import { PickerPlugin } from 'roosterjs-plugin-picker';
import {
Editor as RoosterJsEditor,
EditorOptions,
EditorPlugin,
UndoService,
} from 'roosterjs-editor-core';
import {
HyperLink,
Paste,
ContentEdit,
Watermark,
TableResize,
ContentEditFeatures,
getDefaultContentEditFeatures,
CustomReplace as CustomReplacePlugin,
} from 'roosterjs-editor-plugins';
const styles = require('./Editor.scss');
const assign = require('object-assign');
export interface EditorProps {
plugins: EditorPlugin[];
initState: BuildInPluginState;
className?: string;
undo?: UndoService;
}
let editorInstance: RoosterJsEditor | null = null;
let editorInstanceToggleablePlugins: EditorInstanceToggleablePlugins | null = null;
export default class Editor extends React.Component<EditorProps, BuildInPluginState> {
private contentDiv: HTMLDivElement;
private editor: RoosterJsEditor;
constructor(props: EditorProps) {
super(props);
this.state = props.initState;
}
render() {
return (
<div className={this.props.className}>
<div className={styles.editor} ref={ref => (this.contentDiv = ref)} />
</div>
);
}
componentWillUpdate() {
this.disposeEditor();
}
componentDidUpdate() {
this.initEditor();
}
componentDidMount() {
this.initEditor();
if (editorInstance == null) {
editorInstance = this.editor;
}
}
componentWillUnmount() {
if (editorInstance == this.editor) {
editorInstance = null;
}
this.disposeEditor();
}
resetEditorPlugin(pluginState: BuildInPluginState) {
this.setState(pluginState);
}
private initEditor() {
let pluginList = this.state.pluginList;
editorInstanceToggleablePlugins = {
hyperlink: pluginList.hyperlink ? new HyperLink(this.getLinkCallback()) : null,
paste: pluginList.paste ? new Paste() : null,
contentEdit: pluginList.contentEdit
? new ContentEdit(this.getContentEditOptions())
: null,
watermark: pluginList.watermark ? new Watermark(this.state.watermarkText) : null,
imageResize: pluginList.imageResize ? new ImageResize() : null,
tableResize: pluginList.tableResize ? new TableResize() : null,
pickerPlugin: pluginList.pickerPlugin
? new PickerPlugin(new SampleColorPickerPluginDataProvider(), {
elementIdPrefix: 'samplepicker-',
changeSource: 'SAMPLE_COLOR_PICKER',
triggerCharacter: ':',
isHorizontal: true,
})
: null,
customReplace: pluginList.customReplace ? new CustomReplacePlugin() : null,
};
let plugins = [
...Object.keys(editorInstanceToggleablePlugins).map(
k => (editorInstanceToggleablePlugins as any)[k]
),
...this.props.plugins,
];
let defaultFormat = { ...this.state.defaultFormat };
let options: EditorOptions = {
plugins: plugins,
defaultFormat: defaultFormat,
undo: this.props.undo,
};
this.editor = new RoosterJsEditor(this.contentDiv, options);
}
private disposeEditor() {
this.editor.dispose();
this.editor = null;
}
private getLinkCallback(): (url: string) => string {
let linkCallback: (url: string) => string;
let linkTitle = this.state.linkTitle;
if (linkTitle) {
let index = linkTitle.indexOf(UrlPlaceholder);
if (index >= 0) {
let left = linkTitle.substr(0, index);
let right = linkTitle.substr(index + UrlPlaceholder.length);
linkCallback = url => left + url + right;
} else {
linkCallback = () => linkTitle;
}
} else {
linkCallback = null;
}
return linkCallback;
}
private getContentEditOptions(): ContentEditFeatures {
let defaultFeatures = getDefaultContentEditFeatures();
return assign(defaultFeatures, this.state.contentEditFeatures);
}
}
// expose the active editor the global window for integration tests
Object.defineProperty(window, 'globalRoosterEditor', {
get: () => editorInstance,
});
// expose to the global window for integration tests
Object.defineProperty(window, 'globalRoosterEditorNamedPlugins', {
get: () => editorInstanceToggleablePlugins,
});