Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(doc): add more apply test case (#3865)
Ran Luoe7b2f7857739
diff --git a/.vscode/settings.json b/.vscode/settings.jsonindex 374990ab5d..5e2a73314a 100644--- a/.vscode/settings.json+++ b/.vscode/settings.json@@ -104,6 +104,7 @@"strechability","SUMIF","SUMIFS",+ "syncer","Tahoma","tblp","TBRL",diff --git a/packages/core/src/docs/data-model/text-x/__tests__/apply.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/apply.spec.tsindex e51ea0921d..29517092e4 100644--- a/packages/core/src/docs/data-model/text-x/__tests__/apply.spec.ts+++ b/packages/core/src/docs/data-model/text-x/__tests__/apply.spec.ts@@ -599,4 +599,67 @@ describe('apply method', () => {expect(resultA).toEqual(resultC);expect(composedAction1).toEqual(composedAction2);});++ it('should get the same result when one is delete actions and the other is insert actions', () => {+ const actionsA: TextXAction[] = [+ {+ t: TextXActionType.DELETE,+ len: 2,+ segmentId: '',+ line: 0,+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 2,+ segmentId: '',+ }, {+ t: TextXActionType.INSERT,+ len: 1,+ segmentId: '',+ line: 0,+ body: {+ dataStream: '1',+ },+ },+ {+ t: TextXActionType.INSERT,+ len: 1,+ segmentId: '',+ line: 0,+ body: {+ dataStream: '1',+ },+ },+ ];++ const doc1 = getDefaultDocWithLength2();+ const doc2 = getDefaultDocWithLength2();+ const doc3 = getDefaultDocWithLength2();+ const doc4 = getDefaultDocWithLength2();++ const resultA = TextX.apply(TextX.apply(doc1, actionsA), TextX.transform(actionsB, actionsA, 'left'));+ const resultB = TextX.apply(TextX.apply(doc2, actionsB), TextX.transform(actionsA, actionsB, 'right'));++ const composedAction1 = TextX.compose(actionsA, TextX.transform(actionsB, actionsA, 'left'));+ const composedAction2 = TextX.compose(actionsB, TextX.transform(actionsA, actionsB, 'right'));++ const resultC = TextX.apply(doc3, composedAction1);+ const resultD = TextX.apply(doc4, composedAction2);++ // console.log(JSON.stringify(resultA, null, 2));+ // console.log(JSON.stringify(resultB, null, 2));++ // console.log('composedAction1', JSON.stringify(composedAction1, null, 2));+ // console.log('composedAction2', JSON.stringify(composedAction2, null, 2));++ // console.log(JSON.stringify(resultC, null, 2));++ expect(resultA).toEqual(resultB);+ expect(resultC).toEqual(resultD);+ expect(resultA).toEqual(resultC);+ expect(composedAction1).toEqual(composedAction2);+ });});diff --git a/packages/docs-ui/src/services/doc-state-change-manager.service.ts b/packages/docs-ui/src/services/doc-state-change-manager.service.tsindex 5f8e3a87c4..fe70dc01ef 100644--- a/packages/docs-ui/src/services/doc-state-change-manager.service.ts+++ b/packages/docs-ui/src/services/doc-state-change-manager.service.ts@@ -25,6 +25,11 @@ type ChangeStateCacheType = 'history' | 'collaboration';const DEBOUNCE_DELAY = 300;+interface IStateCache {+ history: IDocStateChangeParams[];+ collaboration: IDocStateChangeParams[];+}+// This class sends out state-changing events, what is the state, the data model,// and the cursor & selection, and this class mainly serves the History(undo/redo) module and// the collaboration module.@@ -51,6 +56,18 @@ export class DocStateChangeManagerService extends RxDisposable {this._listenDocStateChange();}+ getStateCache(unitId: string) {+ return {+ history: this._historyStateCache.get(unitId) ?? [],+ collaboration: this._changeStateCache.get(unitId) ?? [],+ };+ }++ setStateCache(unitId: string, cache: IStateCache) {+ this._historyStateCache.set(unitId, cache.history);+ this._changeStateCache.set(unitId, cache.collaboration);+ }+private _setChangeState(changeState: IDocStateChangeParams) {this._cacheChangeState(changeState, 'history');// Mutations by user or historyService need collaboration.