Skip to content

Commit

Permalink
Fix exportContent to return the complete editor content even with cus…
Browse files Browse the repository at this point in the history
…tom logical root (#2531)

* Fix exportContent to return the complete editor content even with custom logical root

* Update and add tests

* Update test

* Remove extraneous lines

* Fix tests
  • Loading branch information
florian-msft authored Mar 27, 2024
1 parent 0ce1dc2 commit 77454eb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 15 deletions.
21 changes: 12 additions & 9 deletions packages/roosterjs-content-model-core/lib/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
ChangeSource,
cloneModel,
transformColor,
createDomToModelContextWithConfig,
domToContentModel,
} from 'roosterjs-content-model-dom';
import type {
ContentModelDocument,
Expand Down Expand Up @@ -109,17 +111,18 @@ export class Editor implements IEditor {
});

case 'disconnected':
return cloneModel(core.api.createContentModel(core), {
includeCachedElement: this.cloneOptionCallback,
});

case 'clean':
return cloneModel(
core.api.createContentModel(
core,
undefined /*option*/,
mode == 'clean' ? 'none' : undefined /*selectionOverride*/
),
{
includeCachedElement: this.cloneOptionCallback,
}
const domToModelContext = createDomToModelContextWithConfig(
core.environment.domToModelSettings.calculated,
core.api.createEditorContext(core, false /*saveIndex*/)
);

return domToContentModel(core.physicalRoot, domToModelContext);

case 'reduced':
return core.api.createContentModel(core, {
processorOverride: {
Expand Down
69 changes: 63 additions & 6 deletions packages/roosterjs-content-model-core/test/editor/EditorTest.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as cloneModel from 'roosterjs-content-model-dom/lib/modelApi/editing/cloneModel';
import * as createEditorCore from '../../lib/editor/core/createEditorCore';
import * as createEmptyModel from 'roosterjs-content-model-dom/lib/modelApi/creators/createEmptyModel';
import { ChangeSource, tableProcessor } from 'roosterjs-content-model-dom';
import * as createDomToModelContextWithConfig from 'roosterjs-content-model-dom/lib/domToModel/context/createDomToModelContext';
import * as domToContentModel from 'roosterjs-content-model-dom/lib/domToModel/domToContentModel';
import * as transformColor from 'roosterjs-content-model-dom/lib/domUtils/style/transformColor';
import * as createEmptyModel from 'roosterjs-content-model-dom/lib/modelApi/creators/createEmptyModel';
import * as cloneModel from 'roosterjs-content-model-dom/lib/modelApi/editing/cloneModel';
import { CachedElementHandler, EditorCore, Rect } from 'roosterjs-content-model-types';
import { ChangeSource, tableProcessor } from 'roosterjs-content-model-dom';
import { Editor } from '../../lib/editor/Editor';
import * as createEditorCore from '../../lib/editor/core/createEditorCore';
import { reducedModelChildProcessor } from '../../lib/override/reducedModelChildProcessor';

describe('Editor', () => {
Expand Down Expand Up @@ -208,7 +210,7 @@ describe('Editor', () => {
expect(cloneNodeSpy).toHaveBeenCalledWith(true);

expect(model).toBe(mockedClonedModel);
expect(createContentModelSpy).toHaveBeenCalledWith(mockedCore, undefined, undefined);
expect(createContentModelSpy).toHaveBeenCalledWith(mockedCore);
expect(transformColorSpy).not.toHaveBeenCalled();

// Clone in dark mode
Expand All @@ -217,7 +219,7 @@ describe('Editor', () => {
expect(cloneNodeSpy).toHaveBeenCalledWith(true);

expect(model).toBe(mockedClonedModel);
expect(createContentModelSpy).toHaveBeenCalledWith(mockedCore, undefined, undefined);
expect(createContentModelSpy).toHaveBeenCalledWith(mockedCore);
expect(transformColorSpy).toHaveBeenCalledWith(
clonedNode,
true,
Expand All @@ -236,6 +238,61 @@ describe('Editor', () => {
expect(resetSpy).toHaveBeenCalledWith();
});

it('getContentModelCopy, clean, custom logical root', () => {
const div = document.createElement('div');
const mockedEditorContext = 'EDITORCONTEXT' as any;
const mockedModelContext = 'MODELCONTEXT' as any;
const mockedModel = 'MODEL' as any;
const createEditorContextSpy = jasmine
.createSpy('createEditorContext')
.and.returnValue(mockedEditorContext);
const resetSpy = jasmine.createSpy('reset');
const mockedCore = {
logicalRoot: 'LOGICAL',
physicalRoot: 'PHYSICAL',
plugins: [],
darkColorHandler: {
updateKnownColor: updateKnownColorSpy,
reset: resetSpy,
},
lifecycle: {
isDarkMode: false,
},
api: {
createEditorContext: createEditorContextSpy,
setContentModel: setContentModelSpy,
},
environment: {
domToModelSettings: {
calculated: undefined,
},
},
} as any;

createEditorCoreSpy.and.returnValue(mockedCore);

const editor = new Editor(div);

const createDomToModelContextWithConfigSpy = spyOn(
createDomToModelContextWithConfig,
'createDomToModelContextWithConfig'
).and.returnValue(mockedModelContext);
const domToContentModelSpy = spyOn(domToContentModel, 'domToContentModel').and.returnValue(
mockedModel
);

const model = editor.getContentModelCopy('clean');
expect(model).toBe(mockedModel);
expect(createDomToModelContextWithConfigSpy).toHaveBeenCalledWith(
mockedCore.environment.domToModelSettings.calculated,
mockedEditorContext
);
expect(domToContentModelSpy).toHaveBeenCalledWith(
mockedCore.physicalRoot,
mockedModelContext
);
});

it('getEnvironment', () => {
const div = document.createElement('div');
const mockedEnvironment = 'ENVIRONMENT' as any;
Expand Down

0 comments on commit 77454eb

Please sign in to comment.