Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sheets): add watch API to ref-range-service #1635

Merged
merged 10 commits into from
Apr 10, 2024
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
4,325 changes: 2,166 additions & 2,159 deletions docs/tldr/ref-range/move-rows-cols.tldr

Large diffs are not rendered by default.

79 changes: 79 additions & 0 deletions packages/sheets-conditional-formatting/src/assets/icon-map.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const RemoveRowCommandId = 'sheet.command.remove-row';
/**
* This command would remove the selected rows. These selected rows can be non-continuous.
*/
export const RemoveRowCommand: ICommand = {
export const RemoveRowCommand: ICommand<IRemoveRowColCommandParams> = {
type: CommandType.COMMAND,

id: RemoveRowCommandId,
Expand Down
6 changes: 4 additions & 2 deletions packages/sheets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ export { type ISetRangeValuesRangeMutationParams } from './commands/mutations/se
export {
type ISetRowHiddenMutationParams,
type ISetRowVisibleMutationParams,
SetRowHiddenMutation, SetRowVisibleMutation } from './commands/mutations/set-row-visible.mutation';
SetRowHiddenMutation,
SetRowVisibleMutation,
} from './commands/mutations/set-row-visible.mutation';
export { SetTabColorMutation } from './commands/mutations/set-tab-color.mutation';
export { type ISetTabColorMutationParams } from './commands/mutations/set-tab-color.mutation';
export {
Expand Down Expand Up @@ -277,8 +279,8 @@ export {
handleMoveRows,
rotateRange,
runRefRangeMutations,
handleDefaultRangeChangeWithEffectRefCommands,
handleCommonDefaultRangeChangeWithEffectRefCommands,
handleDefaultRangeChangeWithEffectRefCommands,
} from './services/ref-range/util';
export { INTERCEPTOR_POINT } from './services/sheet-interceptor/interceptor-const';
export { SheetInterceptorService } from './services/sheet-interceptor/sheet-interceptor.service';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/**
* 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 { afterEach, beforeEach, describe, expect, it } from 'vitest';
import type { ICommand, IRange, IWorkbookData, Nullable } from '@univerjs/core';
import { ICommandService, ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';

import { RefRangeService } from '../ref-range.service';
import { SelectionManagerService } from '../../selection-manager.service';
import { InsertColMutation } from '../../../commands/mutations/insert-row-col.mutation';
import type { IInsertColMutationParams } from '../../../basics';
import { SheetInterceptorService } from '../../sheet-interceptor/sheet-interceptor.service';

const TEST_WORKBOOK_DATA: IWorkbookData = {
id: 'test',
appVersion: '3.0.0-alpha',
sheets: {
sheet1: {
id: 'sheet1',
cellData: {
0: {
0: {
v: 'A1',
},
1: {
v: 'A2',
},
},
},
rowCount: 10,
columnCount: 10,
},
sheet2: {
id: 'sheet2',
},
},
locale: LocaleType.ZH_CN,
name: '',
sheetOrder: [],
styles: {},
};

export function createRefRangeTestBed() {
const univer = new Univer();
const injector = univer.__getInjector();
const get = injector.get.bind(injector);

class TestPlugin extends Plugin {
static override type = PluginType.Sheet;

constructor(
_config: undefined,
@Inject(Injector) override readonly _injector: Injector
) {
super('test-plugin');
}
}

([
[RefRangeService],
[SelectionManagerService],
[SheetInterceptorService],
] as Dependency[]).forEach((d) => injector.add(d));

const commandService = get(ICommandService);

([
InsertColMutation,
] as ICommand[]).forEach((command) => commandService.registerCommand(command));

univer.registerPlugin(TestPlugin);
const sheet = univer.createUniverSheet(TEST_WORKBOOK_DATA);

const univerInstanceService = get(IUniverInstanceService);
univerInstanceService.focusUniverInstance('test');

const logService = get(ILogService);
logService.setLogLevel(LogLevel.SILENT);

return {
univer,
get,
sheet,
};
}

describe('test "RefRangeService"', () => {
let univer: Univer;
let refRangeService: RefRangeService;
let commandService: ICommandService;

describe('test "watchRange"', () => {
beforeEach(() => {
const bed = createRefRangeTestBed();

univer = bed.univer;
refRangeService = bed.get(RefRangeService);
commandService = bed.get(ICommandService);
});

afterEach(() => {
univer.dispose();
});

it('should emit updated range when a mutation happens', () => {
let beforeRange: Nullable<IRange> = null;
let afterRange: Nullable<IRange> = null;

const watchedRange: IRange = { startRow: 0, startColumn: 1, endColumn: 3, endRow: 0 };
refRangeService.watchRange('test', 'sheet1', watchedRange, (before, after) => {
beforeRange = before;
afterRange = after;
});

expect(commandService.syncExecuteCommand(InsertColMutation.id, {
unitId: 'test',
subUnitId: 'sheet1',
range: { startRow: 0, endRow: 9, startColumn: 2, endColumn: 2 },
} as IInsertColMutationParams)).toBeTruthy();
expect(beforeRange).toEqual(watchedRange);
expect(afterRange).toEqual({ startRow: 0, startColumn: 1, endColumn: 4, endRow: 0 });
});

it('should not emit when mutation not related to the watched range', () => {
let beforeRange: Nullable<IRange> = null;
let afterRange: Nullable<IRange> = null;

const watchedRange: IRange = { startRow: 0, startColumn: 1, endColumn: 3, endRow: 0 };
refRangeService.watchRange('test', 'sheet1', watchedRange, (before, after) => {
beforeRange = before;
afterRange = after;
});

// On another sheet.
expect(commandService.syncExecuteCommand(InsertColMutation.id, {
unitId: 'test',
subUnitId: 'sheet2',
range: { startRow: 0, endRow: 9, startColumn: 2, endColumn: 2 },
} as IInsertColMutationParams)).toBeTruthy();
expect(beforeRange).toBeNull();
expect(afterRange).toBeNull();

// On a not relevant range.
expect(commandService.syncExecuteCommand(InsertColMutation.id, {
unitId: 'test',
subUnitId: 'sheet1',
range: { startRow: 0, endRow: 9, startColumn: 5, endColumn: 5 },
} as IInsertColMutationParams)).toBeTruthy();
expect(beforeRange).toBeNull();
expect(afterRange).toBeNull();
});
});
});
Loading
Loading