Skip to content

Commit

Permalink
Content Model: Fix #209223 (#1879)
Browse files Browse the repository at this point in the history
* Fix #209223

* fix build and test
  • Loading branch information
JiuqingSong authored Jun 14, 2023
1 parent b8b7255 commit 2c44dc5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ContentModelEditorCore } from '../../publicTypes/ContentModelEditorCore';
import { getSelectionPath } from 'roosterjs-editor-dom';
import { SwitchShadowEdit } from 'roosterjs-editor-types';

/**
Expand All @@ -12,14 +13,23 @@ export const switchShadowEdit: SwitchShadowEdit = (editorCore, isOn): void => {
const core = editorCore as ContentModelEditorCore;

if (isOn != !!core.lifecycle.shadowEditFragment) {
if (isOn && !core.cachedModel) {
core.cachedModel = core.api.createContentModel(core);
}
if (isOn) {
if (!core.cachedModel) {
core.cachedModel = core.api.createContentModel(core);
}

const range = core.api.getSelectionRange(core, true /*tryGetFromCache*/);

core.originalApi.switchShadowEdit(editorCore, isOn);
core.lifecycle.shadowEditSelectionPath =
range && getSelectionPath(core.contentDiv, range);
core.lifecycle.shadowEditFragment = core.contentDiv.ownerDocument.createDocumentFragment();
} else {
if (core.cachedModel) {
core.api.setContentModel(core, core.cachedModel);
}

if (!isOn && core.cachedModel) {
core.api.setContentModel(core, core.cachedModel);
core.lifecycle.shadowEditFragment = null;
core.lifecycle.shadowEditSelectionPath = null;
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ describe('switchShadowEdit', () => {
let core: ContentModelEditorCore;
let createContentModel: jasmine.Spy;
let setContentModel: jasmine.Spy;
let originalSwitchShadowEdit: jasmine.Spy;
let getSelectionRange: jasmine.Spy;

beforeEach(() => {
createContentModel = jasmine.createSpy('createContentModel').and.returnValue(mockedModel);
setContentModel = jasmine.createSpy('setContentModel');
originalSwitchShadowEdit = jasmine.createSpy('originalSwitchShadowEdit');
getSelectionRange = jasmine.createSpy('getSelectionRange');

core = ({
api: {
createContentModel,
setContentModel,
},
originalApi: {
switchShadowEdit: originalSwitchShadowEdit,
getSelectionRange,
},
lifecycle: {},
contentDiv: document.createElement('div'),
} as any) as ContentModelEditorCore;
});

Expand All @@ -32,7 +31,6 @@ describe('switchShadowEdit', () => {
switchShadowEdit(core, true);

expect(createContentModel).toHaveBeenCalledWith(core);
expect(originalSwitchShadowEdit).toHaveBeenCalledWith(core, true);
expect(setContentModel).not.toHaveBeenCalled();
expect(core.cachedModel).toBe(mockedModel);
});
Expand All @@ -43,7 +41,6 @@ describe('switchShadowEdit', () => {
switchShadowEdit(core, true);

expect(createContentModel).not.toHaveBeenCalled();
expect(originalSwitchShadowEdit).toHaveBeenCalledWith(core, true);
expect(setContentModel).not.toHaveBeenCalled();
expect(core.cachedModel).toBe(mockedCachedModel);
});
Expand All @@ -52,7 +49,6 @@ describe('switchShadowEdit', () => {
switchShadowEdit(core, false);

expect(createContentModel).not.toHaveBeenCalled();
expect(originalSwitchShadowEdit).not.toHaveBeenCalled();
expect(setContentModel).not.toHaveBeenCalled();
expect(core.cachedModel).toBe(undefined);
});
Expand All @@ -63,7 +59,6 @@ describe('switchShadowEdit', () => {
switchShadowEdit(core, false);

expect(createContentModel).not.toHaveBeenCalled();
expect(originalSwitchShadowEdit).not.toHaveBeenCalled();
expect(setContentModel).not.toHaveBeenCalled();
expect(core.cachedModel).toBe(mockedCachedModel);
});
Expand All @@ -78,7 +73,6 @@ describe('switchShadowEdit', () => {
switchShadowEdit(core, true);

expect(createContentModel).not.toHaveBeenCalled();
expect(originalSwitchShadowEdit).not.toHaveBeenCalled();
expect(setContentModel).not.toHaveBeenCalled();
expect(core.cachedModel).toBe(undefined);
});
Expand All @@ -89,7 +83,6 @@ describe('switchShadowEdit', () => {
switchShadowEdit(core, true);

expect(createContentModel).not.toHaveBeenCalled();
expect(originalSwitchShadowEdit).not.toHaveBeenCalled();
expect(setContentModel).not.toHaveBeenCalled();
expect(core.cachedModel).toBe(mockedCachedModel);
});
Expand All @@ -98,7 +91,6 @@ describe('switchShadowEdit', () => {
switchShadowEdit(core, false);

expect(createContentModel).not.toHaveBeenCalled();
expect(originalSwitchShadowEdit).toHaveBeenCalledWith(core, false);
expect(setContentModel).not.toHaveBeenCalled();
expect(core.cachedModel).toBe(undefined);
});
Expand All @@ -109,7 +101,6 @@ describe('switchShadowEdit', () => {
switchShadowEdit(core, false);

expect(createContentModel).not.toHaveBeenCalled();
expect(originalSwitchShadowEdit).toHaveBeenCalledWith(core, false);
expect(setContentModel).toHaveBeenCalledWith(core, mockedCachedModel);
expect(core.cachedModel).toBe(mockedCachedModel);
});
Expand Down

0 comments on commit 2c44dc5

Please sign in to comment.