Skip to content

Commit 3ab45b3

Browse files
authored
Graduate option "disableCache" (#3132)
* Graduate option "disableCache" * Improve
1 parent 622a089 commit 3ab45b3

File tree

11 files changed

+159
-88
lines changed

11 files changed

+159
-88
lines changed

demo/scripts/controlsV2/mainPane/MainPane.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ export class MainPane extends React.Component<{}, MainPaneState> {
394394
editorCreator={this.state.editorCreator}
395395
dir={this.state.isRtl ? 'rtl' : 'ltr'}
396396
knownColors={this.knownColors}
397-
disableCache={this.state.initState.disableCache}
398397
announcerStringGetter={getAnnouncingString}
399398
experimentalFeatures={Array.from(
400399
this.state.initState.experimentalFeatures

demo/scripts/controlsV2/sidePane/editorOptions/EditorOptionsPlugin.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const initialState: OptionState = {
3232
watermarkText: 'Type content here ...',
3333
forcePreserveRatio: false,
3434
isRtl: false,
35-
disableCache: false,
3635
tableFeaturesContainerSelector: '#' + 'EditorContainer',
3736
allowExcelNoBorderTable: false,
3837
imageMenu: true,

demo/scripts/controlsV2/sidePane/editorOptions/OptionState.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export interface OptionState {
4848

4949
// Editor options
5050
isRtl: boolean;
51-
disableCache: boolean;
5251
experimentalFeatures: Set<ExperimentalFeature>;
5352
}
5453

demo/scripts/controlsV2/sidePane/editorOptions/OptionsPane.tsx

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export class OptionsPane extends React.Component<OptionPaneProps, OptionState> {
2929
private exportForm = React.createRef<HTMLFormElement>();
3030
private exportData = React.createRef<HTMLInputElement>();
3131
private rtl = React.createRef<HTMLInputElement>();
32-
private disableCache = React.createRef<HTMLInputElement>();
3332

3433
constructor(props: OptionPaneProps) {
3534
super(props);
@@ -75,16 +74,6 @@ export class OptionsPane extends React.Component<OptionPaneProps, OptionState> {
7574
/>
7675
<label htmlFor="pageRtl">Show controls from right to left</label>
7776
</div>
78-
<div>
79-
<input
80-
id="disableCache"
81-
type="checkbox"
82-
checked={this.state.disableCache}
83-
onChange={this.onToggleCacheModel}
84-
ref={this.disableCache}
85-
/>
86-
<label htmlFor="disableCache">Disable Content Model Cache</label>
87-
</div>
8877
<hr />
8978
<div>
9079
<button onClick={this.onExportRoosterContentModel}>
@@ -130,7 +119,6 @@ export class OptionsPane extends React.Component<OptionPaneProps, OptionState> {
130119
forcePreserveRatio: this.state.forcePreserveRatio,
131120

132121
isRtl: this.state.isRtl,
133-
disableCache: this.state.disableCache,
134122
tableFeaturesContainerSelector: this.state.tableFeaturesContainerSelector,
135123
allowExcelNoBorderTable: this.state.allowExcelNoBorderTable,
136124
listMenu: this.state.listMenu,
@@ -176,12 +164,6 @@ export class OptionsPane extends React.Component<OptionPaneProps, OptionState> {
176164
MainPane.getInstance().setPageDirection(isRtl);
177165
};
178166

179-
private onToggleCacheModel = () => {
180-
this.resetState(state => {
181-
state.disableCache = this.disableCache.current.checked;
182-
}, true);
183-
};
184-
185167
private getHtml() {
186168
return `${htmlStart}${htmlButtons}${jsCode}${htmlEnd}`;
187169
}

packages/roosterjs-content-model-core/lib/corePlugin/cache/CachePlugin.ts

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,17 @@ class CachePlugin implements PluginWithState<CachePluginState> {
2525
* @param contentDiv The editor content DIV
2626
*/
2727
constructor(option: EditorOptions, contentDiv: HTMLDivElement) {
28-
this.state = {};
29-
30-
if (!option.disableCache) {
31-
this.state.domIndexer = new DomIndexerImpl(
28+
this.state = {
29+
domIndexer: new DomIndexerImpl(
3230
option.experimentalFeatures &&
3331
option.experimentalFeatures.indexOf('PersistCache') >= 0,
3432
option.experimentalFeatures &&
3533
option.experimentalFeatures.indexOf(
3634
'KeepSelectionMarkerWhenEnteringTextNode'
3735
) >= 0
38-
);
39-
this.state.textMutationObserver = createTextMutationObserver(
40-
contentDiv,
41-
this.onMutation
42-
);
43-
}
36+
),
37+
textMutationObserver: createTextMutationObserver(contentDiv, this.onMutation),
38+
};
4439

4540
if (option.enableParagraphMap) {
4641
this.state.paragraphMap = createParagraphMap();
@@ -64,7 +59,7 @@ class CachePlugin implements PluginWithState<CachePluginState> {
6459
this.editor = editor;
6560
this.editor.getDocument().addEventListener('selectionchange', this.onNativeSelectionChange);
6661

67-
this.state.textMutationObserver?.startObserving();
62+
this.state.textMutationObserver.startObserving();
6863
}
6964

7065
/**
@@ -73,7 +68,7 @@ class CachePlugin implements PluginWithState<CachePluginState> {
7368
* called, plugin should not call to any editor method since it will result in error.
7469
*/
7570
dispose() {
76-
this.state.textMutationObserver?.stopObserving();
71+
this.state.textMutationObserver.stopObserving();
7772

7873
if (this.editor) {
7974
this.editor
@@ -105,22 +100,12 @@ class CachePlugin implements PluginWithState<CachePluginState> {
105100
case 'logicalRootChanged':
106101
this.invalidateCache();
107102

108-
if (this.state.textMutationObserver) {
109-
this.state.textMutationObserver.stopObserving();
110-
this.state.textMutationObserver = createTextMutationObserver(
111-
event.logicalRoot,
112-
this.onMutation
113-
);
114-
this.state.textMutationObserver.startObserving();
115-
}
116-
break;
117-
118-
case 'keyDown':
119-
case 'input':
120-
if (!this.state.textMutationObserver) {
121-
// When updating cache is not enabled, need to clear the cache to make sure other plugins can get an up-to-date content model
122-
this.invalidateCache();
123-
}
103+
this.state.textMutationObserver.stopObserving();
104+
this.state.textMutationObserver = createTextMutationObserver(
105+
event.logicalRoot,
106+
this.onMutation
107+
);
108+
this.state.textMutationObserver.startObserving();
124109
break;
125110

126111
case 'selectionChanged':
@@ -130,7 +115,7 @@ class CachePlugin implements PluginWithState<CachePluginState> {
130115
case 'contentChanged':
131116
const { contentModel, selection } = event;
132117

133-
if (contentModel && this.state.domIndexer) {
118+
if (contentModel) {
134119
updateCache(this.state, contentModel, selection);
135120
} else {
136121
this.invalidateCache();
@@ -145,7 +130,7 @@ class CachePlugin implements PluginWithState<CachePluginState> {
145130
switch (mutation.type) {
146131
case 'childList':
147132
if (
148-
!this.state.domIndexer?.reconcileChildList(
133+
!this.state.domIndexer.reconcileChildList(
149134
mutation.addedNodes,
150135
mutation.removedNodes
151136
)
@@ -161,7 +146,7 @@ class CachePlugin implements PluginWithState<CachePluginState> {
161146
case 'elementId':
162147
const element = mutation.element;
163148

164-
if (!this.state.domIndexer?.reconcileElementId(element)) {
149+
if (!this.state.domIndexer.reconcileElementId(element)) {
165150
this.invalidateCache();
166151
}
167152

@@ -211,7 +196,7 @@ class CachePlugin implements PluginWithState<CachePluginState> {
211196
if (
212197
!model ||
213198
!newRangeEx ||
214-
!this.state.domIndexer?.reconcileSelection(model, newRangeEx, cachedSelection)
199+
!this.state.domIndexer.reconcileSelection(model, newRangeEx, cachedSelection)
215200
) {
216201
this.invalidateCache();
217202
} else {

packages/roosterjs-content-model-core/test/coreApi/formatContentModel/formatContentModelTest.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ describe('formatContentModel', () => {
5858
},
5959
lifecycle: {},
6060
cache: {
61+
domIndexer: null!,
62+
textMutationObserver: null!,
6163
paragraphMap: mockedParagraphMap,
6264
},
6365
undo: {
@@ -722,6 +724,8 @@ describe('formatContentModel', () => {
722724
expect(setContentModel).not.toHaveBeenCalled();
723725
expect(triggerEvent).not.toHaveBeenCalled();
724726
expect(core.cache).toEqual({
727+
domIndexer: null!,
728+
textMutationObserver: null!,
725729
cachedModel: undefined,
726730
cachedSelection: undefined,
727731
paragraphMap: mockedParagraphMap,
@@ -782,6 +786,8 @@ describe('formatContentModel', () => {
782786
);
783787
expect(triggerEvent).toHaveBeenCalled();
784788
expect(core.cache).toEqual({
789+
domIndexer: null!,
790+
textMutationObserver: null!,
785791
paragraphMap: mockedParagraphMap,
786792
});
787793
});

packages/roosterjs-content-model-core/test/coreApi/restoreUndoSnapshot/restoreSnapshotHTMLTest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,7 @@ describe('restoreSnapshotHTML', () => {
11891189

11901190
const clearIndexSpy = jasmine.createSpy('clearIndex');
11911191
core.cache = {
1192+
textMutationObserver: null!,
11921193
domIndexer: {
11931194
clearIndex: clearIndexSpy,
11941195
} as any,

0 commit comments

Comments
 (0)