Skip to content

Commit

Permalink
Remove "reduced" from getContentModelCopy
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong committed Mar 27, 2024
1 parent 8286c5b commit 6580e0d
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ interface FormatStateContext extends DomToModelContext {

/**
* @internal
* Export for test only
* In order to get format, we can still use the regular child processor. However, to improve performance, we don't need to create
* content model for the whole doc, instead we only need to traverse the tree path that can arrive current selected node.
* This "reduced" child processor will first create a node stack that stores DOM node from root to current common ancestor node of selection,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { reducedModelChildProcessor } from '../../modelApi/common/reducedModelChildProcessor';
import { retrieveModelFormatState } from 'roosterjs-content-model-dom';
import type { IEditor, ContentModelFormatState } from 'roosterjs-content-model-types';

Expand All @@ -7,15 +8,26 @@ import type { IEditor, ContentModelFormatState } from 'roosterjs-content-model-t
*/
export function getFormatState(editor: IEditor): ContentModelFormatState {
const pendingFormat = editor.getPendingFormat();
const model = editor.getContentModelCopy('reduced');
const manager = editor.getSnapshotsManager();
const result: ContentModelFormatState = {
canUndo: manager.hasNewContent || manager.canMove(-1),
canRedo: manager.canMove(1),
isDarkMode: editor.isDarkMode(),
};

retrieveModelFormatState(model, pendingFormat, result);
editor.formatContentModel(
model => {
retrieveModelFormatState(model, pendingFormat, result);

return false;
},
undefined /*options*/,
{
processorOverride: {
child: reducedModelChildProcessor,
},
}
);

return result;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as getSelectionRootNode from 'roosterjs-content-model-dom/lib/domUtils/selection/getSelectionRootNode';
import { createContentModelDocument, createDomToModelContext } from 'roosterjs-content-model-dom';
import { DomToModelContext } from 'roosterjs-content-model-types';
import { reducedModelChildProcessor } from '../../lib/override/reducedModelChildProcessor';
import { reducedModelChildProcessor } from '../../../lib/modelApi/common/reducedModelChildProcessor';

describe('reducedModelChildProcessor', () => {
let context: DomToModelContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ContentModelDocument, ContentModelSegmentFormat } from 'roosterjs-conte
import { ContentModelFormatState } from 'roosterjs-content-model-types';
import { getFormatState } from '../../../lib/publicApi/format/getFormatState';
import { IEditor } from 'roosterjs-content-model-types';
import { reducedModelChildProcessor } from 'roosterjs-content-model-core/lib/override/reducedModelChildProcessor';
import { reducedModelChildProcessor } from '../../../lib/modelApi/common/reducedModelChildProcessor';
import {
createContentModelDocument,
createDomToModelContext,
Expand Down Expand Up @@ -31,7 +31,7 @@ describe('getFormatState', () => {
isDarkMode: () => false,
getZoomScale: () => 1,
getPendingFormat: () => pendingFormat,
getContentModelCopy: () => {
formatContentModel: (callback: Function) => {
const model = createContentModelDocument();
const editorDiv = document.createElement('div');

Expand All @@ -58,7 +58,7 @@ describe('getFormatState', () => {

normalizeContentModel(model);

return model;
callback(model);
},
} as any) as IEditor;

Expand Down
14 changes: 1 addition & 13 deletions packages/roosterjs-content-model-core/lib/editor/Editor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createEditorCore } from './core/createEditorCore';
import { reducedModelChildProcessor } from '../override/reducedModelChildProcessor';
import {
createEmptyModel,
tableProcessor,
Expand Down Expand Up @@ -93,14 +92,10 @@ export class Editor implements IEditor {
* - disconnected: Returns a disconnected clone of Content Model from editor which you can do any change on it and it won't impact the editor content.
* If there is any entity in editor, the returned object will contain cloned copy of entity wrapper element.
* If editor is in dark mode, the cloned entity will be converted back to light mode.
* - reduced: Returns a reduced Content Model that only contains the model of current selection. If there is already a up-to-date cached model, use it
* instead to improve performance. This is mostly used for retrieve current format state.
* - clean: Similar with disconnected, this will return a disconnected model, the difference is "clean" mode will not include any selection info.
* This is usually used for exporting content
*/
getContentModelCopy(
mode: 'connected' | 'disconnected' | 'reduced' | 'clean'
): ContentModelDocument {
getContentModelCopy(mode: 'connected' | 'disconnected' | 'clean'): ContentModelDocument {
const core = this.getCore();

switch (mode) {
Expand All @@ -125,13 +120,6 @@ export class Editor implements IEditor {
core.api.createEditorContext(core, false /*saveIndex*/)
);
return domToContentModel(core.physicalRoot, domToModelContext);

case 'reduced':
return core.api.createContentModel(core, {
processorOverride: {
child: reducedModelChildProcessor,
},
});
}
}

Expand Down
11 changes: 0 additions & 11 deletions packages/roosterjs-content-model-core/test/editor/EditorTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as transformColor from 'roosterjs-content-model-dom/lib/domUtils/style/
import { CachedElementHandler, EditorCore, Rect } from 'roosterjs-content-model-types';
import { ChangeSource, tableProcessor } from 'roosterjs-content-model-dom';
import { Editor } from '../../lib/editor/Editor';
import { reducedModelChildProcessor } from '../../lib/override/reducedModelChildProcessor';

describe('Editor', () => {
let createEditorCoreSpy: jasmine.Spy;
Expand Down Expand Up @@ -134,18 +133,8 @@ describe('Editor', () => {
expect(model1).toBe(mockedModel);
expect(createContentModelSpy).toHaveBeenCalledWith(mockedCore);

const model2 = editor.getContentModelCopy('reduced');

expect(model2).toBe(mockedModel);
expect(createContentModelSpy).toHaveBeenCalledWith(mockedCore, {
processorOverride: {
child: reducedModelChildProcessor,
},
});

editor.dispose();
expect(() => editor.getContentModelCopy('connected')).toThrow();
expect(() => editor.getContentModelCopy('reduced')).toThrow();
expect(resetSpy).toHaveBeenCalledWith();
});

Expand Down
6 changes: 1 addition & 5 deletions packages/roosterjs-content-model-types/lib/editor/IEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,10 @@ export interface IEditor {
* - disconnected: Returns a disconnected clone of Content Model from editor which you can do any change on it and it won't impact the editor content.
* If there is any entity in editor, the returned object will contain cloned copy of entity wrapper element.
* If editor is in dark mode, the cloned entity will be converted back to light mode.
* - reduced: Returns a reduced Content Model that only contains the model of current selection. If there is already a up-to-date cached model, use it
* instead to improve performance. This is mostly used for retrieve current format state.
* - clean: Similar with disconnected, this will return a disconnected model, the difference is "clean" mode will not include any selection info.
* This is usually used for exporting content
*/
getContentModelCopy(
mode: 'connected' | 'disconnected' | 'reduced' | 'clean'
): ContentModelDocument;
getContentModelCopy(mode: 'connected' | 'disconnected' | 'clean'): ContentModelDocument;

/**
* Get current running environment, such as if editor is running on Mac
Expand Down
7 changes: 5 additions & 2 deletions packages/roosterjs-editor-adapter/lib/editor/EditorAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1120,9 +1120,12 @@ export class EditorAdapter extends Editor implements ILegacyEditor {
private retrieveFormatState(): ContentModelFormatState {
const pendingFormat = this.getPendingFormat();
const result: ContentModelFormatState = {};
const model = this.getContentModelCopy('reduced');

retrieveModelFormatState(model, pendingFormat, result);
this.formatContentModel(model => {
retrieveModelFormatState(model, pendingFormat, result);

return false;
});

return result;
}
Expand Down

0 comments on commit 6580e0d

Please sign in to comment.