-
Notifications
You must be signed in to change notification settings - Fork 148
/
AutoFormatPlugin.ts
263 lines (237 loc) · 9.5 KB
/
AutoFormatPlugin.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { ChangeSource } from 'roosterjs-content-model-dom';
import { createLink } from './link/createLink';
import { createLinkAfterSpace } from './link/createLinkAfterSpace';
import { formatTextSegmentBeforeSelectionMarker } from 'roosterjs-content-model-api';
import { keyboardListTrigger } from './list/keyboardListTrigger';
import { transformFraction } from './numbers/transformFraction';
import { transformHyphen } from './hyphen/transformHyphen';
import { transformOrdinals } from './numbers/transformOrdinals';
import { unlink } from './link/unlink';
import type {
ContentChangedEvent,
EditorInputEvent,
EditorPlugin,
FormatContentModelOptions,
IEditor,
KeyDownEvent,
PluginEvent,
} from 'roosterjs-content-model-types';
/**
* Options to customize the Content Model Auto Format Plugin
*/
export type AutoFormatOptions = {
/**
* When true, after type *, ->, -, --, => , —, > and space key a type of bullet list will be triggered. @default true
*/
autoBullet?: boolean;
/**
* When true, after type 1, A, a, i, I followed by ., ), - or between () and space key a type of numbering list will be triggered. @default true
*/
autoNumbering?: boolean;
/**
* When press backspace before a link, remove the hyperlink
*/
autoUnlink?: boolean;
/**
* When paste content, create hyperlink for the pasted link
*/
autoLink?: boolean;
/**
* Transform -- into hyphen, if typed between two words
*/
autoHyphen?: boolean;
/**
* Transform 1/2, 1/4, 3/4 into fraction character
*/
autoFraction?: boolean;
/**
* Transform ordinal numbers into superscript
*/
autoOrdinals?: boolean;
};
/**
* @internal
*/
const DefaultOptions: Partial<AutoFormatOptions> = {
autoBullet: false,
autoNumbering: false,
autoUnlink: false,
autoLink: false,
autoHyphen: false,
autoFraction: false,
autoOrdinals: false,
};
/**
* Auto Format plugin handles auto formatting, such as transforming * characters into a bullet list.
* It can be customized with options to enable or disable auto list features.
*/
export class AutoFormatPlugin implements EditorPlugin {
private editor: IEditor | null = null;
/**
* @param options An optional parameter that takes in an object of type AutoFormatOptions, which includes the following properties:
* - autoBullet: A boolean that enables or disables automatic bullet list formatting. Defaults to false.
* - autoNumbering: A boolean that enables or disables automatic numbering formatting. Defaults to false.
* - autoLink: A boolean that enables or disables automatic hyperlink creation when pasting or typing content. Defaults to false.
* - autoUnlink: A boolean that enables or disables automatic hyperlink removal when pressing backspace. Defaults to false.
* - autoHyphen: A boolean that enables or disables automatic hyphen transformation. Defaults to false.
* - autoFraction: A boolean that enables or disables automatic fraction transformation. Defaults to false.
* - autoOrdinals: A boolean that enables or disables automatic ordinal number transformation. Defaults to false.
*/
constructor(private options: AutoFormatOptions = DefaultOptions) {}
/**
* Get name of this plugin
*/
getName() {
return 'AutoFormat';
}
/**
* The first method that editor will call to a plugin when editor is initializing.
* It will pass in the editor instance, plugin should take this chance to save the
* editor reference so that it can call to any editor method or format API later.
* @param editor The editor object
*/
initialize(editor: IEditor) {
this.editor = editor;
}
/**
* The last method that editor will call to a plugin before it is disposed.
* Plugin can take this chance to clear the reference to editor. After this method is
* called, plugin should not call to any editor method since it will result in error.
*/
dispose() {
this.editor = null;
}
/**
* Core method for a plugin. Once an event happens in editor, editor will call this
* method of each plugin to handle the event as long as the event is not handled
* exclusively by another plugin.
* @param event The event to handle:
*/
onPluginEvent(event: PluginEvent) {
if (this.editor) {
switch (event.eventType) {
case 'input':
this.handleEditorInputEvent(this.editor, event);
break;
case 'keyDown':
this.handleKeyDownEvent(this.editor, event);
break;
case 'contentChanged':
this.handleContentChangedEvent(this.editor, event);
break;
}
}
}
private handleEditorInputEvent(editor: IEditor, event: EditorInputEvent) {
const rawEvent = event.rawEvent;
const selection = editor.getDOMSelection();
if (
rawEvent.inputType === 'insertText' &&
selection &&
selection.type === 'range' &&
selection.range.collapsed
) {
switch (rawEvent.data) {
case ' ':
const formatOptions: FormatContentModelOptions = {
changeSource: '',
apiName: '',
};
formatTextSegmentBeforeSelectionMarker(
editor,
(model, previousSegment, paragraph, _markerFormat, context) => {
const {
autoBullet,
autoNumbering,
autoLink,
autoHyphen,
autoFraction,
autoOrdinals,
} = this.options;
let shouldHyphen = false;
let shouldLink = false;
let shouldList = false;
let shouldFraction = false;
let shouldOrdinals = false;
if (autoBullet || autoNumbering) {
shouldList = keyboardListTrigger(
model,
paragraph,
context,
autoBullet,
autoNumbering
);
}
if (autoLink) {
shouldLink = createLinkAfterSpace(
previousSegment,
paragraph,
context
);
}
if (autoHyphen) {
shouldHyphen = transformHyphen(previousSegment, paragraph, context);
}
if (autoFraction) {
shouldFraction = transformFraction(
previousSegment,
paragraph,
context
);
}
if (autoOrdinals) {
shouldOrdinals = transformOrdinals(
previousSegment,
paragraph,
context
);
}
formatOptions.apiName = getApiName(shouldList, shouldHyphen);
formatOptions.changeSource = getChangeSource(
shouldList,
shouldHyphen,
shouldLink
);
return (
shouldList ||
shouldHyphen ||
shouldLink ||
shouldFraction ||
shouldOrdinals
);
},
formatOptions
);
break;
}
}
}
private handleKeyDownEvent(editor: IEditor, event: KeyDownEvent) {
const rawEvent = event.rawEvent;
if (!rawEvent.defaultPrevented && !event.handledByEditFeature) {
switch (rawEvent.key) {
case 'Backspace':
if (this.options.autoUnlink) {
unlink(editor, rawEvent);
}
break;
}
}
}
private handleContentChangedEvent(editor: IEditor, event: ContentChangedEvent) {
const { autoLink } = this.options;
if (event.source == 'Paste' && autoLink) {
createLink(editor);
}
}
}
const getApiName = (shouldList: boolean, shouldHyphen: boolean) => {
return shouldList ? 'autoToggleList' : shouldHyphen ? 'autoHyphen' : '';
};
const getChangeSource = (shouldList: boolean, shouldHyphen: boolean, shouldLink: boolean) => {
return shouldList || shouldHyphen
? ChangeSource.AutoFormat
: shouldLink
? ChangeSource.AutoLink
: '';
};