Skip to content

Commit

Permalink
Content Model: trigger ShadowEdit events
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong committed Sep 6, 2023
1 parent 764481a commit 98764a7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ContentModelEditorCore } from '../../publicTypes/ContentModelEditorCore';
import { getSelectionPath } from 'roosterjs-editor-dom';
import { SwitchShadowEdit } from 'roosterjs-editor-types';
import { PluginEventType, SwitchShadowEdit } from 'roosterjs-editor-types';

/**
* @internal
Expand All @@ -23,10 +23,28 @@ export const switchShadowEdit: SwitchShadowEdit = (editorCore, isOn): void => {
core.lifecycle.shadowEditSelectionPath =
range && getSelectionPath(core.contentDiv, range);
core.lifecycle.shadowEditFragment = core.contentDiv.ownerDocument.createDocumentFragment();

core.api.triggerEvent(
core,
{
eventType: PluginEventType.EnteredShadowEdit,
fragment: core.lifecycle.shadowEditFragment,
selectionPath: core.lifecycle.shadowEditSelectionPath,
},
false /*broadcast*/
);
} else {
core.lifecycle.shadowEditFragment = null;
core.lifecycle.shadowEditSelectionPath = null;

core.api.triggerEvent(
core,
{
eventType: PluginEventType.LeavingShadowEdit,
},
false /*broadcast*/
);

if (core.cachedModel) {
core.api.setContentModel(core, core.cachedModel);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ContentModelEditorCore } from '../../../lib/publicTypes/ContentModelEditorCore';
import { PluginEventType } from 'roosterjs-editor-types';
import { switchShadowEdit } from '../../../lib/editor/coreApi/switchShadowEdit';

const mockedModel = 'MODEL' as any;
Expand All @@ -9,17 +10,20 @@ describe('switchShadowEdit', () => {
let createContentModel: jasmine.Spy;
let setContentModel: jasmine.Spy;
let getSelectionRange: jasmine.Spy;
let triggerEvent: jasmine.Spy;

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

core = ({
api: {
createContentModel,
setContentModel,
getSelectionRange,
triggerEvent,
},
lifecycle: {},
contentDiv: document.createElement('div'),
Expand All @@ -33,6 +37,16 @@ describe('switchShadowEdit', () => {
expect(createContentModel).toHaveBeenCalledWith(core);
expect(setContentModel).not.toHaveBeenCalled();
expect(core.cachedModel).toBe(mockedModel);
expect(triggerEvent).toHaveBeenCalledTimes(1);
expect(triggerEvent).toHaveBeenCalledWith(
core,
{
eventType: PluginEventType.EnteredShadowEdit,
fragment: document.createDocumentFragment(),
selectionPath: undefined,
},
false
);
});

it('with cache, isOn', () => {
Expand All @@ -43,6 +57,17 @@ describe('switchShadowEdit', () => {
expect(createContentModel).not.toHaveBeenCalled();
expect(setContentModel).not.toHaveBeenCalled();
expect(core.cachedModel).toBe(mockedCachedModel);

expect(triggerEvent).toHaveBeenCalledTimes(1);
expect(triggerEvent).toHaveBeenCalledWith(
core,
{
eventType: PluginEventType.EnteredShadowEdit,
fragment: document.createDocumentFragment(),
selectionPath: undefined,
},
false
);
});

it('no cache, isOff', () => {
Expand All @@ -51,6 +76,8 @@ describe('switchShadowEdit', () => {
expect(createContentModel).not.toHaveBeenCalled();
expect(setContentModel).not.toHaveBeenCalled();
expect(core.cachedModel).toBe(undefined);

expect(triggerEvent).not.toHaveBeenCalled();
});

it('with cache, isOff', () => {
Expand All @@ -61,6 +88,8 @@ describe('switchShadowEdit', () => {
expect(createContentModel).not.toHaveBeenCalled();
expect(setContentModel).not.toHaveBeenCalled();
expect(core.cachedModel).toBe(mockedCachedModel);

expect(triggerEvent).not.toHaveBeenCalled();
});
});

Expand All @@ -75,6 +104,8 @@ describe('switchShadowEdit', () => {
expect(createContentModel).not.toHaveBeenCalled();
expect(setContentModel).not.toHaveBeenCalled();
expect(core.cachedModel).toBe(undefined);

expect(triggerEvent).not.toHaveBeenCalled();
});

it('with cache, isOn', () => {
Expand All @@ -85,6 +116,8 @@ describe('switchShadowEdit', () => {
expect(createContentModel).not.toHaveBeenCalled();
expect(setContentModel).not.toHaveBeenCalled();
expect(core.cachedModel).toBe(mockedCachedModel);

expect(triggerEvent).not.toHaveBeenCalled();
});

it('no cache, isOff', () => {
Expand All @@ -93,6 +126,15 @@ describe('switchShadowEdit', () => {
expect(createContentModel).not.toHaveBeenCalled();
expect(setContentModel).not.toHaveBeenCalled();
expect(core.cachedModel).toBe(undefined);

expect(triggerEvent).toHaveBeenCalledTimes(1);
expect(triggerEvent).toHaveBeenCalledWith(
core,
{
eventType: PluginEventType.LeavingShadowEdit,
},
false
);
});

it('with cache, isOff', () => {
Expand All @@ -103,6 +145,15 @@ describe('switchShadowEdit', () => {
expect(createContentModel).not.toHaveBeenCalled();
expect(setContentModel).toHaveBeenCalledWith(core, mockedCachedModel);
expect(core.cachedModel).toBe(mockedCachedModel);

expect(triggerEvent).toHaveBeenCalledTimes(1);
expect(triggerEvent).toHaveBeenCalledWith(
core,
{
eventType: PluginEventType.LeavingShadowEdit,
},
false
);
});
});
});

0 comments on commit 98764a7

Please sign in to comment.