Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ describe('insertLink', () => {
contentModel: jasmine.anything(),
selection: jasmine.anything(),
changedEntities: [],
skipUndo: false,
});

document.body.removeChild(div);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export const formatContentModel: FormatContentModel = (
data: options?.getChangeData?.(),
formatApiName: options?.apiName,
changedEntities: getChangedEntities(context, rawEvent),
skipUndo:
!(shouldMarkNewContent || shouldAddSnapshot) ||
options?.changeSource == ChangeSource.Keyboard, // Keyboard changes will be handled separately in undo plugin, so we need to skip handling it again
};

core.api.triggerEvent(core, eventData, true /*broadcast*/);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChangeSource, isCursorMovingKey } from 'roosterjs-content-model-dom';
import { createSnapshotsManager } from './SnapshotsManagerImpl';
import { isCursorMovingKey } from 'roosterjs-content-model-dom';
import { undo } from '../../command/undo/undo';
import type {
ContentChangedEvent,
Expand Down Expand Up @@ -211,14 +211,7 @@ class UndoPlugin implements PluginWithState<UndoPluginState> {
}

private onContentChanged(event: ContentChangedEvent) {
if (
!(
this.state.isRestoring ||
event.source == ChangeSource.SwitchToDarkMode ||
event.source == ChangeSource.SwitchToLightMode ||
event.source == ChangeSource.Keyboard
)
) {
if (!this.state.isRestoring && !event.skipUndo) {
this.clearRedoForInput();
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/roosterjs-content-model-core/lib/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ export class Editor implements IEditor {
source: isDarkMode
? ChangeSource.SwitchToDarkMode
: ChangeSource.SwitchToLightMode,
skipUndo: true,
},
true
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
skipUndo: false,
data: undefined,
formatApiName: apiName,
changedEntities: [],
Expand Down Expand Up @@ -156,6 +157,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
skipUndo: false,
data: undefined,
formatApiName: apiName,
changedEntities: [],
Expand Down Expand Up @@ -194,6 +196,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
skipUndo: false,
data: undefined,
formatApiName: apiName,
changedEntities: [],
Expand Down Expand Up @@ -232,6 +235,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
skipUndo: false,
data: undefined,
formatApiName: apiName,
changedEntities: [],
Expand Down Expand Up @@ -270,6 +274,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
skipUndo: false,
data: undefined,
formatApiName: apiName,
changedEntities: [],
Expand Down Expand Up @@ -308,6 +313,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
skipUndo: true,
data: undefined,
formatApiName: apiName,
changedEntities: [],
Expand Down Expand Up @@ -342,6 +348,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: 'TEST',
skipUndo: false,
data: undefined,
formatApiName: apiName,
changedEntities: [],
Expand Down Expand Up @@ -384,6 +391,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: 'TEST',
skipUndo: false,
data: returnData,
formatApiName: apiName,
changedEntities: [],
Expand Down Expand Up @@ -423,6 +431,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
skipUndo: false,
data: undefined,
formatApiName: apiName,
changedEntities: [],
Expand Down Expand Up @@ -475,6 +484,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
skipUndo: false,
data: undefined,
formatApiName: apiName,
changedEntities: [
Expand Down Expand Up @@ -536,6 +546,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
skipUndo: false,
data: mockedData,
formatApiName: apiName,
changedEntities: [
Expand Down Expand Up @@ -580,6 +591,7 @@ describe('formatContentModel', () => {
data: undefined,
formatApiName: apiName,
changedEntities: [],
skipUndo: false,
},
true
);
Expand Down Expand Up @@ -610,6 +622,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
skipUndo: false,
data: undefined,
formatApiName: apiName,
changedEntities: [],
Expand Down Expand Up @@ -647,6 +660,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
skipUndo: false,
data: undefined,
formatApiName: apiName,
changedEntities: [],
Expand Down Expand Up @@ -679,6 +693,7 @@ describe('formatContentModel', () => {
contentModel: undefined,
selection: undefined,
source: ChangeSource.Format,
skipUndo: false,
data: undefined,
formatApiName: apiName,
changedEntities: [],
Expand Down Expand Up @@ -1100,6 +1115,20 @@ describe('formatContentModel', () => {
},
isNested: false,
} as any);
expect(triggerEvent).toHaveBeenCalledWith(
core,
{
eventType: 'contentChanged',
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
data: undefined,
formatApiName: undefined,
changedEntities: [],
skipUndo: false,
},
true
);
});

it('trigger addUndoSnapshot when has entityStates', () => {
Expand All @@ -1123,6 +1152,20 @@ describe('formatContentModel', () => {
isNested: false,
snapshotsManager: {},
} as any);
expect(triggerEvent).toHaveBeenCalledWith(
core,
{
eventType: 'contentChanged',
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
data: undefined,
formatApiName: undefined,
changedEntities: [],
skipUndo: false,
},
true
);
});

it('trigger addUndoSnapshot when has canUndoByBackspace', () => {
Expand All @@ -1145,6 +1188,20 @@ describe('formatContentModel', () => {
isNested: false,
snapshotsManager: {},
} as any);
expect(triggerEvent).toHaveBeenCalledWith(
core,
{
eventType: 'contentChanged',
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
data: undefined,
formatApiName: undefined,
changedEntities: [],
skipUndo: false,
},
true
);
});

it('trigger addUndoSnapshot when has canUndoByBackspace and has valid range selection', () => {
Expand All @@ -1154,14 +1211,15 @@ describe('formatContentModel', () => {
context.canUndoByBackspace = true;
return true;
});

setContentModel.and.returnValue({
const newSelection = {
type: 'range',
range: {
startContainer: mockedContainer,
startOffset: mockedOffset,
},
});
};

setContentModel.and.returnValue(newSelection);

formatContentModel(core, callback);

Expand All @@ -1178,6 +1236,20 @@ describe('formatContentModel', () => {
offset: mockedOffset,
},
} as any);
expect(triggerEvent).toHaveBeenCalledWith(
core,
{
eventType: 'contentChanged',
contentModel: mockedModel,
selection: newSelection,
source: ChangeSource.Format,
data: undefined,
formatApiName: undefined,
changedEntities: [],
skipUndo: false,
},
true
);
});

it('Do not trigger addUndoSnapshot when isNested', () => {
Expand All @@ -1195,6 +1267,53 @@ describe('formatContentModel', () => {
isNested: true,
snapshotsManager: {},
} as any);
expect(triggerEvent).toHaveBeenCalledWith(
core,
{
eventType: 'contentChanged',
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Format,
data: undefined,
formatApiName: undefined,
changedEntities: [],
skipUndo: true,
},
true
);
});

it('Do not trigger addUndoSnapshot when change from Keyboard', () => {
core.undo.isNested = true;

const callback = jasmine.createSpy('callback').and.returnValue(true);

formatContentModel(core, callback, {
changeSource: ChangeSource.Keyboard,
});

expect(callback).toHaveBeenCalledTimes(1);
expect(addUndoSnapshot).toHaveBeenCalledTimes(0);
expect(setContentModel).toHaveBeenCalledTimes(1);
expect(setContentModel).toHaveBeenCalledWith(core, mockedModel, undefined, undefined);
expect(core.undo).toEqual({
isNested: true,
snapshotsManager: {},
} as any);
expect(triggerEvent).toHaveBeenCalledWith(
core,
{
eventType: 'contentChanged',
contentModel: mockedModel,
selection: mockedSelection,
source: ChangeSource.Keyboard,
data: undefined,
formatApiName: undefined,
changedEntities: [],
skipUndo: true,
},
true
);
});
});

Expand Down Expand Up @@ -1257,6 +1376,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: 'Format',
skipUndo: false,
data: undefined,
formatApiName: 'mockedApi',
changedEntities: [],
Expand Down Expand Up @@ -1308,6 +1428,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: 'Format',
skipUndo: false,
data: undefined,
formatApiName: 'mockedApi',
changedEntities: [
Expand Down Expand Up @@ -1360,6 +1481,7 @@ describe('formatContentModel', () => {
contentModel: mockedModel,
selection: mockedSelection,
source: 'Format',
skipUndo: false,
data: undefined,
formatApiName: 'mockedApi',
changedEntities: undefined,
Expand Down
Loading
Loading