-
-
Notifications
You must be signed in to change notification settings - Fork 654
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(facade): add redo undo facade hooks
- Loading branch information
Showing
4 changed files
with
243 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
packages/facade/src/apis/hooks/__tests__/f-undoredo-hooks.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* Copyright 2023-present DreamNum Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { type Injector, IUndoRedoService, IUniverInstanceService, Univer, UniverInstanceType } from '@univerjs/core'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { UniverSheetsPlugin } from '@univerjs/sheets'; | ||
import { UniverRenderEnginePlugin } from '@univerjs/engine-render'; | ||
import { FUniver } from '../../facade'; | ||
import type { FWorksheet } from '../../sheets/f-worksheet'; | ||
|
||
function createUnitTestBed(): { | ||
univer: Univer; | ||
get: Injector['get']; | ||
univerAPI: FUniver; | ||
injector: Injector; | ||
} { | ||
const univer = new Univer(); | ||
const injector = univer.__getInjector(); | ||
univer.registerPlugin(UniverSheetsPlugin); | ||
univer.registerPlugin(UniverRenderEnginePlugin); | ||
|
||
const sheet = univer.createUnit(UniverInstanceType.UNIVER_SHEET, {}); | ||
const univerInstanceService = injector.get(IUniverInstanceService); | ||
univerInstanceService.focusUnit(sheet.getUnitId()); | ||
const univerAPI = FUniver.newAPI(univer); | ||
|
||
return { | ||
univer, | ||
get: injector.get.bind(injector), | ||
univerAPI, | ||
injector, | ||
}; | ||
} | ||
|
||
describe('Test Undo Redo Hooks', () => { | ||
let get: Injector['get']; | ||
let univerAPI: FUniver; | ||
|
||
beforeEach(() => { | ||
const testBed = createUnitTestBed(); | ||
get = testBed.get; | ||
univerAPI = testBed.univerAPI; | ||
}); | ||
|
||
it('undoredo normal case', async () => { | ||
const sheet = univerAPI.getActiveWorkbook()?.getActiveSheet() as FWorksheet; | ||
expect(sheet).not.toBeUndefined(); | ||
const range = sheet.getRange(0, 0); | ||
const emptyFlag = ''; | ||
const text = 'Hello World'; | ||
await range.setValue(emptyFlag); | ||
await range.setValue(text); | ||
univerAPI.getHooks().beforeUndo(() => { | ||
expect(range.getValue()).toBe(text); | ||
}); | ||
univerAPI.getHooks().afterUndo(() => { | ||
expect(range.getValue()).toBe(emptyFlag); | ||
}); | ||
univerAPI.getHooks().beforeRedo(() => { | ||
expect(range.getValue()).toBe(emptyFlag); | ||
}); | ||
univerAPI.getHooks().afterRedo(() => { | ||
expect(range.getValue()).toBe(text); | ||
}); | ||
expect(range.getValue()).toBe(text); | ||
await univerAPI.undo(); | ||
expect(range.getValue()).toBe(emptyFlag); | ||
await univerAPI.redo(); | ||
expect(range.getValue()).toBe(text); | ||
}); | ||
|
||
it('undoredo edge case', async () => { | ||
const sheet = univerAPI.getActiveWorkbook()?.getActiveSheet() as FWorksheet; | ||
expect(sheet).not.toBeUndefined(); | ||
|
||
// manually construct undo redo service | ||
get(IUndoRedoService); | ||
|
||
const beforeUndoFn = vi.fn(); | ||
const afterUndoFn = vi.fn(); | ||
const beforeRedoFn = vi.fn(); | ||
const afterRedoFn = vi.fn(); | ||
|
||
univerAPI.getHooks().beforeUndo(beforeUndoFn); | ||
univerAPI.getHooks().afterUndo(afterUndoFn); | ||
univerAPI.getHooks().beforeRedo(beforeRedoFn); | ||
univerAPI.getHooks().afterRedo(afterRedoFn); | ||
|
||
await univerAPI.undo(); | ||
await univerAPI.redo(); | ||
|
||
expect(beforeUndoFn).toHaveBeenCalledTimes(0); | ||
expect(afterUndoFn).toHaveBeenCalledTimes(0); | ||
expect(beforeRedoFn).toHaveBeenCalledTimes(0); | ||
expect(afterRedoFn).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright 2023-present DreamNum Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { IDisposable, IUndoRedoItem } from '@univerjs/core'; | ||
import { ICommandService, IUndoRedoService, RedoCommand, UndoCommand } from '@univerjs/core'; | ||
import type { FHooks } from '../f-hooks'; | ||
|
||
export const FUndoRedoHooks = { | ||
/** | ||
* Hook that fires before an undo operation is executed. | ||
* @param callback Function to be called when the event is triggered | ||
* @returns A disposable object that can be used to unsubscribe from the event | ||
*/ | ||
beforeUndo(this: FHooks, callback: (action: IUndoRedoItem) => void): IDisposable { | ||
const commandService = this._injector.get(ICommandService); | ||
|
||
return commandService.beforeCommandExecuted((command) => { | ||
if (command.id === UndoCommand.id) { | ||
const undoredoService = this._injector.get(IUndoRedoService); | ||
const action = undoredoService.pitchTopUndoElement(); | ||
if (action) { | ||
callback(action); | ||
} | ||
} | ||
}); | ||
}, | ||
/** | ||
* Hook that fires after an undo operation is executed. | ||
* @param callback Function to be called when the event is triggered | ||
* @returns A disposable object that can be used to unsubscribe from the event | ||
*/ | ||
afterUndo(this: FHooks, callback: (action: IUndoRedoItem) => void): IDisposable { | ||
const commandService = this._injector.get(ICommandService); | ||
|
||
return commandService.onCommandExecuted((command) => { | ||
if (command.id === UndoCommand.id) { | ||
const undoredoService = this._injector.get(IUndoRedoService); | ||
const action = undoredoService.pitchTopUndoElement(); | ||
if (action) { | ||
callback(action); | ||
} | ||
} | ||
}); | ||
}, | ||
/** | ||
* Hook that fires before a redo operation is executed. | ||
* @param callback Function to be called when the event is triggered | ||
* @returns A disposable object that can be used to unsubscribe from the event | ||
*/ | ||
beforeRedo(this: FHooks, callback: (action: IUndoRedoItem) => void): IDisposable { | ||
const commandService = this._injector.get(ICommandService); | ||
|
||
return commandService.beforeCommandExecuted((command) => { | ||
if (command.id === RedoCommand.id) { | ||
const undoredoService = this._injector.get(IUndoRedoService); | ||
const action = undoredoService.pitchTopRedoElement(); | ||
if (action) { | ||
callback(action); | ||
} | ||
} | ||
}); | ||
}, | ||
/** | ||
* Hook that fires after a redo operation is executed. | ||
* @param callback Function to be called when the event is triggered | ||
* @returns A disposable object that can be used to unsubscribe from the event | ||
*/ | ||
afterRedo(this: FHooks, callback: (action: IUndoRedoItem) => void): IDisposable { | ||
const commandService = this._injector.get(ICommandService); | ||
|
||
return commandService.onCommandExecuted((command) => { | ||
if (command.id === RedoCommand.id) { | ||
const undoredoService = this._injector.get(IUndoRedoService); | ||
const action = undoredoService.pitchTopRedoElement(); | ||
if (action) { | ||
callback(action); | ||
} | ||
} | ||
}); | ||
}, | ||
}; |