-
Notifications
You must be signed in to change notification settings - Fork 156
/
LifecyclePlugin.ts
188 lines (165 loc) · 6.01 KB
/
LifecyclePlugin.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
import { ChangeSource, PluginEventType } from 'roosterjs-editor-types';
import { getObjectKeys, setColor } from 'roosterjs-editor-dom';
import type {
EditorOptions,
IEditor,
LifecyclePluginState,
PluginWithState,
PluginEvent,
} from 'roosterjs-editor-types';
const CONTENT_EDITABLE_ATTRIBUTE_NAME = 'contenteditable';
const DARK_MODE_DEFAULT_FORMAT = {
backgroundColors: {
darkModeColor: 'rgb(51,51,51)',
lightModeColor: 'rgb(255,255,255)',
},
textColors: {
darkModeColor: 'rgb(255,255,255)',
lightModeColor: 'rgb(0,0,0)',
},
};
/**
* @internal
* Lifecycle plugin handles editor initialization and disposing
*/
export default class LifecyclePlugin implements PluginWithState<LifecyclePluginState> {
private editor: IEditor | null = null;
private state: LifecyclePluginState;
private initialContent: string;
private initializer: (() => void) | null = null;
private disposer: (() => void) | null = null;
private adjustColor: () => void;
/**
* Construct a new instance of LifecyclePlugin
* @param options The editor options
* @param contentDiv The editor content DIV
*/
constructor(options: EditorOptions, contentDiv: HTMLDivElement) {
this.initialContent = options.initialContent || contentDiv.innerHTML || '';
// Make the container editable and set its selection styles
if (contentDiv.getAttribute(CONTENT_EDITABLE_ATTRIBUTE_NAME) === null) {
this.initializer = () => {
contentDiv.contentEditable = 'true';
contentDiv.style.userSelect = 'text';
};
this.disposer = () => {
contentDiv.style.userSelect = '';
contentDiv.removeAttribute(CONTENT_EDITABLE_ATTRIBUTE_NAME);
};
}
this.adjustColor = options.doNotAdjustEditorColor
? () => {}
: () => {
const { textColors, backgroundColors } = DARK_MODE_DEFAULT_FORMAT;
const { isDarkMode } = this.state;
const darkColorHandler = this.editor?.getDarkColorHandler();
setColor(
contentDiv,
textColors,
false /*isBackground*/,
isDarkMode,
false /*shouldAdaptFontColor*/,
darkColorHandler
);
setColor(
contentDiv,
backgroundColors,
true /*isBackground*/,
isDarkMode,
false /*shouldAdaptFontColor*/,
darkColorHandler
);
};
const getDarkColor = options.getDarkColor ?? ((color: string) => color);
const defaultFormat = options.defaultFormat ? { ...options.defaultFormat } : null;
if (defaultFormat) {
if (defaultFormat.textColor && !defaultFormat.textColors) {
defaultFormat.textColors = {
lightModeColor: defaultFormat.textColor,
darkModeColor: getDarkColor(defaultFormat.textColor),
};
delete defaultFormat.textColor;
}
if (defaultFormat.backgroundColor && !defaultFormat.backgroundColors) {
defaultFormat.backgroundColors = {
lightModeColor: defaultFormat.backgroundColor,
darkModeColor: getDarkColor(defaultFormat.backgroundColor),
};
delete defaultFormat.backgroundColor;
}
}
this.state = {
customData: {},
defaultFormat,
isDarkMode: !!options.inDarkMode,
getDarkColor,
onExternalContentTransform: options.onExternalContentTransform ?? null,
experimentalFeatures: options.experimentalFeatures || [],
shadowEditFragment: null,
shadowEditEntities: null,
shadowEditSelectionPath: null,
shadowEditTableSelectionPath: null,
shadowEditImageSelectionPath: null,
};
}
/**
* Get a friendly name of this plugin
*/
getName() {
return 'Lifecycle';
}
/**
* Initialize this plugin. This should only be called from Editor
* @param editor Editor instance
*/
initialize(editor: IEditor) {
this.editor = editor;
// Ensure initial content and its format
this.editor.setContent(this.initialContent, false /*triggerContentChangedEvent*/);
// Set content DIV to be editable
this.initializer?.();
// Set editor background color for dark mode
this.adjustColor();
// Let other plugins know that we are ready
this.editor.triggerPluginEvent(PluginEventType.EditorReady, {}, true /*broadcast*/);
}
/**
* Dispose this plugin
*/
dispose() {
this.editor?.triggerPluginEvent(PluginEventType.BeforeDispose, {}, true /*broadcast*/);
getObjectKeys(this.state.customData).forEach(key => {
const data = this.state.customData[key];
if (data && data.disposer) {
data.disposer(data.value);
}
delete this.state.customData[key];
});
if (this.disposer) {
this.disposer();
this.disposer = null;
this.initializer = null;
}
this.editor = null;
}
/**
* Get plugin state object
*/
getState() {
return this.state;
}
/**
* Handle events triggered from editor
* @param event PluginEvent object
*/
onPluginEvent(event: PluginEvent) {
if (
event.eventType == PluginEventType.ContentChanged &&
(event.source == ChangeSource.SwitchToDarkMode ||
event.source == ChangeSource.SwitchToLightMode)
) {
this.state.isDarkMode = event.source == ChangeSource.SwitchToDarkMode;
this.adjustColor();
}
}
}