-
Notifications
You must be signed in to change notification settings - Fork 156
/
initOptions.ts
158 lines (139 loc) · 6.8 KB
/
initOptions.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
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
import ShowCursorPosition from './plugins/ShowCursorPosition';
import ShowFromState from './plugins/ShowFormatState';
import { DefaultFormat } from 'roosterjs-editor-types';
import {
HyperLink,
Paste,
ContentEdit,
Watermark,
TableResize,
getDefaultContentEditFeatures,
} from 'roosterjs-editor-plugins';
import { ImageResize } from 'roosterjs-plugin-image-resize';
import { Editor, EditorPlugin, EditorOptions } from 'roosterjs-editor-core';
import { setCurrentEditor } from './currentEditor';
import getCurrentEditor from './currentEditor';
import updateSampleCode from './updateSampleCode';
function initOptions() {
document.getElementById('showHtmlContent').addEventListener('click', () => {
window.alert(getCurrentEditor().getContent(true));
});
document.getElementById('showTextContent').addEventListener('click', () => {
let text = getCurrentEditor().getTextContent();
window.alert(text);
});
document.getElementById('takeSnapshot').addEventListener('click', () => {
let snapshot = getCurrentEditor().getContent(false, true);
(document.getElementById('snapshotTextarea') as HTMLTextAreaElement).value = snapshot;
});
document.getElementById('resumeSnapshot').addEventListener('click', () => {
let snapshot = (document.getElementById('snapshotTextarea') as HTMLTextAreaElement).value;
let editor = getCurrentEditor();
editor.focus();
editor.setContent(snapshot, false);
});
[
'hyperlinkCheckbox',
'pasteCheckbox',
'contentEditCheckbox',
'watermarkCheckbox',
'imageResizeCheckbox',
'tableResizeCheckbox',
'boldCheckbox',
'italicCheckbox',
'underlineCheckbox',
'textColorDefaultFormat',
'fontNameDefaultFormat',
'autoLinkCheckbox',
'indentWhenTabCheckbox',
'outdentWhenShiftTabCheckbox',
'outdentWhenBackspaceOnEmptyFirstLineCheckbox',
'outdentWhenEnterOnEmptyLineCheckbox',
'mergeInNewLineWhenBackspaceOnFirstCharCheckbox',
'unquoteWhenBackspaceOnEmptyFirstLineCheckbox',
'unquoteWhenEnterOnEmptyLineCheckbox',
'autoBulletCheckbox',
'tabInTableCheckbox',
'upDownInTableCheckbox',
'unlinkWhenBackspaceAfterLinkCheckbox',
'defaultShortcutCheckbox',
'smartOrderedListCheckbox',
].forEach(id => {
document.getElementById(id).addEventListener('change', initEditorForOptions);
});
}
export function initEditorForOptions() {
setCurrentEditor(null);
let plugins: EditorPlugin[] = [];
if ((document.getElementById('hyperlinkCheckbox') as HTMLInputElement).checked) {
plugins.push(new HyperLink());
}
if ((document.getElementById('pasteCheckbox') as HTMLInputElement).checked) {
plugins.push(new Paste());
}
let features = getDefaultContentEditFeatures();
let featuresChanged = false;
if ((document.getElementById('contentEditCheckbox') as HTMLInputElement).checked) {
features.autoLink = (document.getElementById('autoLinkCheckbox') as HTMLInputElement).checked;
features.indentWhenTab = (document.getElementById('indentWhenTabCheckbox') as HTMLInputElement).checked;
features.outdentWhenShiftTab = (document.getElementById('outdentWhenShiftTabCheckbox') as HTMLInputElement).checked;
features.outdentWhenBackspaceOnEmptyFirstLine = (document.getElementById('outdentWhenBackspaceOnEmptyFirstLineCheckbox') as HTMLInputElement).checked;
features.outdentWhenEnterOnEmptyLine = (document.getElementById('outdentWhenEnterOnEmptyLineCheckbox') as HTMLInputElement).checked;
features.mergeInNewLineWhenBackspaceOnFirstChar = (document.getElementById('mergeInNewLineWhenBackspaceOnFirstCharCheckbox') as HTMLInputElement).checked;
features.unquoteWhenBackspaceOnEmptyFirstLine = (document.getElementById('unquoteWhenBackspaceOnEmptyFirstLineCheckbox') as HTMLInputElement).checked;
features.unquoteWhenEnterOnEmptyLine = (document.getElementById('unquoteWhenEnterOnEmptyLineCheckbox') as HTMLInputElement).checked;
features.autoBullet = (document.getElementById('autoBulletCheckbox') as HTMLInputElement).checked;
features.tabInTable = (document.getElementById('tabInTableCheckbox') as HTMLInputElement).checked;
features.upDownInTable = (document.getElementById('upDownInTableCheckbox') as HTMLInputElement).checked;
features.unlinkWhenBackspaceAfterLink = (document.getElementById('unlinkWhenBackspaceAfterLinkCheckbox') as HTMLInputElement).checked;
features.defaultShortcut = (document.getElementById('defaultShortcutCheckbox') as HTMLInputElement).checked;
features.smartOrderedList = (document.getElementById('smartOrderedListCheckbox') as HTMLInputElement).checked;
plugins.push(new ContentEdit(features));
let defaultFeatures = getDefaultContentEditFeatures();
let keys = Object.keys(defaultFeatures);
for (let key of keys) {
if (key != 'smartOrderedListStyles' && features[key] != defaultFeatures[key]) {
featuresChanged = true;
break;
}
}
}
if ((document.getElementById('watermarkCheckbox') as HTMLInputElement).checked) {
plugins.push(new Watermark('Type content here...'));
}
if ((document.getElementById('imageResizeCheckbox') as HTMLInputElement).checked) {
plugins.push(new ImageResize());
}
if ((document.getElementById('tableResizeCheckbox') as HTMLInputElement).checked) {
plugins.push(new TableResize());
}
plugins.push(new ShowCursorPosition(document.getElementById('cursorPosition')));
plugins.push(new ShowFromState(document.getElementById('formatState')));
let defaultFormat: DefaultFormat = {};
if ((document.getElementById('boldCheckbox') as HTMLInputElement).checked) {
defaultFormat.bold = true;
}
if ((document.getElementById('italicCheckbox') as HTMLInputElement).checked) {
defaultFormat.italic = true;
}
if ((document.getElementById('underlineCheckbox') as HTMLInputElement).checked) {
defaultFormat.underline = true;
}
const textColor = (document.getElementById('textColorDefaultFormat') as HTMLInputElement).value;
if (textColor) {
defaultFormat.textColor = textColor;
}
const fontFamily = (document.getElementById('fontNameDefaultFormat') as HTMLInputElement).value;
if (fontFamily) {
defaultFormat.fontFamily = fontFamily;
}
let editorOptions: EditorOptions = {
plugins: plugins,
defaultFormat: defaultFormat
};
setCurrentEditor(
new Editor(document.getElementById('editor') as HTMLDivElement, editorOptions)
);
updateSampleCode(plugins, defaultFormat, featuresChanged && features);
}
export default initOptions;