Skip to content

Commit

Permalink
Merge pull request #2440 from microsoft/u/juliaroldi/restore-focus
Browse files Browse the repository at this point in the history
Take snapshot before paste
  • Loading branch information
juliaroldi authored Feb 29, 2024
2 parents eae992c + 5534dc0 commit cb6c80b
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export const formatContentModel: FormatContentModel = (core, formatter, options)
if (shouldAddSnapshot) {
core.undo.isNested = true;

if (core.undo.snapshotsManager.hasNewContent || entityStates) {
core.api.addUndoSnapshot(core, !!canUndoByBackspace);
}
core.api.addUndoSnapshot(core, !!canUndoByBackspace, entityStates);
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ class SnapshotsManagerImpl implements SnapshotsManager {
currentSnapshot.html == snapshot.html &&
!currentSnapshot.entityStates &&
!snapshot.entityStates;
const addSnapshot = !currentSnapshot || shouldAddSnapshot(currentSnapshot, snapshot);

if (this.snapshots.currentIndex < 0 || !currentSnapshot || !isSameSnapshot) {
if (this.snapshots.currentIndex < 0 || addSnapshot) {
this.clearRedo();
this.snapshots.snapshots.push(snapshot);
this.snapshots.currentIndex++;
Expand Down Expand Up @@ -129,3 +130,13 @@ class SnapshotsManagerImpl implements SnapshotsManager {
export function createSnapshotsManager(snapshots?: Snapshots): SnapshotsManager {
return new SnapshotsManagerImpl(snapshots);
}

function shouldAddSnapshot(currentSnapshot: Snapshot, snapshot: Snapshot) {
return (
currentSnapshot.html !== snapshot.html ||
(currentSnapshot.entityStates &&
snapshot.entityStates &&
currentSnapshot.entityStates !== snapshot.entityStates) ||
(!currentSnapshot.entityStates && snapshot.entityStates)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('formatContentModel', () => {
newImages: [],
});
expect(createContentModel).toHaveBeenCalledTimes(1);
expect(addUndoSnapshot).toHaveBeenCalledTimes(1);
expect(addUndoSnapshot).toHaveBeenCalledTimes(2);
expect(addUndoSnapshot).toHaveBeenCalledWith(core, false, undefined);
expect(setContentModel).toHaveBeenCalledTimes(1);
expect(setContentModel).toHaveBeenCalledWith(core, mockedModel, undefined, undefined);
Expand Down Expand Up @@ -725,7 +725,7 @@ describe('formatContentModel', () => {

expect(callback).toHaveBeenCalledTimes(1);
expect(addUndoSnapshot).toHaveBeenCalledTimes(2);
expect(addUndoSnapshot).toHaveBeenCalledWith(core, false);
expect(addUndoSnapshot).toHaveBeenCalledWith(core, false, undefined);
expect(addUndoSnapshot).toHaveBeenCalledWith(core, false, undefined);
expect(setContentModel).toHaveBeenCalledTimes(1);
expect(setContentModel).toHaveBeenCalledWith(core, mockedModel, undefined, undefined);
Expand All @@ -750,7 +750,7 @@ describe('formatContentModel', () => {

expect(callback).toHaveBeenCalledTimes(1);
expect(addUndoSnapshot).toHaveBeenCalledTimes(2);
expect(addUndoSnapshot).toHaveBeenCalledWith(core, false);
expect(addUndoSnapshot).toHaveBeenCalledWith(core, false, mockedEntityState);
expect(addUndoSnapshot).toHaveBeenCalledWith(core, false, mockedEntityState);
expect(setContentModel).toHaveBeenCalledTimes(1);
expect(setContentModel).toHaveBeenCalledWith(core, mockedModel, undefined, undefined);
Expand All @@ -771,7 +771,7 @@ describe('formatContentModel', () => {
formatContentModel(core, callback);

expect(callback).toHaveBeenCalledTimes(1);
expect(addUndoSnapshot).toHaveBeenCalledTimes(1);
expect(addUndoSnapshot).toHaveBeenCalledTimes(2);
expect(addUndoSnapshot).toHaveBeenCalledWith(core, true, undefined);
expect(setContentModel).toHaveBeenCalledTimes(1);
expect(setContentModel).toHaveBeenCalledWith(core, mockedModel, undefined, undefined);
Expand Down Expand Up @@ -800,7 +800,7 @@ describe('formatContentModel', () => {
formatContentModel(core, callback);

expect(callback).toHaveBeenCalledTimes(1);
expect(addUndoSnapshot).toHaveBeenCalledTimes(1);
expect(addUndoSnapshot).toHaveBeenCalledTimes(2);
expect(addUndoSnapshot).toHaveBeenCalledWith(core, true, undefined);
expect(setContentModel).toHaveBeenCalledTimes(1);
expect(setContentModel).toHaveBeenCalledWith(core, mockedModel, undefined, undefined);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,171 @@ describe('SnapshotsManagerImpl.addSnapshot', () => {
]);
});

it('Add snapshot with entity state with equal entity states', () => {
const mockedEntityStates = 'ENTITYSTATES' as any;

service.addSnapshot(
{
html: 'test',
isDarkMode: false,
},
false
);

expect(snapshots.snapshots).toEqual([
{
html: 'test',
isDarkMode: false,
},
]);

service.addSnapshot(
{
html: 'test',
isDarkMode: false,
entityStates: mockedEntityStates,
},
false
);

expect(snapshots.snapshots).toEqual([
{
html: 'test',
isDarkMode: false,
},
{
html: 'test',
isDarkMode: false,
entityStates: mockedEntityStates,
},
]);

service.addSnapshot(
{
html: 'test',
isDarkMode: false,
entityStates: mockedEntityStates,
},
false
);

expect(snapshots.snapshots).toEqual([
{
html: 'test',
isDarkMode: false,
},
{
html: 'test',
isDarkMode: false,
entityStates: mockedEntityStates,
},
]);
});

it('Add snapshot with entity state with different entity states', () => {
const mockedEntityStates = 'ENTITYSTATES' as any;
const mockedEntityStates2 = 'ENTITYSTATES2' as any;

service.addSnapshot(
{
html: 'test',
isDarkMode: false,
},
false
);

expect(snapshots.snapshots).toEqual([
{
html: 'test',
isDarkMode: false,
},
]);

service.addSnapshot(
{
html: 'test',
isDarkMode: false,
entityStates: mockedEntityStates,
},
false
);

expect(snapshots.snapshots).toEqual([
{
html: 'test',
isDarkMode: false,
},
{
html: 'test',
isDarkMode: false,
entityStates: mockedEntityStates,
},
]);

service.addSnapshot(
{
html: 'test',
isDarkMode: false,
entityStates: mockedEntityStates2,
},
false
);

expect(snapshots.snapshots).toEqual([
{
html: 'test',
isDarkMode: false,
},
{
html: 'test',
isDarkMode: false,
entityStates: mockedEntityStates,
},
{
html: 'test',
isDarkMode: false,
entityStates: mockedEntityStates2,
},
]);
});

it('Add snapshot without entity state after a snapshot with empty state', () => {
const mockedEntityStates = 'ENTITYSTATES' as any;

service.addSnapshot(
{
html: 'test',
isDarkMode: false,
entityStates: mockedEntityStates,
},
false
);

expect(snapshots.snapshots).toEqual([
{
html: 'test',
isDarkMode: false,
entityStates: mockedEntityStates,
},
]);

service.addSnapshot(
{
html: 'test',
isDarkMode: false,
},
false
);

expect(snapshots.snapshots).toEqual([
{
html: 'test',
isDarkMode: false,
entityStates: mockedEntityStates,
},
]);
});

it('Has onChanged', () => {
const onChanged = jasmine.createSpy('onChanged');
snapshots.onChanged = onChanged;
Expand Down

0 comments on commit cb6c80b

Please sign in to comment.