Skip to content

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.

refactor: move IConfirmService interface to core package and move merge-related command from sheets-ui to sheets package (#6320)

wpxp123456ea6de15256b0
diff --git a/packages-experimental/debugger/src/views/use-dialog.ts b/packages-experimental/debugger/src/views/use-dialog.ts
index 0b339c5a92..30c0d20908 100644
--- a/packages-experimental/debugger/src/views/use-dialog.ts
+++ b/packages-experimental/debugger/src/views/use-dialog.ts
@@ -14,7 +14,8 @@
* limitations under the License.
*/
-import { IConfirmService, IDialogService, useDependency } from '@univerjs/ui';
+import { IConfirmService } from '@univerjs/core';
+import { IDialogService, useDependency } from '@univerjs/ui';
const menu = [
{
diff --git a/packages-experimental/uniui/src/plugin.ts b/packages-experimental/uniui/src/plugin.ts
index 82ff80f972..fe98a2e550 100644
--- a/packages-experimental/uniui/src/plugin.ts
+++ b/packages-experimental/uniui/src/plugin.ts
@@ -16,7 +16,7 @@
import type { Dependency } from '@univerjs/core';
import type { IUniverUIConfig } from '@univerjs/ui';
-import { DependentOn, ICommandService, IContextService, ILocalStorageService, Inject, Injector, merge, mergeOverrideWithDependencies, Plugin } from '@univerjs/core';
+import { DependentOn, ICommandService, IConfirmService, IContextService, ILocalStorageService, Inject, Injector, merge, mergeOverrideWithDependencies, Plugin } from '@univerjs/core';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import {
BrowserClipboardService,
@@ -41,7 +41,6 @@ import {
IBeforeCloseService,
ICanvasPopupService,
IClipboardInterfaceService,
- IConfirmService,
IContextMenuService,
IDialogService,
IGalleryService,
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 88c967b80a..cbe8247d1d 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -99,6 +99,7 @@ export {
} from './services/command/command.service';
export { IConfigService } from './services/config/config.service';
export { ConfigService } from './services/config/config.service';
+export { IConfirmService, TestConfirmService } from './services/confirm/confirm.service';
export * from './services/context/context';
export { ContextService, IContextService } from './services/context/context.service';
export { ErrorService, type IError } from './services/error/error.service';
diff --git a/packages/ui/src/services/confirm/confirm.service.ts b/packages/core/src/services/confirm/confirm.service.ts
similarity index 61%
rename from packages/ui/src/services/confirm/confirm.service.ts
rename to packages/core/src/services/confirm/confirm.service.ts
index c0a9febad0..8ed9b1b3ab 100644
--- a/packages/ui/src/services/confirm/confirm.service.ts
+++ b/packages/core/src/services/confirm/confirm.service.ts
@@ -14,35 +14,34 @@
* limitations under the License.
*/
-import type { IDisposable } from '@univerjs/core';
-import type { IConfirmPartMethodOptions } from '../../views/components/confirm-part/interface';
-import { createIdentifier } from '@univerjs/core';
+import type { IDisposable } from '../../common/di';
import { Subject } from 'rxjs';
+import { createIdentifier } from '../../common/di';
export const IConfirmService = createIdentifier<IConfirmService>('univer.confirm-service');
-export interface IConfirmService {
- readonly confirmOptions$: Subject<IConfirmPartMethodOptions[]>;
+export interface IConfirmService<T = unknown> {
+ readonly confirmOptions$: Subject<T[]>;
- open(params: IConfirmPartMethodOptions): IDisposable;
- confirm(params: IConfirmPartMethodOptions): Promise<boolean>;
+ open(params: T): IDisposable;
+ confirm(params: T): Promise<boolean>;
close(id: string): void;
}
/**
* This is a mock service for testing purposes.
*/
-export class TestConfirmService implements IConfirmService, IDisposable {
- readonly confirmOptions$ = new Subject<IConfirmPartMethodOptions[]>();
+export class TestConfirmService<T> implements IConfirmService<T>, IDisposable {
+ readonly confirmOptions$ = new Subject<T[]>();
dispose(): void {
this.confirmOptions$.complete();
}
- open(_params: IConfirmPartMethodOptions): IDisposable {
+ open(_params: T): IDisposable {
throw new Error('This is not implemented in the test service!');
}
- confirm(_params: IConfirmPartMethodOptions): Promise<boolean> {
+ confirm(_params: T): Promise<boolean> {
return Promise.resolve(true);
}
diff --git a/packages/docs-ui/src/commands/operations/doc-create-table.operation.ts b/packages/docs-ui/src/commands/operations/doc-create-table.operation.ts
index e10ee49fcb..e041011beb 100644
--- a/packages/docs-ui/src/commands/operations/doc-create-table.operation.ts
+++ b/packages/docs-ui/src/commands/operations/doc-create-table.operation.ts
@@ -15,8 +15,7 @@
*/
import type { IAccessor, ICommand } from '@univerjs/core';
-import { CommandType, ICommandService, LocaleService } from '@univerjs/core';
-import { IConfirmService } from '@univerjs/ui';
+import { CommandType, ICommandService, IConfirmService, LocaleService } from '@univerjs/core';
import { COMPONENT_DOC_CREATE_TABLE_CONFIRM } from '../../views/table/create/component-name';
import { CreateDocTableCommand } from '../commands/table/doc-table-create.command';
diff --git a/packages/docs-ui/src/commands/operations/open-page-setting.operation.ts b/packages/docs-ui/src/commands/operations/open-page-setting.operation.ts
index 5cc9e20a59..a53bf8119b 100644
--- a/packages/docs-ui/src/commands/operations/open-page-setting.operation.ts
+++ b/packages/docs-ui/src/commands/operations/open-page-setting.operation.ts
@@ -15,9 +15,9 @@
*/
import type { ICommand, PaperType } from '@univerjs/core';
+import type { IConfirmPartMethodOptions } from '@univerjs/ui';
import type { IDocPageSetupCommandParams } from '../commands/doc-page-setup.command';
-import { CommandType, ICommandService, LocaleService, PAGE_SIZE } from '@univerjs/core';
-import { IConfirmService } from '@univerjs/ui';
+import { CommandType, ICommandService, IConfirmService, LocaleService, PAGE_SIZE } from '@univerjs/core';
import { PAGE_SETTING_COMPONENT_ID } from '../../views/page-settings';
import { DocPageSetupCommand } from '../commands/doc-page-setup.command';
@@ -25,7 +25,7 @@ export const DocOpenPageSettingCommand: ICommand = {
id: 'docs.operation.open-page-setting',
type: CommandType.OPERATION,
handler: (accessor) => {
- const confirmService = accessor.get(IConfirmService);
+ const confirmService = accessor.get(IConfirmService) as IConfirmService<IConfirmPartMethodOptions>;
const commandService = accessor.get(ICommandService);
const localeService = accessor.get(LocaleService);
const disposable = confirmService.open({
diff --git a/packages/find-replace/src/commands/commands/replace.command.ts b/packages/find-replace/src/commands/commands/replace.command.ts
index e51f38d6e0..9a07007640 100644
--- a/packages/find-replace/src/commands/commands/replace.command.ts
+++ b/packages/find-replace/src/commands/commands/replace.command.ts
@@ -15,9 +15,9 @@
*/
import type { ICommand } from '@univerjs/core';
-import { CommandType, LocaleService } from '@univerjs/core';
-import { IConfirmService, IMessageService } from '@univerjs/ui';
+import { CommandType, IConfirmService, LocaleService } from '@univerjs/core';
import { MessageType } from '@univerjs/design';
+import { IMessageService } from '@univerjs/ui';
import { IFindReplaceService } from '../../services/find-replace.service';
diff --git a/packages/sheets-sort-ui/src/commands/commands/__tests__/formula-reorder.spec.ts b/packages/sheets-sort-ui/src/commands/commands/__tests__/formula-reorder.spec.ts
index 8e7e68859e..49e53436b0 100644
--- a/packages/sheets-sort-ui/src/commands/commands/__tests__/formula-reorder.spec.ts
+++ b/packages/sheets-sort-ui/src/commands/commands/__tests__/formula-reorder.spec.ts
@@ -14,12 +14,9 @@
* limitations under the License.
*/
-import type { ICellData, IDisposable, Injector, Nullable, Univer } from '@univerjs/core';
-import type { IConfirmPartMethodOptions } from '@univerjs/ui';
-import { ICommandService, IUniverInstanceService, RANGE_TYPE } from '@univerjs/core';
+import type { ICellData, Injector, Nullable, Univer } from '@univerjs/core';
+import { ICommandService, IConfirmService, IUniverInstanceService, RANGE_TYPE, TestConfirmService } from '@univerjs/core';
import { ReorderRangeCommand, ReorderRangeMutation, SetRangeValuesMutation, SheetsSelectionsService } from '@univerjs/sheets';
-import { IConfirmService } from '@univerjs/ui';
-import { Subject } from 'rxjs';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { SheetsSortUIService } from '../../../services/sheets-sort-ui.service';
import { createCommandTestBed } from './create-command-test-bed';
@@ -38,26 +35,7 @@ describe('Test "Sort Range Commands"', () => {
beforeEach(() => {
const testBed = createCommandTestBed(undefined, [
- [
- IConfirmService,
- {
- useClass: class MockConfirmService implements IConfirmService {
- confirmOptions$: Subject<IConfirmPartMethodOptions[]> = new Subject();
-
- open(params: IConfirmPartMethodOptions): IDisposable {
- throw new Error('Method not implemented.');
- }
-
- confirm(params: IConfirmPartMethodOptions): Promise<boolean> {
- return Promise.resolve(true);
- }
-
- close(id: string): void {
- throw new Error('Method not implemented.');
- }
- },
- },
- ],
+ [IConfirmService, { useClass: TestConfirmService }],
]);
univer = testBed.univer;
get = testBed.get;
diff --git a/packages/sheets-sort-ui/src/services/sheets-sort-ui.service.tsx b/packages/sheets-sort-ui/src/services/sheets-sort-ui.service.tsx
index 6ee7780b1f..f62c16ecf9 100644
--- a/packages/sheets-sort-ui/src/services/sheets-sort-ui.service.tsx
+++ b/packages/sheets-sort-ui/src/services/sheets-sort-ui.service.tsx
@@ -16,11 +16,11 @@
import type { IRange, Nullable, Workbook, Worksheet } from '@univerjs/core';
import type { ISheetRangeLocation } from '@univerjs/sheets';
-
import type { ISortOption } from '@univerjs/sheets-sort';
import {
Disposable,
ICommandService,
+ IConfirmService,
Inject,
IUniverInstanceService,
LocaleService,
@@ -28,10 +28,8 @@ import {
Tools,
UniverInstanceType,
} from '@univerjs/core';
-
import { expandToContinuousRange, getPrimaryForRange, SetSelectionsOperation, SheetsSelectionsService } from '@univerjs/sheets';
import { SheetsSortService, SortType } from '@univerjs/sheets-sort';
-import { IConfirmService } from '@univerjs/ui';
import { BehaviorSubject } from 'rxjs';
import { ExtendConfirm } from '../views/ExtendConfirm';
diff --git a/packages/sheets-sort-ui/src/views/sheets-sort-ui.stories.tsx b/packages/sheets-sort-ui/src/views/sheets-sort-ui.stories.tsx
deleted file mode 100644
index 063dc3521e..0000000000
--- a/packages/sheets-sort-ui/src/views/sheets-sort-ui.stories.tsx
+++ /dev/null
@@ -1,166 +0,0 @@
-/**
- * Copyright 2023-present DreamNum Co., Ltd.
- *
- * 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 { Meta } from '@storybook/react';
-import type { IWorkbookData } from '@univerjs/core';
-import type {
- ISetSelectionsOperationParams,
-} from '@univerjs/sheets';
-import {
- ICommandService,
- ILogService,
- Inject,
- Injector,
- LocaleService,
- LocaleType,
- LogLevel,
- Plugin,
- registerDependencies,
- Univer,
- UniverInstanceType,
-} from '@univerjs/core';
-import {
- SetSelectionsOperation,
-} from '@univerjs/sheets';
-import { UniverSheetsSortPlugin } from '@univerjs/sheets-sort';
-import {
- IConfirmService,
- IShortcutService,
- IUIPartsService,
- RediContext,
- ShortcutService,
- TestConfirmService,
- UIPartsService,
-} from '@univerjs/ui';
-import { useState } from 'react';
-import { SortRangeCustomCommand } from '../commands/commands/sheets-sort.command';
-import enUS from '../locale/en-US';
-import { SheetsSortUIService } from '../services/sheets-sort-ui.service';
-import { CustomSortPanel } from './CustomSortPanel';
-
-const meta: Meta<typeof CustomSortPanel> = {
- title: 'CustomSortPanel',
- component: CustomSortPanel,
- tags: ['autodocs'],
-};
-
-export default meta;
-
-function createSortStorybookBed(workbookData: IWorkbookData, locale: LocaleType = LocaleType.EN_US) {
- const univer = new Univer({
- locale: LocaleType.EN_US,
- locales: {
- [LocaleType.EN_US]: enUS,
- },
- });
-
- const injector = univer.__getInjector();
-
- class TestPlugin extends Plugin {
- static override type = UniverInstanceType.UNIVER_SHEET;
- static override pluginName = 'test-plugin';
-
- constructor(
- _config: unknown,
- @Inject(Injector) protected readonly _injector: Injector
- ) {
- super();
- }
-
- override onStarting(): void {
- registerDependencies(this._injector, [
- [IShortcutService, { useClass: ShortcutService }],
- [IUIPartsService, { useClass: UIPartsService }],
- [SheetsSortUIService],
- [IConfirmService, { useClass: TestConfirmService }],
- ]);
-
- const commandService = this._injector.get(ICommandService);
- [
- SortRangeCustomCommand,
- ].forEach((command) => commandService.registerCommand(command));
- }
-
- override onReady(): void {
- const commandService = this._injector.get(ICommandService);
- commandService.executeCommand(SetSelectionsOperation.id, {
- unitId: 'test',
- subUnitId: 'sheet-01',
- selections: [{
- range: { endRow: 2, startColumn: 0, endColumn: 0, startRow: 0 },
- primary: { endRow: 0, startColumn: 0, endColumn: 0, startRow: 0, actualColumn: 0, actualRow: 0, isMerged: false, isMergedMainCell: false },
- }],
- } as ISetSelectionsOperationParams);
- commandService.executeCommand(SortRangeCustomCommand.id);
- }
- }
-
- univer.registerPlugin(UniverSheetsSortPlugin);
- univer.registerPlugin(TestPlugin);
-
- injector.get(LocaleService).setLocale(locale);
- // injector.get(LocaleService).load({ enUS, zhCN, ruRU });
- injector.get(ILogService).setLogLevel(LogLevel.VERBOSE);
-
- const sheet = univer.createUnit(UniverInstanceType.UNIVER_SHEET, workbookData);
-
- //
-
- return { univer, injector, sheet };
-}
-
-export const Demo = {
- render() {
- const [bed] = useState(() => createSortStorybookBed(createSortSnapshot()));
- const { injector } = bed;
-
- return (
- <RediContext.Provider value={{ injector }}>
- <div className="univer-w-[640px]">
- <CustomSortPanel />
- </div>
- </RediContext.Provider>
- );
- },
-};
-
-function createSortSnapshot(): IWorkbookData {
- return {
- id: 'test',
- appVersion: '',
- name: 'Sort Storybook',
- locale: LocaleType.EN_US,
- sheetOrder: ['sheet-01'],
- styles: {},
- sheets: {
- 'sheet-01': {
- name: 'Sheet 1',
- id: 'sheet-01',
- cellData: {
- 0: {
- 0: { v: 1 },
- },
- 1: {
- 0: { v: 2 },
- },
- 2: {
- 0: { v: 3 },
- },
- },
- },
- },
- };
-}
diff --git a/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts
index 7bede7dd75..f3900bc01f 100644
--- a/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts
+++ b/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts
@@ -14,19 +14,25 @@
* limitations under the License.
*/
-import type { IDisposable, Injector, IRange, Univer, Workbook } from '@univerjs/core';
-import type { IConfirmPartMethodOptions } from '@univerjs/ui';
+import type { Injector, IRange, Univer, Workbook } from '@univerjs/core';
import {
ICommandService,
+ IConfirmService,
IUniverInstanceService,
LocaleService,
RANGE_TYPE,
RedoCommand,
+ TestConfirmService,
UndoCommand,
UniverInstanceType,
} from '@univerjs/core';
import {
+ AddWorksheetMergeAllCommand,
+ AddWorksheetMergeCommand,
+ AddWorksheetMergeHorizontalCommand,
AddWorksheetMergeMutation,
+ AddWorksheetMergeVerticalCommand,
+ getClearContentMutationParamForRange,
InsertColAfterCommand,
InsertColBeforeCommand,
InsertColByRangeCommand,
@@ -54,16 +60,7 @@ import {
SetSelectionsOperation,
SheetsSelectionsService,
} from '@univerjs/sheets';
-import { IConfirmService } from '@univerjs/ui';
-import { Subject } from 'rxjs';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
-import { getClearContentMutationParamForRange } from '../../../common/utils';
-import {
- AddWorksheetMergeAllCommand,
- AddWorksheetMergeCommand,
- AddWorksheetMergeHorizontalCommand,
- AddWorksheetMergeVerticalCommand,
-} from '../add-worksheet-merge.command';
import { createCommandTestBed } from './create-command-test-bed';
describe('Test add worksheet merge commands', () => {
@@ -74,26 +71,7 @@ describe('Test add worksheet merge commands', () => {
beforeEach(() => {
const testBed = createCommandTestBed(undefined, [
- [
- IConfirmService,
- {
- useClass: class MockConfirmService implements IConfirmService {
- confirmOptions$: Subject<IConfirmPartMethodOptions[]> = new Subject();
-
- open(params: IConfirmPartMethodOptions): IDisposable {
- throw new Error('Method not implemented.');
- }
-
- confirm(params: IConfirmPartMethodOptions): Promise<boolean> {
- return Promise.resolve(true);
- }
-
- close(id: string): void {
- throw new Error('Method not implemented.');
- }
- },
- },
- ],
+ [IConfirmService, { useClass: TestConfirmService }],
[RefRangeService],
[MergeCellController],
]);
diff --git a/packages/sheets-ui/src/commands/commands/__tests__/hide-row-col-confirm.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/hide-row-col-confirm.command.spec.ts
index f1bd9f313d..0602e1ab75 100644
--- a/packages/sheets-ui/src/commands/commands/__tests__/hide-row-col-confirm.command.spec.ts
+++ b/packages/sheets-ui/src/commands/commands/__tests__/hide-row-col-confirm.command.spec.ts
@@ -14,11 +14,14 @@
* limitations under the License.
*/
-import type { IDisposable, Injector, IRange, Univer } from '@univerjs/core';
-import type { IConfirmPartMethodOptions } from '@univerjs/ui';
-import { ICommandService, IUniverInstanceService, LocaleService, RANGE_TYPE } from '@univerjs/core';
+import type { Injector, IRange, Univer } from '@univerjs/core';
+import { ICommandService, IConfirmService, IUniverInstanceService, LocaleService, RANGE_TYPE, TestConfirmService } from '@univerjs/core';
import {
+ AddWorksheetMergeAllCommand,
+ AddWorksheetMergeCommand,
+ AddWorksheetMergeHorizontalCommand,
AddWorksheetMergeMutation,
+ AddWorksheetMergeVerticalCommand,
RemoveWorksheetMergeCommand,
RemoveWorksheetMergeMutation,
SetColHiddenCommand,
@@ -29,15 +32,7 @@ import {
SetSelectionsOperation,
SheetsSelectionsService,
} from '@univerjs/sheets';
-import { IConfirmService } from '@univerjs/ui';
-import { Subject } from 'rxjs';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
-import {
- AddWorksheetMergeAllCommand,
- AddWorksheetMergeCommand,
- AddWorksheetMergeHorizontalCommand,
- AddWorksheetMergeVerticalCommand,
-} from '../add-worksheet-merge.command';
import { HideColConfirmCommand, HideRowConfirmCommand } from '../hide-row-col-confirm.command';
import { createCommandTestBed } from './create-command-test-bed';
@@ -48,26 +43,7 @@ describe('Test hide row col confirm commands', () => {
beforeEach(() => {
const testBed = createCommandTestBed(undefined, [
- [
- IConfirmService,
- {
- useClass: class MockConfirmService implements IConfirmService {
- confirmOptions$: Subject<IConfirmPartMethodOptions[]> = new Subject();
-
- open(params: IConfirmPartMethodOptions): IDisposable {
- throw new Error('Method not implemented.');
- }
-
- confirm(params: IConfirmPartMethodOptions): Promise<boolean> {
- return Promise.resolve(true);
- }
-
- close(id: string): void {
- throw new Error('Method not implemented.');
- }
- },
- },
- ],
+ [IConfirmService, { useClass: TestConfirmService }],
]);
univer = testBed.univer;
diff --git a/packages/sheets-ui/src/commands/commands/__tests__/remove-row-col-confirm.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/remove-row-col-confirm.command.spec.ts
index e184a12fb0..d6aa01c862 100644
--- a/packages/sheets-ui/src/commands/commands/__tests__/remove-row-col-confirm.command.spec.ts
+++ b/packages/sheets-ui/src/commands/commands/__tests__/remove-row-col-confirm.command.spec.ts
@@ -14,11 +14,14 @@
* limitations under the License.
*/
-import type { IDisposable, Injector, Univer } from '@univerjs/core';
-import type { IConfirmPartMethodOptions } from '@univerjs/ui';
-import { ICommandService, IUniverInstanceService, LocaleService, RANGE_TYPE } from '@univerjs/core';
+import type { Injector, Univer } from '@univerjs/core';
+import { ICommandService, IConfirmService, IUniverInstanceService, LocaleService, RANGE_TYPE, TestConfirmService } from '@univerjs/core';
import {
+ AddWorksheetMergeAllCommand,
+ AddWorksheetMergeCommand,
+ AddWorksheetMergeHorizontalCommand,
AddWorksheetMergeMutation,
+ AddWorksheetMergeVerticalCommand,
InsertColByRangeCommand,
InsertRowByRangeCommand,
RemoveColByRangeCommand,
@@ -33,15 +36,7 @@ import {
SetSelectionsOperation,
SheetsSelectionsService,
} from '@univerjs/sheets';
-import { IConfirmService } from '@univerjs/ui';
-import { Subject } from 'rxjs';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
-import {
- AddWorksheetMergeAllCommand,
- AddWorksheetMergeCommand,
- AddWorksheetMergeHorizontalCommand,
- AddWorksheetMergeVerticalCommand,
-} from '../add-worksheet-merge.command';
import { RemoveColConfirmCommand, RemoveRowConfirmCommand } from '../remove-row-col-confirm.command';
import { createCommandTestBed } from './create-command-test-bed';
@@ -52,26 +47,7 @@ describe('Test remove row col confirm commands', () => {
beforeEach(() => {
const testBed = createCommandTestBed(undefined, [
- [
- IConfirmService,
- {
- useClass: class MockConfirmService implements IConfirmService {
- confirmOptions$: Subject<IConfirmPartMethodOptions[]> = new Subject();
-
- open(params: IConfirmPartMethodOptions): IDisposable {
- throw new Error('Method not implemented.');
- }
-
- confirm(params: IConfirmPartMethodOptions): Promise<boolean> {
- return Promise.resolve(true);
- }
-
- close(id: string): void {
- throw new Error('Method not implemented.');
- }
- },
- },
- ],
+ [IConfirmService, { useClass: TestConfirmService }],
]);
univer = testBed.univer;
diff --git a/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts b/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts
deleted file mode 100644
index a31a9d5502..0000000000
--- a/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts
+++ /dev/null
@@ -1,250 +0,0 @@
-/**
- * Copyright 2023-present DreamNum Co., Ltd.
- *
- * 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.
- */
-
-/* eslint-disable max-lines-per-function */
-
-import type { IAccessor, ICommand, IMutationInfo, IRange, Workbook } from '@univerjs/core';
-import type { IAddWorksheetMergeMutationParams, IRemoveWorksheetMergeMutationParams } from '@univerjs/sheets';
-import {
- CommandType,
- Dimension,
- ICommandService,
- IUndoRedoService,
- IUniverInstanceService,
- LocaleService,
- sequenceExecute,
- Tools,
- UniverInstanceType,
-} from '@univerjs/core';
-import {
- AddMergeRedoSelectionsOperationFactory,
- AddMergeUndoMutationFactory,
- AddMergeUndoSelectionsOperationFactory,
- AddWorksheetMergeMutation,
- getAddMergeMutationRangeByType,
- RemoveMergeUndoMutationFactory,
- RemoveWorksheetMergeMutation,
- SheetInterceptorService,
- SheetsSelectionsService,
-} from '@univerjs/sheets';
-import { IConfirmService } from '@univerjs/ui';
-
-import { checkCellContentInRanges, getClearContentMutationParamsForRanges } from '../../common/utils';
-import { getMergeableSelectionsByType, MergeType } from './utils/selection-utils';
-
-export interface IAddMergeCommandParams {
- value?: Dimension.ROWS | Dimension.COLUMNS;
- selections: IRange[];
- unitId: string;
- subUnitId: string;
- defaultMerge?: boolean;
-}
-
-export const AddWorksheetMergeCommand: ICommand = {
- type: CommandType.COMMAND,
- id: 'sheet.command.add-worksheet-merge',
-
- handler: async (accessor: IAccessor, params: IAddMergeCommandParams) => {
- const commandService = accessor.get(ICommandService);
- const undoRedoService = accessor.get(IUndoRedoService);
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const confirmService = accessor.get(IConfirmService);
- const localeService = accessor.get(LocaleService);
-
- const unitId = params.unitId;
- const subUnitId = params.subUnitId;
- const selections = params.selections;
- const ranges = getAddMergeMutationRangeByType(selections, params.value);
- const worksheet = univerInstanceService.getUniverSheetInstance(unitId)!.getSheetBySheetId(subUnitId)!;
-
- const redoMutations: IMutationInfo[] = [];
- const undoMutations: IMutationInfo[] = [];
-
- // First we should check if there are values in the going-to-be-merged cells.
- const willClearSomeCell = checkCellContentInRanges(worksheet, ranges);
- if (willClearSomeCell && !params.defaultMerge) {
- const result = await confirmService.confirm({
- id: 'merge.confirm.add-worksheet-merge',
- title: {
- title: 'merge.confirm.warning',
- },
- children: {
- title: 'merge.confirm.title',
- },
- cancelText: localeService.t('merge.confirm.cancel'),
- confirmText: localeService.t('merge.confirm.confirm'),
- });
-
- if (!result) {
- return false;
- }
- }
-
- // prepare redo mutations
- const removeMergeMutationParams: IRemoveWorksheetMergeMutationParams = {
- unitId,
- subUnitId,
- ranges: Tools.deepClone(ranges),
- };
- const addMergeMutationParams: IAddWorksheetMergeMutationParams = {
- unitId,
- subUnitId,
- ranges: Tools.deepClone(ranges),
- };
-
- // prepare undo mutations
- const undoRemoveMergeMutationParams = RemoveMergeUndoMutationFactory(accessor, removeMergeMutationParams);
- const undoMutationParams = AddMergeUndoMutationFactory(accessor, addMergeMutationParams);
-
- // params should be the merged cells to be deleted accurately, rather than the selection
- if (undoRemoveMergeMutationParams.ranges.length > 0) {
- redoMutations.push({ id: RemoveWorksheetMergeMutation.id, params: undoRemoveMergeMutationParams });
- }
-
- redoMutations.push({ id: AddWorksheetMergeMutation.id, params: addMergeMutationParams });
- undoMutations.push({ id: RemoveWorksheetMergeMutation.id, params: undoMutationParams });
- if (undoRemoveMergeMutationParams.ranges.length > 0) {
- undoMutations.push({ id: AddWorksheetMergeMutation.id, params: undoRemoveMergeMutationParams });
- }
-
- // add set range values mutations to undo redo mutations
- if (willClearSomeCell) {
- const data = getClearContentMutationParamsForRanges(accessor, unitId, worksheet, ranges);
- redoMutations.unshift(...data.redos);
- undoMutations.push(...data.undos);
- }
-
- const addMergeRedoSelectionsMutation = AddMergeRedoSelectionsOperationFactory(accessor, params, ranges);
- addMergeRedoSelectionsMutation && redoMutations.push(addMergeRedoSelectionsMutation);
-
- const addMergeUndoSelectionsMutation = AddMergeUndoSelectionsOperationFactory(accessor, params);
- addMergeUndoSelectionsMutation && undoMutations.push(addMergeUndoSelectionsMutation);
-
- const sheetInterceptorService = accessor.get(SheetInterceptorService);
- const interceptor = sheetInterceptorService.onCommandExecute({
- id: AddWorksheetMergeCommand.id,
- params: { unitId, subUnitId, ranges },
- });
-
- redoMutations.push(...interceptor.redos);
- undoMutations.push(...interceptor.undos);
-
- const result = sequenceExecute(redoMutations, commandService);
- if (result.result) {
- undoRedoService.pushUndoRedo({
- unitID: unitId,
- undoMutations,
- redoMutations,
- });
- return true;
- }
- return false;
- },
-};
-
-export const AddWorksheetMergeAllCommand: ICommand = {
- type: CommandType.COMMAND,
- id: 'sheet.command.add-worksheet-merge-all',
- handler: async (accessor) => {
- const commandService = accessor.get(ICommandService);
- const selectionManagerService = accessor.get(SheetsSelectionsService);
- const selections = selectionManagerService.getCurrentSelections()?.map((s) => s.range);
- const mergeableSelections = getMergeableSelectionsByType(MergeType.MergeAll, selections);
- if (!mergeableSelections?.length) {
- return false;
- }
-
- const univerInstanceService = accessor.get(IUniverInstanceService);
-
- const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
- if (!workbook) return false;
-
- const workSheet = workbook.getActiveSheet();
- if (!workSheet) return false;
-
- const unitId = workbook.getUnitId();
- const subUnitId = workSheet.getSheetId();
-
- return commandService.executeCommand(AddWorksheetMergeCommand.id, {
- selections: mergeableSelections,
- unitId,
- subUnitId,
- } as IAddMergeCommandParams);
- },
-};
-
-export const AddWorksheetMergeVerticalCommand: ICommand = {
- type: CommandType.COMMAND,
- id: 'sheet.command.add-worksheet-merge-vertical',
- handler: async (accessor) => {
- const commandService = accessor.get(ICommandService);
- const selectionManagerService = accessor.get(SheetsSelectionsService);
- const selections = selectionManagerService.getCurrentSelections()?.map((s) => s.range);
- const mergeableSelections = getMergeableSelectionsByType(MergeType.MergeVertical, selections);
- if (!mergeableSelections?.length) {
- return false;
- }
-
- const univerInstanceService = accessor.get(IUniverInstanceService);
-
- const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
- if (!workbook) return false;
-
- const workSheet = workbook.getActiveSheet();
- if (!workSheet) return false;
-
- const unitId = workbook.getUnitId();
- const subUnitId = workSheet.getSheetId();
-
- return commandService.executeCommand(AddWorksheetMergeCommand.id, {
- value: Dimension.COLUMNS,
- selections: mergeableSelections,
- unitId,
- subUnitId,
- } as IAddMergeCommandParams);
- },
-};
-
-export const AddWorksheetMergeHorizontalCommand: ICommand = {
- type: CommandType.COMMAND,
- id: 'sheet.command.add-worksheet-merge-horizontal',
- handler: async (accessor) => {
- const commandService = accessor.get(ICommandService);
- const selectionManagerService = accessor.get(SheetsSelectionsService);
- const selections = selectionManagerService.getCurrentSelections()?.map((s) => s.range);
- const mergeableSelections = getMergeableSelectionsByType(MergeType.MergeHorizontal, selections);
- if (!mergeableSelections?.length) {
- return false;
- }
-
- const univerInstanceService = accessor.get(IUniverInstanceService);
-
- const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
- if (!workbook) return false;
-
- const workSheet = workbook.getActiveSheet();
- if (!workSheet) return false;
-
- const unitId = workbook.getUnitId();
- const subUnitId = workSheet.getSheetId();
- return commandService.executeCommand(AddWorksheetMergeCommand.id, {
- value: Dimension.ROWS,
- selections: mergeableSelections,
- unitId,
- subUnitId,
- } as IAddMergeCommandParams);
- },
-};
diff --git a/packages/sheets-ui/src/commands/commands/delete-range-move-left-confirm.command.ts b/packages/sheets-ui/src/commands/commands/delete-range-move-left-confirm.command.ts
index 54422553ab..c89640d9be 100644
--- a/packages/sheets-ui/src/commands/commands/delete-range-move-left-confirm.command.ts
+++ b/packages/sheets-ui/src/commands/commands/delete-range-move-left-confirm.command.ts
@@ -15,9 +15,8 @@
*/
import type { ICommand, IRange } from '@univerjs/core';
-import { CommandType, ICommandService, IUniverInstanceService, LocaleService, Rectangle } from '@univerjs/core';
+import { CommandType, ICommandService, IConfirmService, IUniverInstanceService, LocaleService, Rectangle } from '@univerjs/core';
import { DeleteRangeMoveLeftCommand, getSheetCommandTarget, SheetsSelectionsService } from '@univerjs/sheets';
-import { IConfirmService } from '@univerjs/ui';
export const DeleteRangeMoveLeftConfirmCommand: ICommand = {
type: CommandType.COMMAND,
diff --git a/packages/sheets-ui/src/commands/commands/delete-range-move-up-confirm.command.ts b/packages/sheets-ui/src/commands/commands/delete-range-move-up-confirm.command.ts
index 0544fa68f9..f6212c0f70 100644
--- a/packages/sheets-ui/src/commands/commands/delete-range-move-up-confirm.command.ts
+++ b/packages/sheets-ui/src/commands/commands/delete-range-move-up-confirm.command.ts
@@ -15,9 +15,8 @@
*/
import type { ICommand, IRange } from '@univerjs/core';
-import { CommandType, ICommandService, IUniverInstanceService, LocaleService, Rectangle } from '@univerjs/core';
+import { CommandType, ICommandService, IConfirmService, IUniverInstanceService, LocaleService, Rectangle } from '@univerjs/core';
import { DeleteRangeMoveUpCommand, getSheetCommandTarget, SheetsSelectionsService } from '@univerjs/sheets';
-import { IConfirmService } from '@univerjs/ui';
export const DeleteRangeMoveUpConfirmCommand: ICommand = {
type: CommandType.COMMAND,
diff --git a/packages/sheets-ui/src/commands/commands/hide-row-col-confirm.command.ts b/packages/sheets-ui/src/commands/commands/hide-row-col-confirm.command.ts
index a42766d437..87b0e79f96 100644
--- a/packages/sheets-ui/src/commands/commands/hide-row-col-confirm.command.ts
+++ b/packages/sheets-ui/src/commands/commands/hide-row-col-confirm.command.ts
@@ -15,9 +15,8 @@
*/
import type { IAccessor, ICommand } from '@univerjs/core';
-import { CommandType, ICommandService, IUniverInstanceService, LocaleService } from '@univerjs/core';
+import { CommandType, ICommandService, IConfirmService, IUniverInstanceService, LocaleService } from '@univerjs/core';
import { getSheetCommandTarget, SetColHiddenCommand, SetRowHiddenCommand, SheetsSelectionsService } from '@univerjs/sheets';
-import { IConfirmService } from '@univerjs/ui';
import { isAllColumnsCovered, isAllRowsCovered } from './utils/selection-utils';
diff --git a/packages/sheets-ui/src/commands/commands/insert-range-move-down-confirm.command.ts b/packages/sheets-ui/src/commands/commands/insert-range-move-down-confirm.command.ts
index 6590c06c89..ec2b692cf3 100644
--- a/packages/sheets-ui/src/commands/commands/insert-range-move-down-confirm.command.ts
+++ b/packages/sheets-ui/src/commands/commands/insert-range-move-down-confirm.command.ts
@@ -15,9 +15,8 @@
*/
import type { ICommand, IRange } from '@univerjs/core';
-import { CommandType, ICommandService, IUniverInstanceService, LocaleService, Rectangle } from '@univerjs/core';
+import { CommandType, ICommandService, IConfirmService, IUniverInstanceService, LocaleService, Rectangle } from '@univerjs/core';
import { getSheetCommandTarget, InsertRangeMoveDownCommand, SheetsSelectionsService } from '@univerjs/sheets';
-import { IConfirmService } from '@univerjs/ui';
export const InsertRangeMoveDownConfirmCommand: ICommand = {
type: CommandType.COMMAND,
diff --git a/packages/sheets-ui/src/commands/commands/insert-range-move-right-confirm.command.ts b/packages/sheets-ui/src/commands/commands/insert-range-move-right-confirm.command.ts
index b797dbea1b..d3301596a8 100644
--- a/packages/sheets-ui/src/commands/commands/insert-range-move-right-confirm.command.ts
+++ b/packages/sheets-ui/src/commands/commands/insert-range-move-right-confirm.command.ts
@@ -15,9 +15,8 @@
*/
import type { ICommand, IRange } from '@univerjs/core';
-import { CommandType, ICommandService, IUniverInstanceService, LocaleService, Rectangle } from '@univerjs/core';
+import { CommandType, ICommandService, IConfirmService, IUniverInstanceService, LocaleService, Rectangle } from '@univerjs/core';
import { getSheetCommandTarget, InsertRangeMoveRightCommand, SheetsSelectionsService } from '@univerjs/sheets';
-import { IConfirmService } from '@univerjs/ui';
export const InsertRangeMoveRightConfirmCommand: ICommand = {
type: CommandType.COMMAND,
diff --git a/packages/sheets-ui/src/commands/commands/remove-row-col-confirm.command.ts b/packages/sheets-ui/src/commands/commands/remove-row-col-confirm.command.ts
index c5c4c02b05..5786320285 100644
--- a/packages/sheets-ui/src/commands/commands/remove-row-col-confirm.command.ts
+++ b/packages/sheets-ui/src/commands/commands/remove-row-col-confirm.command.ts
@@ -15,10 +15,9 @@
*/
import type { IAccessor, ICommand } from '@univerjs/core';
-import { CommandType, ICommandService, IUniverInstanceService, LocaleService } from '@univerjs/core';
import type { IRemoveRowColCommandParams } from '@univerjs/sheets';
+import { CommandType, ICommandService, IConfirmService, IUniverInstanceService, LocaleService } from '@univerjs/core';
import { getSheetCommandTarget, RemoveColCommand, RemoveRowCommand, SheetsSelectionsService } from '@univerjs/sheets';
-import { IConfirmService } from '@univerjs/ui';
import { isAllColumnsCovered, isAllRowsCovered } from './utils/selection-utils';
diff --git a/packages/sheets-ui/src/commands/commands/remove-sheet-confirm.command.ts b/packages/sheets-ui/src/commands/commands/remove-sheet-confirm.command.ts
index ec930e27e5..ce2ff49aee 100644
--- a/packages/sheets-ui/src/commands/commands/remove-sheet-confirm.command.ts
+++ b/packages/sheets-ui/src/commands/commands/remove-sheet-confirm.command.ts
@@ -16,9 +16,8 @@
import type { IAccessor, ICommand } from '@univerjs/core';
import type { IUniverSheetsConfig } from '@univerjs/sheets';
-import { CommandType, ICommandService, IConfigService, IUniverInstanceService, LocaleService } from '@univerjs/core';
+import { CommandType, ICommandService, IConfigService, IConfirmService, IUniverInstanceService, LocaleService } from '@univerjs/core';
import { countCells, defaultLargeSheetOperationConfig, getSheetCommandTarget, RemoveSheetCommand, SHEETS_PLUGIN_CONFIG_KEY } from '@univerjs/sheets';
-import { IConfirmService } from '@univerjs/ui';
interface IRemoveSheetConfirmCommandParams {
subUnitId: string;
diff --git a/packages/sheets-ui/src/common/utils.ts b/packages/sheets-ui/src/common/utils.ts
index 303772849c..72d6992d42 100644
--- a/packages/sheets-ui/src/common/utils.ts
+++ b/packages/sheets-ui/src/common/utils.ts
@@ -14,14 +14,12 @@
* limitations under the License.
*/
-import type { IAccessor, ICellData, IMutationInfo, IPosition, IRange, Nullable, Workbook, Worksheet } from '@univerjs/core';
+import type { IPosition, IRange, Workbook, Worksheet } from '@univerjs/core';
import type { IBoundRectNoAngle, IRender, Scene, SpreadsheetSkeleton } from '@univerjs/engine-render';
import type { ICollaborator } from '@univerjs/protocol';
-import type { ISetRangeValuesMutationParams, ISheetLocation } from '@univerjs/sheets';
+import type { ISheetLocation } from '@univerjs/sheets';
import type { ISheetSkeletonManagerParam } from '../services/sheet-skeleton-manager.service';
-import { CellModeEnum, ObjectMatrix } from '@univerjs/core';
import { SHEET_VIEWPORT_KEY, Vector2 } from '@univerjs/engine-render';
-import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '@univerjs/sheets';
export function getUserListEqual(userList1: ICollaborator[], userList2: ICollaborator[]) {
if (userList1.length !== userList2.length) return false;
@@ -52,60 +50,6 @@ export function checkCellContentInRange(worksheet: Worksheet, range: IRange): bo
return someCellGoingToBeRemoved;
}
-export function getClearContentMutationParamsForRanges(
- accessor: IAccessor,
- unitId: string,
- worksheet: Worksheet,
- ranges: IRange[]
-): { undos: IMutationInfo[]; redos: IMutationInfo[] } {
- const undos: IMutationInfo[] = [];
- const redos: IMutationInfo[] = [];
-
- const subUnitId = worksheet.getSheetId();
-
- // Use the following file as a reference.
- // packages/sheets/src/commands/commands/clear-selection-all.command.ts
- // packages/sheets/src/commands/mutations/set-range-values.mutation.ts
- ranges.forEach((range) => {
- const redoMatrix = getClearContentMutationParamForRange(worksheet, range);
- const redoMutationParams: ISetRangeValuesMutationParams = {
- unitId,
- subUnitId,
- cellValue: redoMatrix.getData(),
- };
- const undoMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(
- accessor,
- redoMutationParams
- );
-
- undos.push({ id: SetRangeValuesMutation.id, params: undoMutationParams });
- redos.push({ id: SetRangeValuesMutation.id, params: redoMutationParams });
- });
-
- return {
- undos,
- redos,
- };
-}
-
-export function getClearContentMutationParamForRange(worksheet: Worksheet, range: IRange): ObjectMatrix<Nullable<ICellData>> {
- const { startRow, startColumn, endColumn, endRow } = range;
- const cellMatrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn, CellModeEnum.Raw);
- const redoMatrix = new ObjectMatrix<Nullable<ICellData>>();
- let leftTopCellValue: Nullable<ICellData> = null;
- cellMatrix.forValue((row, col, cellData) => {
- if (cellData && row >= startRow && col >= startColumn) {
- if (!leftTopCellValue && worksheet.cellHasValue(cellData) && (cellData.v !== '' || (cellData.p?.body?.dataStream?.length ?? 0) > 2)) {
- leftTopCellValue = cellData;
- }
- redoMatrix.setValue(row, col, null);
- }
- });
- redoMatrix.setValue(startRow, startColumn, leftTopCellValue);
-
- return redoMatrix;
-}
-
export function getCellIndexByOffsetWithMerge(offsetX: number, offsetY: number, scene: Scene, skeleton: SpreadsheetSkeleton) {
const activeViewport = scene.getActiveViewportByCoord(
Vector2.FromArray([offsetX, offsetY])
diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
index 499b147610..7865220df2 100644
--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
@@ -69,6 +69,7 @@ import { MessageType } from '@univerjs/design';
import { convertBodyToHtml, DocSelectionRenderService } from '@univerjs/docs-ui';
import { IRenderManagerService, withCurrentTypeOfRenderer } from '@univerjs/engine-render';
import {
+ AddWorksheetMergeCommand,
InsertColMutation,
InsertRowMutation,
MAX_CELL_PER_SHEET_KEY,
@@ -84,7 +85,6 @@ import {
} from '@univerjs/sheets';
import { BuiltInUIPart, connectInjector, IMessageService, IUIPartsService } from '@univerjs/ui';
import { Subject, takeUntil } from 'rxjs';
-import { AddWorksheetMergeCommand } from '../../commands/commands/add-worksheet-merge.command';
import {
SheetCopyCommand,
SheetCutCommand,
diff --git a/packages/sheets-ui/src/controllers/format-painter/format-painter.controller.ts b/packages/sheets-ui/src/controllers/format-painter/format-painter.controller.ts
index b7c59f3029..437a5cf1c1 100644
--- a/packages/sheets-ui/src/controllers/format-painter/format-painter.controller.ts
+++ b/packages/sheets-ui/src/controllers/format-painter/format-painter.controller.ts
@@ -34,6 +34,7 @@ import {
AddMergeUndoMutationFactory,
AddWorksheetMergeMutation,
getAddMergeMutationRangeByType,
+ getClearContentMutationParamsForRanges,
RemoveMergeUndoMutationFactory,
RemoveWorksheetMergeMutation,
SelectionMoveType,
@@ -44,7 +45,7 @@ import {
SheetInterceptorService,
SheetsSelectionsService,
} from '@univerjs/sheets';
-import { checkCellContentInRanges, getClearContentMutationParamsForRanges } from '../../common/utils';
+import { checkCellContentInRanges } from '../../common/utils';
import { FormatPainterStatus, IFormatPainterService } from '../../services/format-painter/format-painter.service';
export class FormatPainterController extends Disposable {
diff --git a/packages/sheets-ui/src/controllers/menu/merge.menu.ts b/packages/sheets-ui/src/controllers/menu/merge.menu.ts
index 265e5f86e5..0e4aa4f6ea 100644
--- a/packages/sheets-ui/src/controllers/menu/merge.menu.ts
+++ b/packages/sheets-ui/src/controllers/menu/merge.menu.ts
@@ -17,16 +17,20 @@
import type { IAccessor } from '@univerjs/core';
import type { IMenuButtonItem, IMenuSelectorItem } from '@univerjs/ui';
import { UniverInstanceType } from '@univerjs/core';
-import { RangeProtectionPermissionEditPoint, RemoveWorksheetMergeCommand, WorkbookEditablePermission, WorksheetEditPermission, WorksheetSetCellStylePermission, WorksheetSetCellValuePermission } from '@univerjs/sheets';
-import { getMenuHiddenObservable, MenuItemType } from '@univerjs/ui';
-
-import { combineLatestWith, map } from 'rxjs';
import {
AddWorksheetMergeAllCommand,
AddWorksheetMergeCommand,
AddWorksheetMergeHorizontalCommand,
AddWorksheetMergeVerticalCommand,
-} from '../../commands/commands/add-worksheet-merge.command';
+ RangeProtectionPermissionEditPoint,
+ RemoveWorksheetMergeCommand,
+ WorkbookEditablePermission,
+ WorksheetEditPermission,
+ WorksheetSetCellStylePermission,
+ WorksheetSetCellValuePermission,
+} from '@univerjs/sheets';
+import { getMenuHiddenObservable, MenuItemType } from '@univerjs/ui';
+import { combineLatestWith, map } from 'rxjs';
import { getSheetSelectionsDisabled$ } from '../utils/selections-tools';
import { getCurrentRangeDisable$, getObservableWithExclusiveRange$ } from './menu-util';
diff --git a/packages/sheets-ui/src/controllers/mobile/mobile-sheet-ui.controller.ts b/packages/sheets-ui/src/controllers/mobile/mobile-sheet-ui.controller.ts
index 584c3eb7ad..85d500d72f 100644
--- a/packages/sheets-ui/src/controllers/mobile/mobile-sheet-ui.controller.ts
+++ b/packages/sheets-ui/src/controllers/mobile/mobile-sheet-ui.controller.ts
@@ -35,12 +35,6 @@ import {
IShortcutService,
IUIPartsService,
} from '@univerjs/ui';
-import {
- AddWorksheetMergeAllCommand,
- AddWorksheetMergeCommand,
- AddWorksheetMergeHorizontalCommand,
- AddWorksheetMergeVerticalCommand,
-} from '../../commands/commands/add-worksheet-merge.command';
import { AutoClearContentCommand, AutoFillCommand } from '../../commands/commands/auto-fill.command';
import { DeleteRangeMoveLeftConfirmCommand } from '../../commands/commands/delete-range-move-left-confirm.command';
import { DeleteRangeMoveUpConfirmCommand } from '../../commands/commands/delete-range-move-up-confirm.command';
@@ -203,10 +197,6 @@ export class SheetUIMobileController extends Disposable {
private _initCommands(): void {
[
- AddWorksheetMergeAllCommand,
- AddWorksheetMergeCommand,
- AddWorksheetMergeHorizontalCommand,
- AddWorksheetMergeVerticalCommand,
ChangeZoomRatioCommand,
ExpandSelectionCommand,
MoveSelectionCommand,
diff --git a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
index 25b5c26e59..d067603a40 100644
--- a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
+++ b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
@@ -27,12 +27,6 @@ import {
SetUnderlineCommand,
} from '@univerjs/sheets';
import { BuiltInUIPart, ComponentManager, connectInjector, ILayoutService, IMenuManagerService, IShortcutService, IUIPartsService } from '@univerjs/ui';
-import {
- AddWorksheetMergeAllCommand,
- AddWorksheetMergeCommand,
- AddWorksheetMergeHorizontalCommand,
- AddWorksheetMergeVerticalCommand,
-} from '../commands/commands/add-worksheet-merge.command';
import { AutoClearContentCommand, AutoFillCommand } from '../commands/commands/auto-fill.command';
import { DeleteRangeMoveLeftConfirmCommand } from '../commands/commands/delete-range-move-left-confirm.command';
import { DeleteRangeMoveUpConfirmCommand } from '../commands/commands/delete-range-move-up-confirm.command';
@@ -194,13 +188,8 @@ export class SheetUIController extends Disposable {
this.disposeWithMe(componentManager.register('HideGridlinesDoubleIcon', HideGridlinesDoubleIcon));
}
- // eslint-disable-next-line max-lines-per-function
private _initCommands(): void {
[
- AddWorksheetMergeAllCommand,
- AddWorksheetMergeCommand,
- AddWorksheetMergeHorizontalCommand,
- AddWorksheetMergeVerticalCommand,
ChangeZoomRatioCommand,
ExpandSelectionCommand,
MoveSelectionCommand,
diff --git a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarTabs.tsx b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarTabs.tsx
index 6ef69b141d..4a6c18a364 100644
--- a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarTabs.tsx
+++ b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarTabs.tsx
@@ -18,7 +18,7 @@ import type { ICommandInfo } from '@univerjs/core';
import type { IUniverUIConfig } from '@univerjs/ui';
import type { IBaseSheetBarProps } from './SheetBarItem';
import type { IScrollState } from './utils/slide-tab-bar';
-import { ICommandService, IPermissionService, LocaleService, nameCharacterCheck, Quantity } from '@univerjs/core';
+import { ICommandService, IConfirmService, IPermissionService, LocaleService, nameCharacterCheck, Quantity } from '@univerjs/core';
import { DropdownLegacy } from '@univerjs/design';
import { LockIcon } from '@univerjs/icons';
@@ -37,7 +37,7 @@ import {
WorkbookRenameSheetPermission,
WorksheetProtectionRuleModel,
} from '@univerjs/sheets';
-import { ContextMenuPosition, IConfirmService, UI_PLUGIN_CONFIG_KEY, UIMenu, useConfigValue, useDependency, useObservable } from '@univerjs/ui';
+import { ContextMenuPosition, UI_PLUGIN_CONFIG_KEY, UIMenu, useConfigValue, useDependency, useObservable } from '@univerjs/ui';
import { useCallback, useEffect, useRef, useState } from 'react';
import { merge } from 'rxjs';
import { useActiveWorkbook } from '../../../components/hook';
diff --git a/packages/sheets/src/commands/commands/__tests__/add-merge-command.spec.ts b/packages/sheets/src/commands/commands/__tests__/add-merge-command.spec.ts
index cb47e573c3..8c9f257468 100644
--- a/packages/sheets/src/commands/commands/__tests__/add-merge-command.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/add-merge-command.spec.ts
@@ -15,13 +15,14 @@
*/
import type { Injector, IWorkbookData } from '@univerjs/core';
-import { ICommandService, IUniverInstanceService, LocaleService, LocaleType } from '@univerjs/core';
+import { ICommandService, IConfirmService, IUniverInstanceService, LocaleService, LocaleType, RANGE_TYPE, TestConfirmService } from '@univerjs/core';
import { beforeEach, describe, expect, it } from 'vitest';
import enUS from '../../../locale/en-US';
import zhCN from '../../../locale/zh-CN';
import { AddWorksheetMergeMutation } from '../../mutations/add-worksheet-merge.mutation';
import { RemoveWorksheetMergeMutation } from '../../mutations/remove-worksheet-merge.mutation';
import { SetRangeValuesMutation } from '../../mutations/set-range-values.mutation';
+import { SetSelectionsOperation } from '../../operations/selection.operation';
import { AddWorksheetMergeCommand } from '../add-worksheet-merge.command';
import { createCommandTestBed } from './create-command-test-bed';
@@ -128,7 +129,9 @@ describe('add-merge-command', () => {
let commandService: ICommandService;
beforeEach(() => {
- const testBed = createCommandTestBed(WORKBOOK_DATA_DEMO);
+ const testBed = createCommandTestBed(WORKBOOK_DATA_DEMO, [
+ [IConfirmService, { useClass: TestConfirmService }],
+ ]);
get = testBed.get;
commandService = get(ICommandService);
@@ -136,10 +139,26 @@ describe('add-merge-command', () => {
commandService.registerCommand(SetRangeValuesMutation);
commandService.registerCommand(RemoveWorksheetMergeMutation);
commandService.registerCommand(AddWorksheetMergeMutation);
+ commandService.registerCommand(SetSelectionsOperation);
get(LocaleService).load({ zhCN, enUS });
});
it('test merge cell contain merge cell', async () => {
+ await commandService.executeCommand(SetSelectionsOperation.id, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ selections: [
+ {
+ range: {
+ rangeType: RANGE_TYPE.NORMAL,
+ startRow: 1,
+ endRow: 3,
+ startColumn: 5,
+ endColumn: 5,
+ },
+ },
+ ],
+ });
expect(
await commandService.executeCommand(AddWorksheetMergeCommand.id, {
unitId: 'test',
diff --git a/packages/sheets/src/commands/commands/__tests__/clear-selection.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/clear-selection.command.spec.ts
index da7374ec3b..fc39772190 100644
--- a/packages/sheets/src/commands/commands/__tests__/clear-selection.command.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/clear-selection.command.spec.ts
@@ -19,9 +19,11 @@ import type { ISetRangeValuesCommandParams } from '../set-range-values.command';
import {
CellValueType,
ICommandService,
+ IConfirmService,
IUniverInstanceService,
RANGE_TYPE,
RedoCommand,
+ TestConfirmService,
UndoCommand,
} from '@univerjs/core';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
@@ -44,7 +46,11 @@ describe('Test clear selection content commands', () => {
let commandService: ICommandService;
beforeEach(() => {
- const testBed = createCommandTestBed(undefined, [[MergeCellController], [RefRangeService]]);
+ const testBed = createCommandTestBed(undefined, [
+ [MergeCellController],
+ [RefRangeService],
+ [IConfirmService, { useClass: TestConfirmService }],
+ ]);
univer = testBed.univer;
get = testBed.get;
get(MergeCellController);
diff --git a/packages/sheets/src/commands/commands/__tests__/set-border.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-border.command.spec.ts
index a29ea9af4a..a0d8d6a5bf 100644
--- a/packages/sheets/src/commands/commands/__tests__/set-border.command.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/set-border.command.spec.ts
@@ -15,7 +15,7 @@
*/
import type { IBorderData, Injector, IRange, Univer } from '@univerjs/core';
-import { BorderStyleTypes, BorderType, ICommandService, IUniverInstanceService, RANGE_TYPE } from '@univerjs/core';
+import { BorderStyleTypes, BorderType, ICommandService, IConfirmService, IUniverInstanceService, RANGE_TYPE, TestConfirmService } from '@univerjs/core';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { SheetsSelectionsService } from '../../../services/selections/selection.service';
import { AddWorksheetMergeMutation } from '../../mutations/add-worksheet-merge.mutation';
@@ -38,7 +38,9 @@ describe('Test style commands', () => {
let commandService: ICommandService;
let getBorder: ({ startRow, startColumn }: IRange) => IBorderData | undefined;
beforeEach(() => {
- const testBed = createCommandTestBed();
+ const testBed = createCommandTestBed(undefined, [
+ [IConfirmService, { useClass: TestConfirmService }],
+ ]);
univer = testBed.univer;
get = testBed.get;
getBorder = ({ startRow, startColumn }: IRange): IBorderData | undefined => {
diff --git a/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts b/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts
index 72c082f2ac..95aab59e2e 100644
--- a/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts
+++ b/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts
@@ -14,33 +14,30 @@
* limitations under the License.
*/
-import type { IAccessor, ICellData, ICommand, IMutationInfo, Injector, IRange, Nullable, Workbook, Worksheet } from '@univerjs/core';
-import type {
- IAddWorksheetMergeMutationParams,
- IRemoveWorksheetMergeMutationParams,
-} from '../../basics/interfaces/mutation-interface';
-
+import type { IAccessor, ICellData, ICommand, IMutationInfo, Injector, IRange, Nullable, Worksheet } from '@univerjs/core';
+import type { IAddWorksheetMergeMutationParams, IRemoveWorksheetMergeMutationParams } from '../../basics/interfaces/mutation-interface';
import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';
import {
CellModeEnum,
CommandType,
Dimension,
ICommandService,
+ IConfirmService,
IUndoRedoService,
IUniverInstanceService,
+ LocaleService,
ObjectMatrix,
Rectangle,
sequenceExecute,
- UniverInstanceType,
+ Tools,
} from '@univerjs/core';
import { getAddMergeMutationRangeByType } from '../../controllers/merge-cell.controller';
import { SheetsSelectionsService } from '../../services/selections/selection.service';
+import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
import { AddMergeUndoMutationFactory, AddWorksheetMergeMutation } from '../mutations/add-worksheet-merge.mutation';
-import {
- RemoveMergeUndoMutationFactory,
- RemoveWorksheetMergeMutation,
-} from '../mutations/remove-worksheet-merge.mutation';
+import { RemoveMergeUndoMutationFactory, RemoveWorksheetMergeMutation } from '../mutations/remove-worksheet-merge.mutation';
import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';
+import { AddMergeRedoSelectionsOperationFactory, AddMergeUndoSelectionsOperationFactory } from '../utils/handle-merge-operation';
import { getSheetCommandTarget } from './utils/target-util';
export interface IAddMergeCommandParams {
@@ -48,6 +45,13 @@ export interface IAddMergeCommandParams {
selections: IRange[];
unitId: string;
subUnitId: string;
+ defaultMerge?: boolean;
+}
+
+export enum MergeType {
+ MergeAll = 'mergeAll',
+ MergeVertical = 'mergeVertical',
+ MergeHorizontal = 'mergeHorizontal',
}
function checkCellContentInRanges(worksheet: Worksheet, ranges: IRange[]): boolean {
@@ -68,7 +72,7 @@ function checkCellContentInRange(worksheet: Worksheet, range: IRange): boolean {
return someCellGoingToBeRemoved;
}
-function getClearContentMutationParamsForRanges(
+export function getClearContentMutationParamsForRanges(
accessor: IAccessor,
unitId: string,
worksheet: Worksheet,
@@ -104,15 +108,20 @@ function getClearContentMutationParamsForRanges(
};
}
-function getClearContentMutationParamForRange(worksheet: Worksheet, range: IRange): ObjectMatrix<Nullable<ICellData>> {
+export function getClearContentMutationParamForRange(worksheet: Worksheet, range: IRange): ObjectMatrix<Nullable<ICellData>> {
const { startRow, startColumn, endColumn, endRow } = range;
const cellMatrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn, CellModeEnum.Raw);
const redoMatrix = new ObjectMatrix<Nullable<ICellData>>();
+ let leftTopCellValue: Nullable<ICellData> = null;
cellMatrix.forValue((row, col, cellData) => {
- if (cellData && (row !== startRow || col !== startColumn)) {
+ if (cellData && row >= startRow && col >= startColumn) {
+ if (!leftTopCellValue && worksheet.cellHasValue(cellData) && (cellData.v !== '' || (cellData.p?.body?.dataStream?.length ?? 0) > 2)) {
+ leftTopCellValue = cellData;
+ }
redoMatrix.setValue(row, col, null);
}
});
+ redoMatrix.setValue(startRow, startColumn, leftTopCellValue);
return redoMatrix;
}
@@ -120,51 +129,91 @@ function getClearContentMutationParamForRange(worksheet: Worksheet, range: IRang
export const AddWorksheetMergeCommand: ICommand = {
type: CommandType.COMMAND,
id: 'sheet.command.add-worksheet-merge',
+ // eslint-disable-next-line max-lines-per-function
+ handler: async (accessor: IAccessor, params: IAddMergeCommandParams) => {
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const target = getSheetCommandTarget(univerInstanceService, params);
+ if (!target) return false;
- handler: (accessor: IAccessor, params: IAddMergeCommandParams) => {
+ const { worksheet } = target;
+ const { unitId, subUnitId, selections } = params;
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
- const univerInstanceService = accessor.get(IUniverInstanceService);
-
- const unitId = params.unitId;
- const subUnitId = params.subUnitId;
- const selections = params.selections;
const ranges = getAddMergeMutationRangeByType(selections, params.value);
- const worksheet = univerInstanceService.getUniverSheetInstance(unitId)!.getSheetBySheetId(subUnitId)!;
+
+ // First we should check if there are values in the going-to-be-merged cells.
+ const willClearSomeCell = checkCellContentInRanges(worksheet, ranges);
+ if (willClearSomeCell && !params.defaultMerge) {
+ const confirmService = accessor.get(IConfirmService);
+ const localeService = accessor.get(LocaleService);
+
+ const result = await confirmService.confirm({
+ id: 'merge.confirm.add-worksheet-merge',
+ title: {
+ title: 'merge.confirm.warning',
+ },
+ children: {
+ title: 'merge.confirm.title',
+ },
+ cancelText: localeService.t('merge.confirm.cancel'),
+ confirmText: localeService.t('merge.confirm.confirm'),
+ });
+
+ if (!result) return false;
+ }
const redoMutations: IMutationInfo[] = [];
const undoMutations: IMutationInfo[] = [];
- // First we should check if there are values in the going-to-be-merged cells.
- const willRemoveSomeCell = checkCellContentInRanges(worksheet, ranges);
-
// prepare redo mutations
const removeMergeMutationParams: IRemoveWorksheetMergeMutationParams = {
unitId,
subUnitId,
- ranges,
+ ranges: Tools.deepClone(ranges),
};
const addMergeMutationParams: IAddWorksheetMergeMutationParams = {
unitId,
subUnitId,
- ranges,
+ ranges: Tools.deepClone(ranges),
};
- redoMutations.push({ id: RemoveWorksheetMergeMutation.id, params: removeMergeMutationParams });
- redoMutations.push({ id: AddWorksheetMergeMutation.id, params: addMergeMutationParams });
// prepare undo mutations
const undoRemoveMergeMutationParams = RemoveMergeUndoMutationFactory(accessor, removeMergeMutationParams);
const undoMutationParams = AddMergeUndoMutationFactory(accessor, addMergeMutationParams);
+
+ // params should be the merged cells to be deleted accurately, rather than the selection
+ if (undoRemoveMergeMutationParams.ranges.length > 0) {
+ redoMutations.push({ id: RemoveWorksheetMergeMutation.id, params: undoRemoveMergeMutationParams });
+ }
+
+ redoMutations.push({ id: AddWorksheetMergeMutation.id, params: addMergeMutationParams });
undoMutations.push({ id: RemoveWorksheetMergeMutation.id, params: undoMutationParams });
- undoMutations.push({ id: AddWorksheetMergeMutation.id, params: undoRemoveMergeMutationParams });
+ if (undoRemoveMergeMutationParams.ranges.length > 0) {
+ undoMutations.push({ id: AddWorksheetMergeMutation.id, params: undoRemoveMergeMutationParams });
+ }
// add set range values mutations to undo redo mutations
- if (willRemoveSomeCell) {
+ if (willClearSomeCell) {
const data = getClearContentMutationParamsForRanges(accessor, unitId, worksheet, ranges);
redoMutations.unshift(...data.redos);
undoMutations.push(...data.undos);
}
+ const addMergeRedoSelectionsMutation = AddMergeRedoSelectionsOperationFactory(accessor, params, ranges);
+ addMergeRedoSelectionsMutation && redoMutations.push(addMergeRedoSelectionsMutation);
+
+ const addMergeUndoSelectionsMutation = AddMergeUndoSelectionsOperationFactory(accessor, params);
+ addMergeUndoSelectionsMutation && undoMutations.push(addMergeUndoSelectionsMutation);
+
+ const sheetInterceptorService = accessor.get(SheetInterceptorService);
+ const interceptor = sheetInterceptorService.onCommandExecute({
+ id: AddWorksheetMergeCommand.id,
+ params: { unitId, subUnitId, ranges },
+ });
+
+ redoMutations.push(...interceptor.redos);
+ undoMutations.push(...interceptor.undos);
+
const result = sequenceExecute(redoMutations, commandService);
if (result.result) {
undoRedoService.pushUndoRedo({
@@ -182,25 +231,24 @@ export const AddWorksheetMergeAllCommand: ICommand = {
type: CommandType.COMMAND,
id: 'sheet.command.add-worksheet-merge-all',
handler: async (accessor) => {
- const commandService = accessor.get(ICommandService);
const selectionManagerService = accessor.get(SheetsSelectionsService);
const selections = selectionManagerService.getCurrentSelections()?.map((s) => s.range);
- if (!selections?.length) {
+ const mergeableSelections = getMergeableSelectionsByType(MergeType.MergeAll, selections);
+ if (!mergeableSelections?.length) {
return false;
}
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
- if (!workbook) return false;
-
- const workSheet = workbook.getActiveSheet();
- if (!workSheet) return false;
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const target = getSheetCommandTarget(univerInstanceService);
+ if (!target) return false;
- const unitId = workbook.getUnitId();
- const subUnitId = workSheet.getSheetId();
+ const { worksheet } = target;
+ const unitId = worksheet.getUnitId();
+ const subUnitId = worksheet.getSheetId();
+ const commandService = accessor.get(ICommandService);
return commandService.executeCommand(AddWorksheetMergeCommand.id, {
- selections,
+ selections: mergeableSelections,
unitId,
subUnitId,
} as IAddMergeCommandParams);
@@ -211,26 +259,25 @@ export const AddWorksheetMergeVerticalCommand: ICommand = {
type: CommandType.COMMAND,
id: 'sheet.command.add-worksheet-merge-vertical',
handler: async (accessor) => {
- const commandService = accessor.get(ICommandService);
const selectionManagerService = accessor.get(SheetsSelectionsService);
const selections = selectionManagerService.getCurrentSelections()?.map((s) => s.range);
- if (!selections?.length) {
+ const mergeableSelections = getMergeableSelectionsByType(MergeType.MergeVertical, selections);
+ if (!mergeableSelections?.length) {
return false;
}
- const univerInstanceService = accessor.get(IUniverInstanceService);
-
- const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
- if (!workbook) return false;
- const workSheet = workbook.getActiveSheet();
- if (!workSheet) return false;
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const target = getSheetCommandTarget(univerInstanceService);
+ if (!target) return false;
- const unitId = workbook.getUnitId();
- const subUnitId = workSheet.getSheetId();
+ const { worksheet } = target;
+ const unitId = worksheet.getUnitId();
+ const subUnitId = worksheet.getSheetId();
+ const commandService = accessor.get(ICommandService);
return commandService.executeCommand(AddWorksheetMergeCommand.id, {
value: Dimension.COLUMNS,
- selections,
+ selections: mergeableSelections,
unitId,
subUnitId,
} as IAddMergeCommandParams);
@@ -241,25 +288,25 @@ export const AddWorksheetMergeHorizontalCommand: ICommand = {
type: CommandType.COMMAND,
id: 'sheet.command.add-worksheet-merge-horizontal',
handler: async (accessor) => {
- const commandService = accessor.get(ICommandService);
const selectionManagerService = accessor.get(SheetsSelectionsService);
const selections = selectionManagerService.getCurrentSelections()?.map((s) => s.range);
- if (!selections?.length) {
+ const mergeableSelections = getMergeableSelectionsByType(MergeType.MergeHorizontal, selections);
+ if (!mergeableSelections?.length) {
return false;
}
- const univerInstanceService = accessor.get(IUniverInstanceService);
-
- const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
- if (!workbook) return false;
- const workSheet = workbook.getActiveSheet();
- if (!workSheet) return false;
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const target = getSheetCommandTarget(univerInstanceService);
+ if (!target) return false;
+ const { workbook, worksheet } = target;
const unitId = workbook.getUnitId();
- const subUnitId = workSheet.getSheetId();
+ const subUnitId = worksheet.getSheetId();
+ const commandService = accessor.get(ICommandService);
+
return commandService.executeCommand(AddWorksheetMergeCommand.id, {
value: Dimension.ROWS,
- selections,
+ selections: mergeableSelections,
unitId,
subUnitId,
} as IAddMergeCommandParams);
@@ -288,3 +335,31 @@ export function addMergeCellsUtil(injector: Injector, unitId: string, subUnitId:
defaultMerge,
});
}
+
+export function getMergeableSelectionsByType(type: MergeType, selections: Nullable<IRange[]>): Nullable<IRange[]> {
+ if (!selections) return null;
+ if (type === MergeType.MergeAll) {
+ return selections.filter((selection) => {
+ if (selection.startRow === selection.endRow && selection.startColumn === selection.endColumn) {
+ return false;
+ }
+ return true;
+ });
+ } else if (type === MergeType.MergeVertical) {
+ return selections.filter((selection) => {
+ if (selection.startRow === selection.endRow) {
+ return false;
+ }
+ return true;
+ });
+ } else if (type === MergeType.MergeHorizontal) {
+ return selections.filter((selection) => {
+ if (selection.startColumn === selection.endColumn) {
+ return false;
+ }
+ return true;
+ });
+ }
+
+ return selections;
+}
diff --git a/packages/sheets/src/commands/utils/handle-merge-operation.ts b/packages/sheets/src/commands/utils/handle-merge-operation.ts
index 45f1d40831..f1703b82f7 100644
--- a/packages/sheets/src/commands/utils/handle-merge-operation.ts
+++ b/packages/sheets/src/commands/utils/handle-merge-operation.ts
@@ -26,7 +26,7 @@ export const AddMergeRedoSelectionsOperationFactory = (accessor: IAccessor, para
const selectionManagerService = accessor.get(SheetsSelectionsService);
const selectionsBeforeMutation = selectionManagerService.getCurrentSelections();
const { value, selections, unitId, subUnitId } = params;
- if (selectionsBeforeMutation) {
+ if (selectionsBeforeMutation && selectionsBeforeMutation.length > 0) {
const lastSelectionBeforeMutation = selectionsBeforeMutation[selectionsBeforeMutation?.length - 1];
const primaryBeforeMutation = lastSelectionBeforeMutation.primary;
if (primaryBeforeMutation) {
@@ -84,7 +84,7 @@ export const AddMergeUndoSelectionsOperationFactory = (accessor: IAccessor, para
const selectionManagerService = accessor.get(SheetsSelectionsService);
const selectionsBeforeMutation = selectionManagerService.getCurrentSelections();
const { unitId, subUnitId } = params;
- if (selectionsBeforeMutation) {
+ if (selectionsBeforeMutation && selectionsBeforeMutation.length > 0) {
const lastSelectionBeforeMutation = selectionsBeforeMutation[selectionsBeforeMutation?.length - 1];
const primaryBeforeMutation = lastSelectionBeforeMutation.primary;
if (primaryBeforeMutation) {
diff --git a/packages/sheets/src/controllers/basic-worksheet.controller.ts b/packages/sheets/src/controllers/basic-worksheet.controller.ts
index 703f92f851..e5338555ee 100644
--- a/packages/sheets/src/controllers/basic-worksheet.controller.ts
+++ b/packages/sheets/src/controllers/basic-worksheet.controller.ts
@@ -18,6 +18,7 @@ import type { IDisposable, IMutation, IStyleData } from '@univerjs/core';
import { Disposable, ICommandService, IConfigService, Optional } from '@univerjs/core';
import { DataSyncPrimaryController } from '@univerjs/rpc';
import { AddRangeProtectionCommand } from '../commands/commands/add-range-protection.command';
+import { AddWorksheetMergeAllCommand, AddWorksheetMergeCommand, AddWorksheetMergeHorizontalCommand, AddWorksheetMergeVerticalCommand } from '../commands/commands/add-worksheet-merge.command';
import { AddWorksheetProtectionCommand } from '../commands/commands/add-worksheet-protection.command';
import { SetWorksheetRangeThemeStyleCommand } from '../commands/commands/add-worksheet-range-theme.command';
import { AppendRowCommand } from '../commands/commands/append-row.command';
@@ -218,6 +219,10 @@ export class BasicWorksheetController extends Disposable implements IDisposable
const onlyRegisterFormulaRelatedMutations = this._configService.getConfig(ONLY_REGISTER_FORMULA_RELATED_MUTATIONS_KEY) ?? false;
if (!onlyRegisterFormulaRelatedMutations) {
[
+ AddWorksheetMergeCommand,
+ AddWorksheetMergeAllCommand,
+ AddWorksheetMergeVerticalCommand,
+ AddWorksheetMergeHorizontalCommand,
AppendRowCommand,
ClearSelectionAllCommand,
ClearSelectionContentCommand,
diff --git a/packages/sheets/src/facade/__tests__/f-range.spec.ts b/packages/sheets/src/facade/__tests__/f-range.spec.ts
index 97d39b7a33..0ee0f68a50 100644
--- a/packages/sheets/src/facade/__tests__/f-range.spec.ts
+++ b/packages/sheets/src/facade/__tests__/f-range.spec.ts
@@ -18,7 +18,7 @@
import type { ICellData, Injector, IStyleData, Nullable } from '@univerjs/core';
import type { FUniver } from '@univerjs/core/facade';
-import { HorizontalAlign, ICommandService, IUniverInstanceService, LifecycleStages, VerticalAlign, WrapStrategy } from '@univerjs/core';
+import { HorizontalAlign, ICommandService, IConfirmService, IUniverInstanceService, LifecycleStages, TestConfirmService, VerticalAlign, WrapStrategy } from '@univerjs/core';
import { AddWorksheetMergeCommand, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand } from '@univerjs/sheets';
import { beforeEach, describe, expect, it } from 'vitest';
import { createFacadeTestBed } from './create-test-bed';
@@ -41,7 +41,9 @@ describe('Test FRange', () => {
) => Nullable<IStyleData>;
beforeEach(() => {
- const testBed = createFacadeTestBed();
+ const testBed = createFacadeTestBed(undefined, [
+ [IConfirmService, { useClass: TestConfirmService }],
+ ]);
get = testBed.get;
univerAPI = testBed.univerAPI;
diff --git a/packages/sheets/src/facade/__tests__/f-worksheet.spec.ts b/packages/sheets/src/facade/__tests__/f-worksheet.spec.ts
index 3453f4d7ca..974b18f3ae 100644
--- a/packages/sheets/src/facade/__tests__/f-worksheet.spec.ts
+++ b/packages/sheets/src/facade/__tests__/f-worksheet.spec.ts
@@ -16,7 +16,7 @@
import type { Injector, Workbook } from '@univerjs/core';
import type { FUniver } from '@univerjs/core/facade';
-import { ICommandService, IUniverInstanceService, RANGE_TYPE, UniverInstanceType } from '@univerjs/core';
+import { ICommandService, IConfirmService, IUniverInstanceService, RANGE_TYPE, TestConfirmService, UniverInstanceType } from '@univerjs/core';
import { AddWorksheetMergeCommand, AddWorksheetMergeMutation, CancelFrozenCommand, InsertColByRangeCommand, InsertColCommand, InsertColMutation, InsertRowByRangeCommand, InsertRowCommand, InsertRowMutation, MoveColsCommand, MoveColsMutation, MoveRowsCommand, MoveRowsMutation, RemoveColByRangeCommand, RemoveColCommand, RemoveColMutation, RemoveRowByRangeCommand, RemoveRowCommand, RemoveRowMutation, RemoveWorksheetMergeCommand, RemoveWorksheetMergeMutation, SetColDataCommand, SetColDataMutation, SetColHiddenCommand, SetColHiddenMutation, SetColVisibleMutation, SetColWidthCommand, SetFrozenCommand, SetFrozenMutation, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetRowDataCommand, SetRowDataMutation, SetRowHeightCommand, SetRowHiddenCommand, SetRowHiddenMutation, SetRowVisibleMutation, SetSelectionsOperation, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand, SetWorksheetColWidthMutation, SetWorksheetRowHeightMutation, SetWorksheetRowIsAutoHeightCommand, SetWorksheetRowIsAutoHeightMutation, SheetsSelectionsService } from '@univerjs/sheets';
import { beforeEach, describe, expect, it } from 'vitest';
import { createWorksheetTestBed } from './create-worksheet-test-bed';
@@ -29,7 +29,9 @@ describe('Test FWorksheet', () => {
let setSelection: (startRow: number, endRow: number, startColumn: number, endColumn: number) => void;
beforeEach(() => {
- const testBed = createWorksheetTestBed();
+ const testBed = createWorksheetTestBed(undefined, [
+ [IConfirmService, { useClass: TestConfirmService }],
+ ]);
get = testBed.get;
univerAPI = testBed.univerAPI;
diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts
index ae6310d471..b53d4593a6 100644
--- a/packages/sheets/src/index.ts
+++ b/packages/sheets/src/index.ts
@@ -62,6 +62,8 @@ export {
AddWorksheetMergeCommand,
AddWorksheetMergeHorizontalCommand,
AddWorksheetMergeVerticalCommand,
+ getClearContentMutationParamForRange,
+ getClearContentMutationParamsForRanges,
} from './commands/commands/add-worksheet-merge.command';
export { AddWorksheetProtectionCommand } from './commands/commands/add-worksheet-protection.command';
export { SetWorksheetRangeThemeStyleCommand } from './commands/commands/add-worksheet-range-theme.command';
diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.ts
index 333b9ebaaa..963f0e01a6 100644
--- a/packages/ui/src/index.ts
+++ b/packages/ui/src/index.ts
@@ -65,7 +65,6 @@ export {
} from './services/clipboard/clipboard-interface.service';
export { supportClipboardAPI } from './services/clipboard/clipboard-utils';
export { CopyCommand, CutCommand, PasteCommand, SheetPasteShortKeyCommandName } from './services/clipboard/clipboard.command';
-export { IConfirmService, TestConfirmService } from './services/confirm/confirm.service';
export { DesktopConfirmService } from './services/confirm/desktop-confirm.service';
export { ContextMenuService, type IContextMenuHandler, IContextMenuService } from './services/contextmenu/contextmenu.service';
export { DesktopDialogService } from './services/dialog/desktop-dialog.service';
diff --git a/packages/ui/src/mobile-plugin.ts b/packages/ui/src/mobile-plugin.ts
index ff7cfea650..dbf6d04800 100644
--- a/packages/ui/src/mobile-plugin.ts
+++ b/packages/ui/src/mobile-plugin.ts
@@ -15,7 +15,7 @@
*/
import type { IUniverUIConfig } from './controllers/config.schema';
-import { DependentOn, generateRandomId, IConfigService, IContextService, ILocalStorageService, Inject, Injector, merge, mergeOverrideWithDependencies, Plugin, registerDependencies, touchDependencies } from '@univerjs/core';
+import { DependentOn, generateRandomId, IConfigService, IConfirmService, IContextService, ILocalStorageService, Inject, Injector, merge, mergeOverrideWithDependencies, Plugin, registerDependencies, touchDependencies } from '@univerjs/core';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { ComponentManager } from './common/component-manager';
import { ZIndexManager } from './common/z-index-manager';
@@ -27,7 +27,6 @@ import { MobileUIController } from './controllers/ui/ui-mobile.controller';
import { IUIController } from './controllers/ui/ui.controller';
import { DesktopBeforeCloseService, IBeforeCloseService } from './services/before-close/before-close.service';
import { BrowserClipboardService, IClipboardInterfaceService } from './services/clipboard/clipboard-interface.service';
-import { IConfirmService } from './services/confirm/confirm.service';
import { DesktopConfirmService } from './services/confirm/desktop-confirm.service';
import { ContextMenuService, IContextMenuService } from './services/contextmenu/contextmenu.service';
import { DesktopDialogService } from './services/dialog/desktop-dialog.service';
diff --git a/packages/ui/src/plugin.ts b/packages/ui/src/plugin.ts
index 2f8ff92209..f004d6d537 100644
--- a/packages/ui/src/plugin.ts
+++ b/packages/ui/src/plugin.ts
@@ -15,7 +15,7 @@
*/
import type { IUniverUIConfig } from './controllers/config.schema';
-import { DependentOn, generateRandomId, IConfigService, IContextService, ILocalStorageService, Inject, Injector, merge, mergeOverrideWithDependencies, Plugin, registerDependencies, touchDependencies } from '@univerjs/core';
+import { DependentOn, generateRandomId, IConfigService, IConfirmService, IContextService, ILocalStorageService, Inject, Injector, merge, mergeOverrideWithDependencies, Plugin, registerDependencies, touchDependencies } from '@univerjs/core';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { ComponentManager } from './common/component-manager';
import { ZIndexManager } from './common/z-index-manager';
@@ -27,7 +27,6 @@ import { DesktopUIController } from './controllers/ui/ui-desktop.controller';
import { IUIController } from './controllers/ui/ui.controller';
import { DesktopBeforeCloseService, IBeforeCloseService } from './services/before-close/before-close.service';
import { BrowserClipboardService, IClipboardInterfaceService } from './services/clipboard/clipboard-interface.service';
-import { IConfirmService } from './services/confirm/confirm.service';
import { DesktopConfirmService } from './services/confirm/desktop-confirm.service';
import { ContextMenuService, IContextMenuService } from './services/contextmenu/contextmenu.service';
import { DesktopDialogService } from './services/dialog/desktop-dialog.service';
diff --git a/packages/ui/src/services/confirm/desktop-confirm.service.ts b/packages/ui/src/services/confirm/desktop-confirm.service.ts
index 4b97866b96..2e09b65d98 100644
--- a/packages/ui/src/services/confirm/desktop-confirm.service.ts
+++ b/packages/ui/src/services/confirm/desktop-confirm.service.ts
@@ -14,17 +14,15 @@
* limitations under the License.
*/
-import type { IDisposable } from '@univerjs/core';
+import type { IConfirmService, IDisposable } from '@univerjs/core';
import type { IConfirmPartMethodOptions } from '../../views/components/confirm-part/interface';
-import type { IConfirmService } from './confirm.service';
-
import { Disposable, Inject, Injector, toDisposable } from '@univerjs/core';
import { BehaviorSubject } from 'rxjs';
import { connectInjector } from '../../utils/di';
import { ConfirmPart } from '../../views/components/confirm-part/ConfirmPart';
import { BuiltInUIPart, IUIPartsService } from '../parts/parts.service';
-export class DesktopConfirmService extends Disposable implements IConfirmService {
+export class DesktopConfirmService extends Disposable implements IConfirmService<IConfirmPartMethodOptions> {
private _confirmOptions: IConfirmPartMethodOptions[] = [];
readonly confirmOptions$ = new BehaviorSubject<IConfirmPartMethodOptions[]>([]);
diff --git a/packages/ui/src/views/components/confirm-part/ConfirmPart.tsx b/packages/ui/src/views/components/confirm-part/ConfirmPart.tsx
index 1077bbf376..56eb87e115 100644
--- a/packages/ui/src/views/components/confirm-part/ConfirmPart.tsx
+++ b/packages/ui/src/views/components/confirm-part/ConfirmPart.tsx
@@ -16,11 +16,10 @@
import type { IConfirmProps } from '@univerjs/design';
import type { IConfirmChildrenProps, IConfirmPartMethodOptions, IContextConfirmProps } from './interface';
+import { IConfirmService } from '@univerjs/core';
import { Confirm } from '@univerjs/design';
import { cloneElement, useEffect, useState } from 'react';
-
import { CustomLabel } from '../../../components/custom-label/CustomLabel';
-import { IConfirmService } from '../../../services/confirm/confirm.service';
import { useDependency } from '../../../utils/di';
const ContextConfirm = (props: IContextConfirmProps) => {
@@ -66,7 +65,7 @@ const ContextConfirm = (props: IContextConfirmProps) => {
};
export function ConfirmPart() {
- const confirmService = useDependency(IConfirmService);
+ const confirmService = useDependency(IConfirmService) as IConfirmService<IConfirmPartMethodOptions>;
const [confirmOptions, setConfirmOptions] = useState<IConfirmPartMethodOptions[]>([]);