Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x 1x 1x 1x 1x 1x 125x 125x 125x 1x 1x 124x 124x 124x 1x 92x 92x 92x 92x 1x 124x 1x 389x 389x 1x 1x 1x 1x 1x 1x 6x 6x 6x 65x 65x 114x 57x 45x 12x 57x 125x 10x 10x 2x 1x 2x 2x 2x 2x 2x 1x 2x 4x 4x 125x 910x 1x 32x 31x 31x 31x 1x 67x 67x 67x 67x 67x 67x 67x 10x 7x 3x 57x 1x 1x 125x | import { areSameSelections } from './areSameSelections'; import { createParagraphMap } from './ParagraphMapImpl'; import { createTextMutationObserver } from './textMutationObserver'; import { DomIndexerImpl } from './domIndexerImpl'; import { updateCache } from './updateCache'; import type { Mutation } from './MutationType'; import type { CachePluginState, IEditor, PluginEvent, PluginWithState, EditorOptions, } from 'roosterjs-content-model-types'; /** * ContentModel cache plugin manages cached Content Model, and refresh the cache when necessary */ class CachePlugin implements PluginWithState<CachePluginState> { private editor: IEditor | null = null; private state: CachePluginState; /** * Construct a new instance of CachePlugin class * @param option The editor option * @param contentDiv The editor content DIV */ constructor(option: EditorOptions, contentDiv: HTMLDivElement) { this.state = option.disableCache ? {} : { domIndexer: new DomIndexerImpl( option.experimentalFeatures && option.experimentalFeatures.indexOf('PersistCache') >= 0 ), textMutationObserver: createTextMutationObserver(contentDiv, this.onMutation), paragraphMap: createParagraphMap(), }; } /** * Get name of this plugin */ getName() { return 'Cache'; } /** * 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; this.editor.getDocument().addEventListener('selectionchange', this.onNativeSelectionChange); this.state.textMutationObserver?.startObserving(); } /** * 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.state.textMutationObserver?.stopObserving(); Eif (this.editor) { this.editor .getDocument() .removeEventListener('selectionchange', this.onNativeSelectionChange); this.editor = null; } } /** * Get plugin state object */ getState(): CachePluginState { return this.state; } /** * 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) { Iif (!this.editor) { return; } switch (event.eventType) { case 'logicalRootChanged': this.invalidateCache(); Eif (this.state.textMutationObserver) { this.state.textMutationObserver.stopObserving(); this.state.textMutationObserver = createTextMutationObserver( event.logicalRoot, this.onMutation ); this.state.textMutationObserver.startObserving(); } break; case 'keyDown': case 'input': Eif (!this.state.textMutationObserver) { // When updating cache is not enabled, need to clear the cache to make sure other plugins can get an up-to-date content model this.invalidateCache(); } break; case 'selectionChanged': this.updateCachedModel(this.editor); break; case 'contentChanged': const { contentModel, selection } = event; if (contentModel && this.state.domIndexer) { updateCache(this.state, contentModel, selection); } else { this.invalidateCache(); } break; } } private onMutation = (mutation: Mutation) => { Eif (this.editor) { switch (mutation.type) { case 'childList': if ( !this.state.domIndexer?.reconcileChildList( mutation.addedNodes, mutation.removedNodes ) ) { this.invalidateCache(); } break; case 'text': this.updateCachedModel(this.editor, true /*forceUpdate*/); break; case 'elementId': const element = mutation.element; if (!this.state.domIndexer?.reconcileElementId(element)) { this.invalidateCache(); } break; case 'unknown': this.invalidateCache(); break; } } }; private onNativeSelectionChange = () => { Iif (this.editor?.hasFocus()) { this.updateCachedModel(this.editor); } }; private invalidateCache() { if (!this.editor?.isInShadowEdit()) { this.state.cachedModel = undefined; this.state.cachedSelection = undefined; // Clear paragraph indexer to prevent stale references to old paragraphs // It will be rebuild next time when we create a new Content Model this.state.paragraphMap?.clear(); } } private updateCachedModel(editor: IEditor, forceUpdate?: boolean) { Iif (editor.isInShadowEdit()) { return; } const cachedSelection = this.state.cachedSelection; this.state.cachedSelection = undefined; // Clear it to force getDOMSelection() retrieve the latest selection range const newRangeEx = editor.getDOMSelection() || undefined; const model = this.state.cachedModel; const isSelectionChanged = forceUpdate || !cachedSelection || !newRangeEx || !areSameSelections(newRangeEx, cachedSelection); if (isSelectionChanged) { if ( !model || !newRangeEx || !this.state.domIndexer?.reconcileSelection(model, newRangeEx, cachedSelection) ) { this.invalidateCache(); } else { updateCache(this.state, model, newRangeEx); } } else { this.state.cachedSelection = cachedSelection; } } } /** * @internal * Create a new instance of CachePlugin class. * @param option The editor option * @param contentDiv The editor content DIV */ export function createCachePlugin( option: EditorOptions, contentDiv: HTMLDivElement ): PluginWithState<CachePluginState> { return new CachePlugin(option, contentDiv); } |