Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong committed Sep 20, 2023
1 parent a3ec520 commit a1866ea
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ describe('childProcessor', () => {
isSelected: true,
format: {},
},
{ segmentType: 'Text', text: 'test2test3', format: {} },
{ segmentType: 'Text', text: 'test2', format: {} },
{ segmentType: 'Text', text: 'test3', format: {} },
],
isImplicit: true,
format: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,13 @@ describe('generalProcessor', () => {
segments: [
{
segmentType: 'Text',
text: 'tes',
text: 't',
format: {},
isSelected: true,
},
{
segmentType: 'Text',
text: 'es',
format: {},
isSelected: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ describe('textProcessor', () => {
segments: [
{
segmentType: 'Text',
text: 'test0test1',
text: 'test0',
format: {},
},
{
segmentType: 'Text',
text: 'test1',
format: {},
},
],
Expand Down Expand Up @@ -255,7 +260,13 @@ describe('textProcessor', () => {
segments: [
{
segmentType: 'Text',
text: 'test1test2',
text: 'test1',
isSelected: true,
format: {},
},
{
segmentType: 'Text',
text: 'test2',
isSelected: true,
format: {},
},
Expand Down Expand Up @@ -441,7 +452,14 @@ describe('textProcessor', () => {

expect(doc).toEqual({
blockGroupType: 'Document',
blocks: [],
blocks: [
{
blockType: 'Paragraph',
segments: [],
format: {},
isImplicit: true,
},
],
});
});

Expand All @@ -453,7 +471,14 @@ describe('textProcessor', () => {

expect(doc).toEqual({
blockGroupType: 'Document',
blocks: [],
blocks: [
{
blockType: 'Paragraph',
segments: [],
format: {},
isImplicit: true,
},
],
});
});

Expand Down Expand Up @@ -494,7 +519,12 @@ describe('textProcessor', () => {
{
segmentType: 'Text',
format: {},
text: 'test ',
text: 'test',
},
{
segmentType: 'Text',
format: {},
text: ' ',
},
],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { cloneModel } from '../../modelApi/common/cloneModel';
import { contentModelDomIndexer } from '../utils/contentModelDomIndexer';
import { DomToModelContext, DomToModelOption } from 'roosterjs-content-model-types';
import { DomToModelOption } from 'roosterjs-content-model-types';
import { SelectionRangeEx } from 'roosterjs-editor-types';
import {
createDomToModelContext,
Expand Down Expand Up @@ -48,21 +47,9 @@ function internalCreateContentModel(
option?: DomToModelOption
) {
const editorContext = core.api.createEditorContext(core);
let domToModelContext: DomToModelContext;

if (option) {
domToModelContext = createDomToModelContext(
editorContext,
...(core.defaultDomToModelOptions || []),
option
);
} else {
editorContext.domIndexer = contentModelDomIndexer;
domToModelContext = createDomToModelContextWithConfig(
core.defaultDomToModelConfig,
editorContext
);
}
const domToModelContext = option
? createDomToModelContext(editorContext, ...(core.defaultDomToModelOptions || []), option)
: createDomToModelContextWithConfig(core.defaultDomToModelConfig, editorContext);

return domToContentModel(core.contentDiv, domToModelContext, selection);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { contentModelDomIndexer } from '../utils/contentModelDomIndexer';
import { ModelToDomContext } from 'roosterjs-content-model-types';
import { SetContentModel } from '../../publicTypes/ContentModelEditorCore';
import {
contentModelToDom,
Expand All @@ -14,29 +12,17 @@ import {
* @param model The content model to set
* @param option Additional options to customize the behavior of Content Model to DOM conversion
*/
export const setContentModel: SetContentModel = (core, model, option) => {
export const setContentModel: SetContentModel = (core, model, option, onNodeCreated) => {
const editorContext = core.api.createEditorContext(core);
let modelToDomContext: ModelToDomContext;

if (option) {
modelToDomContext = createModelToDomContext(
editorContext,
...(core.defaultModelToDomOptions || []),
option
);
} else {
editorContext.domIndexer = contentModelDomIndexer;
modelToDomContext = createModelToDomContextWithConfig(
core.defaultModelToDomConfig,
editorContext
);
}

const modelToDomContext = option
? createModelToDomContext(editorContext, ...(core.defaultModelToDomOptions || []), option)
: createModelToDomContextWithConfig(core.defaultModelToDomConfig, editorContext);
const range = contentModelToDom(
core.contentDiv.ownerDocument,
core.contentDiv,
model,
modelToDomContext
modelToDomContext,
onNodeCreated
);

core.contentDiv.normalize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,11 @@ export default class ContentModelCachePlugin
{
const { contentModel, rangeEx } = event as ContentModelContentChangedEvent;

if (contentModel) {
if (contentModel && this.state.domIndexer) {
this.state.cachedModel = contentModel;
this.state.cachedRangeEx = rangeEx;
} else {
this.editor.invalidateCache();
this.editor.createContentModel();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('createEditorContext', () => {
defaultFormat,
darkColorHandler,
addDelimiterForEntity,
cache: {},
} as any) as ContentModelEditorCore;

const context = createEditorContext(core);
Expand All @@ -37,6 +38,7 @@ describe('createEditorContext', () => {
defaultFormat,
addDelimiterForEntity,
allowCacheElement: true,
domIndexer: undefined,
});
});
});
Expand Down Expand Up @@ -71,6 +73,7 @@ describe('createEditorContext - checkZoomScale', () => {
defaultFormat,
darkColorHandler,
addDelimiterForEntity,
cache: {},
} as any) as ContentModelEditorCore;
});

Expand All @@ -89,6 +92,7 @@ describe('createEditorContext - checkZoomScale', () => {
addDelimiterForEntity,
zoomScale: 1,
allowCacheElement: true,
domIndexer: undefined,
});
});

Expand All @@ -107,6 +111,7 @@ describe('createEditorContext - checkZoomScale', () => {
addDelimiterForEntity,
zoomScale: 2,
allowCacheElement: true,
domIndexer: undefined,
});
});

Expand All @@ -125,6 +130,7 @@ describe('createEditorContext - checkZoomScale', () => {
addDelimiterForEntity,
zoomScale: 0.5,
allowCacheElement: true,
domIndexer: undefined,
});
});
});
Expand Down Expand Up @@ -159,6 +165,7 @@ describe('createEditorContext - checkRootDir', () => {
defaultFormat,
darkColorHandler,
addDelimiterForEntity,
cache: {},
} as any) as ContentModelEditorCore;
});

Expand All @@ -175,6 +182,7 @@ describe('createEditorContext - checkRootDir', () => {
darkColorHandler,
addDelimiterForEntity,
allowCacheElement: true,
domIndexer: undefined,
});
});

Expand All @@ -192,6 +200,7 @@ describe('createEditorContext - checkRootDir', () => {
addDelimiterForEntity,
isRootRtl: true,
allowCacheElement: true,
domIndexer: undefined,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('setContentModel', () => {
let createModelToDomContextWithConfigSpy: jasmine.Spy;
let select: jasmine.Spy;
let getSelectionRange: jasmine.Spy;
let normalizeSpy: jasmine.Spy;

beforeEach(() => {
contentModelToDomSpy = spyOn(contentModelToDom, 'contentModelToDom').and.returnValue(
Expand All @@ -37,6 +38,9 @@ describe('setContentModel', () => {
).and.returnValue(mockedContext);
select = jasmine.createSpy('select');
getSelectionRange = jasmine.createSpy('getSelectionRange');
normalizeSpy = jasmine.createSpy('normalize');

mockedDiv.normalize = normalizeSpy;

core = ({
contentDiv: mockedDiv,
Expand Down Expand Up @@ -67,6 +71,8 @@ describe('setContentModel', () => {
undefined
);
expect(select).toHaveBeenCalledWith(core, mockedRange);
expect(normalizeSpy).toHaveBeenCalledTimes(1);
expect(normalizeSpy).toHaveBeenCalledWith();
});

it('with default option, no shadow edit', () => {
Expand All @@ -84,6 +90,8 @@ describe('setContentModel', () => {
undefined
);
expect(select).toHaveBeenCalledWith(core, mockedRange);
expect(normalizeSpy).toHaveBeenCalledTimes(1);
expect(normalizeSpy).toHaveBeenCalledWith();
});

it('with default option, no shadow edit, with additional option', () => {
Expand All @@ -106,6 +114,8 @@ describe('setContentModel', () => {
undefined
);
expect(select).toHaveBeenCalledWith(core, mockedRange);
expect(normalizeSpy).toHaveBeenCalledTimes(1);
expect(normalizeSpy).toHaveBeenCalledWith();
});

it('no default option, with shadow edit', () => {
Expand All @@ -125,5 +135,7 @@ describe('setContentModel', () => {
undefined
);
expect(select).not.toHaveBeenCalled();
expect(normalizeSpy).toHaveBeenCalledTimes(1);
expect(normalizeSpy).toHaveBeenCalledWith();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('createContentModelEditorCore', () => {
contentDiv: {
style: {},
},
cache: {},
cache: { domIndexer: undefined },
copyPaste: { allowedCustomPasteType: [] },
} as any);
});
Expand Down Expand Up @@ -270,7 +270,7 @@ describe('createContentModelEditorCore', () => {
contentDiv: {
style: {},
},
cache: {},
cache: { domIndexer: undefined },
copyPaste: { allowedCustomPasteType: [] },
} as any);
});
Expand Down Expand Up @@ -330,7 +330,7 @@ describe('createContentModelEditorCore', () => {
contentDiv: {
style: {},
},
cache: {},
cache: { domIndexer: undefined },
copyPaste: { allowedCustomPasteType: [] },
} as any);
});
Expand Down Expand Up @@ -397,7 +397,7 @@ describe('createContentModelEditorCore', () => {
contentDiv: {
style: {},
},
cache: {},
cache: { domIndexer: undefined },
copyPaste: { allowedCustomPasteType: [] },
} as any);
});
Expand Down
Loading

0 comments on commit a1866ea

Please sign in to comment.