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
fix: getCurrent methods should possibly return null (#1892)
Wenzhao Hu859d7fc2d90f
diff --git a/eslint.config.js b/eslint.config.jsindex 007cc2a4a0..c54e575a2d 100644--- a/eslint.config.js+++ b/eslint.config.js@@ -31,6 +31,8 @@ export default antfu({'playwright.config.ts','**/*.spec.ts','**/*.spec.tsx',+ '**/*.test.ts',+ '**/*.test.tsx',], // do not check test filesrules: {complexity: ['warn', { max: 20 }],diff --git a/examples/src/plugins/debugger/commands/operations/save-snapshot.operations.ts b/examples/src/plugins/debugger/commands/operations/save-snapshot.operations.tsindex 0c14e3734f..d829c52146 100644--- a/examples/src/plugins/debugger/commands/operations/save-snapshot.operations.ts+++ b/examples/src/plugins/debugger/commands/operations/save-snapshot.operations.ts@@ -52,7 +52,7 @@ export const SaveSnapshotOptions: ICommand = {const exportController = accessor.get(ExportController);const recordController = accessor.get(RecordController);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const snapshot = localPersistenceService.saveWorkbook(workbook);const gitHash = process.env.GIT_COMMIT_HASH;diff --git a/examples/src/plugins/debugger/commands/operations/set.editable.operation.ts b/examples/src/plugins/debugger/commands/operations/set.editable.operation.tsindex 41ba48f0b7..47e8c95e28 100644--- a/examples/src/plugins/debugger/commands/operations/set.editable.operation.ts+++ b/examples/src/plugins/debugger/commands/operations/set.editable.operation.ts@@ -34,7 +34,7 @@ export const SetEditable: ICommand = {} else {const univerPermissionService = accessor.get(UniverPermissionService);const univerInstanceService = accessor.get(IUniverInstanceService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();+ const unitId = univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();const editable = univerPermissionService.getEditable(unitId);univerPermissionService.setEditable(unitId, !editable);}diff --git a/examples/src/plugins/debugger/commands/operations/sidebar.operation.ts b/examples/src/plugins/debugger/commands/operations/sidebar.operation.tsindex 3eac6f35d2..33a7eea590 100644--- a/examples/src/plugins/debugger/commands/operations/sidebar.operation.ts+++ b/examples/src/plugins/debugger/commands/operations/sidebar.operation.ts@@ -31,7 +31,7 @@ export const SidebarOperation: ICommand = {const sidebarService = accessor.get(ISidebarService);const editorService = accessor.get(IEditorService);const univerInstanceService = accessor.get(IUniverInstanceService);- const unit = univerInstanceService.getCurrentUniverSheetInstance();+ const unit = univerInstanceService.getCurrentUniverSheetInstance()!;switch (params.value) {case 'open':editorService.setOperationSheetUnitId(unit.getUnitId());diff --git a/examples/src/plugins/debugger/views/test-editor/TestTextEditor.tsx b/examples/src/plugins/debugger/views/test-editor/TestTextEditor.tsxindex 7d09e0d601..41705b6232 100644--- a/examples/src/plugins/debugger/views/test-editor/TestTextEditor.tsx+++ b/examples/src/plugins/debugger/views/test-editor/TestTextEditor.tsx@@ -39,7 +39,7 @@ const editorStyle: React.CSSProperties = {*/export const TestEditorContainer = () => {const univerInstanceService = useDependency(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;if (workbook == null) {return;}diff --git a/packages/core/src/services/instance/instance.service.ts b/packages/core/src/services/instance/instance.service.tsindex 3a3edacba2..0795090342 100644--- a/packages/core/src/services/instance/instance.service.ts+++ b/packages/core/src/services/instance/instance.service.ts@@ -27,13 +27,13 @@ import type { IDocumentData, ISlideData, IWorkbookData } from '../../types/interimport { FOCUSING_DOC, FOCUSING_SHEET, FOCUSING_SLIDE } from '../context/context';import { IContextService } from '../context/context.service';+/**+ * Type of built-in univer document instances.+ */export enum UniverInstanceType {UNKNOWN = 0,-DOC = 1,-SHEET = 2,-SLIDE = 3,}@@ -44,8 +44,10 @@ export interface IUniverHandler {}/**- * IUniverInstanceService holds all the current univer instances. And it also manages- * the focused univer instance.+ * IUniverInstanceService holds all the current univer instances and provides a set of+ * methods to add and remove univer instances.+ *+ * It also manages the focused univer instance.*/export interface IUniverInstanceService {focused$: Observable<Nullable<string>>;@@ -78,9 +80,10 @@ export interface IUniverInstanceService {getUniverDocInstance(id: string): Nullable<DocumentDataModel>;getUniverSlideInstance(id: string): Nullable<SlideDataModel>;- getCurrentUniverSheetInstance(): Workbook;- getCurrentUniverDocInstance(): DocumentDataModel;- getCurrentUniverSlideInstance(): SlideDataModel;+ getCurrentUniverSheetInstance(): Nullable<Workbook>;+ getCurrentUniverDocInstance(): Nullable<DocumentDataModel>;+ getCurrentUniverSlideInstance(): Nullable<SlideDataModel>;+setCurrentUniverSheetInstance(id: string): void;setCurrentUniverDocInstance(id: string): void;setCurrentUniverSlideInstance(id: string): void;diff --git a/packages/core/src/services/permission/univer.permission.service.ts b/packages/core/src/services/permission/univer.permission.service.tsindex df28247310..2d10aeaa2e 100644--- a/packages/core/src/services/permission/univer.permission.service.ts+++ b/packages/core/src/services/permission/univer.permission.service.ts@@ -39,18 +39,18 @@ export class UniverPermissionService extends Disposable {});}- getEditable(unitID?: string) {- let unitId = unitID;+ getEditable(unitId = this._univerInstanceService.getCurrentUniverSheetInstance()?.getUnitId()) {if (!unitId) {- unitId = this._univerInstanceService.getCurrentUniverSheetInstance().getUnitId();+ return;}+const univerEditablePermission = new UniverEditablePermission(unitId);const permission = this._permissionService.getPermissionPoint(unitId, univerEditablePermission.id);return permission?.value;}- setEditable(unitID: string, v: boolean) {- const univerEditablePermission = new UniverEditablePermission(unitID);- this._permissionService.updatePermissionPoint(unitID, univerEditablePermission.id, v);+ setEditable(unitId: string, v: boolean) {+ const univerEditablePermission = new UniverEditablePermission(unitId);+ this._permissionService.updatePermissionPoint(unitId, univerEditablePermission.id, v);}}diff --git a/packages/data-validation/src/controllers/dv-resource.controller.ts b/packages/data-validation/src/controllers/dv-resource.controller.tsindex 1fd0c7283f..56b79c619c 100644--- a/packages/data-validation/src/controllers/dv-resource.controller.ts+++ b/packages/data-validation/src/controllers/dv-resource.controller.ts@@ -82,7 +82,7 @@ export class DataValidationResourceController extends Disposable {})));- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;handleWorkbookAdd(workbook);}}diff --git a/packages/data-validation/src/controllers/dv-sheet.controller.ts b/packages/data-validation/src/controllers/dv-sheet.controller.tsindex e892e6c009..8a3d1d0b39 100644--- a/packages/data-validation/src/controllers/dv-sheet.controller.ts+++ b/packages/data-validation/src/controllers/dv-sheet.controller.ts@@ -39,7 +39,7 @@ export class DataValidationSheetController extends Disposable {getMutations: (commandInfo) => {if (commandInfo.id === RemoveSheetCommand.id) {const params = commandInfo.params as IRemoveSheetCommandParams;- const unitId = params.unitId || this._univerInstanceService.getCurrentUniverSheetInstance().getUnitId();+ const unitId = params.unitId || this._univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();const workbook = this._univerInstanceService.getUniverSheetInstance(unitId);if (!workbook) {return { redos: [], undos: [] };diff --git a/packages/docs-ui/src/controllers/back-scroll.controller.ts b/packages/docs-ui/src/controllers/back-scroll.controller.tsindex 389f64633e..03a70f5d95 100644--- a/packages/docs-ui/src/controllers/back-scroll.controller.ts+++ b/packages/docs-ui/src/controllers/back-scroll.controller.ts@@ -29,7 +29,7 @@ export class BackScrollController extends RxDisposable {@Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,@Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService,@IEditorService private readonly _editorService: IEditorService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService) {super();@@ -117,6 +117,6 @@ export class BackScrollController extends RxDisposable {}private _getDocObject() {- return getDocObject(this._currentUniverService, this._renderManagerService);+ return getDocObject(this._univerInstanceService, this._renderManagerService);}}diff --git a/packages/docs-ui/src/controllers/doc-editor-bridge.controller.ts b/packages/docs-ui/src/controllers/doc-editor-bridge.controller.tsindex 2dcdd963b4..190a62114c 100644--- a/packages/docs-ui/src/controllers/doc-editor-bridge.controller.ts+++ b/packages/docs-ui/src/controllers/doc-editor-bridge.controller.ts@@ -29,7 +29,7 @@ export class DocEditorBridgeController extends Disposable {private _initialEditors = new Set<string>();constructor(- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,@IEditorService private readonly _editorService: IEditorService,@ICommandService private readonly _commandService: ICommandService,@@ -81,7 +81,7 @@ export class DocEditorBridgeController extends Disposable {const skeleton = this._docSkeletonManagerService.getSkeletonByUnitId(unitId)?.skeleton;- const editorDataModel = this._currentUniverService.getUniverDocInstance(unitId);+ const editorDataModel = this._univerInstanceService.getUniverDocInstance(unitId);if (editor == null || editor.render == null || skeleton == null || editorDataModel == null) {return;@@ -166,7 +166,7 @@ export class DocEditorBridgeController extends Disposable {this.disposeWithMe(this._textSelectionRenderManager.onBlur$.subscribe(() => {- const unitId = this._currentUniverService.getCurrentUniverDocInstance().getUnitId();+ const unitId = this._univerInstanceService.getCurrentUniverDocInstance()?.getUnitId();if (unitId == null) {return;}@@ -217,7 +217,7 @@ export class DocEditorBridgeController extends Disposable {// })// );- const currentUniverSheet = this._currentUniverService.getAllUniverSheetsInstance();+ const currentUniverSheet = this._univerInstanceService.getAllUniverSheetsInstance();currentUniverSheet.forEach((unit) => {const unitId = unit.getUnitId();const render = this._renderManagerService.getRenderById(unitId);@@ -248,7 +248,7 @@ export class DocEditorBridgeController extends Disposable {}private _valueChange() {- const unitId = this._currentUniverService.getCurrentUniverDocInstance().getUnitId();+ const unitId = this._univerInstanceService.getCurrentUniverDocInstance()?.getUnitId();if (unitId == null) {return;}diff --git a/packages/docs-ui/src/controllers/doc-ui.controller.ts b/packages/docs-ui/src/controllers/doc-ui.controller.tsindex af82e23525..f03219e329 100644--- a/packages/docs-ui/src/controllers/doc-ui.controller.ts+++ b/packages/docs-ui/src/controllers/doc-ui.controller.ts@@ -60,7 +60,7 @@ export class DocUIController extends Disposable {@IEditorService private readonly _editorService: IEditorService,@IMenuService private readonly _menuService: IMenuService,@IUIController private readonly _uiController: IDesktopUIController,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService) {super();@@ -110,7 +110,7 @@ export class DocUIController extends Disposable {}private _initDocBackground() {- const firstDocUnitId = this._currentUniverService.getAllUniverDocsInstance()[0].getUnitId();+ const firstDocUnitId = this._univerInstanceService.getAllUniverDocsInstance()[0].getUnitId();if (firstDocUnitId == null) {return;}diff --git a/packages/docs-ui/src/controllers/floating-object.controller.ts b/packages/docs-ui/src/controllers/floating-object.controller.tsindex 1e1bd639b0..097bb82cde 100644--- a/packages/docs-ui/src/controllers/floating-object.controller.ts+++ b/packages/docs-ui/src/controllers/floating-object.controller.ts@@ -39,7 +39,7 @@ export class FloatingObjectController extends Disposable {constructor(@Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@ICommandService private readonly _commandService: ICommandService,@IFloatingObjectManagerService private readonly _floatingObjectManagerService: IFloatingObjectManagerService,diff --git a/packages/docs-ui/src/controllers/menu/menu.ts b/packages/docs-ui/src/controllers/menu/menu.tsindex 442707a214..6c04ed9909 100644--- a/packages/docs-ui/src/controllers/menu/menu.ts+++ b/packages/docs-ui/src/controllers/menu/menu.ts@@ -612,9 +612,9 @@ export function BackgroundColorSelectorMenuItemFactory(accessor: IAccessor): IMe}function getFontStyleAtCursor(accessor: IAccessor) {- const currentUniverService = accessor.get(IUniverInstanceService);+ const univerInstanceService = accessor.get(IUniverInstanceService);const textSelectionService = accessor.get(TextSelectionManagerService);- const editorDataModel = currentUniverService.getCurrentUniverDocInstance();+ const editorDataModel = univerInstanceService.getCurrentUniverDocInstance();const activeTextRange = textSelectionService.getActiveRange();if (editorDataModel == null || activeTextRange == null) {@@ -635,9 +635,9 @@ function getFontStyleAtCursor(accessor: IAccessor) {}function getParagraphStyleAtCursor(accessor: IAccessor) {- const currentUniverService = accessor.get(IUniverInstanceService);+ const univerInstanceService = accessor.get(IUniverInstanceService);const textSelectionService = accessor.get(TextSelectionManagerService);- const editorDataModel = currentUniverService.getCurrentUniverDocInstance();+ const editorDataModel = univerInstanceService.getCurrentUniverDocInstance();const activeTextRange = textSelectionService.getActiveRange();if (editorDataModel == null || activeTextRange == null) {diff --git a/packages/docs-ui/src/controllers/page-render.controller.ts b/packages/docs-ui/src/controllers/page-render.controller.tsindex c0cde3b93e..3848456a1d 100644--- a/packages/docs-ui/src/controllers/page-render.controller.ts+++ b/packages/docs-ui/src/controllers/page-render.controller.ts@@ -34,7 +34,7 @@ export class PageRenderController extends Disposable {constructor(@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@IEditorService private readonly _editorService: IEditorService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService) {super();@@ -55,7 +55,7 @@ export class PageRenderController extends Disposable {const currentRender = this._renderManagerService.getRenderById(unitId);- if (this._editorService.isEditor(unitId) || this._currentUniverService.getUniverDocInstance(unitId) == null) {+ if (this._editorService.isEditor(unitId) || this._univerInstanceService.getUniverDocInstance(unitId) == null) {return;}diff --git a/packages/docs-ui/src/controllers/text-selection.controller.ts b/packages/docs-ui/src/controllers/text-selection.controller.tsindex 50b9d26864..54d47b8631 100644--- a/packages/docs-ui/src/controllers/text-selection.controller.ts+++ b/packages/docs-ui/src/controllers/text-selection.controller.ts@@ -30,7 +30,7 @@ export class TextSelectionController extends Disposable {constructor(@Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@ICommandService private readonly _commandService: ICommandService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@ITextSelectionRenderManager@@ -66,7 +66,7 @@ export class TextSelectionController extends Disposable {return;}- if (this._currentUniverService.getUniverDocInstance(unitId) == null) {+ if (this._univerInstanceService.getUniverDocInstance(unitId) == null) {return;}@@ -115,10 +115,9 @@ export class TextSelectionController extends Disposable {return;}- const currentDocInstance = this._currentUniverService.getCurrentUniverDocInstance();-- if (currentDocInstance.getUnitId() !== unitId) {- this._currentUniverService.setCurrentUniverDocInstance(unitId);+ const currentDocInstance = this._univerInstanceService.getCurrentUniverDocInstance();+ if (currentDocInstance?.getUnitId() !== unitId) {+ this._univerInstanceService.setCurrentUniverDocInstance(unitId);}this._textSelectionRenderManager.eventTrigger(evt);@@ -183,9 +182,9 @@ export class TextSelectionController extends Disposable {/*** The object for selecting data in the editor is set to the current sheet.*/- const sheetInstances = this._currentUniverService.getAllUniverSheetsInstance();+ const sheetInstances = this._univerInstanceService.getAllUniverSheetsInstance();if (sheetInstances.length > 0) {- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;this._editorService.setOperationSheetUnitId(workbook.getUnitId());// this._editorService.setOperationSheetSubUnitId(workbook.getActiveSheet().getSheetId());}diff --git a/packages/docs-ui/src/controllers/zoom.controller.ts b/packages/docs-ui/src/controllers/zoom.controller.tsindex 79c1b62cb6..dd1339db25 100644--- a/packages/docs-ui/src/controllers/zoom.controller.ts+++ b/packages/docs-ui/src/controllers/zoom.controller.ts@@ -40,7 +40,7 @@ export class ZoomController extends Disposable {constructor(@Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@ICommandService private readonly _commandService: ICommandService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService,@@ -92,6 +92,11 @@ export class ZoomController extends Disposable {return;}+ const documentModel = this._univerInstanceService.getCurrentUniverDocInstance();+ if (!documentModel) {+ return;+ }+const deltaFactor = Math.abs(e.deltaX);let ratioDelta = deltaFactor < 40 ? 0.2 : deltaFactor < 80 ? 0.4 : 0.2;ratioDelta *= e.deltaY > 0 ? -1 : 1;@@ -99,8 +104,6 @@ export class ZoomController extends Disposable {ratioDelta /= 2;}- const documentModel = this._currentUniverService.getCurrentUniverDocInstance();-const currentRatio = documentModel.zoomRatio;let nextRatio = +Number.parseFloat(`${currentRatio + ratioDelta}`).toFixed(1);@@ -135,7 +138,8 @@ export class ZoomController extends Disposable {return;}- const documentModel = this._currentUniverService.getCurrentUniverDocInstance();+ const documentModel = this._univerInstanceService.getCurrentUniverDocInstance();+ if (!documentModel) return;const zoomRatio = documentModel.zoomRatio || 1;@@ -151,7 +155,8 @@ export class ZoomController extends Disposable {this.disposeWithMe(this._commandService.onCommandExecuted((command: ICommandInfo) => {if (updateCommandList.includes(command.id)) {- const documentModel = this._currentUniverService.getCurrentUniverDocInstance();+ const documentModel = this._univerInstanceService.getCurrentUniverDocInstance();+ if (!documentModel) return;const params = command.params;const { unitId } = params as ISetDocMutationParams;@@ -244,6 +249,6 @@ export class ZoomController extends Disposable {}private _getDocObject() {- return getDocObject(this._currentUniverService, this._renderManagerService);+ return getDocObject(this._univerInstanceService, this._renderManagerService);}}diff --git a/packages/docs-ui/src/docs-ui-plugin.ts b/packages/docs-ui/src/docs-ui-plugin.tsindex b811426aef..37f65c13cb 100644--- a/packages/docs-ui/src/docs-ui-plugin.ts+++ b/packages/docs-ui/src/docs-ui-plugin.ts@@ -143,6 +143,8 @@ export class UniverDocsUIPlugin extends Plugin {const editorService = this._injector.get(IEditorService);try {const doc = currentService.getCurrentUniverDocInstance();+ if (!doc) return;+const id = doc.getUnitId();if (!editorService.isEditor(id)) {currentService.focusUniverInstance(doc.getUnitId());diff --git a/packages/docs-ui/src/services/clipboard/clipboard.service.ts b/packages/docs-ui/src/services/clipboard/clipboard.service.tsindex 4cf1f8686d..0c6cf5a858 100644--- a/packages/docs-ui/src/services/clipboard/clipboard.service.ts+++ b/packages/docs-ui/src/services/clipboard/clipboard.service.ts@@ -74,7 +74,7 @@ export class DocClipboardService extends Disposable implements IDocClipboardServprivate _umdToHtml = new UDMToHtmlService();constructor(- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@ILogService private readonly _logService: ILogService,@ICommandService private readonly _commandService: ICommandService,@IClipboardInterfaceService private readonly _clipboardInterfaceService: IClipboardInterfaceService,@@ -236,11 +236,10 @@ export class DocClipboardService extends Disposable implements IDocClipboardServprivate _getDocumentBodyInRanges(): IDocumentBody[] {const ranges = this._textSelectionManagerService.getSelections();- const docDataModel = this._currentUniverService.getCurrentUniverDocInstance();-+ const doc = this._univerInstanceService.getCurrentUniverDocInstance();const results: IDocumentBody[] = [];- if (ranges == null) {+ if (ranges == null || !doc) {return results;}@@ -255,7 +254,7 @@ export class DocClipboardService extends Disposable implements IDocClipboardServcontinue;}- const docBody = docDataModel.sliceBody(startOffset, endOffset);+ const docBody = doc.sliceBody(startOffset, endOffset);if (docBody == null) {continue;}diff --git a/packages/docs-ui/src/views/doc-canvas-view.ts b/packages/docs-ui/src/views/doc-canvas-view.tsindex 9d97aa19ba..2c34e5050b 100644--- a/packages/docs-ui/src/views/doc-canvas-view.ts+++ b/packages/docs-ui/src/views/doc-canvas-view.ts@@ -41,7 +41,7 @@ export class DocCanvasView extends RxDisposable {constructor(@IRenderManagerService private readonly _renderManagerService: RenderManagerService,@IConfigService private readonly _configService: IConfigService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IEditorService private readonly _editorService: IEditorService) {super();@@ -53,11 +53,11 @@ export class DocCanvasView extends RxDisposable {this._create(unitId);});- this._currentUniverService.currentDoc$.pipe(takeUntil(this.dispose$)).subscribe((documentModel) => {+ this._univerInstanceService.currentDoc$.pipe(takeUntil(this.dispose$)).subscribe((documentModel) => {this._create(documentModel?.getUnitId());});- this._currentUniverService.getAllUniverDocsInstance().forEach((documentModel) => {+ this._univerInstanceService.getAllUniverDocsInstance().forEach((documentModel) => {this._create(documentModel.getUnitId());});}@@ -71,7 +71,7 @@ export class DocCanvasView extends RxDisposable {return;}- const model = this._currentUniverService.getUniverDocInstance(unitId);+ const model = this._univerInstanceService.getUniverDocInstance(unitId);if (model == null) {return;diff --git a/packages/docs/src/basics/component-tools.ts b/packages/docs/src/basics/component-tools.tsindex d44be61a90..9ad7c1e7ef 100644--- a/packages/docs/src/basics/component-tools.ts+++ b/packages/docs/src/basics/component-tools.ts@@ -28,17 +28,17 @@ export function getDocObject(renderManagerService: IRenderManagerService): Nullable<IDocObjectParam> {const documentModel = univerInstanceService.getCurrentUniverDocInstance();+ if (!documentModel) {+ return null;+ }const unitId = documentModel.getUnitId();-const currentRender = renderManagerService.getRenderById(unitId);-if (currentRender == null) {return;}const { mainComponent, scene, engine } = currentRender;-const document = mainComponent as Documents;return {diff --git a/packages/docs/src/commands/commands/break-line.command.ts b/packages/docs/src/commands/commands/break-line.command.tsindex f95ef32772..b1c32e783e 100644--- a/packages/docs/src/commands/commands/break-line.command.ts+++ b/packages/docs/src/commands/commands/break-line.command.ts@@ -57,18 +57,20 @@ export const BreakLineCommand: ICommand = {handler: async (accessor) => {const textSelectionManagerService = accessor.get(TextSelectionManagerService);- const currentUniverService = accessor.get(IUniverInstanceService);+ const univerInstanceService = accessor.get(IUniverInstanceService);const commandService = accessor.get(ICommandService);const activeRange = textSelectionManagerService.getActiveRange();-if (activeRange == null) {return false;}- const docDataModel = currentUniverService.getCurrentUniverDocInstance();- const unitId = docDataModel.getUnitId();+ const docDataModel = univerInstanceService.getCurrentUniverDocInstance();+ if (!docDataModel) {+ return false;+ }+ const unitId = docDataModel.getUnitId();const { startOffset, segmentId, style } = activeRange;// move selectiondiff --git a/packages/docs/src/commands/commands/clipboard.inner.command.ts b/packages/docs/src/commands/commands/clipboard.inner.command.tsindex 236c414934..f4cdb57f5f 100644--- a/packages/docs/src/commands/commands/clipboard.inner.command.ts+++ b/packages/docs/src/commands/commands/clipboard.inner.command.ts@@ -54,15 +54,18 @@ export const InnerPasteCommand: ICommand<IInnerPasteCommandParams> = {const { segmentId, body, textRanges } = params;const commandService = accessor.get(ICommandService);const textSelectionManagerService = accessor.get(TextSelectionManagerService);- const currentUniverService = accessor.get(IUniverInstanceService);+ const univerInstanceService = accessor.get(IUniverInstanceService);const selections = textSelectionManagerService.getSelections();-if (!Array.isArray(selections) || selections.length === 0) {return false;}- const docsModel = currentUniverService.getCurrentUniverDocInstance();+ const docsModel = univerInstanceService.getCurrentUniverDocInstance();+ if (!docsModel) {+ return false;+ }+const unitId = docsModel.getUnitId();const doMutation: IMutationInfo<IRichTextEditingMutationParams> = {@@ -132,7 +135,7 @@ export const CutContentCommand: ICommand<IInnerCutCommandParams> = {const { segmentId, textRanges } = params;const commandService = accessor.get(ICommandService);const textSelectionManagerService = accessor.get(TextSelectionManagerService);- const currentUniverService = accessor.get(IUniverInstanceService);+ const univerInstanceService = accessor.get(IUniverInstanceService);const selections = textSelectionManagerService.getSelections();@@ -140,12 +143,13 @@ export const CutContentCommand: ICommand<IInnerCutCommandParams> = {return false;}- const unitId = currentUniverService.getCurrentUniverDocInstance().getUnitId();-- const documentModel = currentUniverService.getUniverDocInstance(unitId);+ const unitId = univerInstanceService.getCurrentUniverDocInstance()?.getUnitId();+ if (!unitId) {+ return false;+ }+ const documentModel = univerInstanceService.getUniverDocInstance(unitId);const originBody = getDocsUpdateBody(documentModel!.snapshot, segmentId);-if (originBody == null) {return false;}diff --git a/packages/docs/src/commands/commands/delete.command.ts b/packages/docs/src/commands/commands/delete.command.tsindex 1e7fb6932b..e4ae0fb13d 100644--- a/packages/docs/src/commands/commands/delete.command.ts+++ b/packages/docs/src/commands/commands/delete.command.ts@@ -42,7 +42,7 @@ export const DeleteLeftCommand: ICommand = {handler: async (accessor) => {const textSelectionManagerService = accessor.get(TextSelectionManagerService);const docSkeletonManagerService = accessor.get(DocSkeletonManagerService);- const currentUniverService = accessor.get(IUniverInstanceService);+ const univerInstanceService = accessor.get(IUniverInstanceService);const commandService = accessor.get(ICommandService);const activeRange = textSelectionManagerService.getActiveRange();@@ -55,7 +55,10 @@ export const DeleteLeftCommand: ICommand = {return false;}- const docDataModel = currentUniverService.getCurrentUniverDocInstance();+ const docDataModel = univerInstanceService.getCurrentUniverDocInstance();+ if (!docDataModel) {+ return false;+ }const { startOffset, collapsed, segmentId, style } = activeRange;@@ -190,7 +193,7 @@ export const DeleteRightCommand: ICommand = {handler: async (accessor) => {const textSelectionManagerService = accessor.get(TextSelectionManagerService);const docSkeletonManagerService = accessor.get(DocSkeletonManagerService);- const currentUniverService = accessor.get(IUniverInstanceService);+ const univerInstanceService = accessor.get(IUniverInstanceService);const commandService = accessor.get(ICommandService);const activeRange = textSelectionManagerService.getActiveRange();@@ -204,7 +207,10 @@ export const DeleteRightCommand: ICommand = {return false;}- const docDataModel = currentUniverService.getCurrentUniverDocInstance();+ const docDataModel = univerInstanceService.getCurrentUniverDocInstance();+ if (!docDataModel) {+ return false;+ }const { startOffset, collapsed, segmentId, style } = activeRange;@@ -266,7 +272,7 @@ export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {handler: async (accessor, params: IMergeTwoParagraphParams) => {const textSelectionManagerService = accessor.get(TextSelectionManagerService);- const currentUniverService = accessor.get(IUniverInstanceService);+ const univerInstanceService = accessor.get(IUniverInstanceService);const commandService = accessor.get(ICommandService);const { direction, range } = params;@@ -278,7 +284,10 @@ export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {return false;}- const docDataModel = currentUniverService.getCurrentUniverDocInstance();+ const docDataModel = univerInstanceService.getCurrentUniverDocInstance();+ if (!docDataModel) {+ return false;+ }const { startOffset, collapsed, segmentId, style } = activeRange;diff --git a/packages/docs/src/commands/commands/inline-format.command.ts b/packages/docs/src/commands/commands/inline-format.command.tsindex 6562489546..77a50abb80 100644--- a/packages/docs/src/commands/commands/inline-format.command.ts+++ b/packages/docs/src/commands/commands/inline-format.command.ts@@ -267,7 +267,7 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {const { segmentId, value, preCommandId } = params;const commandService = accessor.get(ICommandService);const textSelectionManagerService = accessor.get(TextSelectionManagerService);- const currentUniverService = accessor.get(IUniverInstanceService);+ const univerInstanceService = accessor.get(IUniverInstanceService);const selections = textSelectionManagerService.getSelections();@@ -275,13 +275,17 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {return false;}- let docsModel = currentUniverService.getCurrentUniverDocInstance();+ let docsModel = univerInstanceService.getCurrentUniverDocInstance();+ if (!docsModel) {+ return false;+ }+let unitId = docsModel.getUnitId();// When setting the formula bar style, the effect will be displayed in the cell editor,// and the formula bar only displays plain text.if (unitId === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY) {- docsModel = currentUniverService.getUniverDocInstance(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)!;+ docsModel = univerInstanceService.getUniverDocInstance(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)!;unitId = docsModel.getUnitId();}diff --git a/packages/docs/src/commands/commands/list.command.ts b/packages/docs/src/commands/commands/list.command.tsindex b9e9b61cf9..d39ffa256f 100644--- a/packages/docs/src/commands/commands/list.command.ts+++ b/packages/docs/src/commands/commands/list.command.ts@@ -44,12 +44,15 @@ export const ListOperationCommand: ICommand<IListOperationCommandParams> = {handler: (accessor, params: IListOperationCommandParams) => {const textSelectionManagerService = accessor.get(TextSelectionManagerService);- const currentUniverService = accessor.get(IUniverInstanceService);+ const univerInstanceService = accessor.get(IUniverInstanceService);const commandService = accessor.get(ICommandService);const { listType } = params;- const dataModel = currentUniverService.getCurrentUniverDocInstance();+ const dataModel = univerInstanceService.getCurrentUniverDocInstance();+ if (!dataModel) {+ return false;+ }const activeRange = textSelectionManagerService.getActiveRange();const selections = textSelectionManagerService.getSelections() ?? [];@@ -175,7 +178,7 @@ export const ListOperationCommand: ICommand<IListOperationCommandParams> = {},};-interface IBulletListCommandParams {}+interface IBulletListCommandParams { }export const BulletListCommand: ICommand<IBulletListCommandParams> = {id: 'doc.command.bullet-list',@@ -191,7 +194,7 @@ export const BulletListCommand: ICommand<IBulletListCommandParams> = {},};-interface IOrderListCommandParams {}+interface IOrderListCommandParams { }export const OrderListCommand: ICommand<IOrderListCommandParams> = {id: 'doc.command.order-list',diff --git a/packages/docs/src/commands/commands/paragraph-align.command.ts b/packages/docs/src/commands/commands/paragraph-align.command.tsindex 504ba6fa44..c1b81b2938 100644--- a/packages/docs/src/commands/commands/paragraph-align.command.ts+++ b/packages/docs/src/commands/commands/paragraph-align.command.ts@@ -15,7 +15,8 @@*/import type { ICommand, IMutationInfo, IParagraphStyle } from '@univerjs/core';-import { CommandType, HorizontalAlign,+import {+ CommandType, HorizontalAlign,ICommandService,IUniverInstanceService,MemoryCursor,@@ -40,28 +41,24 @@ export const AlignOperationCommand: ICommand<IAlignOperationCommandParams> = {handler: (accessor, params: IAlignOperationCommandParams) => {const textSelectionManagerService = accessor.get(TextSelectionManagerService);- const currentUniverService = accessor.get(IUniverInstanceService);+ const univerInstanceService = accessor.get(IUniverInstanceService);const commandService = accessor.get(ICommandService);const { alignType } = params;- const dataModel = currentUniverService.getCurrentUniverDocInstance();+ const dataModel = univerInstanceService.getCurrentUniverDocInstance();+ if (!dataModel) return false;const activeRange = textSelectionManagerService.getActiveRange();const selections = textSelectionManagerService.getSelections() ?? [];const paragraphs = dataModel.getBody()?.paragraphs;const serializedSelections = selections.map(serializeTextRange);- if (activeRange == null || paragraphs == null) {- return false;- }+ if (activeRange == null || paragraphs == null) return false;const currentParagraphs = getParagraphsInRange(activeRange, paragraphs);-const { segmentId } = activeRange;-const unitId = dataModel.getUnitId();-const isAlreadyAligned = currentParagraphs.every((paragraph) => paragraph.paragraphStyle?.horizontalAlign === alignType);const doMutation: IMutationInfo<IRichTextEditingMutationParams> = {@@ -74,11 +71,9 @@ export const AlignOperationCommand: ICommand<IAlignOperationCommandParams> = {};const memoryCursor = new MemoryCursor();-memoryCursor.reset();const textX = new TextX();-for (const paragraph of currentParagraphs) {const { startIndex } = paragraph;@@ -125,7 +120,7 @@ export const AlignOperationCommand: ICommand<IAlignOperationCommandParams> = {},};-interface IAlignLeftCommandParams {}+interface IAlignLeftCommandParams { }export const AlignLeftCommand: ICommand<IAlignLeftCommandParams> = {id: 'doc.command.align-left',@@ -141,7 +136,7 @@ export const AlignLeftCommand: ICommand<IAlignLeftCommandParams> = {},};-interface IAlignCenterCommandParams {}+interface IAlignCenterCommandParams { }export const AlignCenterCommand: ICommand<IAlignCenterCommandParams> = {id: 'doc.command.align-center',@@ -157,7 +152,7 @@ export const AlignCenterCommand: ICommand<IAlignCenterCommandParams> = {},};-interface IAlignRightCommandParams {}+interface IAlignRightCommandParams { }export const AlignRightCommand: ICommand<IAlignRightCommandParams> = {id: 'doc.command.align-right',@@ -173,7 +168,7 @@ export const AlignRightCommand: ICommand<IAlignRightCommandParams> = {},};-interface IAlignJustifyCommandParams {}+interface IAlignJustifyCommandParams { }export const AlignJustifyCommand: ICommand<IAlignJustifyCommandParams> = {id: 'doc.command.align-justify',diff --git a/packages/docs/src/commands/commands/set-doc-zoom-ratio.command.ts b/packages/docs/src/commands/commands/set-doc-zoom-ratio.command.tsindex 70789be185..0e25769cb9 100644--- a/packages/docs/src/commands/commands/set-doc-zoom-ratio.command.ts+++ b/packages/docs/src/commands/commands/set-doc-zoom-ratio.command.ts@@ -38,7 +38,9 @@ export const SetDocZoomRatioCommand: ICommand = {const undoRedoService = accessor.get(IUndoRedoService);const univerInstanceService = accessor.get(IUniverInstanceService);- let documentId = univerInstanceService.getCurrentUniverDocInstance().getUnitId();+ let documentId = univerInstanceService.getCurrentUniverDocInstance()?.getUnitId();+ if (!documentId) return false;+let zoomRatio = 1;if (params) {diff --git a/packages/docs/src/commands/operations/select-all.operation.ts b/packages/docs/src/commands/operations/select-all.operation.tsindex 7a0193b0eb..e79c3d35f3 100644--- a/packages/docs/src/commands/operations/select-all.operation.ts+++ b/packages/docs/src/commands/operations/select-all.operation.ts@@ -19,7 +19,7 @@ import { CommandType, IUniverInstanceService } from '@univerjs/core';import { TextSelectionManagerService } from '../../services/text-selection-manager.service';-interface ISelectAllOperationParams {}+interface ISelectAllOperationParams { }export const SelectAllOperation: ICommand<ISelectAllOperationParams> = {id: 'doc.operation.select-all',@@ -30,7 +30,10 @@ export const SelectAllOperation: ICommand<ISelectAllOperationParams> = {const univerInstanceService = accessor.get(IUniverInstanceService);const textSelectionManagerService = accessor.get(TextSelectionManagerService);- const prevBody = univerInstanceService.getCurrentUniverDocInstance().getSnapshot().body;+ const currentDoc = univerInstanceService.getCurrentUniverDocInstance();+ if (!currentDoc) return false;++ const prevBody = currentDoc.getSnapshot().body;if (prevBody == null) {return false;diff --git a/packages/docs/src/controllers/ime-input.controller.ts b/packages/docs/src/controllers/ime-input.controller.tsindex 43a8d1dfed..299e14071f 100644--- a/packages/docs/src/controllers/ime-input.controller.ts+++ b/packages/docs/src/controllers/ime-input.controller.ts@@ -46,7 +46,7 @@ export class IMEInputController extends Disposable {constructor(@Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@ITextSelectionRenderManager private readonly _textSelectionRenderManager: ITextSelectionRenderManager,@Inject(IMEInputManagerService) private readonly _imeInputManagerService: IMEInputManagerService,@ICommandService private readonly _commandService: ICommandService@@ -107,7 +107,10 @@ export class IMEInputController extends Disposable {return;}- const documentModel = this._currentUniverService.getCurrentUniverDocInstance();+ const documentModel = this._univerInstanceService.getCurrentUniverDocInstance();+ if (!documentModel) {+ return;+ }const { event, activeRange } = config;diff --git a/packages/docs/src/controllers/move-cursor.controller.ts b/packages/docs/src/controllers/move-cursor.controller.tsindex 540b4e3a6b..1929cd03a5 100644--- a/packages/docs/src/controllers/move-cursor.controller.ts+++ b/packages/docs/src/controllers/move-cursor.controller.ts@@ -46,7 +46,7 @@ export class MoveCursorController extends Disposable {constructor(@Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService,@ICommandService private readonly _commandService: ICommandService@@ -62,7 +62,7 @@ export class MoveCursorController extends Disposable {this._onInputSubscription?.unsubscribe();}- private _initialize() {}+ private _initialize() { }private _commandExecutedListener() {const updateCommandList = [MoveCursorOperation.id, MoveSelectionOperation.id];@@ -95,7 +95,10 @@ export class MoveCursorController extends Disposable {private _handleShiftMoveSelection(direction: Direction) {const activeRange = this._textSelectionManagerService.getActiveRange();const allRanges = this._textSelectionManagerService.getSelections()!;- const docDataModel = this._currentUniverService.getCurrentUniverDocInstance();+ const docDataModel = this._univerInstanceService.getCurrentUniverDocInstance();+ if (!docDataModel) {+ return;+ }const skeleton = this._docSkeletonManagerService.getCurrent()?.skeleton;@@ -196,7 +199,10 @@ export class MoveCursorController extends Disposable {private _handleMoveCursor(direction: Direction) {const activeRange = this._textSelectionManagerService.getActiveRange();const allRanges = this._textSelectionManagerService.getSelections();- const docDataModel = this._currentUniverService.getCurrentUniverDocInstance();+ const docDataModel = this._univerInstanceService.getCurrentUniverDocInstance();+ if (!docDataModel) {+ return false;+ }const skeleton = this._docSkeletonManagerService.getCurrent()?.skeleton;@@ -479,6 +485,6 @@ export class MoveCursorController extends Disposable {}private _getDocObject() {- return getDocObject(this._currentUniverService, this._renderManagerService);+ return getDocObject(this._univerInstanceService, this._renderManagerService);}}diff --git a/packages/docs/src/controllers/normal-input.controller.ts b/packages/docs/src/controllers/normal-input.controller.tsindex 3277098b50..2d937a3438 100644--- a/packages/docs/src/controllers/normal-input.controller.ts+++ b/packages/docs/src/controllers/normal-input.controller.ts@@ -30,7 +30,7 @@ export class NormalInputController extends Disposable {constructor(@Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@ITextSelectionRenderManager private readonly _textSelectionRenderManager: ITextSelectionRenderManager,@ICommandService private readonly _commandService: ICommandService@@ -56,7 +56,10 @@ export class NormalInputController extends Disposable {return;}- const documentModel = this._currentUniverService.getCurrentUniverDocInstance();+ const documentModel = this._univerInstanceService.getCurrentUniverDocInstance();+ if (!documentModel) {+ return;+ }const unitId = documentModel.getUnitId();@@ -98,9 +101,9 @@ export class NormalInputController extends Disposable {});}- private _commandExecutedListener() {}+ private _commandExecutedListener() { }private _getDocObject() {- return getDocObject(this._currentUniverService, this._renderManagerService);+ return getDocObject(this._univerInstanceService, this._renderManagerService);}}diff --git a/packages/docs/src/services/doc-skeleton-manager.service.ts b/packages/docs/src/services/doc-skeleton-manager.service.tsindex 1fa583fd33..f1bd2d61b8 100644--- a/packages/docs/src/services/doc-skeleton-manager.service.ts+++ b/packages/docs/src/services/doc-skeleton-manager.service.ts@@ -49,7 +49,7 @@ export class DocSkeletonManagerService extends RxDisposable {constructor(@Inject(LocaleService) private readonly _localeService: LocaleService,@Inject(DocViewModelManagerService) private readonly _docViewModelManagerService: DocViewModelManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService) {super();this._initialize();@@ -84,10 +84,10 @@ export class DocSkeletonManagerService extends RxDisposable {this._setCurrent(docViewModel);});- this._currentUniverService.docDisposed$.pipe(takeUntil(this.dispose$)).subscribe((documentModel) => {+ this._univerInstanceService.docDisposed$.pipe(takeUntil(this.dispose$)).subscribe((documentModel) => {this._docSkeletonMap.delete(documentModel.getUnitId());- this._currentSkeletonUnitId = this._currentUniverService.getCurrentUniverDocInstance().getUnitId();+ this._currentSkeletonUnitId = this._univerInstanceService.getCurrentUniverDocInstance()?.getUnitId() ?? '';});}diff --git a/packages/docs/src/services/doc-view-model-manager.service.ts b/packages/docs/src/services/doc-view-model-manager.service.tsindex f1cbeb6fae..31d1124799 100644--- a/packages/docs/src/services/doc-view-model-manager.service.ts+++ b/packages/docs/src/services/doc-view-model-manager.service.ts@@ -33,7 +33,7 @@ export class DocViewModelManagerService extends RxDisposable {private readonly _currentDocViewModel$ = new BehaviorSubject<Nullable<IDocumentViewModelManagerParam>>(null);readonly currentDocViewModel$ = this._currentDocViewModel$.asObservable();- constructor(@IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService) {+ constructor(@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService) {super();this._initialize();}@@ -48,15 +48,15 @@ export class DocViewModelManagerService extends RxDisposable {}private _init() {- this._currentUniverService.currentDoc$.pipe(takeUntil(this.dispose$)).subscribe((documentModel) => {+ this._univerInstanceService.currentDoc$.pipe(takeUntil(this.dispose$)).subscribe((documentModel) => {this._create(documentModel);});- this._currentUniverService.getAllUniverDocsInstance().forEach((documentModel) => {+ this._univerInstanceService.getAllUniverDocsInstance().forEach((documentModel) => {this._create(documentModel);});- this._currentUniverService.docDisposed$.pipe(takeUntil(this.dispose$)).subscribe((documentModel) => {+ this._univerInstanceService.docDisposed$.pipe(takeUntil(this.dispose$)).subscribe((documentModel) => {this._docViewModelMap.delete(documentModel.getUnitId());});}@@ -80,7 +80,7 @@ export class DocViewModelManagerService extends RxDisposable {}private _setCurrent(unitId: string) {- const documentDataModel = this._currentUniverService.getUniverDocInstance(unitId);+ const documentDataModel = this._univerInstanceService.getUniverDocInstance(unitId);if (documentDataModel == null) {throw new Error(`Document data model with id ${unitId} not found when build view model.`);}diff --git a/packages/engine-formula/src/controller/calculate.controller.ts b/packages/engine-formula/src/controller/calculate.controller.tsindex 4278fb5dba..811f4347ce 100644--- a/packages/engine-formula/src/controller/calculate.controller.ts+++ b/packages/engine-formula/src/controller/calculate.controller.ts@@ -42,7 +42,7 @@ export class CalculateController extends Disposable {constructor(@ICommandService private readonly _commandService: ICommandService,@Inject(CalculateFormulaService) private readonly _calculateFormulaService: CalculateFormulaService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@Inject(FormulaDataModel) private readonly _formulaDataModel: FormulaDataModel) {super();diff --git a/packages/engine-formula/src/engine/analysis/__tests__/create-command-test-bed.ts b/packages/engine-formula/src/engine/analysis/__tests__/create-command-test-bed.tsindex f5a6a34fab..80022c0fe4 100644--- a/packages/engine-formula/src/engine/analysis/__tests__/create-command-test-bed.ts+++ b/packages/engine-formula/src/engine/analysis/__tests__/create-command-test-bed.ts@@ -244,32 +244,7 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie}override onStarting(injector: Injector): void {- injector.add([CalculateFormulaService]);- injector.add([Lexer]);- injector.add([LexerTreeBuilder]);-- injector.add([IFormulaCurrentConfigService, { useClass: FormulaCurrentConfigService }]);- injector.add([IFormulaRuntimeService, { useClass: FormulaRuntimeService }]);- injector.add([IFunctionService, { useClass: FunctionService }]);- injector.add([IOtherFormulaManagerService, { useClass: OtherFormulaManagerService }]);- injector.add([IDefinedNamesService, { useClass: DefinedNamesService }]);- injector.add([ISuperTableService, { useClass: SuperTableService }]);-- injector.add([FormulaDependencyGenerator]);- injector.add([Interpreter]);- injector.add([AstTreeBuilder]);-- injector.add([AstRootNodeFactory]);- injector.add([FunctionNodeFactory]);- injector.add([LambdaNodeFactory]);- injector.add([LambdaParameterNodeFactory]);- injector.add([OperatorNodeFactory]);- injector.add([PrefixNodeFactory]);- injector.add([ReferenceNodeFactory]);- injector.add([SuffixNodeFactory]);- injector.add([UnionNodeFactory]);- injector.add([ValueNodeFactory]);-+ registerFormulaDependencies(injector);dependencies?.forEach((d) => injector.add(d));}@@ -288,7 +263,7 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencielogService.setLogLevel(LogLevel.SILENT); // change this to `true` to debug tests via logsconst sheetData: ISheetData = {};- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const sheetId = workbook.getActiveSheet().getSheetId();workbook.getSheets().forEach((sheet) => {@@ -311,3 +286,31 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependenciesheetData,};}++function registerFormulaDependencies(injector: Injector) {+ injector.add([CalculateFormulaService]);+ injector.add([Lexer]);+ injector.add([LexerTreeBuilder]);++ injector.add([IFormulaCurrentConfigService, { useClass: FormulaCurrentConfigService }]);+ injector.add([IFormulaRuntimeService, { useClass: FormulaRuntimeService }]);+ injector.add([IFunctionService, { useClass: FunctionService }]);+ injector.add([IOtherFormulaManagerService, { useClass: OtherFormulaManagerService }]);+ injector.add([IDefinedNamesService, { useClass: DefinedNamesService }]);+ injector.add([ISuperTableService, { useClass: SuperTableService }]);++ injector.add([FormulaDependencyGenerator]);+ injector.add([Interpreter]);+ injector.add([AstTreeBuilder]);++ injector.add([AstRootNodeFactory]);+ injector.add([FunctionNodeFactory]);+ injector.add([LambdaNodeFactory]);+ injector.add([LambdaParameterNodeFactory]);+ injector.add([OperatorNodeFactory]);+ injector.add([PrefixNodeFactory]);+ injector.add([ReferenceNodeFactory]);+ injector.add([SuffixNodeFactory]);+ injector.add([UnionNodeFactory]);+ injector.add([ValueNodeFactory]);+}diff --git a/packages/engine-formula/src/functions/__tests__/create-function-test-bed.ts b/packages/engine-formula/src/functions/__tests__/create-function-test-bed.tsindex 0ca8cc2171..95def5645f 100644--- a/packages/engine-formula/src/functions/__tests__/create-function-test-bed.ts+++ b/packages/engine-formula/src/functions/__tests__/create-function-test-bed.ts@@ -211,7 +211,7 @@ export function createFunctionTestBed(workbookConfig?: IWorkbookData, dependencilogService.setLogLevel(LogLevel.SILENT); // change this to `true` to debug tests via logsconst sheetData: ISheetData = {};- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const sheetId = workbook.getActiveSheet().getSheetId();workbook.getSheets().forEach((sheet) => {diff --git a/packages/engine-formula/src/models/formula-data.model.ts b/packages/engine-formula/src/models/formula-data.model.tsindex 43df28e934..bf097f08eb 100644--- a/packages/engine-formula/src/models/formula-data.model.ts+++ b/packages/engine-formula/src/models/formula-data.model.ts@@ -49,7 +49,7 @@ export class FormulaDataModel extends Disposable {private _numfmtItemMap: INumfmtItemMap = {};constructor(- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@Inject(LexerTreeBuilder) private readonly _lexerTreeBuilder: LexerTreeBuilder) {super();@@ -302,13 +302,14 @@ export class FormulaDataModel extends Disposable {}initFormulaData() {- // load formula data from workbook config data-- const unitFile = this._currentUniverService.getAllUniverSheetsInstance();- if (unitFile.length === 0) {+ // Load formula data from workbook config data.+ const allSheets = this._univerInstanceService.getAllUniverSheetsInstance();+ if (allSheets.length === 0) {return;}- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();++ // Since there is at least a sheet, there must be current univer sheet instance.+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();this._formulaData[unitId] = {};@@ -322,7 +323,7 @@ export class FormulaDataModel extends Disposable {}getCalculateData() {- const unitAllSheet = this._currentUniverService.getAllUniverSheetsInstance();+ const unitAllSheet = this._univerInstanceService.getAllUniverSheetsInstance();const allUnitData: IUnitData = {};diff --git a/packages/engine-formula/src/services/current-data.service.ts b/packages/engine-formula/src/services/current-data.service.tsindex bd0cde3da7..a338e7c63b 100644--- a/packages/engine-formula/src/services/current-data.service.ts+++ b/packages/engine-formula/src/services/current-data.service.ts@@ -110,7 +110,7 @@ export class FormulaCurrentConfigService extends Disposable implements IFormulaCprivate _executeUnitId: Nullable<string> = '';private _executeSubUnitId: Nullable<string> = '';- constructor(@IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService) {+ constructor(@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService) {super();}@@ -260,9 +260,9 @@ export class FormulaCurrentConfigService extends Disposable implements IFormulaC}// private _loadOtherFormulaData() {- // const unitAllDoc = this._currentUniverService.getAllUniverDocsInstance();+ // const unitAllDoc = this._univerInstanceService.getAllUniverDocsInstance();- // const unitAllSlide = this._currentUniverService.getAllUniverSlidesInstance();+ // const unitAllSlide = this._univerInstanceService.getAllUniverSlidesInstance();// const otherFormulaData: IOtherFormulaData = {};@@ -361,10 +361,10 @@ export class FormulaCurrentConfigService extends Disposable implements IFormulaC}private _loadSheetData() {- const unitAllSheet = this._currentUniverService.getAllUniverSheetsInstance();+ const unitAllSheet = this._univerInstanceService.getAllUniverSheetsInstance();- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;+ const worksheet = workbook?.getActiveSheet();this._executeUnitId = workbook?.getUnitId();this._executeSubUnitId = worksheet?.getSheetId();diff --git a/packages/facade/src/apis/facade.ts b/packages/facade/src/apis/facade.tsindex 4f99666558..42c4a63443 100644--- a/packages/facade/src/apis/facade.ts+++ b/packages/facade/src/apis/facade.ts@@ -115,7 +115,7 @@ export class FUniver {* @returns the currently focused Univer spreadsheet.*/getActiveWorkbook(): FWorkbook | null {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;if (!workbook) {return null;}diff --git a/packages/facade/src/apis/sheets/f-workbook.ts b/packages/facade/src/apis/sheets/f-workbook.tsindex c75cca9eb4..7048ed81db 100644--- a/packages/facade/src/apis/sheets/f-workbook.ts+++ b/packages/facade/src/apis/sheets/f-workbook.ts@@ -160,7 +160,7 @@ export class FWorkbook {onSelectionChange(callback: (selections: IRange[]) => void): IDisposable {return toDisposable(this._selectionManagerService.selectionMoveEnd$.subscribe((selections) => {- if (this._univerInstanceService.getCurrentUniverSheetInstance().getUnitId() !== this.id) {+ if (this._univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId() !== this.id) {return;}diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-average-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-average-cf.command.tsindex 248885e1d5..d1d843e220 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-average-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-average-cf.command.ts@@ -20,6 +20,7 @@ import {ICommandService,IUniverInstanceService,} from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import { AddConditionalRuleMutation, CFRuleType, CFSubRuleType, ConditionalFormattingRuleModel } from '@univerjs/sheets-conditional-formatting';import type { IAddConditionalRuleMutationParams, IAverageHighlightCell, IConditionFormattingRule } from '@univerjs/sheets-conditional-formatting';@@ -29,7 +30,6 @@ interface IAddAverageCfParams {style: IAverageHighlightCell['style'];operator: IAverageHighlightCell['operator'];}-// { ranges: [{ startRow: 0, endRow: 10, startColumn: 3, endColumn: 3 }, { startRow: 0, endRow: 10, startColumn: 5, endColumn: 5 }], style: { fs: 30 }, operator: 'greaterThan' };export const AddAverageCfCommand: ICommand<IAddAverageCfParams> = {type: CommandType.COMMAND,id: 'sheet.command.add-average-conditional-rule',@@ -40,12 +40,12 @@ export const AddAverageCfCommand: ICommand<IAddAverageCfParams> = {const { ranges, style, stopIfTrue, operator } = params;const conditionalFormattingRuleModel = accessor.get(ConditionalFormattingRuleModel);const commandService = accessor.get(ICommandService);-const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const cfId = conditionalFormattingRuleModel.createCfId(unitId, subUnitId);const rule: IConditionFormattingRule = {ranges,diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-cf.command.tsindex 2156a02f23..7d36b3c8b6 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-cf.command.ts@@ -21,6 +21,7 @@ import {IUndoRedoService,IUniverInstanceService,} from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import type { IAddConditionalRuleMutationParams, IConditionFormattingRule, MakePropertyOptional } from '@univerjs/sheets-conditional-formatting';import { AddConditionalRuleMutation, AddConditionalRuleMutationUndoFactory, ConditionalFormattingRuleModel } from '@univerjs/sheets-conditional-formatting';@@ -42,10 +43,10 @@ export const AddCfCommand: ICommand<IAddCfCommandParams> = {const commandService = accessor.get(ICommandService);const conditionalFormattingRuleModel = accessor.get(ConditionalFormattingRuleModel);const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = params.unitId ?? workbook.getUnitId();- const subUnitId = params.subUnitId ?? worksheet.getSheetId();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const cfId = conditionalFormattingRuleModel.createCfId(unitId, subUnitId);const config: IAddConditionalRuleMutationParams = { unitId, subUnitId, rule: { ...rule, cfId: rule.cfId || cfId } };const undo = AddConditionalRuleMutationUndoFactory(accessor, config);diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-color-scale-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-color-scale-cf.command.tsindex 3b7de51502..efbe678101 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-color-scale-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-color-scale-cf.command.ts@@ -20,6 +20,7 @@ import {ICommandService,IUniverInstanceService,} from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import { AddConditionalRuleMutation, CFRuleType, ConditionalFormattingRuleModel } from '@univerjs/sheets-conditional-formatting';import type { IAddConditionalRuleMutationParams, IColorScale, IConditionFormattingRule } from '@univerjs/sheets-conditional-formatting';@@ -41,10 +42,11 @@ export const AddColorScaleConditionalRuleCommand: ICommand<IAddColorScaleConditiconst commandService = accessor.get(ICommandService);const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const cfId = conditionalFormattingRuleModel.createCfId(unitId, subUnitId);const rule: IConditionFormattingRule = {ranges, cfId,diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-data-bar-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-data-bar-cf.command.tsindex e2d63b2c03..24d82fb6c4 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-data-bar-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-data-bar-cf.command.ts@@ -20,6 +20,7 @@ import {ICommandService,IUniverInstanceService,} from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import { AddConditionalRuleMutation, CFRuleType, ConditionalFormattingRuleModel } from '@univerjs/sheets-conditional-formatting';import type { IAddConditionalRuleMutationParams, IConditionFormattingRule, IDataBar } from '@univerjs/sheets-conditional-formatting';@@ -44,12 +45,13 @@ export const AddDataBarConditionalRuleCommand: ICommand<IAddUniqueValuesConditioconst { ranges, min, max, nativeColor, positiveColor, isGradient, stopIfTrue, isShowValue } = params;const conditionalFormattingRuleModel = accessor.get(ConditionalFormattingRuleModel);const univerInstanceService = accessor.get(IUniverInstanceService);++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const commandService = accessor.get(ICommandService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();const cfId = conditionalFormattingRuleModel.createCfId(unitId, subUnitId);const rule: IConditionFormattingRule = {ranges, cfId,diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-duplicate-values-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-duplicate-values-cf.command.tsindex 2e07ad7604..bbdb844111 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-duplicate-values-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-duplicate-values-cf.command.ts@@ -20,6 +20,7 @@ import {ICommandService,IUniverInstanceService,} from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import { AddConditionalRuleMutation, CFRuleType, CFSubRuleType, ConditionalFormattingRuleModel } from '@univerjs/sheets-conditional-formatting';import type { IAddConditionalRuleMutationParams, IConditionFormattingRule, IHighlightCell } from '@univerjs/sheets-conditional-formatting';@@ -40,10 +41,11 @@ export const AddDuplicateValuesCfCommand: ICommand<IAddDuplicateValuesConditionaconst commandService = accessor.get(ICommandService);const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const cfId = conditionalFormattingRuleModel.createCfId(unitId, subUnitId);const rule: IConditionFormattingRule = {ranges, cfId,diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-number-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-number-cf.command.tsindex e1d1e84405..b0a5f4accf 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-number-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-number-cf.command.ts@@ -20,6 +20,7 @@ import {ICommandService,IUniverInstanceService,} from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import { AddConditionalRuleMutation, CFNumberOperator, CFRuleType, CFSubRuleType, ConditionalFormattingRuleModel } from '@univerjs/sheets-conditional-formatting';import type { IAddConditionalRuleMutationParams, IConditionFormattingRule, INumberHighlightCell } from '@univerjs/sheets-conditional-formatting';@@ -41,10 +42,10 @@ export const AddNumberCfCommand: ICommand<IAddNumberCfParams> = {const conditionalFormattingRuleModel = accessor.get(ConditionalFormattingRuleModel);const commandService = accessor.get(ICommandService);const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const cfId = conditionalFormattingRuleModel.createCfId(unitId, subUnitId);let rule: IConditionFormattingRule;if ([CFNumberOperator.between, CFNumberOperator.notBetween].includes(operator)) {diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-rank-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-rank-cf.command.tsindex 5a8b36a8ee..949430298b 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-rank-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-rank-cf.command.ts@@ -20,6 +20,7 @@ import {ICommandService,IUniverInstanceService,} from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import { AddConditionalRuleMutation, CFRuleType, CFSubRuleType, ConditionalFormattingRuleModel } from '@univerjs/sheets-conditional-formatting';import type { IAddConditionalRuleMutationParams, IConditionFormattingRule, IRankHighlightCell } from '@univerjs/sheets-conditional-formatting';@@ -44,10 +45,10 @@ export const AddRankCfCommand: ICommand<IAddRankCfParams> = {const univerInstanceService = accessor.get(IUniverInstanceService);const commandService = accessor.get(ICommandService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const cfId = conditionalFormattingRuleModel.createCfId(unitId, subUnitId);const rule: IConditionFormattingRule = {ranges, cfId,diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-text-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-text-cf.command.tsindex d3ae66ded9..fdaf66168f 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-text-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-text-cf.command.ts@@ -21,6 +21,7 @@ import {IUniverInstanceService,} from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import { AddConditionalRuleMutation, CFRuleType, CFSubRuleType, ConditionalFormattingRuleModel } from '@univerjs/sheets-conditional-formatting';import type { IAddConditionalRuleMutationParams, IConditionFormattingRule, ITextHighlightCell } from '@univerjs/sheets-conditional-formatting';@@ -41,12 +42,13 @@ export const AddTextCfCommand: ICommand<IAddAverageCfParams> = {const { ranges, style, stopIfTrue, operator, value } = params;const conditionalFormattingRuleModel = accessor.get(ConditionalFormattingRuleModel);const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const commandService = accessor.get(ICommandService);- const worksheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();const cfId = conditionalFormattingRuleModel.createCfId(unitId, subUnitId);const rule: IConditionFormattingRule = {ranges, cfId,diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-time-period-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-time-period-cf.command.tsindex cf1565e551..40940e115d 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-time-period-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-time-period-cf.command.ts@@ -20,6 +20,7 @@ import {ICommandService,IUniverInstanceService,} from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import { AddConditionalRuleMutation, CFRuleType, CFSubRuleType, ConditionalFormattingRuleModel } from '@univerjs/sheets-conditional-formatting';import type { IAddConditionalRuleMutationParams, IConditionFormattingRule, ITimePeriodHighlightCell } from '@univerjs/sheets-conditional-formatting';@@ -42,10 +43,10 @@ export const AddTimePeriodCfCommand: ICommand<IAddTimePeriodCf> = {const univerInstanceService = accessor.get(IUniverInstanceService);const commandService = accessor.get(ICommandService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const cfId = conditionalFormattingRuleModel.createCfId(unitId, subUnitId);const rule: IConditionFormattingRule<ITimePeriodHighlightCell> = {ranges, cfId,diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-unique-values-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-unique-values-cf.command.tsindex 8c2ed0d477..3edd452be2 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/add-unique-values-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/add-unique-values-cf.command.ts@@ -20,6 +20,7 @@ import {ICommandService,IUniverInstanceService,} from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import { AddConditionalRuleMutation, CFRuleType, CFSubRuleType, ConditionalFormattingRuleModel } from '@univerjs/sheets-conditional-formatting';import type { IAddConditionalRuleMutationParams, IConditionFormattingRule, IHighlightCell } from '@univerjs/sheets-conditional-formatting';@@ -40,10 +41,10 @@ export const AddUniqueValuesCfCommand: ICommand<IAddUniqueValuesConditionalRulePconst univerInstanceService = accessor.get(IUniverInstanceService);const commandService = accessor.get(ICommandService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const cfId = conditionalFormattingRuleModel.createCfId(unitId, subUnitId);const rule: IConditionFormattingRule = {ranges, cfId,diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/clear-range-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/clear-range-cf.command.tsindex 4aa1b449fe..3d5d416d9c 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/clear-range-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/clear-range-cf.command.ts@@ -26,7 +26,7 @@ import {Range,sequenceExecute,} from '@univerjs/core';-import { createTopMatrixFromMatrix, findAllRectangle, SelectionManagerService } from '@univerjs/sheets';+import { createTopMatrixFromMatrix, findAllRectangle, getSheetCommandTarget, SelectionManagerService } from '@univerjs/sheets';import { ConditionalFormattingRuleModel, DeleteConditionalRuleMutation, DeleteConditionalRuleMutationUndoFactory, SetConditionalRuleMutation, setConditionalRuleMutationUndoFactory } from '@univerjs/sheets-conditional-formatting';import type { IConditionFormattingRule, IDeleteConditionalRuleMutationParams, ISetConditionalRuleMutationParams } from '@univerjs/sheets-conditional-formatting';@@ -46,13 +46,13 @@ export const ClearRangeCfCommand: ICommand<IClearRangeCfParams> = {const conditionalFormattingRuleModel = accessor.get(ConditionalFormattingRuleModel);const univerInstanceService = accessor.get(IUniverInstanceService);const commandService = accessor.get(ICommandService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();const undoRedoService = accessor.get(IUndoRedoService);const selectionManagerService = accessor.get(SelectionManagerService);- const worksheet = workbook.getActiveSheet();- const unitId = params.unitId ?? workbook.getUnitId();- const subUnitId = params.subUnitId ?? worksheet.getSheetId();+ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) return false;++ const { unitId, subUnitId } = target;const ranges = selectionManagerService.getSelections()?.map((selection) => selection.range) || [];const allRuleList = conditionalFormattingRuleModel.getSubunitRules(unitId, subUnitId);if (!allRuleList?.length || !ranges.length) {diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/clear-worksheet-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/clear-worksheet-cf.command.tsindex d3eb2636b0..5b29be7227 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/clear-worksheet-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/clear-worksheet-cf.command.ts@@ -22,6 +22,7 @@ import {IUniverInstanceService,sequenceExecute,} from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import { ConditionalFormattingRuleModel, DeleteConditionalRuleMutation, DeleteConditionalRuleMutationUndoFactory } from '@univerjs/sheets-conditional-formatting';import type { IDeleteConditionalRuleMutationParams } from '@univerjs/sheets-conditional-formatting';@@ -37,12 +38,12 @@ export const ClearWorksheetCfCommand: ICommand<IClearWorksheetCfParams> = {const conditionalFormattingRuleModel = accessor.get(ConditionalFormattingRuleModel);const univerInstanceService = accessor.get(IUniverInstanceService);const commandService = accessor.get(ICommandService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();const undoRedoService = accessor.get(IUndoRedoService);- const worksheet = workbook.getActiveSheet();- const unitId = params?.unitId ?? workbook.getUnitId();- const subUnitId = params?.subUnitId ?? worksheet.getSheetId();+ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) return false;++ const { unitId, subUnitId } = target;const ruleList = conditionalFormattingRuleModel.getSubunitRules(unitId, subUnitId);if (!ruleList?.length) {return false;diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/delete-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/delete-cf.command.tsindex 1e731b70c6..2d522b19ae 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/delete-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/delete-cf.command.ts@@ -21,6 +21,7 @@ import {IUndoRedoService,IUniverInstanceService,} from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import type { IDeleteConditionalRuleMutationParams } from '@univerjs/sheets-conditional-formatting';import { DeleteConditionalRuleMutation, DeleteConditionalRuleMutationUndoFactory } from '@univerjs/sheets-conditional-formatting';@@ -39,10 +40,10 @@ export const DeleteCfCommand: ICommand<IDeleteCfCommandParams> = {const undoRedoService = accessor.get(IUndoRedoService);const commandService = accessor.get(ICommandService);const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = params.unitId ?? workbook.getUnitId();- const subUnitId = params.subUnitId ?? worksheet.getSheetId();+ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) return false;++ const { unitId, subUnitId } = target;const config: IDeleteConditionalRuleMutationParams = { unitId, subUnitId, cfId: params.cfId };const undos = DeleteConditionalRuleMutationUndoFactory(accessor, config);const result = commandService.syncExecuteCommand(DeleteConditionalRuleMutation.id, config);diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/move-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/move-cf.command.tsindex 1e0ee0bd27..815303f0d6 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/move-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/move-cf.command.ts@@ -21,6 +21,7 @@ import {IUndoRedoService,IUniverInstanceService,} from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import type { IAnchor, IMoveConditionalRuleMutationParams } from '@univerjs/sheets-conditional-formatting';import { ConditionalFormattingRuleModel, MoveConditionalRuleMutation, MoveConditionalRuleMutationUndoFactory, transformSupportSymmetryAnchor } from '@univerjs/sheets-conditional-formatting';@@ -42,10 +43,11 @@ export const moveCfCommand: ICommand<IMoveCfCommand> = {const commandService = accessor.get(ICommandService);const univerInstanceService = accessor.get(IUniverInstanceService);const conditionalFormattingRuleModel = accessor.get(ConditionalFormattingRuleModel);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = params.unitId ?? workbook.getUnitId();- const subUnitId = params.subUnitId ?? worksheet.getSheetId();++ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) return false;++ const { unitId, subUnitId } = target;const anchorList = transformSupportSymmetryAnchor(params.start, params.end, conditionalFormattingRuleModel.getSubunitRules(unitId, subUnitId) || [], (rule) => rule.cfId);if (!anchorList) {return false;diff --git a/packages/sheets-conditional-formatting-ui/src/commands/commands/set-cf.command.ts b/packages/sheets-conditional-formatting-ui/src/commands/commands/set-cf.command.tsindex 174ef08fae..a21826b53e 100644--- a/packages/sheets-conditional-formatting-ui/src/commands/commands/set-cf.command.ts+++ b/packages/sheets-conditional-formatting-ui/src/commands/commands/set-cf.command.ts@@ -21,6 +21,7 @@ import {IUndoRedoService,IUniverInstanceService,} from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import type { IConditionFormattingRule, ISetConditionalRuleMutationParams } from '@univerjs/sheets-conditional-formatting';import { SetConditionalRuleMutation, setConditionalRuleMutationUndoFactory } from '@univerjs/sheets-conditional-formatting';@@ -36,14 +37,15 @@ export const SetCfCommand: ICommand<ISetCfCommandParams> = {if (!params) {return false;}- const undoRedoService = accessor.get(IUndoRedoService);+ const undoRedoService = accessor.get(IUndoRedoService);const commandService = accessor.get(ICommandService);const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = params.unitId ?? workbook.getUnitId();- const subUnitId = params.subUnitId ?? worksheet.getSheetId();++ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) return false;++ const { unitId, subUnitId } = target;const config: ISetConditionalRuleMutationParams = { unitId, subUnitId, rule: params.rule };const undos = setConditionalRuleMutationUndoFactory(accessor, config);const result = commandService.syncExecuteCommand(SetConditionalRuleMutation.id, config);diff --git a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/colorScale.tsx b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/colorScale.tsxindex 1da1a0670f..bbb53b9c94 100644--- a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/colorScale.tsx+++ b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/colorScale.tsx@@ -32,8 +32,8 @@ const createOptionItem = (text: string, localeService: LocaleService) => ({ labeconst TextInput = (props: { id: string; type: CFValueType | 'none';value: number | string;onChange: (v: number | string) => void; className: string }) => {const { type, className, onChange, id, value } = props;const univerInstanceService = useDependency(IUniverInstanceService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const unitId = univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();+ const subUnitId = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId();const formulaInitValue = useMemo(() => {return String(value).startsWith('=') ? String(value) : '=';}, [value]);diff --git a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/dataBar.tsx b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/dataBar.tsxindex f45d2156dc..50774006eb 100644--- a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/dataBar.tsx+++ b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/dataBar.tsx@@ -32,8 +32,8 @@ const createOptionItem = (text: CFValueType, localeService: LocaleService) => ({const InputText = (props: { disabled?: boolean; id: string; className: string; type: CFValueType;value: string | number;onChange: (v: string | number) => void }) => {const { onChange, className, value, type, id, disabled = false } = props;const univerInstanceService = useDependency(IUniverInstanceService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const unitId = univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();+ const subUnitId = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId();const _value = useRef(value);const config = useMemo(() => {if ([CFValueType.percentile, CFValueType.percent].includes(type)) {diff --git a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/formula.tsx b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/formula.tsxindex 9d4bd38648..ec9a417ed3 100644--- a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/formula.tsx+++ b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/formula.tsx@@ -30,7 +30,7 @@ export const FormulaStyleEditor = (props: IStyleEditorProps) => {const { onChange, interceptorManager } = props;const localeService = useDependency(LocaleService);const univerInstanceService = useDependency(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const rule = props.rule?.type === CFRuleType.highlightCell ? props.rule : undefined as IRankHighlightCell | IAverageHighlightCell | undefined;diff --git a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/iconSet.tsx b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/iconSet.tsxindex fd839dfe19..83b7e56771 100644--- a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/iconSet.tsx+++ b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/iconSet.tsx@@ -38,8 +38,8 @@ const getIcon = (iconType: string, iconId: string | number) => {const TextInput = (props: { id: number; type: CFValueType; value: number | string;onChange: (v: number | string) => void; error?: string }) => {const univerInstanceService = useDependency(IUniverInstanceService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const unitId = univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();+ const subUnitId = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId();const className = useMemo(() => {if (props.error) {return styles.errorInput;diff --git a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/index.tsx b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/index.tsxindex e51ad35aba..eeb14d8d5c 100644--- a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/index.tsx+++ b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-edit/index.tsx@@ -47,8 +47,8 @@ interface IRuleEditProps {onCancel: () => void;}-const getUnitId = (univerInstanceService: IUniverInstanceService) => univerInstanceService.getCurrentUniverSheetInstance().getUnitId();-const getSubUnitId = (univerInstanceService: IUniverInstanceService) => univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+const getUnitId = (univerInstanceService: IUniverInstanceService) => univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();+const getSubUnitId = (univerInstanceService: IUniverInstanceService) => univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId();export const RuleEdit = (props: IRuleEditProps) => {const localeService = useDependency(LocaleService);@@ -182,7 +182,7 @@ export const RuleEdit = (props: IRuleEditProps) => {const handleSubmit = () => {const beforeSubmitResult = interceptorManager.fetchThroughInterceptors(interceptorManager.getInterceptPoints().beforeSubmit)(true, null);const getRanges = () => {- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();const ranges = rangeResult.current.map((range) => setEndForRange(range, worksheet.getRowCount(), worksheet.getColumnCount()));const result = ranges.filter((range) => !(Number.isNaN(range.startRow) || Number.isNaN(range.startColumn)));return result;diff --git a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-list/index.tsx b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-list/index.tsxindex 4a5ac59f05..7958516bc5 100644--- a/packages/sheets-conditional-formatting-ui/src/components/panel/rule-list/index.tsx+++ b/packages/sheets-conditional-formatting-ui/src/components/panel/rule-list/index.tsx@@ -113,7 +113,7 @@ export const RuleList = (props: IRuleListProps) => {const localeService = useDependency(LocaleService);const conditionalFormattingI18nController = useDependency(ConditionalFormattingI18nController);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const worksheet = workbook.getActiveSheet();const subUnitId = worksheet.getSheetId();@@ -176,7 +176,7 @@ export const RuleList = (props: IRuleListProps) => {const commandList = [SetSelectionsOperation.id, AddConditionalRuleMutation.id, SetConditionalRuleMutation.id, DeleteConditionalRuleMutation.id, MoveConditionalRuleMutation.id];const disposable = commandService.onCommandExecuted((commandInfo) => {const { id, params } = commandInfo;- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();+ const unitId = univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();if (commandList.includes(id) && (params as { unitId: string }).unitId === unitId) {commandSubscribe.next(null);}@@ -239,8 +239,8 @@ export const RuleList = (props: IRuleListProps) => {}, []);const handleDelete = (rule: IConditionFormattingRule) => {- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const unitId = univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();+ const subUnitId = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId();commandService.executeCommand(DeleteCfCommand.id, { unitId, subUnitId, cfId: rule.cfId } as IDeleteCfCommandParams);};@@ -250,8 +250,8 @@ export const RuleList = (props: IRuleListProps) => {const handleDragStop = (_layout: unknown, from: { y: number }, to: { y: number }) => {draggingIdSet(-1);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const unitId = univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();+ const subUnitId = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId();const getSaveIndex = (index: number) => {const length = ruleList.length;return Math.min(length - 1, Math.max(0, index));diff --git a/packages/sheets-conditional-formatting-ui/src/controllers/cf.auto-fill.controller.ts b/packages/sheets-conditional-formatting-ui/src/controllers/cf.auto-fill.controller.tsindex dd9d7dce71..5467be12f4 100644--- a/packages/sheets-conditional-formatting-ui/src/controllers/cf.auto-fill.controller.ts+++ b/packages/sheets-conditional-formatting-ui/src/controllers/cf.auto-fill.controller.ts@@ -48,8 +48,8 @@ export class ConditionalFormattingAutoFillController extends Disposable {relativeRange: IRange,matrixMap: Map<string, ObjectMatrix<1>>) => {- const unitId = this._univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = this._univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const unitId = this._univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();+ const subUnitId = this._univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId();const sourceRange = {startRow: sourceStartCell.row,startColumn: sourceStartCell.col,@@ -137,8 +137,8 @@ export class ConditionalFormattingAutoFillController extends Disposable {});};const generalApplyFunc = (sourceRange: IRange, targetRange: IRange) => {- const unitId = this._univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = this._univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const unitId = this._univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();+ const subUnitId = this._univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId();const matrixMap: Map<string, ObjectMatrix<1>> = new Map();const redos: IMutationInfo[] = [];diff --git a/packages/sheets-conditional-formatting-ui/src/controllers/cf.clear.controller.ts b/packages/sheets-conditional-formatting-ui/src/controllers/cf.clear.controller.tsindex 693ecf3faa..590486c93d 100644--- a/packages/sheets-conditional-formatting-ui/src/controllers/cf.clear.controller.ts+++ b/packages/sheets-conditional-formatting-ui/src/controllers/cf.clear.controller.ts@@ -46,7 +46,7 @@ export class ConditionalFormattingClearController extends Disposable {if (!ranges) {return defaultV;}- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const allRules = this._conditionalFormattingRuleModel.getSubunitRules(workbook.getUnitId(), worksheet.getSheetId());if (!allRules || !allRules.length) {diff --git a/packages/sheets-conditional-formatting-ui/src/controllers/cf.copy-paste.controller.ts b/packages/sheets-conditional-formatting-ui/src/controllers/cf.copy-paste.controller.tsindex 9e7af504f1..616f2f0d5b 100644--- a/packages/sheets-conditional-formatting-ui/src/controllers/cf.copy-paste.controller.ts+++ b/packages/sheets-conditional-formatting-ui/src/controllers/cf.copy-paste.controller.ts@@ -120,7 +120,7 @@ export class ConditionalFormattingCopyPasteController extends Disposable {pasteType: string;}) {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const sheet = workbook.getActiveSheet();const unitId = workbook.getUnitId();const subUnitId = sheet.getSheetId();diff --git a/packages/sheets-conditional-formatting-ui/src/controllers/cf.ref-range.controller.ts b/packages/sheets-conditional-formatting-ui/src/controllers/cf.ref-range.controller.tsindex 427fa5ee34..8b1f42e483 100644--- a/packages/sheets-conditional-formatting-ui/src/controllers/cf.ref-range.controller.ts+++ b/packages/sheets-conditional-formatting-ui/src/controllers/cf.ref-range.controller.ts@@ -94,7 +94,7 @@ export class RefRangeController extends Disposable {toDisposable(this._conditionalFormattingRuleModel.$ruleChange.subscribe((option) => {const { unitId, subUnitId, rule } = option;- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();if (option.unitId !== workbook.getUnitId() || option.subUnitId !== worksheet.getSheetId()) {return;diff --git a/packages/sheets-conditional-formatting-ui/src/controllers/cf.render.controller.ts b/packages/sheets-conditional-formatting-ui/src/controllers/cf.render.controller.tsindex 83cbdabfe0..57c566b8e2 100644--- a/packages/sheets-conditional-formatting-ui/src/controllers/cf.render.controller.ts+++ b/packages/sheets-conditional-formatting-ui/src/controllers/cf.render.controller.ts@@ -57,7 +57,7 @@ export class RenderController extends Disposable {this.disposeWithMe(this._renderManagerService.currentRender$.subscribe((renderId) => {renderId && register(renderId);}));- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;if (workbook) {register(workbook.getUnitId());}@@ -66,20 +66,20 @@ export class RenderController extends Disposable {_initSkeleton() {const markDirtySkeleton = () => {this._sheetSkeletonManagerService.reCalculate();- const unitId = this._univerInstanceService.getCurrentUniverSheetInstance().getUnitId();+ const unitId = this._univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();this._renderManagerService.getRenderById(unitId)?.mainComponent?.makeDirty();};// After the conditional formatting is marked dirty to drive a rendering, to trigger the window within the conditional formatting recalculationthis.disposeWithMe(this._conditionalFormattingViewModel.markDirty$.pipe(bufferTime(16), filter((v) => {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return v.filter((item) => item.unitId === workbook.getUnitId() && item.subUnitId === worksheet.getSheetId()).length > 0;})).subscribe(markDirtySkeleton));// Sort and delete does not mark dirty.this.disposeWithMe(this._conditionalFormattingRuleModel.$ruleChange.pipe(bufferTime(16), filter((v) => {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return v.filter((item) => ['sort', 'delete'].includes(item.type) && item.unitId === workbook.getUnitId() && item.subUnitId === worksheet.getSheetId()).length > 0;})).subscribe(markDirtySkeleton));@@ -87,7 +87,7 @@ export class RenderController extends Disposable {// Once the calculation is complete, a view update is triggered// This rendering does not trigger conditional formatting recalculation,because the rule is not mark dirtythis.disposeWithMe(this._conditionalFormattingService.ruleComputeStatus$.pipe(bufferTime(16), filter((v) => {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return v.filter((item) => item.unitId === workbook.getUnitId() && item.subUnitId === worksheet.getSheetId()).length > 0;})).subscribe(markDirtySkeleton));diff --git a/packages/sheets-conditional-formatting-ui/src/menu/manage-rule.ts b/packages/sheets-conditional-formatting-ui/src/menu/manage-rule.tsindex 66f0841e20..33699d5e18 100644--- a/packages/sheets-conditional-formatting-ui/src/menu/manage-rule.ts+++ b/packages/sheets-conditional-formatting-ui/src/menu/manage-rule.ts@@ -45,7 +45,7 @@ export const FactoryManageConditionalFormattingRule = (componentManager: Componenew Observable<null>((commandSubscribe) => {const disposable = commandService.onCommandExecuted((commandInfo) => {const { id, params } = commandInfo;- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();+ const unitId = univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();if (commandList.includes(id) && (params as { unitId: string }).unitId === unitId) {commandSubscribe.next(null);}@@ -54,8 +54,8 @@ export const FactoryManageConditionalFormattingRule = (componentManager: Compone})).pipe(debounceTime(16)).subscribe(() => {const ranges = selectionManagerService.getSelections()?.map((selection) => selection.range) || [];- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const unitId = univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();+ const subUnitId = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId();const allRule = conditionalFormattingRuleModel.getSubunitRules(unitId, subUnitId) || [];const ruleList = allRule.filter((rule) => rule.ranges.some((ruleRange) => ranges.some((range) => Rectangle.intersects(range, ruleRange))));subscriber.next(!!ruleList.length);@@ -67,7 +67,7 @@ export const FactoryManageConditionalFormattingRule = (componentManager: Componenew Observable<null>((commandSubscribe) => {const disposable = commandService.onCommandExecuted((commandInfo) => {const { id, params } = commandInfo;- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();+ const unitId = univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();if (commandList.includes(id) && (params as { unitId: string }).unitId === unitId) {commandSubscribe.next(null);}@@ -75,8 +75,8 @@ export const FactoryManageConditionalFormattingRule = (componentManager: Componereturn () => disposable.dispose();})).pipe(debounceTime(16)).subscribe(() => {- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const unitId = univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();+ const subUnitId = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId();const allRule = conditionalFormattingRuleModel.getSubunitRules(unitId, subUnitId) || [];subscriber.next(!!allRule.length);})diff --git a/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts b/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.tsindex b598a1cd90..791eedb192 100644--- a/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts+++ b/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts@@ -404,5 +404,5 @@ export class ConditionalFormattingService extends Disposable {}}-const getUnitId = (u: IUniverInstanceService) => u.getCurrentUniverSheetInstance().getUnitId();-const getSubUnitId = (u: IUniverInstanceService) => u.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+const getUnitId = (u: IUniverInstanceService) => u.getCurrentUniverSheetInstance()!.getUnitId();+const getSubUnitId = (u: IUniverInstanceService) => u.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId();diff --git a/packages/sheets-data-validation/src/commands/commands/data-validation.command.ts b/packages/sheets-data-validation/src/commands/commands/data-validation.command.tsindex ad64d56144..291456bad5 100644--- a/packages/sheets-data-validation/src/commands/commands/data-validation.command.ts+++ b/packages/sheets-data-validation/src/commands/commands/data-validation.command.ts@@ -18,6 +18,7 @@ import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService,import type { ICommand, IMutationInfo, IRange, ISheetDataValidationRule } from '@univerjs/core';import type { IAddDataValidationMutationParams, IUpdateDataValidationMutationParams } from '@univerjs/data-validation';import { AddDataValidationMutation, createDefaultNewRule, DataValidationModel, RemoveDataValidationMutation, UpdateDataValidationMutation, UpdateRuleType } from '@univerjs/data-validation';+import { getSheetCommandTarget } from '@univerjs/sheets';import type { SheetDataValidationManager } from '../../models/sheet-data-validation-manager';import { OpenValidationPanelOperation } from '../operations/data-validation.operation';import type { RangeMutation } from '../../models/rule-matrix';@@ -206,8 +207,10 @@ export const AddSheetDataValidationAndOpenCommand: ICommand = {id: 'data-validation.command.addRuleAndOpen',async handler(accessor) {const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { workbook, worksheet } = target;const rule = createDefaultNewRule(accessor);const commandService = accessor.get(ICommandService);const unitId = workbook.getUnitId();diff --git a/packages/sheets-data-validation/src/commands/operations/data-validation.operation.ts b/packages/sheets-data-validation/src/commands/operations/data-validation.operation.tsindex 8a307ed08e..00c346e064 100644--- a/packages/sheets-data-validation/src/commands/operations/data-validation.operation.ts+++ b/packages/sheets-data-validation/src/commands/operations/data-validation.operation.ts@@ -17,6 +17,7 @@import { CommandType, type ICommand, ICommandService, IUniverInstanceService } from '@univerjs/core';import { ISidebarService } from '@univerjs/ui';import { DataValidationModel } from '@univerjs/data-validation';+import { getSheetCommandTarget } from '@univerjs/sheets';import { DataValidationDropdownManagerService } from '../../services/dropdown-manager.service';import { DataValidationPanelService } from '../../services/data-validation-panel.service';@@ -39,10 +40,10 @@ export const OpenValidationPanelOperation: ICommand<IOpenValidationPanelOperatioconst dataValidationModel = accessor.get(DataValidationModel);const univerInstanceService = accessor.get(IUniverInstanceService);const sidebarService = accessor.get(ISidebarService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const rule = ruleId ? dataValidationModel.getRuleById(unitId, subUnitId, ruleId) : undefined;dataValidationPanelService.open();dataValidationPanelService.setActiveRule(rule && {diff --git a/packages/sheets-data-validation/src/controllers/dv-alert.controller.ts b/packages/sheets-data-validation/src/controllers/dv-alert.controller.tsindex 8b5cff0dae..a9e8e098a3 100644--- a/packages/sheets-data-validation/src/controllers/dv-alert.controller.ts+++ b/packages/sheets-data-validation/src/controllers/dv-alert.controller.ts@@ -39,7 +39,7 @@ export class DataValidationAlertController extends Disposable {private _initCellAlertPopup() {this.disposeWithMe(this._hoverManagerService.currentCell$.subscribe((cellPos) => {if (cellPos) {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const manager = this._sheetDataValidationService.currentManager?.manager;const cellData = worksheet.getCell(cellPos.location.row, cellPos.location.col);diff --git a/packages/sheets-data-validation/src/controllers/dv-ref-range.controller.ts b/packages/sheets-data-validation/src/controllers/dv-ref-range.controller.tsindex c67541700a..00e5d6c0ae 100644--- a/packages/sheets-data-validation/src/controllers/dv-ref-range.controller.ts+++ b/packages/sheets-data-validation/src/controllers/dv-ref-range.controller.ts@@ -220,7 +220,7 @@ export class DataValidationRefRangeController extends Disposable {const gridRange = deserializeRangeWithSheet(formula1 ?? '');const id = this._getIdWithUnitId(propUnitId, propSubUnitId, ruleId);const rangeUnitId = gridRange.unitId || propUnitId;- const workbook = this._univerInstanceService.getUniverSheetInstance(rangeUnitId) ?? this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getUniverSheetInstance(rangeUnitId) ?? this._univerInstanceService.getCurrentUniverSheetInstance()!;const sheetId = workbook.getSheetBySheetName(gridRange.sheetName)?.getSheetId() ?? propSubUnitId;const worksheet = workbook.getSheetBySheetId(sheetId) ?? workbook.getActiveSheet();const disposableCollection = new DisposableCollection();diff --git a/packages/sheets-data-validation/src/controllers/dv-render.controller.ts b/packages/sheets-data-validation/src/controllers/dv-render.controller.tsindex 279a1c6a01..2981268d77 100644--- a/packages/sheets-data-validation/src/controllers/dv-render.controller.ts+++ b/packages/sheets-data-validation/src/controllers/dv-render.controller.ts@@ -169,7 +169,7 @@ export class DataValidationRenderController extends RxDisposable {private _initSkeletonChange() {const markSkeletonDirty = () => {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const subUnitId = workbook.getActiveSheet().getSheetId();const skeleton = this._sheetSkeletonManagerService.getOrCreateSkeleton({ unitId, sheetId: subUnitId });diff --git a/packages/sheets-data-validation/src/controllers/dv.controller.ts b/packages/sheets-data-validation/src/controllers/dv.controller.tsindex ccb64b1bea..6afad2e2ee 100644--- a/packages/sheets-data-validation/src/controllers/dv.controller.ts+++ b/packages/sheets-data-validation/src/controllers/dv.controller.ts@@ -76,7 +76,7 @@ export class DataValidationController extends RxDisposable {}private _initInstanceChange() {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;this._sheetDataValidationService.switchCurrent(workbook.getUnitId(), workbook.getActiveSheet().getSheetId());this.disposeWithMe(workbook.activeSheet$.subscribe((worksheet) => {@@ -93,7 +93,7 @@ export class DataValidationController extends RxDisposable {this._sheetInterceptorService.interceptCommand({getMutations: (commandInfo) => {if (commandInfo.id === ClearSelectionAllCommand.id) {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const worksheet = workbook.getActiveSheet();const subUnitId = worksheet.getSheetId();diff --git a/packages/sheets-data-validation/src/validators/list-multiple-validator.ts b/packages/sheets-data-validation/src/validators/list-multiple-validator.tsindex 1c6310db39..6e950992fa 100644--- a/packages/sheets-data-validation/src/validators/list-multiple-validator.ts+++ b/packages/sheets-data-validation/src/validators/list-multiple-validator.ts@@ -85,7 +85,7 @@ export class ListMultipleValidator extends BaseDataValidator {getList(rule: IDataValidationRule, propUnitId?: string, propSubUnitId?: string) {const { formula1 = '' } = rule;const univerInstanceService = this.injector.get(IUniverInstanceService);- const workbook = (propUnitId ? univerInstanceService.getUniverSheetInstance(propUnitId) : undefined) ?? univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = (propUnitId ? univerInstanceService.getUniverSheetInstance(propUnitId) : undefined) ?? univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = (propSubUnitId ? workbook.getSheetBySheetId(propSubUnitId) : undefined) ?? workbook.getActiveSheet();const unitId = workbook.getUnitId();const subUnitId = worksheet.getSheetId();diff --git a/packages/sheets-data-validation/src/validators/list-validator.ts b/packages/sheets-data-validation/src/validators/list-validator.tsindex 39b2a47354..b6d49a9325 100644--- a/packages/sheets-data-validation/src/validators/list-validator.ts+++ b/packages/sheets-data-validation/src/validators/list-validator.ts@@ -85,7 +85,7 @@ export class ListValidator extends BaseDataValidator {getList(rule: IDataValidationRule, currentUnitId?: string, currentSubUnitId?: string) {const { formula1 = '' } = rule;const univerInstanceService = this.injector.get(IUniverInstanceService);- const workbook = (currentUnitId ? univerInstanceService.getUniverSheetInstance(currentUnitId) : undefined) ?? univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = (currentUnitId ? univerInstanceService.getUniverSheetInstance(currentUnitId) : undefined) ?? univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = (currentSubUnitId ? workbook.getSheetBySheetId(currentSubUnitId) : undefined) ?? workbook.getActiveSheet();const unitId = workbook.getUnitId();const subUnitId = worksheet.getSheetId();diff --git a/packages/sheets-data-validation/src/validators/util.ts b/packages/sheets-data-validation/src/validators/util.tsindex 3bddef3ad8..411a52454a 100644--- a/packages/sheets-data-validation/src/validators/util.ts+++ b/packages/sheets-data-validation/src/validators/util.ts@@ -21,7 +21,7 @@ import { getCellValueOrigin } from '../utils/get-cell-data-origin';export function getSheetRangeValueSet(grid: IUnitRangeName, univerInstanceService: IUniverInstanceService, currUnitId: string, currSubUnitId: string) {const set = new Set<string>();const unitId = grid.unitId || currUnitId;- const workbook = univerInstanceService.getUniverSheetInstance(unitId) ?? univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getUniverSheetInstance(unitId) ?? univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getSheetBySheetName(grid.sheetName) ?? workbook.getSheetBySheetId(currSubUnitId) ?? workbook.getActiveSheet();Range.foreach(grid.range, (row, col) => {const data = worksheet.getCellRaw(row, col);diff --git a/packages/sheets-data-validation/src/views/formula-input/list-formula-input.tsx b/packages/sheets-data-validation/src/views/formula-input/list-formula-input.tsxindex 80eb5d2e57..4b1185d94e 100644--- a/packages/sheets-data-validation/src/views/formula-input/list-formula-input.tsx+++ b/packages/sheets-data-validation/src/views/formula-input/list-formula-input.tsx@@ -154,7 +154,7 @@ export function ListFormulaInput(props: IFormulaInputProps) {const [refRange, setRefRange] = useState(isRefRange === '1' ? formula1 : '');const localeService = useDependency(LocaleService);const univerInstanceService = useDependency(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const [refColors, setRefColors] = useState(() => formula2.split(','));@@ -285,7 +285,7 @@ export function ListFormulaInput(props: IFormulaInputProps) {});setRefRange('');} else {- const workbook = univerInstanceService.getUniverSheetInstance(range.unitId) ?? univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getUniverSheetInstance(range.unitId) ?? univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook?.getSheetBySheetId(range.sheetId) ?? workbook.getActiveSheet();const rangeStr = serializeRangeWithSheet(worksheet.getName(), range.range);onChange?.({diff --git a/packages/sheets-data-validation/src/views/list/index.tsx b/packages/sheets-data-validation/src/views/list/index.tsxindex ff7fa05967..b14f936c4f 100644--- a/packages/sheets-data-validation/src/views/list/index.tsx+++ b/packages/sheets-data-validation/src/views/list/index.tsx@@ -34,7 +34,7 @@ export const DataValidationList = memo(() => {const commandService = useDependency(ICommandService);const injector = useDependency(Injector);const dataValidationPanelService = useDependency(DataValidationPanelService);- const workbook = useObservable(univerInstanceService.currentSheet$, univerInstanceService.getCurrentUniverSheetInstance()) ?? univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = useObservable(univerInstanceService.currentSheet$, univerInstanceService.getCurrentUniverSheetInstance()) ?? univerInstanceService.getCurrentUniverSheetInstance()!;const localeService = useDependency(LocaleService);const [rules, setRules] = useState<ISheetDataValidationRule[]>([]);const worksheet = useObservable(workbook.activeSheet$, workbook.getActiveSheet()) ?? workbook.getActiveSheet();diff --git a/packages/sheets-find-replace/src/controllers/sheet-find-replace.controller.ts b/packages/sheets-find-replace/src/controllers/sheet-find-replace.controller.tsindex e9c239bd08..481aa359fc 100644--- a/packages/sheets-find-replace/src/controllers/sheet-find-replace.controller.ts+++ b/packages/sheets-find-replace/src/controllers/sheet-find-replace.controller.ts@@ -896,7 +896,7 @@ class SheetsFindReplaceProvider extends Disposable implements IFindReplaceProvid// NOTE: If there are multi Workbook instances then we should create `SheetFindModel` for each of them.// But we don't need to implement that in the foreseeable future.- const currentWorkbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const currentWorkbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;if (currentWorkbook) {const sheetFind = this._injector.createInstance(SheetFindModel, currentWorkbook);this._findModelsByUnitId.set(currentWorkbook.getUnitId(), sheetFind);diff --git a/packages/sheets-formula/src/commands/operations/insert-function.operation.ts b/packages/sheets-formula/src/commands/operations/insert-function.operation.tsindex 5a146e8c03..1e2d945dc1 100644--- a/packages/sheets-formula/src/commands/operations/insert-function.operation.ts+++ b/packages/sheets-formula/src/commands/operations/insert-function.operation.ts@@ -28,6 +28,7 @@ import {import { serializeRange } from '@univerjs/engine-formula';import {getCellAtRowCol,+ getSheetCommandTarget,NORMAL_SELECTION_PLUGIN_NAME,SelectionManagerService,SetSelectionsOperation,@@ -56,11 +57,10 @@ export const InsertFunctionOperation: ICommand = {return false;}- const workbook = accessor.get(IUniverInstanceService).getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;- const unitId = workbook.getUnitId();- const sheetId = worksheet.getSheetId();+ const { worksheet, unitId, subUnitId } = target;const cellMatrix = worksheet.getCellMatrix();const { value } = params;@@ -146,7 +146,7 @@ export const InsertFunctionOperation: ICommand = {const setSelectionParams = {unitId,- subUnitId: sheetId,+ subUnitId,pluginName: NORMAL_SELECTION_PLUGIN_NAME,selections: [resultRange],};diff --git a/packages/sheets-formula/src/controllers/active-dirty.controller.ts b/packages/sheets-formula/src/controllers/active-dirty.controller.tsindex 67e1409971..b73a146676 100644--- a/packages/sheets-formula/src/controllers/active-dirty.controller.ts+++ b/packages/sheets-formula/src/controllers/active-dirty.controller.ts@@ -53,7 +53,7 @@ import { Inject } from '@wendellhu/redi';export class ActiveDirtyController extends Disposable {constructor(@IActiveDirtyManagerService private readonly _activeDirtyManagerService: IActiveDirtyManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@Inject(FormulaDataModel) private readonly _formulaDataModel: FormulaDataModel,@IDefinedNamesService private readonly _definedNamesService: IDefinedNamesService@@ -261,7 +261,7 @@ export class ActiveDirtyController extends Disposable {const dirtyRanges: IUnitRange[] = [];- const workbook = this._currentUniverService.getUniverSheetInstance(unitId);+ const workbook = this._univerInstanceService.getUniverSheetInstance(unitId);const worksheet = workbook?.getSheetBySheetId(sheetId);@@ -307,7 +307,7 @@ export class ActiveDirtyController extends Disposable {const dirtyRanges: IUnitRange[] = [];- const workbook = this._currentUniverService.getUniverSheetInstance(unitId);+ const workbook = this._univerInstanceService.getUniverSheetInstance(unitId);const worksheet = workbook?.getSheetBySheetId(sheetId);diff --git a/packages/sheets-formula/src/controllers/defined-name.controller.ts b/packages/sheets-formula/src/controllers/defined-name.controller.tsindex 8d773e48db..bcd4024ccc 100644--- a/packages/sheets-formula/src/controllers/defined-name.controller.ts+++ b/packages/sheets-formula/src/controllers/defined-name.controller.ts@@ -146,7 +146,7 @@ export class DefinedNameController extends Disposable {}private _getUnitIdAndSheetId() {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;if (workbook == null) {return {};}diff --git a/packages/sheets-formula/src/controllers/formula-clipboard.controller.ts b/packages/sheets-formula/src/controllers/formula-clipboard.controller.tsindex 927e2a7452..b5f64ed30c 100644--- a/packages/sheets-formula/src/controllers/formula-clipboard.controller.ts+++ b/packages/sheets-formula/src/controllers/formula-clipboard.controller.ts@@ -89,7 +89,7 @@ export class FormulaClipboardController extends Disposable {};const pastedRange = pasteTo.range;const matrix = data;- const workbook = this._currentUniverSheet.getCurrentUniverSheetInstance();+ const workbook = this._currentUniverSheet.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const subUnitId = workbook.getActiveSheet().getSheetId();diff --git a/packages/sheets-formula/src/controllers/prompt.controller.ts b/packages/sheets-formula/src/controllers/prompt.controller.tsindex 89b279befc..7894ef659e 100644--- a/packages/sheets-formula/src/controllers/prompt.controller.ts+++ b/packages/sheets-formula/src/controllers/prompt.controller.ts@@ -163,7 +163,7 @@ export class PromptController extends Disposable {@Inject(ThemeService) private readonly _themeService: ThemeService,@Inject(SelectionManagerService) private readonly _selectionManagerService: SelectionManagerService,@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@Inject(ISelectionRenderService) private readonly _selectionRenderService: ISelectionRenderService,@Inject(IDescriptionService) private readonly _descriptionService: IDescriptionService,@Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService,@@ -340,7 +340,7 @@ export class PromptController extends Disposable {private _initialChangeEditor() {this.disposeWithMe(- this._currentUniverService.currentDoc$.subscribe((documentDataModel) => {+ this._univerInstanceService.currentDoc$.subscribe((documentDataModel) => {if (documentDataModel == null) {return;}@@ -755,12 +755,12 @@ export class PromptController extends Disposable {}// private _getCurrentBody() {- // const documentModel = this._currentUniverService.getCurrentUniverDocInstance();+ // const documentModel = this._univerInstanceService.getCurrentUniverDocInstance();// return documentModel?.snapshot?.body;// }private _getCurrentBodyDataStreamAndOffset() {- const documentModel = this._currentUniverService.getCurrentUniverDocInstance();+ const documentModel = this._univerInstanceService.getCurrentUniverDocInstance();if (!documentModel?.snapshot?.body) {return;@@ -781,15 +781,14 @@ export class PromptController extends Disposable {private _getFormulaAndCellEditorBody(unitIds: string[]) {return unitIds.map((unitId) => {- const dataModel = this._currentUniverService.getUniverDocInstance(unitId);+ const dataModel = this._univerInstanceService.getUniverDocInstance(unitId);return dataModel?.getBody();});}private _editorModelUnitIds() {- const currentDocumentDataModel = this._currentUniverService.getCurrentUniverDocInstance();-+ const currentDocumentDataModel = this._univerInstanceService.getCurrentUniverDocInstance()!;const unitId = currentDocumentDataModel.getUnitId();if (this._editorService.isEditor(unitId) && !this._editorService.isSheetEditor(unitId)) {@@ -989,7 +988,7 @@ export class PromptController extends Disposable {const selectionWithStyle: ISelectionWithStyle[] = [];- const workbook = this._currentUniverService.getUniverSheetInstance(unitId);+ const workbook = this._univerInstanceService.getUniverSheetInstance(unitId);const worksheet = workbook?.getSheetBySheetId(sheetId);if (worksheet == null) {@@ -1110,13 +1109,13 @@ export class PromptController extends Disposable {}private _getSheetIdByName(unitId: string, sheetName: string) {- const workbook = this._currentUniverService.getUniverSheetInstance(unitId);+ const workbook = this._univerInstanceService.getUniverSheetInstance(unitId);return workbook?.getSheetBySheetName(normalizeSheetName(sheetName))?.getSheetId();}private _getSheetNameById(unitId: string, sheetId: string) {- const workbook = this._currentUniverService.getUniverSheetInstance(unitId);+ const workbook = this._univerInstanceService.getUniverSheetInstance(unitId);const sheetName = workbook?.getSheetBySheetId(sheetId)?.getName() || '';@@ -1127,7 +1126,7 @@ export class PromptController extends Disposable {const current = this._sheetSkeletonManagerService.getCurrent();if (current == null) {- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return {unitId: workbook.getUnitId(),@@ -1145,7 +1144,7 @@ export class PromptController extends Disposable {}private _getOpenForCurrentSheet() {- const documentDataModel = this._currentUniverService.getCurrentUniverDocInstance();+ const documentDataModel = this._univerInstanceService.getCurrentUniverDocInstance()!;const editorUnitId = documentDataModel.getUnitId();const editor = this._editorService.getEditor(editorUnitId);if (editor == null) {@@ -1258,7 +1257,7 @@ export class PromptController extends Disposable {this._currentInsertRefStringIndex = textSelectionOffset;if (editorUnitId == null) {- editorUnitId = this._currentUniverService.getCurrentUniverDocInstance().getUnitId();+ editorUnitId = this._univerInstanceService.getCurrentUniverDocInstance()!.getUnitId();}this._fitEditorSize();@@ -1317,8 +1316,8 @@ export class PromptController extends Disposable {}private _fitEditorSize() {- const currentDocumentDataModel = this._currentUniverService.getCurrentUniverDocInstance();- const editorUnitId = currentDocumentDataModel.getUnitId();+ const currentDocumentDataModel = this._univerInstanceService.getCurrentUniverDocInstance();+ const editorUnitId = currentDocumentDataModel!.getUnitId();if (this._editorService.isEditor(editorUnitId) && !this._editorService.isSheetEditor(editorUnitId)) {return;}@@ -1334,9 +1333,9 @@ export class PromptController extends Disposable {* @param textRuns*/private _updateEditorModel(dataStream: string, textRuns: ITextRun[]) {- const documentDataModel = this._currentUniverService.getCurrentUniverDocInstance();+ const documentDataModel = this._univerInstanceService.getCurrentUniverDocInstance();- const editorUnitId = documentDataModel.getUnitId();+ const editorUnitId = documentDataModel!.getUnitId();if (!this._editorService.isEditor(editorUnitId)) {return;@@ -2027,7 +2026,7 @@ export class PromptController extends Disposable {}private _getEditorObject() {- const editorUnitId = this._currentUniverService.getCurrentUniverDocInstance().getUnitId();+ const editorUnitId = this._univerInstanceService.getCurrentUniverDocInstance()!.getUnitId();const editor = this._editorService.getEditor(editorUnitId);return editor?.render;// return getEditorObject(this._editorBridgeService.getCurrentEditorId(), this._renderManagerService);diff --git a/packages/sheets-formula/src/controllers/update-formula.controller.ts b/packages/sheets-formula/src/controllers/update-formula.controller.tsindex c6371ce459..aa518292b0 100644--- a/packages/sheets-formula/src/controllers/update-formula.controller.ts+++ b/packages/sheets-formula/src/controllers/update-formula.controller.ts@@ -160,7 +160,7 @@ enum OriginRangeEdgeType {@OnLifecycle(LifecycleStages.Ready, UpdateFormulaController)export class UpdateFormulaController extends Disposable {constructor(- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@ICommandService private readonly _commandService: ICommandService,@Inject(LexerTreeBuilder) private readonly _lexerTreeBuilder: LexerTreeBuilder,@Inject(FormulaDataModel) private readonly _formulaDataModel: FormulaDataModel,@@ -415,7 +415,7 @@ export class UpdateFormulaController extends Disposable {toRange: { startRow: toStartRow, endRow: toEndRow },} = params;- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const worksheet = workbook.getActiveSheet();const sheetId = worksheet.getSheetId();@@ -453,7 +453,7 @@ export class UpdateFormulaController extends Disposable {toRange: { startColumn: toStartCol, endColumn: toEndCol },} = params;- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const worksheet = workbook.getActiveSheet();const sheetId = worksheet.getSheetId();@@ -1387,7 +1387,7 @@ export class UpdateFormulaController extends Disposable {}private _getCurrentSheetInfo() {- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const sheetId = workbook.getActiveSheet().getSheetId();diff --git a/packages/sheets-formula/src/formula-ui-plugin.ts b/packages/sheets-formula/src/formula-ui-plugin.tsindex b6a653b947..2c8fb204ed 100644--- a/packages/sheets-formula/src/formula-ui-plugin.ts+++ b/packages/sheets-formula/src/formula-ui-plugin.ts@@ -56,7 +56,7 @@ export class UniverSheetsFormulaPlugin extends Plugin {private _config: Partial<IFormulaUIConfig>,@Inject(Injector) override readonly _injector: Injector,@Inject(LocaleService) private readonly _localeService: LocaleService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService) {super(FORMULA_UI_PLUGIN_NAME);}diff --git a/packages/sheets-formula/src/services/formula-ref-range.service.ts b/packages/sheets-formula/src/services/formula-ref-range.service.tsindex 6f103a39be..9baecac623 100644--- a/packages/sheets-formula/src/services/formula-ref-range.service.ts+++ b/packages/sheets-formula/src/services/formula-ref-range.service.ts@@ -43,7 +43,7 @@ export class FormulaRefRangeService extends Disposable {const sequenceNodes = this._lexerTreeBuilder.sequenceNodesBuilder(formula);const disposableCollection = new DisposableCollection();const handleChange = (params: EffectRefRangeParams) => {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const unitId = workbook.getUnitId();const subUnitId = worksheet.getSheetId();@@ -81,7 +81,7 @@ export class FormulaRefRangeService extends Disposable {if (typeof node === 'object' && node.nodeType === sequenceNodeType.REFERENCE) {const gridRangeName = deserializeRangeWithSheet(node.token);const { range, unitId, sheetName } = gridRangeName;- const workbook = unitId ? this._univerInstanceService.getUniverSheetInstance(unitId) : this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = unitId ? this._univerInstanceService.getUniverSheetInstance(unitId) : this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = sheetName ? workbook?.getSheetBySheetName(sheetName) : workbook?.getActiveSheet();if (!worksheet) {return;diff --git a/packages/sheets-formula/src/views/prompt/help-function/HelpFunction.tsx b/packages/sheets-formula/src/views/prompt/help-function/HelpFunction.tsxindex f7cb16356a..53b0fabb3f 100644--- a/packages/sheets-formula/src/views/prompt/help-function/HelpFunction.tsx+++ b/packages/sheets-formula/src/views/prompt/help-function/HelpFunction.tsx@@ -75,7 +75,7 @@ export function HelpFunction() {}, []);function getPosition() {- const documentDataModel = univerInstanceService.getCurrentUniverDocInstance();+ const documentDataModel = univerInstanceService.getCurrentUniverDocInstance()!;const editorUnitId = documentDataModel.getUnitId();diff --git a/packages/sheets-formula/src/views/prompt/search-function/SearchFunction.tsx b/packages/sheets-formula/src/views/prompt/search-function/SearchFunction.tsxindex bd53f0a7bd..0f14b8e81b 100644--- a/packages/sheets-formula/src/views/prompt/search-function/SearchFunction.tsx+++ b/packages/sheets-formula/src/views/prompt/search-function/SearchFunction.tsx@@ -96,7 +96,7 @@ export function SearchFunction() {}, []);function getPosition() {- const documentDataModel = univerInstanceService.getCurrentUniverDocInstance();+ const documentDataModel = univerInstanceService.getCurrentUniverDocInstance()!;const editorUnitId = documentDataModel.getUnitId();diff --git a/packages/sheets-numfmt/src/commands/commands/add-decimal.command.ts b/packages/sheets-numfmt/src/commands/commands/add-decimal.command.tsindex 9bdcb504a9..fcdb9f777b 100644--- a/packages/sheets-numfmt/src/commands/commands/add-decimal.command.ts+++ b/packages/sheets-numfmt/src/commands/commands/add-decimal.command.ts@@ -16,7 +16,7 @@import type { ICommand } from '@univerjs/core';import { CommandType, ICommandService, IUniverInstanceService, Range } from '@univerjs/core';-import { INumfmtService, SelectionManagerService } from '@univerjs/sheets';+import { getSheetCommandTarget, INumfmtService, SelectionManagerService } from '@univerjs/sheets';import type { IAccessor } from '@wendellhu/redi';import { getDecimalFromPattern, setPatternDecimal } from '../../utils/decimal';@@ -36,10 +36,11 @@ export const AddDecimalCommand: ICommand = {if (!selections || !selections.length) {return false;}- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const sheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = sheet.getSheetId();++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;let maxDecimals = 0;selections.forEach((selection) => {diff --git a/packages/sheets-numfmt/src/commands/commands/set-numfmt.command.ts b/packages/sheets-numfmt/src/commands/commands/set-numfmt.command.tsindex 1281720191..6d250b005c 100644--- a/packages/sheets-numfmt/src/commands/commands/set-numfmt.command.ts+++ b/packages/sheets-numfmt/src/commands/commands/set-numfmt.command.ts@@ -31,6 +31,7 @@ import type {import {factoryRemoveNumfmtUndoMutation,factorySetNumfmtUndoMutation,+ getSheetCommandTarget,rangeMerge,RemoveNumfmtMutation,SetNumfmtMutation,@@ -56,10 +57,10 @@ export const SetNumfmtCommand: ICommand<ISetNumfmtCommandParams> = {const univerInstanceService = accessor.get(IUniverInstanceService);const undoRedoService = accessor.get(IUndoRedoService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const unitId = workbook.getUnitId();- const worksheet = workbook.getActiveSheet();- const subUnitId = worksheet.getSheetId();+ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) return false;++ const { unitId, subUnitId } = target;const setCells = params.values.filter((value) => !!value.pattern) as ISetCellsNumfmt;const removeCells = params.values.filter((value) => !value.pattern);const setRedos = transformCellsToRange(unitId, subUnitId, setCells);diff --git a/packages/sheets-numfmt/src/commands/commands/subtract-decimal.command.ts b/packages/sheets-numfmt/src/commands/commands/subtract-decimal.command.tsindex aabade337a..81fbd77a8a 100644--- a/packages/sheets-numfmt/src/commands/commands/subtract-decimal.command.ts+++ b/packages/sheets-numfmt/src/commands/commands/subtract-decimal.command.ts@@ -16,7 +16,7 @@import type { ICommand } from '@univerjs/core';import { CommandType, ICommandService, IUniverInstanceService, Range } from '@univerjs/core';-import { INumfmtService, SelectionManagerService } from '@univerjs/sheets';+import { getSheetCommandTarget, INumfmtService, SelectionManagerService } from '@univerjs/sheets';import type { IAccessor } from '@wendellhu/redi';import { getDecimalFromPattern, setPatternDecimal } from '../../utils/decimal';@@ -36,10 +36,11 @@ export const SubtractDecimalCommand: ICommand = {if (!selections || !selections.length) {return false;}- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const sheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = sheet.getSheetId();++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;let maxDecimals = 0;selections.forEach((selection) => {diff --git a/packages/sheets-numfmt/src/controllers/__tests__/auto-fill.controller.spec.ts b/packages/sheets-numfmt/src/controllers/__tests__/auto-fill.controller.spec.tsindex 48c044e97f..ce744caf02 100644--- a/packages/sheets-numfmt/src/controllers/__tests__/auto-fill.controller.spec.ts+++ b/packages/sheets-numfmt/src/controllers/__tests__/auto-fill.controller.spec.ts@@ -47,7 +47,7 @@ describe('test auto fill', () => {const univerInstanceService = testBed.get(IUniverInstanceService);testBed.get(NumfmtAutoFillController);testBed.get(NumfmtCellContent);- workbook = univerInstanceService.getCurrentUniverSheetInstance();+ workbook = univerInstanceService.getCurrentUniverSheetInstance()!;worksheet = workbook.getActiveSheet();});it('test fill format ,repeat range is divisible', () => {diff --git a/packages/sheets-numfmt/src/controllers/__tests__/cell-content.controller.spec.ts b/packages/sheets-numfmt/src/controllers/__tests__/cell-content.controller.spec.tsindex 84b7ef0bb0..15dba7366e 100644--- a/packages/sheets-numfmt/src/controllers/__tests__/cell-content.controller.spec.ts+++ b/packages/sheets-numfmt/src/controllers/__tests__/cell-content.controller.spec.ts@@ -52,7 +52,7 @@ describe('test cell-content', () => {},},};- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();testBed.get(NumfmtCellContent);const value = worksheet.getCell(0, 0);diff --git a/packages/sheets-numfmt/src/controllers/__tests__/editor.controller.spec.ts b/packages/sheets-numfmt/src/controllers/__tests__/editor.controller.spec.tsindex 6f969754b8..69f557adbc 100644--- a/packages/sheets-numfmt/src/controllers/__tests__/editor.controller.spec.ts+++ b/packages/sheets-numfmt/src/controllers/__tests__/editor.controller.spec.ts@@ -53,7 +53,7 @@ describe('test editor', () => {const univerInstanceService = testBed.get(IUniverInstanceService);testBed.get(NumfmtEditorController);testBed.get(NumfmtCellContent);- workbook = univerInstanceService.getCurrentUniverSheetInstance();+ workbook = univerInstanceService.getCurrentUniverSheetInstance()!;worksheet = workbook.getActiveSheet();});diff --git a/packages/sheets-numfmt/src/controllers/numfmt.auto-fill.controller.ts b/packages/sheets-numfmt/src/controllers/numfmt.auto-fill.controller.tsindex 2269fd65bd..1e00ec061a 100644--- a/packages/sheets-numfmt/src/controllers/numfmt.auto-fill.controller.ts+++ b/packages/sheets-numfmt/src/controllers/numfmt.auto-fill.controller.ts@@ -50,8 +50,9 @@ export class NumfmtAutoFillController extends Disposable {targetStartCell: { row: number; col: number },relativeRange: IRange) => {- const unitId = this._univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = this._univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;+ const unitId = workbook.getUnitId();+ const subUnitId = workbook.getActiveSheet().getSheetId();const sourceRange = {startRow: sourceStartCell.row,startColumn: sourceStartCell.col,diff --git a/packages/sheets-numfmt/src/controllers/numfmt.controller.ts b/packages/sheets-numfmt/src/controllers/numfmt.controller.tsindex 6bfe117cc0..ca081d4571 100644--- a/packages/sheets-numfmt/src/controllers/numfmt.controller.ts+++ b/packages/sheets-numfmt/src/controllers/numfmt.controller.ts@@ -107,7 +107,7 @@ export class NumfmtController extends Disposable implements INumfmtController {if (!range) {return false;}- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const sheet = workbook.getActiveSheet();const cellValue = sheet.getCellRaw(range.startRow, range.startColumn);@@ -193,11 +193,9 @@ export class NumfmtController extends Disposable implements INumfmtController {switch (command.id) {case ClearSelectionAllCommand.id:case ClearSelectionFormatCommand.id: {- const unitId = self._univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = self._univerInstanceService- .getCurrentUniverSheetInstance()- .getActiveSheet()- .getSheetId();+ const workbook = self._univerInstanceService.getCurrentUniverSheetInstance()!;+ const unitId = workbook.getUnitId();+ const subUnitId = workbook.getActiveSheet().getSheetId();const selections = self._selectionManagerService.getSelectionRanges();if (!selections?.length) {break;@@ -283,7 +281,7 @@ export class NumfmtController extends Disposable implements INumfmtController {})).subscribe(({ disposableCollection, selectionRanges }) => {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;this.openPanel();disposableCollection.add(this._sheetInterceptorService.intercept(INTERCEPTOR_POINT.CELL_CONTENT, {diff --git a/packages/sheets-numfmt/src/controllers/numfmt.copy-paste.controller.ts b/packages/sheets-numfmt/src/controllers/numfmt.copy-paste.controller.tsindex 8562a45bc3..1a3cbce3f5 100644--- a/packages/sheets-numfmt/src/controllers/numfmt.copy-paste.controller.ts+++ b/packages/sheets-numfmt/src/controllers/numfmt.copy-paste.controller.ts@@ -117,7 +117,7 @@ export class NumfmtCopyPasteController extends Disposable {pasteType: string;}) {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const sheet = workbook.getActiveSheet();const unitId = workbook.getUnitId();const subUnitId = sheet.getSheetId();diff --git a/packages/sheets-numfmt/src/controllers/numfmt.editor.controller.ts b/packages/sheets-numfmt/src/controllers/numfmt.editor.controller.tsindex a5a8d86f97..f957c780d2 100644--- a/packages/sheets-numfmt/src/controllers/numfmt.editor.controller.ts+++ b/packages/sheets-numfmt/src/controllers/numfmt.editor.controller.ts@@ -200,11 +200,9 @@ export class NumfmtEditorController extends Disposable {getMutations(command) {switch (command.id) {case SetRangeValuesCommand.id: {- const unitId = self._univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = self._univerInstanceService- .getCurrentUniverSheetInstance()- .getActiveSheet()- .getSheetId();+ const workbook = self._univerInstanceService.getCurrentUniverSheetInstance()!;+ const unitId = workbook.getUnitId();+ const subUnitId = workbook.getActiveSheet().getSheetId();const list = self._collectEffectMutation.getEffects();if (!list.length) {return {diff --git a/packages/sheets-numfmt/src/controllers/numfmt.ref-range.controller.ts b/packages/sheets-numfmt/src/controllers/numfmt.ref-range.controller.tsindex 0752f3d9b7..e449515552 100644--- a/packages/sheets-numfmt/src/controllers/numfmt.ref-range.controller.ts+++ b/packages/sheets-numfmt/src/controllers/numfmt.ref-range.controller.ts@@ -114,12 +114,9 @@ export class NumfmtRefRangeController extends Disposable {)).subscribe((disposableCollection) => {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();- const subUnitId = this._univerInstanceService- .getCurrentUniverSheetInstance()- .getActiveSheet()- .getSheetId();+ const subUnitId = workbook.getActiveSheet().getSheetId();const model = this._numfmtService.getModel(unitId, subUnitId);const disposableMap: Map<string, IDisposable> = new Map();const register = (commandInfo: EffectRefRangeParams, row: number, col: number) => {diff --git a/packages/sheets-numfmt/src/controllers/numfmt.sheet.controller.ts b/packages/sheets-numfmt/src/controllers/numfmt.sheet.controller.tsindex 2dc07f0c54..1c88aa7c84 100644--- a/packages/sheets-numfmt/src/controllers/numfmt.sheet.controller.ts+++ b/packages/sheets-numfmt/src/controllers/numfmt.sheet.controller.ts@@ -45,8 +45,8 @@ export class NumfmtSheetController extends Disposable {getMutations: (commandInfo) => {if (commandInfo.id === RemoveSheetCommand.id) {const params = commandInfo.params as IRemoveSheetCommandParams;- const unitId = params.unitId || getunitId(this._univerInstanceService);- const subUnitId = params.subUnitId || getsubUnitId(this._univerInstanceService);+ const unitId = params.unitId || getUnitId(this._univerInstanceService);+ const subUnitId = params.subUnitId || getSubUnitID(this._univerInstanceService);const model = this._numfmtService.getModel(unitId, subUnitId);if (!model) {return { redos: [], undos: [] };@@ -73,5 +73,5 @@ export class NumfmtSheetController extends Disposable {}}-const getunitId = (u: IUniverInstanceService) => u.getCurrentUniverSheetInstance().getUnitId();-const getsubUnitId = (u: IUniverInstanceService) => u.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+const getUnitId = (u: IUniverInstanceService) => u.getCurrentUniverSheetInstance()!.getUnitId();+const getSubUnitID = (u: IUniverInstanceService) => u.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId();diff --git a/packages/sheets-numfmt/src/menu/menu.ts b/packages/sheets-numfmt/src/menu/menu.tsindex e77ddca571..f60cfcb839 100644--- a/packages/sheets-numfmt/src/menu/menu.ts+++ b/packages/sheets-numfmt/src/menu/menu.ts@@ -135,7 +135,7 @@ export const FactoryOtherMenuItem = (componentManager: ComponentManager) => {).subscribe(() => {const selections = selectionManagerService.getSelections();if (selections && selections[0]) {- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const range = selections[0].range;const row = range.startRow;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.tsindex 03c4fbb29b..db84677744 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@@ -349,7 +349,7 @@ describe('Test add worksheet merge commands', () => {]);const commandService = get(ICommandService);const univerInstanceService = get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const mergeData = worksheet.getConfig().mergeData;expect(mergeData.length).toBe(0);@@ -444,7 +444,7 @@ describe('Test add worksheet merge commands', () => {]);const commandService = get(ICommandService);const univerInstanceService = get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const mergeData = worksheet.getConfig().mergeData;expect(mergeData.length).toBe(0);diff --git a/packages/sheets-ui/src/commands/commands/__tests__/auto-fill.controller.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/auto-fill.controller.spec.tsindex 547eae6303..1e050242ab 100644--- a/packages/sheets-ui/src/commands/commands/__tests__/auto-fill.controller.spec.ts+++ b/packages/sheets-ui/src/commands/commands/__tests__/auto-fill.controller.spec.ts@@ -332,7 +332,7 @@ describe('Test auto fill rules in controller', () => {describe('auto fill', () => {describe('auto fill the numbers', async () => {it('correct situation', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');// test number(autoFillController as any)._triggerAutoFill(@@ -373,7 +373,7 @@ describe('Test auto fill rules in controller', () => {describe('auto fill the extend numbers', async () => {it('correct situation', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');// test extend number(autoFillController as any)._triggerAutoFill(@@ -397,7 +397,7 @@ describe('Test auto fill rules in controller', () => {describe('auto fill the chinese numbers', async () => {it('correct situation', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');// test chinese number(autoFillController as any)._triggerAutoFill(@@ -421,7 +421,7 @@ describe('Test auto fill rules in controller', () => {describe('auto fill the chinese week', async () => {it('correct situation', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');// test chinese week@@ -446,7 +446,7 @@ describe('Test auto fill rules in controller', () => {describe('auto fill the loop series', async () => {it('correct situation', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');// test loop series(autoFillController as any)._triggerAutoFill(@@ -470,7 +470,7 @@ describe('Test auto fill rules in controller', () => {describe('auto fill the other string', async () => {it('correct situation', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');// test other string(autoFillController as any)._triggerAutoFill(@@ -494,7 +494,7 @@ describe('Test auto fill rules in controller', () => {describe('auto fill the mixed mode', async () => {it('correct situation', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');// test mixed mode(autoFillController as any)._triggerAutoFill(@@ -533,7 +533,7 @@ describe('Test auto fill rules in controller', () => {describe('auto fill the merged cell', async () => {it('test primary', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');const selectionManagerService = get(SelectionManagerService);@@ -583,7 +583,7 @@ describe('Test auto fill rules in controller', () => {describe('auto fill clear', async () => {it('test primary will move to upper left corner', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');const selectionManagerService = get(SelectionManagerService);@@ -635,7 +635,7 @@ describe('Test auto fill rules in controller', () => {describe('auto fill range is auto detected', async () => {it('correct situation', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');// test other string(autoFillController as any)._handleDbClickFill({@@ -663,7 +663,7 @@ describe('Test auto fill rules in controller', () => {describe('auto fill in left direction', async () => {it('correct situation', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');// test other string(autoFillController as any)._triggerAutoFill(@@ -695,7 +695,7 @@ describe('Test auto fill rules in controller', () => {describe('auto fill with equal ratio & style', async () => {it('correct situation', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');// equal ratio(autoFillController as any)._triggerAutoFill(@@ -743,7 +743,7 @@ describe('Test auto fill rules in controller', () => {describe('auto fill without format', async () => {it('correct situation', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');// equal ratio(autoFillController as any)._triggerAutoFill(@@ -810,7 +810,7 @@ describe('Test auto fill rules in controller', () => {describe('auto fill from single cell', async () => {it('correct situation', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');// test right(autoFillController as any)._triggerAutoFill(diff --git a/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.tsindex fd3e802d33..41d973cad6 100644--- a/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts+++ b/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts@@ -248,7 +248,7 @@ describe('Test format painter rules in controller', () => {describe('format painter', () => {describe('format painter the numbers', async () => {it('correct situation', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');await commandService.executeCommand(SetSelectionsOperation.id, {unitId: 'workbook-01',diff --git a/packages/sheets-ui/src/commands/commands/__tests__/set-frozen.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/set-frozen.command.spec.tsindex 00168a946b..545cbc45a6 100644--- a/packages/sheets-ui/src/commands/commands/__tests__/set-frozen.command.spec.ts+++ b/packages/sheets-ui/src/commands/commands/__tests__/set-frozen.command.spec.ts@@ -86,7 +86,7 @@ describe('Test commands used for change selections', () => {const getFreeze = () => {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getConfig().freeze;};diff --git a/packages/sheets-ui/src/commands/commands/__tests__/set-selections.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/set-selections.command.spec.tsindex de5238a96a..c45bdc2660 100644--- a/packages/sheets-ui/src/commands/commands/__tests__/set-selections.command.spec.ts+++ b/packages/sheets-ui/src/commands/commands/__tests__/set-selections.command.spec.ts@@ -114,26 +114,26 @@ describe('Test commands used for change selections', () => {function getRowCount(): number {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getRowCount();}function getColCount(): number {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getColumnCount();}function getRowVisible(row: number): boolean {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getRowVisible(row);}function getColVisible(col: number): boolean {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getColVisible(col);}diff --git a/packages/sheets-ui/src/commands/commands/auto-fill.command.ts b/packages/sheets-ui/src/commands/commands/auto-fill.command.tsindex 0113e51054..ef954db21e 100644--- a/packages/sheets-ui/src/commands/commands/auto-fill.command.ts+++ b/packages/sheets-ui/src/commands/commands/auto-fill.command.ts@@ -17,7 +17,7 @@import type { ICommand, IRange } from '@univerjs/core';import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core';import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';-import { SelectionManagerService, SetRangeValuesMutation, SetRangeValuesUndoMutationFactory, SetSelectionsOperation } from '@univerjs/sheets';+import { getSheetCommandTarget, SetRangeValuesMutation, SetRangeValuesUndoMutationFactory, SetSelectionsOperation } from '@univerjs/sheets';import type { IAccessor } from '@wendellhu/redi';import { generateNullCellValue } from '../../services/auto-fill/tools';@@ -43,14 +43,13 @@ export const AutoClearContentCommand: ICommand = {type: CommandType.COMMAND,handler: async (accessor: IAccessor, params: IAutoClearContentCommand) => {const univerInstanceService = accessor.get(IUniverInstanceService);- const selectionManagerService = accessor.get(SelectionManagerService);const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const unitId = workbook.getUnitId();- const worksheet = workbook.getActiveSheet();- const subUnitId = worksheet.getSheetId();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const { clearRange, selectionRange } = params;const clearMutationParams: ISetRangeValuesMutationParams = {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 .tsindex 2396e30c29..251c366f6d 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@@ -16,7 +16,7 @@import type { ICommand, IRange } from '@univerjs/core';import { CommandType, ICommandService, IUniverInstanceService, LocaleService, Rectangle } from '@univerjs/core';-import { DeleteRangeMoveLeftCommand, SelectionManagerService } from '@univerjs/sheets';+import { DeleteRangeMoveLeftCommand, getSheetCommandTarget, SelectionManagerService } from '@univerjs/sheets';import { IConfirmService } from '@univerjs/ui';export const DeleteRangeMoveLeftConfirmCommand: ICommand = {@@ -33,13 +33,16 @@ export const DeleteRangeMoveLeftConfirmCommand: ICommand = {if (!selection) {return false;}- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- let range = selection[0].range;+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { worksheet } = target;+ let range = selection[0].range;if (!range) {return false;}+range = { ...range, endColumn: worksheet.getColumnCount() - 1 };const getRowLength = (range: IRange) => range.endRow - range.startRow;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.tsindex 90af20fff0..42df3164b2 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@@ -16,7 +16,7 @@import type { ICommand, IRange } from '@univerjs/core';import { CommandType, ICommandService, IUniverInstanceService, LocaleService, Rectangle } from '@univerjs/core';-import { DeleteRangeMoveUpCommand, SelectionManagerService } from '@univerjs/sheets';+import { DeleteRangeMoveUpCommand, getSheetCommandTarget, SelectionManagerService } from '@univerjs/sheets';import { IConfirmService } from '@univerjs/ui';export const DeleteRangeMoveUpConfirmCommand: ICommand = {@@ -30,16 +30,15 @@ export const DeleteRangeMoveUpConfirmCommand: ICommand = {const univerInstanceService = accessor.get(IUniverInstanceService);const selection = selectionManagerService.getSelections();- if (!selection) {- return false;- }- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();+ if (!selection) return false;++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { worksheet } = target;let range = selection[0].range;+ if (!range) return false;- if (!range) {- return false;- }range = { ...range, endRow: worksheet.getColumnCount() - 1 };const getColLength = (range: IRange) => range.endColumn - range.startColumn;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.tsindex d077d0c4c8..9b22c06b05 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@@ -16,7 +16,7 @@import type { ICommand } from '@univerjs/core';import { CommandType, ICommandService, IUniverInstanceService, LocaleService } from '@univerjs/core';-import { SelectionManagerService, SetColHiddenCommand, SetRowHiddenCommand } from '@univerjs/sheets';+import { getSheetCommandTarget, SelectionManagerService, SetColHiddenCommand, SetRowHiddenCommand } from '@univerjs/sheets';import { IConfirmService } from '@univerjs/ui';import type { IAccessor } from '@wendellhu/redi';@@ -29,14 +29,15 @@ export const HideRowConfirmCommand: ICommand = {const selectionManagerService = accessor.get(SelectionManagerService);const ranges = selectionManagerService.getSelections()?.map((s) => s.range);- if (!ranges?.length) {- return false;- }+ if (!ranges?.length) return false;const commandService = accessor.get(ICommandService);const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { worksheet } = target;const allRowRanges = worksheet.getVisibleRows();if (isAllRowsCovered(allRowRanges, ranges)) {@@ -74,8 +75,11 @@ export const HideColConfirmCommand: ICommand = {const commandService = accessor.get(ICommandService);const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { worksheet } = target;const allColumnRanges = worksheet.getVisibleCols();if (isAllColumnsCovered(allColumnRanges, ranges)) {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.tsindex dd1a3ac749..915ad816f6 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@@ -16,7 +16,7 @@import type { ICommand, IRange } from '@univerjs/core';import { CommandType, ICommandService, IUniverInstanceService, LocaleService, Rectangle } from '@univerjs/core';-import { InsertRangeMoveDownCommand, SelectionManagerService } from '@univerjs/sheets';+import { getSheetCommandTarget, InsertRangeMoveDownCommand, SelectionManagerService } from '@univerjs/sheets';import { IConfirmService } from '@univerjs/ui';export const InsertRangeMoveDownConfirmCommand: ICommand = {@@ -33,8 +33,11 @@ export const InsertRangeMoveDownConfirmCommand: ICommand = {if (!selection) {return false;}- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { worksheet } = target;let range = selection[0].range;if (!range) {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.tsindex 411f50cdd4..533a6b4e66 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@@ -16,7 +16,7 @@import type { ICommand, IRange } from '@univerjs/core';import { CommandType, ICommandService, IUniverInstanceService, LocaleService, Rectangle } from '@univerjs/core';-import { InsertRangeMoveRightCommand, SelectionManagerService } from '@univerjs/sheets';+import { getSheetCommandTarget, InsertRangeMoveRightCommand, SelectionManagerService } from '@univerjs/sheets';import { IConfirmService } from '@univerjs/ui';export const InsertRangeMoveRightConfirmCommand: ICommand = {@@ -33,8 +33,12 @@ export const InsertRangeMoveRightConfirmCommand: ICommand = {if (!selection) {return false;}- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { worksheet } = target;+let range = selection[0].range;if (!range) {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.tsindex 063b1bd697..dcdbc1745c 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@@ -17,7 +17,7 @@import type { ICommand } from '@univerjs/core';import { CommandType, ICommandService, IUniverInstanceService, LocaleService } from '@univerjs/core';import type { IRemoveRowColCommandParams } from '@univerjs/sheets';-import { RemoveColCommand, RemoveRowCommand, SelectionManagerService } from '@univerjs/sheets';+import { getSheetCommandTarget, RemoveColCommand, RemoveRowCommand, SelectionManagerService } from '@univerjs/sheets';import { IConfirmService } from '@univerjs/ui';import type { IAccessor } from '@wendellhu/redi';@@ -39,8 +39,11 @@ export const RemoveRowConfirmCommand: ICommand = {const commandService = accessor.get(ICommandService);const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { worksheet } = target;const allRowRanges = worksheet.getVisibleRows();if (isAllRowsCovered(allRowRanges, [range])) {@@ -81,8 +84,11 @@ export const RemoveColConfirmCommand: ICommand = {const commandService = accessor.get(ICommandService);const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { worksheet } = target;const allColumnRanges = worksheet.getVisibleCols();if (isAllColumnsCovered(allColumnRanges, [range])) {diff --git a/packages/sheets-ui/src/commands/commands/set-format-painter.command.ts b/packages/sheets-ui/src/commands/commands/set-format-painter.command.tsindex 1da663d934..e1ca00e5b7 100644--- a/packages/sheets-ui/src/commands/commands/set-format-painter.command.ts+++ b/packages/sheets-ui/src/commands/commands/set-format-painter.command.ts@@ -34,6 +34,7 @@ import {AddMergeUndoMutationFactory,AddWorksheetMergeMutation,getAddMergeMutationRangeByType,+ getSheetCommandTarget,INTERCEPTOR_POINT,RemoveMergeUndoMutationFactory,RemoveWorksheetMergeMutation,@@ -105,12 +106,15 @@ export const ApplyFormatPainterCommand: ICommand = {const univerInstanceService = accessor.get(IUniverInstanceService);const selectionManagerService = accessor.get(SelectionManagerService);const sheetInterceptorService = accessor.get(SheetInterceptorService);++ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) return false;++ const { worksheet, unitId, subUnitId } = target;const {styleValues: value,styleRange: range,mergeRanges,- unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId(),- subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId(),} = params;const currentSelections = range ? [range] : selectionManagerService.getSelectionRanges();@@ -172,7 +176,6 @@ export const ApplyFormatPainterCommand: ICommand = {// handle mergeconst ranges = getAddMergeMutationRangeByType(mergeRanges);- const worksheet = univerInstanceService.getUniverSheetInstance(unitId)!.getSheetBySheetId(subUnitId)!;const mergeRedos: IMutationInfo[] = [];const mergeUndos: IMutationInfo[] = [];diff --git a/packages/sheets-ui/src/commands/commands/set-frozen.command.ts b/packages/sheets-ui/src/commands/commands/set-frozen.command.tsindex b94f1c12d5..0dff6db414 100644--- a/packages/sheets-ui/src/commands/commands/set-frozen.command.ts+++ b/packages/sheets-ui/src/commands/commands/set-frozen.command.ts@@ -17,7 +17,7 @@import type { ICommand } from '@univerjs/core';import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, RANGE_TYPE } from '@univerjs/core';import type { ISetFrozenMutationParams } from '@univerjs/sheets';-import { SelectionManagerService, SetFrozenMutation, SetFrozenMutationFactory } from '@univerjs/sheets';+import { getSheetCommandTarget, SelectionManagerService, SetFrozenMutation, SetFrozenMutationFactory } from '@univerjs/sheets';import { ScrollManagerService } from '../../services/scroll-manager.service';@@ -38,8 +38,10 @@ export const SetSelectionFrozenCommand: ICommand<ISetSelectionFrozenCommandParamconst { type } = params || {};const univerInstanceService = accessor.get(IUniverInstanceService);const undoRedoService = accessor.get(IUndoRedoService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const commandService = accessor.get(ICommandService);const selectionManagerService = accessor.get(SelectionManagerService);const selections = selectionManagerService.getSelections();@@ -133,8 +135,10 @@ export const CancelFrozenCommand: ICommand = {const commandService = accessor.get(ICommandService);const univerInstanceService = accessor.get(IUniverInstanceService);const undoRedoService = accessor.get(IUndoRedoService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const redoMutationParams: ISetFrozenMutationParams = {unitId,diff --git a/packages/sheets-ui/src/commands/commands/set-scroll.command.ts b/packages/sheets-ui/src/commands/commands/set-scroll.command.tsindex 12be8066a6..d357a7c497 100644--- a/packages/sheets-ui/src/commands/commands/set-scroll.command.ts+++ b/packages/sheets-ui/src/commands/commands/set-scroll.command.ts@@ -17,6 +17,7 @@import type { ICommand, IRange } from '@univerjs/core';import { CommandType, ICommandService, IUniverInstanceService } from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import { ScrollManagerService } from '../../services/scroll-manager.service';import { SetScrollOperation } from '../operations/scroll.operation';import { ScrollController } from '../../controllers/scroll.controller';@@ -35,9 +36,12 @@ export const SetScrollRelativeCommand: ICommand<ISetScrollRelativeCommandParams>handler: async (accessor, params = { offsetX: 0, offsetY: 0 }) => {const commandService = accessor.get(ICommandService);const scrollManagerService = accessor.get(ScrollManagerService);- const currentUniverService = accessor.get(IUniverInstanceService);- const workbook = currentUniverService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();+ const univerInstanceService = accessor.get(IUniverInstanceService);++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId, worksheet } = target;const { xSplit, ySplit } = worksheet.getConfig().freeze;const currentScroll = scrollManagerService.getCurrentScroll();const { offsetX = 0, offsetY = 0 } = params || {};@@ -49,8 +53,8 @@ export const SetScrollRelativeCommand: ICommand<ISetScrollRelativeCommandParams>} = currentScroll || {};return commandService.executeCommand(SetScrollOperation.id, {- unitId: workbook.getUnitId(),- sheetId: worksheet.getSheetId(),+ unitId,+ sheetId: subUnitId,sheetViewStartRow: sheetViewStartRow + ySplit,sheetViewStartColumn: sheetViewStartColumn + xSplit,offsetX: currentOffsetX + offsetX,@@ -80,11 +84,13 @@ export const ScrollCommand: ICommand<IScrollCommandParams> = {const univerInstanceService = accessor.get(IUniverInstanceService);const scrollManagerService = accessor.get(ScrollManagerService);- const currentWorkbook = univerInstanceService.getCurrentUniverSheetInstance();- const currentWorksheet = currentWorkbook.getActiveSheet();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { workbook, worksheet } = target;const currentScroll = scrollManagerService.getCurrentScroll();- if (!currentWorksheet) {+ if (!worksheet) {return false;}@@ -96,12 +102,12 @@ export const ScrollCommand: ICommand<IScrollCommandParams> = {offsetY: currentOffsetY,} = currentScroll || {};- const { xSplit, ySplit } = currentWorksheet.getConfig().freeze;+ const { xSplit, ySplit } = worksheet.getConfig().freeze;const commandService = accessor.get(ICommandService);return commandService.syncExecuteCommand(SetScrollOperation.id, {- unitId: currentWorkbook.getUnitId(),- sheetId: currentWorksheet.getSheetId(),+ unitId: workbook.getUnitId(),+ sheetId: worksheet.getSheetId(),sheetViewStartRow: sheetViewStartRow ?? (currentRow ?? 0) + ySplit,sheetViewStartColumn: sheetViewStartColumn ?? (currentColumn ?? 0) + xSplit,offsetX: offsetX ?? currentOffsetX,@@ -134,19 +140,14 @@ export const ResetScrollCommand: ICommand = {type: CommandType.COMMAND,handler: async (accessor) => {const univerInstanceService = accessor.get(IUniverInstanceService);+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;- const currentWorkbook = univerInstanceService.getCurrentUniverSheetInstance();- const currentWorksheet = currentWorkbook.getActiveSheet();-- if (!currentWorksheet) {- return false;- }-+ const { subUnitId, unitId } = target;const commandService = accessor.get(ICommandService);-return commandService.executeCommand(SetScrollOperation.id, {- unitId: currentWorkbook.getUnitId(),- sheetId: currentWorksheet.getSheetId(),+ unitId,+ sheetId: subUnitId,sheetViewStartRow: 0,sheetViewStartColumn: 0,});diff --git a/packages/sheets-ui/src/commands/commands/set-selection.command.ts b/packages/sheets-ui/src/commands/commands/set-selection.command.tsindex c322292b89..e1f79c44a0 100644--- a/packages/sheets-ui/src/commands/commands/set-selection.command.ts+++ b/packages/sheets-ui/src/commands/commands/set-selection.command.ts@@ -18,6 +18,7 @@ import type { Direction, ICommand, IRange } from '@univerjs/core';import { CommandType, ICommandService, IUniverInstanceService, RANGE_TYPE, Rectangle, Tools } from '@univerjs/core';import {getCellAtRowCol,+ getSheetCommandTarget,NORMAL_SELECTION_PLUGIN_NAME,SelectionManagerService,SetSelectionsOperation,@@ -65,8 +66,10 @@ export const MoveSelectionCommand: ICommand<IMoveSelectionCommandParams> = {return false;}- const workbook = accessor.get(IUniverInstanceService).getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;++ const { workbook, worksheet } = target;const selection = accessor.get(SelectionManagerService).getLast();if (!selection) {return false;@@ -136,9 +139,16 @@ export const MoveSelectionEnterAndTabCommand: ICommand<IMoveSelectionEnterAndTabreturn false;}- const workbook = accessor.get(IUniverInstanceService).getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;++ const { workbook, worksheet } = target;+const selection = accessor.get(SelectionManagerService).getLast();+ if (!selection) {+ return false;+ }+const unitId = workbook.getUnitId();const sheetId = worksheet.getSheetId();if (!selection) {@@ -276,40 +286,43 @@ export const ExpandSelectionCommand: ICommand<IExpandSelectionCommandParams> = {return false;}- const currentWorkbook = accessor.get(IUniverInstanceService).getCurrentUniverSheetInstance();- const currentWorksheet = currentWorkbook.getActiveSheet();+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;++ const { worksheet, unitId, subUnitId } = target;const selection = accessor.get(SelectionManagerService).getLast();if (!selection) {return false;}+const { range: startRange, primary } = selection;const { jumpOver, direction } = params;- const isShrink = checkIfShrink(selection, direction, currentWorksheet);+ const isShrink = checkIfShrink(selection, direction, worksheet);const destRange = !isShrink? jumpOver === JumpOver.moveGap- ? expandToNextGapRange(startRange, direction, currentWorksheet)- : expandToNextCell(startRange, direction, currentWorksheet)+ ? expandToNextGapRange(startRange, direction, worksheet)+ : expandToNextCell(startRange, direction, worksheet): jumpOver === JumpOver.moveGap? shrinkToNextGapRange(startRange,// TODO: should fix on SelectionManagerService's side{ ...Rectangle.clone(primary), rangeType: RANGE_TYPE.NORMAL },direction,- currentWorksheet+ worksheet)- : shrinkToNextCell(startRange, direction, currentWorksheet);+ : shrinkToNextCell(startRange, direction, worksheet);if (Rectangle.equals(destRange, startRange)) {return false;}return accessor.get(ICommandService).executeCommand(SetSelectionsOperation.id, {- unitId: currentWorkbook.getUnitId(),- subUnitId: currentWorksheet.getSheetId(),+ unitId,+ subUnitId,pluginName: NORMAL_SELECTION_PLUGIN_NAME,selections: [{@@ -342,14 +355,16 @@ export const SelectAllCommand: ICommand<ISelectAllCommandParams> = {},handler: async (accessor, params = { expandToGapFirst: true, loop: false }) => {const selection = accessor.get(SelectionManagerService).getLast();- const workbook = accessor.get(IUniverInstanceService).getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;++ const { worksheet, unitId, subUnitId } = target;if (!selection || !worksheet) {return false;}- const id = `${workbook.getUnitId()}|${worksheet.getSheetId()}`;+ const id = `${unitId}|${subUnitId}`;if (id !== SELECTED_RANGE_WORKSHEET) {RANGES_STACK = [];SELECTED_RANGE_WORKSHEET = id;@@ -399,8 +414,8 @@ export const SelectAllCommand: ICommand<ISelectAllCommandParams> = {}return accessor.get(ICommandService).executeCommand(SetSelectionsOperation.id, {- unitId: workbook.getUnitId(),- subUnitId: worksheet.getSheetId(),+ unitId,+ subUnitId,pluginName: NORMAL_SELECTION_PLUGIN_NAME,selections: [{diff --git a/packages/sheets-ui/src/commands/commands/set-zoom-ratio.command.ts b/packages/sheets-ui/src/commands/commands/set-zoom-ratio.command.tsindex 2d22d09bd3..2d91f2e0d4 100644--- a/packages/sheets-ui/src/commands/commands/set-zoom-ratio.command.ts+++ b/packages/sheets-ui/src/commands/commands/set-zoom-ratio.command.ts@@ -17,6 +17,7 @@import type { ICommand } from '@univerjs/core';import { CommandType, ICommandService, IUniverInstanceService } from '@univerjs/core';+import { getSheetCommandTarget } from '@univerjs/sheets';import { SHEET_ZOOM_RANGE } from '../../common/keys';import { SetZoomRatioOperation } from '../operations/set-zoom-ratio.operation';@@ -41,11 +42,13 @@ export const ChangeZoomRatioCommand: ICommand<IChangeZoomRatioCommandParams> = {if (!params) {return false;}++ const univerInstanceService = accessor.get(IUniverInstanceService);+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { worksheet, subUnitId, unitId } = target;const { delta, reset } = params;- const workbook = accessor.get(IUniverInstanceService).getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();let zoom = reset ? 100 : Math.round((worksheet.getConfig().zoomRatio + delta) * 100);zoom = Math.max(SHEET_ZOOM_RANGE[0], zoom);@@ -68,8 +71,8 @@ export const SetZoomRatioCommand: ICommand<ISetZoomRatioCommandParams> = {if (!params) {return false;}- const { unitId, subUnitId, zoomRatio } = params;+ const { unitId, subUnitId, zoomRatio } = params;return accessor.get(ICommandService).executeCommand(SetZoomRatioOperation.id, {unitId,subUnitId,diff --git a/packages/sheets-ui/src/commands/operations/sidebar-defined-name.operation.ts b/packages/sheets-ui/src/commands/operations/sidebar-defined-name.operation.tsindex 9d656aa9ad..92881c7d9a 100644--- a/packages/sheets-ui/src/commands/operations/sidebar-defined-name.operation.ts+++ b/packages/sheets-ui/src/commands/operations/sidebar-defined-name.operation.ts@@ -18,6 +18,7 @@ import type { ICommand } from '@univerjs/core';import { CommandType, IUniverInstanceService, LocaleService } from '@univerjs/core';import { IEditorService, ISidebarService } from '@univerjs/ui';import type { IAccessor } from '@wendellhu/redi';+import { getSheetCommandTarget } from '@univerjs/sheets';import { DEFINED_NAME_CONTAINER } from '../../views/defined-name/component-name';export interface IUIComponentCommandParams {@@ -32,10 +33,14 @@ export const SidebarDefinedNameOperation: ICommand = {const editorService = accessor.get(IEditorService);const localeService = accessor.get(LocaleService);const univerInstanceService = accessor.get(IUniverInstanceService);- const unit = univerInstanceService.getCurrentUniverSheetInstance();++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId } = target;switch (params.value) {case 'open':- editorService.setOperationSheetUnitId(unit.getUnitId());+ editorService.setOperationSheetUnitId(unitId);sidebarService.open({header: { title: localeService.t('definedName.featureTitle') },children: { label: DEFINED_NAME_CONTAINER },diff --git a/packages/sheets-ui/src/controllers/auto-fill.controller.ts b/packages/sheets-ui/src/controllers/auto-fill.controller.tsindex a257cffa16..d351512440 100644--- a/packages/sheets-ui/src/controllers/auto-fill.controller.ts+++ b/packages/sheets-ui/src/controllers/auto-fill.controller.ts@@ -275,8 +275,12 @@ export class AutoFillController extends Disposable {} else {return;}- const unitId = this._univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = this._univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();++ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ if (!workbook) return;++ const unitId = workbook.getUnitId();+ const subUnitId = workbook.getActiveSheet().getSheetId();this._autoFillService.direction = direction;this._autoFillService.autoFillLocation = {source,@@ -354,8 +358,8 @@ export class AutoFillController extends Disposable {const {source,target,- unitId = this._univerInstanceService.getCurrentUniverSheetInstance().getUnitId(),- subUnitId = this._univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId(),+ unitId = this._univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId(),+ subUnitId = this._univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId(),} = this._autoFillService.autoFillLocation || {};const direction = this._autoFillService.direction;if (!source || !target) {@@ -524,7 +528,7 @@ export class AutoFillController extends Disposable {endColumn: copyEndColumn,} = source;const currentCellDatas = this._univerInstanceService- .getCurrentUniverSheetInstance()+ .getCurrentUniverSheetInstance()!.getActiveSheet().getCellMatrix();const rules = this._autoFillService.getRules();@@ -600,7 +604,7 @@ export class AutoFillController extends Disposable {}private _getMergeApplyData(source: IRange, target: IRange, direction: Direction, csLen: number) {- const mergeData = this._univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getMergeData();+ const mergeData = this._univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getMergeData();const applyMergeRanges = [];for (let i = source.startRow; i <= source.endRow; i++) {for (let j = source.startColumn; j <= source.endColumn; j++) {@@ -673,7 +677,7 @@ export class AutoFillController extends Disposable {const { source, target } = location;// cache original data of apply rangeconst currentCellDatas = this._univerInstanceService- .getCurrentUniverSheetInstance()+ .getCurrentUniverSheetInstance()!.getActiveSheet().getCellMatrix();// cache the original data in currentCellDatas in apply range for later use / refill@@ -776,7 +780,7 @@ export class AutoFillController extends Disposable {// deal with styleslet applyMergeRanges: IRange[] = [];- const style = this._univerInstanceService.getCurrentUniverSheetInstance().getStyles();+ const style = this._univerInstanceService.getCurrentUniverSheetInstance()!.getStyles();if (hasStyle) {applyMergeRanges = this._getMergeApplyData(source, target, direction, csLen);applyDatas.forEach((row) => {diff --git a/packages/sheets-ui/src/controllers/auto-height.controller.ts b/packages/sheets-ui/src/controllers/auto-height.controller.tsindex b29c888f9f..98b96fe32e 100644--- a/packages/sheets-ui/src/controllers/auto-height.controller.ts+++ b/packages/sheets-ui/src/controllers/auto-height.controller.ts@@ -57,7 +57,7 @@ export class AutoHeightController {const { skeleton } = sheetSkeletonService.getCurrent()!;const rowsAutoHeightInfo = skeleton.calculateAutoHeightInRange(ranges);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const subUnitId = workbook.getActiveSheet().getSheetId();diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.tsindex af19337b60..6657699eb5 100644--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts@@ -521,9 +521,7 @@ export class SheetClipboardController extends RxDisposable {label: 'specialPaste.value',},onPasteCells: (pasteFrom, pasteTo, data, pasteType) => {- const workbook = self._currentUniverSheet.getCurrentUniverSheetInstance();- const unitId = workbook.getUnitId();- const subUnitId = workbook.getActiveSheet().getSheetId();+ const workbook = self._currentUniverSheet.getCurrentUniverSheetInstance()!;return getSetCellValueMutations(pasteTo, data, accessor);},};@@ -533,9 +531,7 @@ export class SheetClipboardController extends RxDisposable {label: 'specialPaste.format',},onPasteCells(pasteFrom, pasteTo, matrix, pasteType) {- const workbook = self._currentUniverSheet.getCurrentUniverSheetInstance();- const unitId = workbook.getUnitId();- const subUnitId = workbook.getActiveSheet().getSheetId();+ const workbook = self._currentUniverSheet.getCurrentUniverSheetInstance()!;const redoMutationsInfo: IMutationInfo[] = [];const undoMutationsInfo: IMutationInfo[] = [];@@ -581,7 +577,7 @@ export class SheetClipboardController extends RxDisposable {};},onPasteColumns(pasteTo, colProperties, payload) {- const workbook = self._currentUniverSheet.getCurrentUniverSheetInstance();+ const workbook = self._currentUniverSheet.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const subUnitId = workbook.getActiveSheet().getSheetId();const redoMutations: IMutationInfo[] = [];@@ -627,7 +623,7 @@ export class SheetClipboardController extends RxDisposable {label: 'specialPaste.besidesBorder',},onPasteCells(pasteFrom, pasteTo, matrix, payload) {- const workbook = self._currentUniverSheet.getCurrentUniverSheetInstance();+ const workbook = self._currentUniverSheet.getCurrentUniverSheetInstance()!;const redoMutationsInfo: IMutationInfo[] = [];const undoMutationsInfo: IMutationInfo[] = [];const { range, unitId, subUnitId } = pasteTo;diff --git a/packages/sheets-ui/src/controllers/contextmenu/contextmenu.controller.ts b/packages/sheets-ui/src/controllers/contextmenu/contextmenu.controller.tsindex a2b9ab8d18..462b68873a 100644--- a/packages/sheets-ui/src/controllers/contextmenu/contextmenu.controller.ts+++ b/packages/sheets-ui/src/controllers/contextmenu/contextmenu.controller.ts@@ -39,7 +39,7 @@ import { getSheetObject } from '../utils/component-tools';@OnLifecycle(LifecycleStages.Rendered, SheetContextMenuController)export class SheetContextMenuController extends Disposable {constructor(- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IContextMenuService private readonly _contextMenuService: IContextMenuService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@Inject(SelectionManagerService)@@ -47,7 +47,7 @@ export class SheetContextMenuController extends Disposable {@ISelectionRenderService private readonly _selectionRenderService: ISelectionRenderService) {super();- this._currentUniverService.currentSheet$.subscribe((workbook) => {+ this._univerInstanceService.currentSheet$.subscribe((workbook) => {if (workbook == null) {throw new Error('workbook is null');}@@ -56,7 +56,7 @@ export class SheetContextMenuController extends Disposable {}private _addListeners(): void {- const objects = getSheetObject(this._currentUniverService, this._renderManagerService);+ const objects = getSheetObject(this._univerInstanceService, this._renderManagerService);if (!objects) {return;}diff --git a/packages/sheets-ui/src/controllers/editor-bridge.controller.ts b/packages/sheets-ui/src/controllers/editor-bridge.controller.tsindex 43f1729473..f5b4e130c3 100644--- a/packages/sheets-ui/src/controllers/editor-bridge.controller.ts+++ b/packages/sheets-ui/src/controllers/editor-bridge.controller.ts@@ -40,7 +40,7 @@ export class EditorBridgeController extends RxDisposable {constructor(@Inject(Injector) private readonly _injector: Injector,@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@ICommandService private readonly _commandService: ICommandService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@IEditorBridgeService private readonly _editorBridgeService: IEditorBridgeService,@@ -148,7 +148,7 @@ export class EditorBridgeController extends RxDisposable {private _initialChangeEditorListener() {this.disposeWithMe(- this._currentUniverService.currentDoc$.subscribe((documentDataModel) => {+ this._univerInstanceService.currentDoc$.subscribe((documentDataModel) => {if (documentDataModel == null) {return;}@@ -260,7 +260,7 @@ export class EditorBridgeController extends RxDisposable {}private _getCurrentUnitIdAndSheetId() {- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return {unitId: workbook.getUnitId(),@@ -270,7 +270,7 @@ export class EditorBridgeController extends RxDisposable {}private _getSheetObject() {- return getSheetObject(this._currentUniverService, this._renderManagerService);+ return getSheetObject(this._univerInstanceService, this._renderManagerService);}private _commandExecutedListener() {diff --git a/packages/sheets-ui/src/controllers/editor/editing.controller.ts b/packages/sheets-ui/src/controllers/editor/editing.controller.tsindex 1a592dcd1e..7dd4010ca4 100644--- a/packages/sheets-ui/src/controllers/editor/editing.controller.ts+++ b/packages/sheets-ui/src/controllers/editor/editing.controller.ts@@ -32,7 +32,7 @@ import { takeUntil } from 'rxjs';@OnLifecycle(LifecycleStages.Rendered, EditingController)export class EditingController extends RxDisposable {constructor(- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@Inject(IUndoRedoService) private readonly _undoRedoService: IUndoRedoService) {@@ -49,7 +49,7 @@ export class EditingController extends RxDisposable {}private _listenEditorBlur() {- this._currentUniverService.currentDoc$.pipe(takeUntil(this.dispose$)).subscribe((docDataModel) => {+ this._univerInstanceService.currentDoc$.pipe(takeUntil(this.dispose$)).subscribe((docDataModel) => {if (docDataModel == null) {return;}@@ -78,8 +78,8 @@ export class EditingController extends RxDisposable {* formulaEngine?.calculate(`=lambda(x,y, x*y*x)(sum(1,(1+2)*3),2)+1-max(100,200)`);*/const sheetData: ISheetData = {};- this._currentUniverService- .getCurrentUniverSheetInstance()+ this._univerInstanceService+ .getCurrentUniverSheetInstance()!.getSheets().forEach((sheet) => {const sheetConfig = sheet.getConfig();@@ -139,6 +139,6 @@ export class EditingController extends RxDisposable {private _commandExecutedListener() {}private _getDocObject() {- return getDocObject(this._currentUniverService, this._renderManagerService);+ return getDocObject(this._univerInstanceService, this._renderManagerService);}}diff --git a/packages/sheets-ui/src/controllers/editor/end-edit.controller.ts b/packages/sheets-ui/src/controllers/editor/end-edit.controller.tsindex 4621ceda81..4053e9d14b 100644--- a/packages/sheets-ui/src/controllers/editor/end-edit.controller.ts+++ b/packages/sheets-ui/src/controllers/editor/end-edit.controller.ts@@ -83,7 +83,7 @@ export class EndEditController extends Disposable {private _cursorChange: CursorChange = CursorChange.InitialState;constructor(- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@ICommandService private readonly _commandService: ICommandService,@IEditorBridgeService private readonly _editorBridgeService: IEditorBridgeService,@@ -184,7 +184,7 @@ export class EndEditController extends Disposable {return;}- const workbook = this._currentUniverService.getUniverSheetInstance(unitId);+ const workbook = this._univerInstanceService.getUniverSheetInstance(unitId);const worksheet = workbook?.getSheetBySheetId(sheetId);diff --git a/packages/sheets-ui/src/controllers/editor/start-edit.controller.ts b/packages/sheets-ui/src/controllers/editor/start-edit.controller.tsindex 7b3b58e001..1d0361aaf0 100644--- a/packages/sheets-ui/src/controllers/editor/start-edit.controller.ts+++ b/packages/sheets-ui/src/controllers/editor/start-edit.controller.ts@@ -83,7 +83,7 @@ export class StartEditController extends Disposable {@Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,@Inject(DocViewModelManagerService) private readonly _docViewModelManagerService: DocViewModelManagerService,@IContextService private readonly _contextService: IContextService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@IEditorBridgeService private readonly _editorBridgeService: IEditorBridgeService,@ICellEditorManagerService private readonly _cellEditorManagerService: ICellEditorManagerService,@@ -181,7 +181,7 @@ export class StartEditController extends Disposable {documentModel!.updateDocumentDataPageSize((endX - startX) / scaleX);}- this._currentUniverService.changeDoc(editorUnitId, documentModel!);+ this._univerInstanceService.changeDoc(editorUnitId, documentModel!);this._contextService.setContextValue(FOCUSING_EDITOR_BUT_HIDDEN, true);@@ -609,7 +609,7 @@ export class StartEditController extends Disposable {this._textSelectionRenderManager.onInputBefore$.subscribe((config) => {const isFocusFormulaEditor = this._contextService.getContextValue(FOCUSING_FORMULA_EDITOR);const isFocusSheets = this._contextService.getContextValue(FOCUSING_SHEET);- const unitId = this._currentUniverService.getCurrentUniverDocInstance().getUnitId();+ const unitId = this._univerInstanceService.getCurrentUniverDocInstance()!.getUnitId();if (isFocusSheets && !isFocusFormulaEditor && this._editorService.isSheetEditor(unitId)) {this._showEditorByKeyboard(config);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.tsindex 0be244dc8e..a435d770cd 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@@ -39,7 +39,7 @@ export class FormatPainterController extends Disposable {constructor(@ICommandService private readonly _commandService: ICommandService,@IFormatPainterService private readonly _formatPainterService: IFormatPainterService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@ISelectionRenderService private readonly _selectionRenderService: ISelectionRenderService) {@@ -89,8 +89,9 @@ export class FormatPainterController extends Disposable {private async _applyFormatPainter(range: IRange) {const { styles: stylesMatrix, merges } = this._formatPainterService.getSelectionFormat();- const unitId = this._currentUniverService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = this._currentUniverService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;+ const unitId = workbook.getUnitId();+ const subUnitId = workbook.getActiveSheet().getSheetId();if (!stylesMatrix) return;const { startRow, startColumn, endRow, endColumn } = stylesMatrix.getDataRange();@@ -147,6 +148,6 @@ export class FormatPainterController extends Disposable {}private _getSheetObject() {- return getSheetObject(this._currentUniverService, this._renderManagerService);+ return getSheetObject(this._univerInstanceService, this._renderManagerService);}}diff --git a/packages/sheets-ui/src/controllers/freeze.controller.ts b/packages/sheets-ui/src/controllers/freeze.controller.tsindex 3b099cdc6a..fb4441110c 100644--- a/packages/sheets-ui/src/controllers/freeze.controller.ts+++ b/packages/sheets-ui/src/controllers/freeze.controller.ts@@ -140,7 +140,7 @@ export class FreezeController extends Disposable {constructor(@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@ICommandService private readonly _commandService: ICommandService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@ISelectionRenderService private readonly _selectionRenderService: ISelectionRenderService,@@ -586,7 +586,7 @@ export class FreezeController extends Disposable {return;}- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const oldFreeze = worksheet.getConfig()?.freeze;let xSplit = oldFreeze?.xSplit || 0;@@ -1074,7 +1074,7 @@ export class FreezeController extends Disposable {}private _refreshCurrent() {- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const freeze = worksheet.getConfig().freeze;@@ -1125,7 +1125,7 @@ export class FreezeController extends Disposable {}const createFreezeMutationAndRefresh = (newFreeze: IFreeze) => {- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const worksheet = workbook.getActiveSheet();const subUnitId = worksheet.getSheetId();@@ -1353,7 +1353,7 @@ export class FreezeController extends Disposable {this._commandService.onCommandExecuted((command: ICommandInfo) => {if (updateCommandList.includes(command.id)) {const lastFreeze = this._lastFreeze;- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const params = command.params as ISetFrozenMutationParams;@@ -1536,7 +1536,7 @@ export class FreezeController extends Disposable {}private _getSheetObject() {- return getSheetObject(this._currentUniverService, this._renderManagerService);+ return getSheetObject(this._univerInstanceService, this._renderManagerService);}private _refreshFreeze(diff --git a/packages/sheets-ui/src/controllers/header-menu.controller.ts b/packages/sheets-ui/src/controllers/header-menu.controller.tsindex 8a937f01d8..73c709fc78 100644--- a/packages/sheets-ui/src/controllers/header-menu.controller.ts+++ b/packages/sheets-ui/src/controllers/header-menu.controller.ts@@ -63,7 +63,7 @@ export class HeaderMenuController extends Disposable {constructor(@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@IContextMenuService private readonly _contextMenuService: IContextMenuService,@ICommandService private readonly _commandService: ICommandService,@@ -280,11 +280,11 @@ export class HeaderMenuController extends Disposable {}private _getSheetObject() {- return getSheetObject(this._currentUniverService, this._renderManagerService);+ return getSheetObject(this._univerInstanceService, this._renderManagerService);}private _getSelectionOnColumn(column: number): ISetSelectionsOperationParams {- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return {diff --git a/packages/sheets-ui/src/controllers/header-move.controller.ts b/packages/sheets-ui/src/controllers/header-move.controller.tsindex 92a1a1c68f..3af4fcce26 100644--- a/packages/sheets-ui/src/controllers/header-move.controller.ts+++ b/packages/sheets-ui/src/controllers/header-move.controller.ts@@ -123,7 +123,7 @@ export class HeaderMoveController extends Disposable {constructor(@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@ICommandService private readonly _commandService: ICommandService,@ISelectionRenderService private readonly _selectionRenderService: ISelectionRenderService,@@ -538,6 +538,6 @@ export class HeaderMoveController extends Disposable {}private _getSheetObject() {- return getSheetObject(this._currentUniverService, this._renderManagerService);+ return getSheetObject(this._univerInstanceService, this._renderManagerService);}}diff --git a/packages/sheets-ui/src/controllers/header-resize.controller.ts b/packages/sheets-ui/src/controllers/header-resize.controller.tsindex c05bf5b1ba..f6e2fe9009 100644--- a/packages/sheets-ui/src/controllers/header-resize.controller.ts+++ b/packages/sheets-ui/src/controllers/header-resize.controller.ts@@ -84,7 +84,7 @@ export class HeaderResizeController extends Disposable {constructor(@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@ICommandService private readonly _commandService: ICommandService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@IEditorBridgeService private readonly _editorBridgeService: IEditorBridgeService@@ -480,7 +480,7 @@ export class HeaderResizeController extends Disposable {});this._upObserver = scene.onPointerUpObserver.add((upEvt: IPointerEvent | IMouseEvent) => {- const sheetObject = getSheetObject(this._currentUniverService, this._renderManagerService);+ const sheetObject = getSheetObject(this._univerInstanceService, this._renderManagerService);if (sheetObject == null) {return;@@ -562,6 +562,6 @@ export class HeaderResizeController extends Disposable {}private _getSheetObject() {- return getSheetObject(this._currentUniverService, this._renderManagerService);+ return getSheetObject(this._univerInstanceService, this._renderManagerService);}}diff --git a/packages/sheets-ui/src/controllers/hover.controller.ts b/packages/sheets-ui/src/controllers/hover.controller.tsindex c629290458..900b5795d3 100644--- a/packages/sheets-ui/src/controllers/hover.controller.ts+++ b/packages/sheets-ui/src/controllers/hover.controller.ts@@ -37,7 +37,7 @@ export class HoverController extends Disposable {private _initPointerEvent() {const currentRender = this._renderManagerService.getRenderById(- this._univerInstanceService.getCurrentUniverSheetInstance().getUnitId()+ this._univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId());if (!currentRender) {return;diff --git a/packages/sheets-ui/src/controllers/menu/__tests__/menu.spec.ts b/packages/sheets-ui/src/controllers/menu/__tests__/menu.spec.tsindex ebaeaaebe6..dc53df8d75 100644--- a/packages/sheets-ui/src/controllers/menu/__tests__/menu.spec.ts+++ b/packages/sheets-ui/src/controllers/menu/__tests__/menu.spec.ts@@ -69,7 +69,7 @@ describe('Test menu items', () => {const sheetPermissionService = get(SheetPermissionService);const univerPermissionService = get(UniverPermissionService);const univerInstanceService = get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const menuItem = get(Injector).invoke(BoldMenuItemFactory);disposableCollection.add(toDisposable(menuItem.activated$!.subscribe((v: boolean) => (activated = v))));diff --git a/packages/sheets-ui/src/controllers/menu/__tests__/row-col.menu.spec.ts b/packages/sheets-ui/src/controllers/menu/__tests__/row-col.menu.spec.tsindex de9a9b4833..11525736fa 100644--- a/packages/sheets-ui/src/controllers/menu/__tests__/row-col.menu.spec.ts+++ b/packages/sheets-ui/src/controllers/menu/__tests__/row-col.menu.spec.ts@@ -87,21 +87,21 @@ describe('Test row col menu items', () => {function getRowCount(): number {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getRowCount();}function getColCount(): number {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getColumnCount();}async function selectRow(rowStart: number, rowEnd: number): Promise<boolean> {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const endColumn = getColCount() - 1;return commandService.executeCommand<ISetSelectionsOperationParams, boolean>(SetSelectionsOperation.id, {@@ -129,7 +129,7 @@ describe('Test row col menu items', () => {async function selectColumn(columnStart: number, columnEnd: number): Promise<boolean> {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const endRow = getRowCount() - 1;return commandService.executeCommand<ISetSelectionsOperationParams, boolean>(SetSelectionsOperation.id, {diff --git a/packages/sheets-ui/src/controllers/menu/menu.ts b/packages/sheets-ui/src/controllers/menu/menu.tsindex d0f4bcfcea..d04778f62f 100644--- a/packages/sheets-ui/src/controllers/menu/menu.ts+++ b/packages/sheets-ui/src/controllers/menu/menu.ts@@ -161,7 +161,7 @@ export function BoldMenuItemFactory(accessor: IAccessor): IMenuButtonItem {const id = c.id;if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id || id === SetWorksheetActiveOperation.id) {const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let isBold = FontWeight.NORMAL;if (primary != null) {@@ -190,7 +190,7 @@ export function BoldMenuItemFactory(accessor: IAccessor): IMenuButtonItem {});const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let isBold = FontWeight.NORMAL;if (primary != null) {@@ -225,7 +225,7 @@ export function ItalicMenuItemFactory(accessor: IAccessor): IMenuButtonItem {const id = c.id;if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id || id === SetWorksheetActiveOperation.id) {const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let isItalic = FontItalic.NORMAL;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -253,7 +253,7 @@ export function ItalicMenuItemFactory(accessor: IAccessor): IMenuButtonItem {});const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let isItalic = FontItalic.NORMAL;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -287,7 +287,7 @@ export function UnderlineMenuItemFactory(accessor: IAccessor): IMenuButtonItem {const id = c.id;if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id || id === SetWorksheetActiveOperation.id) {const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let isUnderline;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -315,7 +315,7 @@ export function UnderlineMenuItemFactory(accessor: IAccessor): IMenuButtonItem {});const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let isUnderline;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -349,7 +349,7 @@ export function StrikeThroughMenuItemFactory(accessor: IAccessor): IMenuButtonItconst id = c.id;if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id || id === SetWorksheetActiveOperation.id) {const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let st;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -377,7 +377,7 @@ export function StrikeThroughMenuItemFactory(accessor: IAccessor): IMenuButtonIt});const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let st;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -420,7 +420,7 @@ export function FontFamilySelectorMenuItemFactory(accessor: IAccessor): IMenuSel}const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let ff;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -431,7 +431,7 @@ export function FontFamilySelectorMenuItemFactory(accessor: IAccessor): IMenuSel});const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let ff;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -474,7 +474,7 @@ export function FontSizeSelectorMenuItemFactory(accessor: IAccessor): IMenuSelecconst id = c.id;if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id || id === SetWorksheetActiveOperation.id) {const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let fs;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -500,7 +500,7 @@ export function FontSizeSelectorMenuItemFactory(accessor: IAccessor): IMenuSelec});const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let fs;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -647,7 +647,7 @@ export function HorizontalAlignMenuItemFactory(accessor: IAccessor): IMenuSelect}const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let ha;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -658,7 +658,7 @@ export function HorizontalAlignMenuItemFactory(accessor: IAccessor): IMenuSelect});const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let ha;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -712,7 +712,7 @@ export function VerticalAlignMenuItemFactory(accessor: IAccessor): IMenuSelector}const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let va;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -723,7 +723,7 @@ export function VerticalAlignMenuItemFactory(accessor: IAccessor): IMenuSelector});const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let va;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -777,7 +777,7 @@ export function WrapTextMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<}const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let ws;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -788,7 +788,7 @@ export function WrapTextMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<});const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let ws;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -857,7 +857,7 @@ export function TextRotateMenuItemFactory(accessor: IAccessor): IMenuSelectorIte}const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let tr;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -872,7 +872,7 @@ export function TextRotateMenuItemFactory(accessor: IAccessor): IMenuSelectorIte});const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let tr;if (primary != null) {const range = worksheet.getRange(primary.startRow, primary.startColumn);@@ -1105,7 +1105,7 @@ export function ShowRowMenuItemFactory(accessor: IAccessor): IMenuButtonItem {const selectionManagerService = accessor.get(SelectionManagerService);function hasHiddenRowsInSelections(): boolean {- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();const rowRanges = selectionManagerService.getSelections()?.map((s) => s.range)@@ -1153,7 +1153,7 @@ export function ShowColMenuItemFactory(accessor: IAccessor): IMenuButtonItem {const selectionManagerService = accessor.get(SelectionManagerService);function hasHiddenColsInSelections(): boolean {- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();const colRanges = selectionManagerService.getSelections()?.map((s) => s.range)@@ -1222,7 +1222,7 @@ export function SetRowHeightMenuItemFactory(accessor: IAccessor): IMenuButtonItevalue$: new Observable((subscriber) => {function update() {const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let rowHeight;if (primary != null) {rowHeight = worksheet.getRowHeight(primary.startRow);@@ -1267,7 +1267,7 @@ export function SetColWidthMenuItemFactory(accessor: IAccessor): IMenuButtonItemvalue$: new Observable((subscriber) => {function update() {const primary = selectionManagerService.getLast()?.primary;- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();let colWidth: number = 0;if (primary != null) {diff --git a/packages/sheets-ui/src/controllers/menu/sheet.menu.ts b/packages/sheets-ui/src/controllers/menu/sheet.menu.tsindex 7bccb73465..4b32beea9f 100644--- a/packages/sheets-ui/src/controllers/menu/sheet.menu.ts+++ b/packages/sheets-ui/src/controllers/menu/sheet.menu.ts@@ -51,7 +51,7 @@ export function DeleteSheetMenuItemFactory(accessor: IAccessor): IMenuButtonItemid === InsertSheetMutation.id ||id === SetWorksheetHideMutation.id) {- const worksheets = univerInstanceService.getCurrentUniverSheetInstance().getWorksheets();+ const worksheets = univerInstanceService.getCurrentUniverSheetInstance()!.getWorksheets();// loop through all worksheets Map to see if there is more than one visible sheetconst visibleSheets = Array.from(worksheets.values()).filter((sheet) => sheet.getConfig().hidden === BooleanNumber.FALSE@@ -117,7 +117,7 @@ export function HideSheetMenuItemFactory(accessor: IAccessor): IMenuButtonItem {id === InsertSheetMutation.id ||id === SetWorksheetHideMutation.id) {- const worksheets = univerInstanceService.getCurrentUniverSheetInstance().getWorksheets();+ const worksheets = univerInstanceService.getCurrentUniverSheetInstance()!.getWorksheets();// loop through all worksheets Map to see if there is more than one visible sheetconst visibleSheets = Array.from(worksheets.values()).filter((sheet) => sheet.getConfig().hidden === BooleanNumber.FALSE@@ -135,7 +135,7 @@ export function HideSheetMenuItemFactory(accessor: IAccessor): IMenuButtonItem {export function UnHideSheetMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<any> {const univerInstanceService = accessor.get(IUniverInstanceService);const commandService = accessor.get(ICommandService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const hiddenList = workbook.getHiddenWorksheets().map((s) => ({label: workbook.getSheetBySheetId(s)?.getName() || '',value: s,@@ -190,7 +190,7 @@ export function ShowMenuItemFactory(accessor: IAccessor): IMenuButtonItem {id === InsertSheetMutation.id ||id === SetWorksheetHideMutation.id) {- const worksheets = univerInstanceService.getCurrentUniverSheetInstance().getWorksheets();+ const worksheets = univerInstanceService.getCurrentUniverSheetInstance()!.getWorksheets();// loop through all worksheets Map to see if there is more than one sheetconst visibleSheets = Array.from(worksheets.values());diff --git a/packages/sheets-ui/src/controllers/scroll.controller.ts b/packages/sheets-ui/src/controllers/scroll.controller.tsindex 8daae2249e..a79fb1d32e 100644--- a/packages/sheets-ui/src/controllers/scroll.controller.ts+++ b/packages/sheets-ui/src/controllers/scroll.controller.ts@@ -46,7 +46,7 @@ const SHEET_NAVIGATION_COMMANDS = [MoveSelectionCommand.id, MoveSelectionEnterAnexport class ScrollController extends Disposable {constructor(@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@ICommandService private readonly _commandService: ICommandService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@Inject(SelectionManagerService) private readonly _selectionManagerService: SelectionManagerService,@@ -305,7 +305,7 @@ export class ScrollController extends Disposable {}private _getSheetObject() {- return getSheetObject(this._currentUniverService, this._renderManagerService);+ return getSheetObject(this._univerInstanceService, this._renderManagerService);}private _scrollToSelectionByDirection(range: IRange) {@@ -404,7 +404,7 @@ export class ScrollController extends Disposable {return false;}- const worksheet = this._currentUniverService.getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = this._univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();const {startColumn: freezeStartColumn,startRow: freezeStartRow,diff --git a/packages/sheets-ui/src/controllers/selection.controller.ts b/packages/sheets-ui/src/controllers/selection.controller.tsindex 071fa7d943..c8ccbc257d 100644--- a/packages/sheets-ui/src/controllers/selection.controller.ts+++ b/packages/sheets-ui/src/controllers/selection.controller.ts@@ -55,7 +55,7 @@ import { getSheetObject } from './utils/component-tools';export class SelectionController extends Disposable {constructor(@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@ICommandService private readonly _commandService: ICommandService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@ISelectionRenderService@@ -70,7 +70,7 @@ export class SelectionController extends Disposable {}private _initialize() {- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const sheetObject = this._getSheetObject();if (sheetObject == null) {@@ -108,7 +108,7 @@ export class SelectionController extends Disposable {const { unitId } = item;let { formulaOrRefString } = item;- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;if (unitId !== workbook.getUnitId()) {return;@@ -368,7 +368,7 @@ export class SelectionController extends Disposable {}const lastSelection = params[params.length - 1];- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();this._definedNamesService.setCurrentRange({@@ -393,7 +393,7 @@ export class SelectionController extends Disposable {}private _move(selectionDataWithStyleList: ISelectionWithCoordAndStyle[], type: SelectionMoveType) {- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const sheetId = workbook.getActiveSheet().getSheetId();const current = this._selectionManagerService.getCurrent();@@ -414,7 +414,7 @@ export class SelectionController extends Disposable {}private _getSheetObject() {- return getSheetObject(this._currentUniverService, this._renderManagerService);+ return getSheetObject(this._univerInstanceService, this._renderManagerService);}private _initCommandListener() {@@ -423,7 +423,7 @@ export class SelectionController extends Disposable {this.disposeWithMe(this._commandService.onCommandExecuted((command: ICommandInfo) => {if (updateCommandList.includes(command.id)) {- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const params = command.params as ISetZoomRatioOperationParams;diff --git a/packages/sheets-ui/src/controllers/sheet-render.controller.ts b/packages/sheets-ui/src/controllers/sheet-render.controller.tsindex 2ee31a85ef..aae5b3dccb 100644--- a/packages/sheets-ui/src/controllers/sheet-render.controller.ts+++ b/packages/sheets-ui/src/controllers/sheet-render.controller.ts@@ -47,7 +47,7 @@ export class SheetRenderController extends Disposable {constructor(@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,@IContextService private readonly _contextService: IContextService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@ICommandService private readonly _commandService: ICommandService) {@@ -71,7 +71,7 @@ export class SheetRenderController extends Disposable {const { skeleton: spreadsheetSkeleton, unitId, sheetId } = param;- const workbook = this._currentUniverService.getUniverSheetInstance(unitId);+ const workbook = this._univerInstanceService.getUniverSheetInstance(unitId);const worksheet = workbook?.getSheetBySheetId(sheetId);@@ -108,7 +108,7 @@ export class SheetRenderController extends Disposable {private _initCommandListener(): void {this.disposeWithMe(this._commandService.onCommandExecuted((command: ICommandInfo) => {- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();if (COMMAND_LISTENER_SKELETON_CHANGE.includes(command.id)) {diff --git a/packages/sheets-ui/src/controllers/status-bar.controller.ts b/packages/sheets-ui/src/controllers/status-bar.controller.tsindex 2ad3f981b0..9520e0e99c 100644--- a/packages/sheets-ui/src/controllers/status-bar.controller.ts+++ b/packages/sheets-ui/src/controllers/status-bar.controller.ts@@ -114,13 +114,13 @@ export class StatusBarController extends Disposable {}private _calculateSelection(selections: IRange[], primary: Nullable<ISelectionCell>) {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const sheetId = workbook.getActiveSheet().getSheetId();const sheetData: ISheetData = {};const arrayFormulaMatrixCell = this._formulaDataModel.getArrayFormulaCellData();this._univerInstanceService- .getCurrentUniverSheetInstance()+ .getCurrentUniverSheetInstance()!.getSheets().forEach((sheet) => {const sheetConfig = sheet.getConfig();diff --git a/packages/sheets-ui/src/controllers/utils/component-tools.ts b/packages/sheets-ui/src/controllers/utils/component-tools.tsindex 99a88a544c..44c47c23ff 100644--- a/packages/sheets-ui/src/controllers/utils/component-tools.ts+++ b/packages/sheets-ui/src/controllers/utils/component-tools.ts@@ -43,7 +43,7 @@ export function getSheetObject(univerInstanceService: IUniverInstanceService,renderManagerService: IRenderManagerService): Nullable<ISheetObjectParam> {- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();diff --git a/packages/sheets-ui/src/controllers/zoom.controller.ts b/packages/sheets-ui/src/controllers/zoom.controller.tsindex 6adc047f99..2d6cf4240b 100644--- a/packages/sheets-ui/src/controllers/zoom.controller.ts+++ b/packages/sheets-ui/src/controllers/zoom.controller.ts@@ -45,7 +45,7 @@ interface ISetWorksheetMutationParams {export class ZoomController extends Disposable {constructor(@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@ICommandService private readonly _commandService: ICommandService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@ISelectionRenderService@@ -90,7 +90,7 @@ export class ZoomController extends Disposable {ratioDelta /= 2;}- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const sheet = workbook.getActiveSheet();const currentRatio = sheet.getZoomRatio();let nextRatio = +Number.parseFloat(`${currentRatio + ratioDelta}`).toFixed(1);@@ -116,7 +116,7 @@ export class ZoomController extends Disposable {return;}- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();@@ -134,7 +134,7 @@ export class ZoomController extends Disposable {this.disposeWithMe(this._commandService.onCommandExecuted((command: ICommandInfo) => {if (updateCommandList.includes(command.id)) {- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const params = command.params;@@ -158,6 +158,6 @@ export class ZoomController extends Disposable {}private _getSheetObject() {- return getSheetObject(this._currentUniverService, this._renderManagerService);+ return getSheetObject(this._univerInstanceService, this._renderManagerService);}}diff --git a/packages/sheets-ui/src/services/canvas-pop-manager.service.ts b/packages/sheets-ui/src/services/canvas-pop-manager.service.tsindex dbf00d6c49..345abfe786 100644--- a/packages/sheets-ui/src/services/canvas-pop-manager.service.ts+++ b/packages/sheets-ui/src/services/canvas-pop-manager.service.ts@@ -182,7 +182,7 @@ export class SheetCanvasPopManagerService extends Disposable {* @returns disposable*/attachPopupToObject(targetObject: BaseObject, popup: ICanvasPopup): IDisposable {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const unitId = workbook.getUnitId();const subUnitId = worksheet.getSheetId();@@ -226,7 +226,7 @@ export class SheetCanvasPopManagerService extends Disposable {* @returns disposable*/attachPopupToCell(row: number, col: number, popup: ICanvasPopup, viewport?: Viewport): Nullable<IDisposable> {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const unitId = workbook.getUnitId();const subUnitId = worksheet.getSheetId();diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.tsindex adeb25a432..e39a1d470c 100644--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts@@ -100,7 +100,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardconstructor(@ILogService private readonly _logService: ILogService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@Inject(SelectionManagerService) private readonly _selectionManagerService: SelectionManagerService,@IClipboardInterfaceService private readonly _clipboardInterfaceService: IClipboardInterfaceService,@IUndoRedoService private readonly _undoRedoService: IUndoRedoService,@@ -131,7 +131,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardreturn false; // maybe we should notify user that there is no selection}- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const hooks = this._clipboardHooks;@@ -247,7 +247,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard}private _generateCopyContent(unitId: string, subUnitId: string, range: IRange, hooks: ISheetClipboardHook[]) {- const workbook = this._currentUniverService.getUniverSheetInstance(unitId);+ const workbook = this._univerInstanceService.getUniverSheetInstance(unitId);const worksheet = workbook?.getSheetBySheetId(subUnitId);if (!workbook || !worksheet) {@@ -340,7 +340,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardif (result) {// add to undo redo servicesthis._undoRedoService.pushUndoRedo({- unitID: this._currentUniverService.getCurrentUniverSheetInstance().getUnitId(),+ unitID: this._univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId(),undoMutations: undoMutationsInfo,redoMutations: redoMutationsInfo,});@@ -427,7 +427,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardreturn false;}- const styles = this._currentUniverService.getUniverSheetInstance(copyUnitId)?.getStyles();+ const styles = this._univerInstanceService.getUniverSheetInstance(copyUnitId)?.getStyles();cellMatrix.forValue((row, col, value) => {if (typeof value.s === 'string') {const newValue = Tools.deepClone(value);@@ -448,7 +448,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardreturn false;}- const worksheet = this._currentUniverService+ const worksheet = this._univerInstanceService.getUniverSheetInstance(copyUnitId)?.getSheetBySheetId(copySubUnitId);if (!worksheet) {@@ -611,7 +611,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardrange: IRange,cellMatrix: ObjectMatrix<ICellDataWithSpanInfo>) {- const worksheet = this._currentUniverService.getUniverSheetInstance(unitId)?.getSheetBySheetId(subUnitId);+ const worksheet = this._univerInstanceService.getUniverSheetInstance(unitId)?.getSheetBySheetId(subUnitId);if (!worksheet) {return null;}@@ -660,7 +660,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard}private _getPastingTarget() {- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const selection = this._selectionManagerService.getLast();return {@@ -722,7 +722,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardconst destinationRows = endRow - startRow + 1;const destinationColumns = endColumn - startColumn + 1;- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();// const mergedRange = worksheet.getMergedCell(startRow, startColumn);const mergeData = worksheet.getMergeData();@@ -824,7 +824,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard* @param range*/private _topLeftCellsMatch(rowCount: number, colCount: number, range: IRange): boolean {- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const { startRow, startColumn, endRow, endColumn } = range;diff --git a/packages/sheets-ui/src/services/editor-bridge.service.ts b/packages/sheets-ui/src/services/editor-bridge.service.tsindex 2e78c3f964..8b2854b402 100644--- a/packages/sheets-ui/src/services/editor-bridge.service.ts+++ b/packages/sheets-ui/src/services/editor-bridge.service.ts@@ -127,7 +127,7 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,@ISelectionRenderService private readonly _selectionRenderService: ISelectionRenderService,@Inject(ThemeService) private readonly _themeService: ThemeService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IEditorService private readonly _editorService: IEditorService) {super();@@ -227,7 +227,7 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServendX = skeleton.convertTransformToOffsetX(endX, scaleX, scrollXY);endY = skeleton.convertTransformToOffsetY(endY, scaleY, scrollXY);- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const location = {workbook,diff --git a/packages/sheets-ui/src/services/format-painter/format-painter.service.ts b/packages/sheets-ui/src/services/format-painter/format-painter.service.tsindex e74d477a5a..f11510cff7 100644--- a/packages/sheets-ui/src/services/format-painter/format-painter.service.ts+++ b/packages/sheets-ui/src/services/format-painter/format-painter.service.ts@@ -97,10 +97,10 @@ export class FormatPainterService extends Disposable implements IFormatPainterSeconst range = selection?.range;if (!range) return;const { startRow, endRow, startColumn, endColumn } = range;- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook?.getActiveSheet();const cellData = worksheet.getCellMatrix();- const mergeData = this._univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getMergeData();+ const mergeData = this._univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getMergeData();const styles = workbook.getStyles();const stylesMatrix = new ObjectMatrix<IStyleData>();diff --git a/packages/sheets-ui/src/services/hover-manager.service.ts b/packages/sheets-ui/src/services/hover-manager.service.tsindex e6d7068dfd..2bce7657e4 100644--- a/packages/sheets-ui/src/services/hover-manager.service.ts+++ b/packages/sheets-ui/src/services/hover-manager.service.ts@@ -56,7 +56,7 @@ export class HoverManagerService {return;}const { offsetX, offsetY } = this._lastPosition;- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const skeletonParam = this._sheetSkeletonManagerService.getCurrent();const currentRender = this._renderManagerService.getRenderById(workbook.getUnitId());diff --git a/packages/sheets-ui/src/services/mark-selection/mark-selection.service.ts b/packages/sheets-ui/src/services/mark-selection/mark-selection.service.tsindex 7816841ede..a5ea9a4122 100644--- a/packages/sheets-ui/src/services/mark-selection/mark-selection.service.ts+++ b/packages/sheets-ui/src/services/mark-selection/mark-selection.service.ts@@ -60,7 +60,7 @@ export class MarkSelectionService extends Disposable implements IMarkSelectionSe}addShape(selection: ISelectionWithStyle, exits: string[] = [], zIndex: number = DEFAULT_Z_INDEX): string | null {- const workbook = this._currentService.getCurrentUniverSheetInstance();+ const workbook = this._currentService.getCurrentUniverSheetInstance()!;const subUnitId = workbook.getActiveSheet().getSheetId();const id = Tools.generateRandomId();this._shapeMap.set(id, {@@ -76,8 +76,8 @@ export class MarkSelectionService extends Disposable implements IMarkSelectionSe}refreshShapes() {- const currentUnitId = this._currentService.getCurrentUniverSheetInstance().getUnitId();- const currentSubUnitId = this._currentService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const currentUnitId = this._currentService.getCurrentUniverSheetInstance()!.getUnitId();+ const currentSubUnitId = this._currentService.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId();this._shapeMap.forEach((shape) => {const { unitId, subUnitId, selection, control: oldControl, zIndex } = shape;diff --git a/packages/sheets-ui/src/services/sheet-skeleton-manager.service.ts b/packages/sheets-ui/src/services/sheet-skeleton-manager.service.tsindex fd74ef8321..32277603d8 100644--- a/packages/sheets-ui/src/services/sheet-skeleton-manager.service.ts+++ b/packages/sheets-ui/src/services/sheet-skeleton-manager.service.ts@@ -61,7 +61,7 @@ export class SheetSkeletonManagerService implements IDisposable {readonly currentSkeletonBefore$ = this._currentSkeletonBefore$.asObservable();constructor(- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@Inject(Injector) private readonly _injector: Injector,@Inject(LocaleService) private readonly _localeService: LocaleService) { }@@ -98,7 +98,7 @@ export class SheetSkeletonManagerService implements IDisposable {} else {const { unitId, sheetId } = searchParam;- const workbook = this._currentUniverService.getUniverSheetInstance(searchParam.unitId);+ const workbook = this._univerInstanceService.getUniverSheetInstance(searchParam.unitId);const worksheet = workbook?.getSheetBySheetId(searchParam.sheetId);@@ -159,7 +159,7 @@ export class SheetSkeletonManagerService implements IDisposable {return skeleton.skeleton;}- const workbook = this._currentUniverService.getUniverSheetInstance(searchParam.unitId);+ const workbook = this._univerInstanceService.getUniverSheetInstance(searchParam.unitId);const worksheet = workbook?.getSheetBySheetId(searchParam.sheetId);if (!worksheet || !workbook) {return;diff --git a/packages/sheets-ui/src/services/shortcut-experience.service.ts b/packages/sheets-ui/src/services/shortcut-experience.service.tsindex cdcbfb99f3..3cb6a8340c 100644--- a/packages/sheets-ui/src/services/shortcut-experience.service.ts+++ b/packages/sheets-ui/src/services/shortcut-experience.service.ts@@ -41,7 +41,7 @@ export class ShortcutExperienceService implements IDisposable {private _shortcutParam: IShortcutExperienceParam[] = [];constructor(- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@Inject(LocaleService) private readonly _localeService: LocaleService) {}diff --git a/packages/sheets-ui/src/sheets-ui-plugin.ts b/packages/sheets-ui/src/sheets-ui-plugin.tsindex fd7cd215c4..1df8b4437a 100644--- a/packages/sheets-ui/src/sheets-ui-plugin.ts+++ b/packages/sheets-ui/src/sheets-ui-plugin.ts@@ -76,7 +76,7 @@ export class UniverSheetsUIPlugin extends Plugin {config: undefined,@Inject(Injector) override readonly _injector: Injector,@Inject(LocaleService) private readonly _localeService: LocaleService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService) {super(SHEET_UI_PLUGIN_NAME);@@ -152,7 +152,7 @@ export class UniverSheetsUIPlugin extends Plugin {}private _markSheetAsFocused() {- const univerInstanceService = this._currentUniverService;+ const univerInstanceService = this._univerInstanceService;univerInstanceService.currentSheet$.pipe(filter((v) => !!v)).subscribe((workbook) => {univerInstanceService.focusUniverInstance(workbook!.getUnitId());});diff --git a/packages/sheets-ui/src/views/count-bar/ZoomSlider.tsx b/packages/sheets-ui/src/views/count-bar/ZoomSlider.tsxindex fab79a8bca..d22a9e5756 100644--- a/packages/sheets-ui/src/views/count-bar/ZoomSlider.tsx+++ b/packages/sheets-ui/src/views/count-bar/ZoomSlider.tsx@@ -34,7 +34,7 @@ export function ZoomSlider() {const univerInstanceService = useDependency(IUniverInstanceService);const getCurrentZoom = useCallback(() => {- const currentZoom = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getZoomRatio() * 100 || 100;+ const currentZoom = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getZoomRatio() * 100 || 100;return Math.round(currentZoom);}, [univerInstanceService]);@@ -52,7 +52,7 @@ export function ZoomSlider() {function handleChange(value: number) {setZoom(value);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook?.getActiveSheet();if (worksheet == null) {return;diff --git a/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx b/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsxindex d6a666a2ed..b7b2971f99 100644--- a/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx+++ b/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx@@ -32,7 +32,7 @@ import { SCOPE_WORKBOOK_VALUE } from './component-name';export const DefinedNameContainer = () => {const commandService = useDependency(ICommandService);const univerInstanceService = useDependency(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const localeService = useDependency(LocaleService);const definedNamesService = useDependency(IDefinedNamesService);const selectionManagerService = useDependency(SelectionManagerService);diff --git a/packages/sheets-ui/src/views/defined-name/DefinedNameInput.tsx b/packages/sheets-ui/src/views/defined-name/DefinedNameInput.tsxindex 67a7289b8a..ccf502e67a 100644--- a/packages/sheets-ui/src/views/defined-name/DefinedNameInput.tsx+++ b/packages/sheets-ui/src/views/defined-name/DefinedNameInput.tsx@@ -56,7 +56,7 @@ export const DefinedNameInput = (props: IDefinedNameInputProps) => {} = props;const univerInstanceService = useDependency(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const localeService = useDependency(LocaleService);const definedNamesService = useDependency(IDefinedNamesService);const functionService = useDependency(IFunctionService);diff --git a/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsx b/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsxindex bb6dfcce03..c115e15850 100644--- a/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsx+++ b/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsx@@ -32,7 +32,7 @@ export function DefinedNameOverlay(props: IDefinedNameOverlayProps) {const localeService = useDependency(LocaleService);const definedNamesService = useDependency(IDefinedNamesService);const univerInstanceService = useDependency(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const getDefinedNameMap = () => {diff --git a/packages/sheets-ui/src/views/operate-container/AutoFillPopupMenu.tsx b/packages/sheets-ui/src/views/operate-container/AutoFillPopupMenu.tsxindex 87a09fd8ac..0e720321fb 100644--- a/packages/sheets-ui/src/views/operate-container/AutoFillPopupMenu.tsx+++ b/packages/sheets-ui/src/views/operate-container/AutoFillPopupMenu.tsx@@ -56,7 +56,7 @@ const useUpdate = () => {export const AutoFillPopupMenu: React.FC<{}> = () => {const commandService = useDependency(ICommandService);const sheetSkeletonManagerService = useDependency(SheetSkeletonManagerService);- const currentUniverService = useDependency(IUniverInstanceService);+ const univerInstanceService = useDependency(IUniverInstanceService);const renderManagerService = useDependency(IRenderManagerService);const selectionRenderService = useDependency(ISelectionRenderService);const autoFillService = useDependency(IAutoFillService);@@ -77,7 +77,7 @@ export const AutoFillPopupMenu: React.FC<{}> = () => {};const forceUpdate = useUpdate();- const sheetObject = getSheetObject(currentUniverService, renderManagerService);+ const sheetObject = getSheetObject(univerInstanceService, renderManagerService);if (sheetObject == null) {return null;diff --git a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-menu/SheetBarMenu.tsx b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-menu/SheetBarMenu.tsxindex 037e102e23..526c7eaec4 100644--- a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-menu/SheetBarMenu.tsx+++ b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-menu/SheetBarMenu.tsx@@ -56,7 +56,7 @@ export function SheetBarMenu(props: ISheetBarMenuProps) {const univerInstanceService = useDependency(IUniverInstanceService);const commandService = useDependency(ICommandService);const sheetBarService = useDependency(ISheetBarService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const handleClick = (item: ISheetBarMenuItem) => {const { sheetId } = item;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.tsxindex f92220abae..e4c74a24db 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@@ -62,7 +62,7 @@ export function SheetBarTabs() {const editorBridgeService = useDependency(IEditorBridgeService);const injector = useInjector();- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;useEffect(() => {statusInit();@@ -161,7 +161,7 @@ export function SheetBarTabs() {};const nameRepeatCheck = (name: string) => {- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();const currenSheetName = worksheet.getName();// TODO@Dushusir: no need trigger savediff --git a/packages/sheets-ui/src/views/sheet-canvas-view.ts b/packages/sheets-ui/src/views/sheet-canvas-view.tsindex 9f018da7c6..80f5b90ee8 100644--- a/packages/sheets-ui/src/views/sheet-canvas-view.ts+++ b/packages/sheets-ui/src/views/sheet-canvas-view.ts@@ -53,7 +53,7 @@ export class SheetCanvasView extends RxDisposable {readonly fps$ = this._fps$.asObservable();constructor(- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@ICommandService private readonly _commandService: ICommandService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@ISelectionRenderService@@ -73,11 +73,11 @@ export class SheetCanvasView extends RxDisposable {}private _init() {- this._currentUniverService.currentSheet$.subscribe((workbook) => {+ this._univerInstanceService.currentSheet$.subscribe((workbook) => {this._create(workbook);});- this._currentUniverService.getAllUniverSheetsInstance().forEach((workbook) => {+ this._univerInstanceService.getAllUniverSheetsInstance().forEach((workbook) => {this._create(workbook);});}diff --git a/packages/sheets-zen-editor/src/commands/commands/zen-editor.command.ts b/packages/sheets-zen-editor/src/commands/commands/zen-editor.command.tsindex ae408561ad..33267b6db1 100644--- a/packages/sheets-zen-editor/src/commands/commands/zen-editor.command.ts+++ b/packages/sheets-zen-editor/src/commands/commands/zen-editor.command.ts@@ -22,14 +22,10 @@ import { IZenZoneService, KeyCode } from '@univerjs/ui';export const CancelZenEditCommand: ICommand = {id: 'zen-editor.command.cancel-zen-edit',-type: CommandType.COMMAND,-handler: async (accessor) => {const zenZoneEditorService = accessor.get(IZenZoneService);-const editorBridgeService = accessor.get(IEditorBridgeService);-const univerInstanceManager = accessor.get(IUniverInstanceService);const visibleState = editorBridgeService.isVisible();@@ -44,12 +40,14 @@ export const CancelZenEditCommand: ICommand = {zenZoneEditorService.close();const currentSheetInstance = univerInstanceManager.getCurrentUniverSheetInstance();+ if (currentSheetInstance) {+ univerInstanceManager.focusUniverInstance(currentSheetInstance.getUnitId());+ editorBridgeService.refreshEditCellState();- univerInstanceManager.focusUniverInstance(currentSheetInstance.getUnitId());-- editorBridgeService.refreshEditCellState();+ return true;+ }- return true;+ return false;},};@@ -60,9 +58,7 @@ export const ConfirmZenEditCommand: ICommand = {handler: async (accessor) => {const zenZoneEditorService = accessor.get(IZenZoneService);-const editorBridgeService = accessor.get(IEditorBridgeService);-const univerInstanceManager = accessor.get(IUniverInstanceService);const visibleState = editorBridgeService.isVisible();@@ -76,11 +72,12 @@ export const ConfirmZenEditCommand: ICommand = {zenZoneEditorService.close();const currentSheetInstance = univerInstanceManager.getCurrentUniverSheetInstance();+ if (currentSheetInstance) {+ univerInstanceManager.focusUniverInstance(currentSheetInstance.getUnitId());+ editorBridgeService.refreshEditCellState();+ return true;+ }- univerInstanceManager.focusUniverInstance(currentSheetInstance.getUnitId());-- editorBridgeService.refreshEditCellState();-- return true;+ return false;},};diff --git a/packages/sheets-zen-editor/src/controllers/zen-editor.controller.ts b/packages/sheets-zen-editor/src/controllers/zen-editor.controller.tsindex 5ba6f547a0..177d9b4477 100644--- a/packages/sheets-zen-editor/src/controllers/zen-editor.controller.ts+++ b/packages/sheets-zen-editor/src/controllers/zen-editor.controller.ts@@ -50,7 +50,7 @@ export const DOCS_ZEN_EDITOR_UNIT_ID_KEY = '__defaultDocumentZenEditorSpecialUni@OnLifecycle(LifecycleStages.Steady, ZenEditorController)export class ZenEditorController extends RxDisposable {constructor(- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IZenEditorManagerService private readonly _zenEditorManagerService: IZenEditorManagerService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@ICommandService private readonly _commandService: ICommandService,@@ -103,7 +103,7 @@ export class ZenEditorController extends RxDisposable {},};- this._currentUniverService.createDoc(INITIAL_SNAPSHOT);+ this._univerInstanceService.createDoc(INITIAL_SNAPSHOT);}// Listen to changes in the size of the zen editor container to set the size of the editor.@@ -114,7 +114,7 @@ export class ZenEditorController extends RxDisposable {}const editorObject = getEditorObject(DOCS_ZEN_EDITOR_UNIT_ID_KEY, this._renderManagerService);- const zenEditorDataModel = this._currentUniverService.getUniverDocInstance(DOCS_ZEN_EDITOR_UNIT_ID_KEY);+ const zenEditorDataModel = this._univerInstanceService.getUniverDocInstance(DOCS_ZEN_EDITOR_UNIT_ID_KEY);if (editorObject == null || zenEditorDataModel == null) {return;@@ -155,9 +155,9 @@ export class ZenEditorController extends RxDisposable {// Need to clear undo/redo service when open zen mode.this._undoRedoService.clearUndoRedo(DOCS_ZEN_EDITOR_UNIT_ID_KEY);- this._currentUniverService.focusUniverInstance(DOCS_ZEN_EDITOR_UNIT_ID_KEY);+ this._univerInstanceService.focusUniverInstance(DOCS_ZEN_EDITOR_UNIT_ID_KEY);- this._currentUniverService.setCurrentUniverDocInstance(DOCS_ZEN_EDITOR_UNIT_ID_KEY);+ this._univerInstanceService.setCurrentUniverDocInstance(DOCS_ZEN_EDITOR_UNIT_ID_KEY);const visibleState = this._editorBridgeService.isVisible();if (visibleState.visible === false) {@@ -219,7 +219,7 @@ export class ZenEditorController extends RxDisposable {];const docsSkeletonObject = this._docSkeletonManagerService.getSkeletonByUnitId(unitId);- const docDataModel = this._currentUniverService.getUniverDocInstance(unitId);+ const docDataModel = this._univerInstanceService.getUniverDocInstance(unitId);const docViewModel = this._docViewModelManagerService.getViewModel(unitId);if (docDataModel == null || docViewModel == null || docsSkeletonObject == null) {@@ -336,7 +336,7 @@ export class ZenEditorController extends RxDisposable {if (unitId === DOCS_ZEN_EDITOR_UNIT_ID_KEY) {// sync cell content to formula editor bar when edit cell editor and vice verse.- const editorDocDataModel = this._currentUniverService.getUniverDocInstance(unitId);+ const editorDocDataModel = this._univerInstanceService.getUniverDocInstance(unitId);const docBody = editorDocDataModel?.getBody();const dataStream = docBody?.dataStream;const paragraphs = docBody?.paragraphs;@@ -359,6 +359,6 @@ export class ZenEditorController extends RxDisposable {}private _getDocObject() {- return getDocObject(this._currentUniverService, this._renderManagerService);+ return getDocObject(this._univerInstanceService, this._renderManagerService);}}diff --git a/packages/sheets/src/commands/commands/__tests__/copy-worksheet.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/copy-worksheet.command.spec.tsindex c04f796a52..431ccc6587 100644--- a/packages/sheets/src/commands/commands/__tests__/copy-worksheet.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/copy-worksheet.command.spec.ts@@ -57,7 +57,7 @@ describe('Test copy worksheet commands', () => {describe('copy sheet', () => {describe('copy the only sheet', async () => {it('correct situation', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');function getSheetCopyPart(sheet: Worksheet) {const config = sheet.getConfig();diff --git a/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.tsindex 3892937f28..66895ce2b5 100644--- a/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts@@ -142,14 +142,14 @@ describe('Test insert and remove rows cols commands', () => {function getRowCount(): number {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getRowCount();}function getColCount(): number {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getColumnCount();}@@ -160,21 +160,21 @@ describe('Test insert and remove rows cols commands', () => {function getCellInfo(row: number, col: number): Nullable<ICellData> {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getCellMatrix().getValue(row, col);}function getMergedInfo(row: number, col: number): Nullable<IRange> {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getMergedCell(row, col);}function getMergeData() {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getMergeData();}diff --git a/packages/sheets/src/commands/commands/__tests__/move-range-commands.spec.ts b/packages/sheets/src/commands/commands/__tests__/move-range-commands.spec.tsindex d540e1aa10..e11289fcd6 100644--- a/packages/sheets/src/commands/commands/__tests__/move-range-commands.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/move-range-commands.spec.ts@@ -138,14 +138,14 @@ describe('Test move range commands', () => {function getMergedInfo(row: number, col: number): Nullable<IRange> {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getMergedCell(row, col);}function getMergeData() {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getConfig().mergeData;}diff --git a/packages/sheets/src/commands/commands/__tests__/move-rows-cols.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/move-rows-cols.command.spec.tsindex 6bb48545c1..6a4f31b3e5 100644--- a/packages/sheets/src/commands/commands/__tests__/move-rows-cols.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/move-rows-cols.command.spec.ts@@ -119,42 +119,42 @@ describe('Test move rows cols', () => {function getRowCount(): number {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getRowCount();}function getColCount(): number {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getColumnCount();}function getCellInfo(row: number, col: number): Nullable<ICellData> {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getCellMatrix().getValue(row, col);}function getMergedInfo(row: number, col: number): Nullable<IRange> {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getMergedCell(row, col);}function getRowHeight(row: number): number {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getRowHeight(row);}function getColWidth(col: number): number {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getColumnWidth(col);}diff --git a/packages/sheets/src/commands/commands/__tests__/remove-rows-cols.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/remove-rows-cols.command.spec.tsindex a647c0685f..dca675e8a5 100644--- a/packages/sheets/src/commands/commands/__tests__/remove-rows-cols.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/remove-rows-cols.command.spec.ts@@ -92,14 +92,14 @@ describe('Test remove rows cols', () => {function getColCount(): number {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getColumnCount();}function getCellInfo(row: number, col: number): Nullable<ICellData> {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getCellMatrix().getValue(row, col);}diff --git a/packages/sheets/src/commands/commands/__tests__/remove-sheet.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/remove-sheet.command.spec.tsindex 8e61a0a8e9..f4644ba2f8 100644--- a/packages/sheets/src/commands/commands/__tests__/remove-sheet.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/remove-sheet.command.spec.ts@@ -61,7 +61,7 @@ describe('Test remove worksheet commands', () => {).toBeFalsy();});it('will be working if there are more than one sheet', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');const targetActiveSheet = workbook.getActiveSheet();diff --git a/packages/sheets/src/commands/commands/__tests__/set-col-width.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-col-width.command.spec.tsindex 0c40d36d0c..016d4fcc89 100644--- a/packages/sheets/src/commands/commands/__tests__/set-col-width.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/set-col-width.command.spec.ts@@ -31,7 +31,7 @@ describe('Test set col width commands', () => {let commandService: ICommandService;function getColumnWidth(col: number): number {- const worksheet = get(IUniverInstanceService).getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = get(IUniverInstanceService).getCurrentUniverSheetInstance()!.getActiveSheet();return worksheet.getColumnWidth(col);}@@ -45,7 +45,7 @@ describe('Test set col width commands', () => {commandService.registerCommand(SetColWidthCommand);commandService.registerCommand(SetWorksheetColWidthMutation);- const worksheet = get(IUniverInstanceService).getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = get(IUniverInstanceService).getCurrentUniverSheetInstance()!.getActiveSheet();const maxRow = worksheet.getMaxRows() - 1;const selectionManagerService = get(SelectionManagerService);selectionManagerService.setCurrentSelection({diff --git a/packages/sheets/src/commands/commands/__tests__/set-frozen.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-frozen.command.spec.tsindex dd849a4df1..f9ef359364 100644--- a/packages/sheets/src/commands/commands/__tests__/set-frozen.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/set-frozen.command.spec.ts@@ -108,7 +108,7 @@ describe('Test set frozen commands', () => {describe('set frozen', () => {describe('set frozen', async () => {it('correct situation: ', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');const targetActiveSheet = workbook.getActiveSheet();diff --git a/packages/sheets/src/commands/commands/__tests__/set-row-col-visible.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-row-col-visible.command.spec.tsindex 2f8cf38f22..3d02e172f5 100644--- a/packages/sheets/src/commands/commands/__tests__/set-row-col-visible.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/set-row-col-visible.command.spec.ts@@ -75,26 +75,26 @@ describe('Test row col hide/unhine commands', () => {function getRowCount(): number {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getRowCount();}function getColCount(): number {const currentService = get(IUniverInstanceService);- const workbook = currentService.getCurrentUniverSheetInstance();+ const workbook = currentService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getColumnCount();}function getRowVisible(row: number): boolean {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getRowVisible(row);}function getColVisible(col: number): boolean {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet.getColVisible(col);}diff --git a/packages/sheets/src/commands/commands/__tests__/set-row-height.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-row-height.command.spec.tsindex bd4d4577d5..4b51bbfe93 100644--- a/packages/sheets/src/commands/commands/__tests__/set-row-height.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/set-row-height.command.spec.ts@@ -47,12 +47,12 @@ describe('Test set row height commands', () => {let commandService: ICommandService;function getRowHeight(row: number): number {- const worksheet = get(IUniverInstanceService).getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = get(IUniverInstanceService).getCurrentUniverSheetInstance()!.getActiveSheet();return worksheet.getRowHeight(row);}function getRowIsAutoHeight(row: number): Nullable<BooleanNumber> {- const worksheet = get(IUniverInstanceService).getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = get(IUniverInstanceService).getCurrentUniverSheetInstance()!.getActiveSheet();const rowManager = worksheet.getRowManager();const rowInfo = rowManager.getRow(row);@@ -71,7 +71,7 @@ describe('Test set row height commands', () => {commandService.registerCommand(SetWorksheetRowHeightMutation);commandService.registerCommand(SetWorksheetRowIsAutoHeightMutation);- const worksheet = get(IUniverInstanceService).getCurrentUniverSheetInstance().getActiveSheet();+ const worksheet = get(IUniverInstanceService).getCurrentUniverSheetInstance()!.getActiveSheet();const maxColumn = worksheet.getMaxColumns() - 1;const selectionManager = get(SelectionManagerService);diff --git a/packages/sheets/src/commands/commands/__tests__/set-worksheet-hide.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-worksheet-hide.command.spec.tsindex bf8f91b533..dce1da88bf 100644--- a/packages/sheets/src/commands/commands/__tests__/set-worksheet-hide.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/set-worksheet-hide.command.spec.ts@@ -52,7 +52,7 @@ describe('Test set worksheet hide commands', () => {describe('Set worksheet hide', () => {it('will set current active worksheet hidden', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');const targetActiveSheet = workbook.getActiveSheet();diff --git a/packages/sheets/src/commands/commands/__tests__/set-worksheet-name.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-worksheet-name.command.spec.tsindex 1828d0e8d4..128bfafc36 100644--- a/packages/sheets/src/commands/commands/__tests__/set-worksheet-name.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/set-worksheet-name.command.spec.ts@@ -45,7 +45,7 @@ describe('Test set worksheet name commands', () => {describe('set worksheet name', () => {describe('set worksheet name', async () => {it('correct situation: ', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');expect(diff --git a/packages/sheets/src/commands/commands/__tests__/set-worksheet-order.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-worksheet-order.command.spec.tsindex 810205b9ed..2432a4f0a3 100644--- a/packages/sheets/src/commands/commands/__tests__/set-worksheet-order.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/set-worksheet-order.command.spec.ts@@ -53,7 +53,7 @@ describe('Test set worksheet order commands', () => {describe('set worksheet order', () => {describe('set worksheet order', async () => {it('correct situation: ', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');const targetActiveSheet = workbook.getActiveSheet();diff --git a/packages/sheets/src/commands/commands/__tests__/set-worksheet-show.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-worksheet-show.command.spec.tsindex 4326724f38..8fa439d687 100644--- a/packages/sheets/src/commands/commands/__tests__/set-worksheet-show.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/set-worksheet-show.command.spec.ts@@ -53,7 +53,7 @@ describe('Test set worksheet show commands', () => {describe('set sheet show', () => {describe('set sheet show after make it hidden', async () => {it('correct situation: ', async () => {- const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance()!;if (!workbook) throw new Error('This is an error');const targetActiveSheet = workbook.getActiveSheet();diff --git a/packages/sheets/src/commands/commands/clear-selection-all.command.ts b/packages/sheets/src/commands/commands/clear-selection-all.command.tsindex d1a10776f5..98b3242b27 100644--- a/packages/sheets/src/commands/commands/clear-selection-all.command.ts+++ b/packages/sheets/src/commands/commands/clear-selection-all.command.ts@@ -44,6 +44,8 @@ export const ClearSelectionAllCommand: ICommand = {const sheetInterceptorService = accessor.get(SheetInterceptorService);const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ if (!workbook) return false;+const unitId = workbook.getUnitId();const worksheet = workbook.getActiveSheet();const subUnitId = worksheet.getSheetId();diff --git a/packages/sheets/src/commands/commands/clear-selection-content.command.ts b/packages/sheets/src/commands/commands/clear-selection-content.command.tsindex 91d8e320b8..3f7d9767ea 100644--- a/packages/sheets/src/commands/commands/clear-selection-content.command.ts+++ b/packages/sheets/src/commands/commands/clear-selection-content.command.ts@@ -46,6 +46,8 @@ export const ClearSelectionContentCommand: ICommand = {const sheetInterceptorService = accessor.get(SheetInterceptorService);const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ if (!workbook) return false;+const unitId = workbook.getUnitId();const worksheet = workbook.getActiveSheet();const subUnitId = worksheet.getSheetId();diff --git a/packages/sheets/src/commands/commands/clear-selection-format.command.ts b/packages/sheets/src/commands/commands/clear-selection-format.command.tsindex ce199146ed..20d91961e4 100644--- a/packages/sheets/src/commands/commands/clear-selection-format.command.ts+++ b/packages/sheets/src/commands/commands/clear-selection-format.command.ts@@ -44,6 +44,8 @@ export const ClearSelectionFormatCommand: ICommand = {const sheetInterceptorService = accessor.get(SheetInterceptorService);const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ if (!workbook) return false;+const unitId = workbook.getUnitId();const worksheet = workbook.getActiveSheet();const subUnitId = worksheet.getSheetId();diff --git a/packages/sheets/src/commands/commands/copy-to-worksheet.command.ts b/packages/sheets/src/commands/commands/copy-to-worksheet.command.tsindex ea652ddf5f..8ab231852f 100644--- a/packages/sheets/src/commands/commands/copy-to-worksheet.command.ts+++ b/packages/sheets/src/commands/commands/copy-to-worksheet.command.ts@@ -23,11 +23,12 @@ import {SetWorksheetConfigMutation,SetWorksheetConfigUndoMutationFactory,} from '../mutations/set-worksheet-config.mutation';+import { getSheetCommandTarget } from './utils/target-util';export interface ICopySheetToCommandParams {unitId?: string;subUnitId?: string;- copyTounitId?: string;+ copyToUnitId?: string;copyToSheetId?: string;}@@ -39,21 +40,13 @@ export const CopySheetToCommand: ICommand = {const undoRedoService = accessor.get(IUndoRedoService);const univerInstanceService = accessor.get(IUniverInstanceService);- const unitId = params.unitId || univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId =- params.subUnitId || univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();- const copyTounitId = params.unitId || univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const copyToSheetId =- params.copyToSheetId || univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const copyToWorkbook = univerInstanceService.getUniverSheetInstance(copyTounitId);- if (!copyToWorkbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;- const copyToWorksheet = workbook.getSheetBySheetId(copyToSheetId);- if (!copyToWorksheet) return false;- if (unitId === copyTounitId && subUnitId === copyToSheetId) return false;+ const fromTarget = getSheetCommandTarget(univerInstanceService, { unitId: params.unitId, subUnitId: params.subUnitId });+ const toTarget = getSheetCommandTarget(univerInstanceService, { unitId: params.copyToUnitId, subUnitId: params.copyToSheetId });+ if (!fromTarget || !toTarget) return false;++ const { subUnitId, unitId, worksheet } = fromTarget;+ const { unitId: copyToUnitId, subUnitId: copyToSheetId } = toTarget;+ if (unitId === copyToUnitId && subUnitId === copyToSheetId) return false;const config = Tools.deepClone(worksheet.getConfig());diff --git a/packages/sheets/src/commands/commands/copy-worksheet.command.ts b/packages/sheets/src/commands/commands/copy-worksheet.command.tsindex 32b592fa90..6f40be9304 100644--- a/packages/sheets/src/commands/commands/copy-worksheet.command.ts+++ b/packages/sheets/src/commands/commands/copy-worksheet.command.ts@@ -31,6 +31,7 @@ import type {} from '../../basics/interfaces/mutation-interface';import { InsertSheetMutation, InsertSheetUndoMutationFactory } from '../mutations/insert-sheet.mutation';import { RemoveSheetMutation } from '../mutations/remove-sheet.mutation';+import { getSheetCommandTarget } from './utils/target-util';export interface ICopySheetCommandParams {unitId?: string;@@ -46,21 +47,12 @@ export const CopySheetCommand: ICommand = {const univerInstanceService = accessor.get(IUniverInstanceService);const localeService = accessor.get(LocaleService);- let unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- let subUnitId = univerInstanceService- .getCurrentUniverSheetInstance()-- .getActiveSheet()- .getSheetId();- if (params) {- unitId = params.unitId ?? unitId;- subUnitId = params.subUnitId ?? subUnitId;+ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) {+ return false;}- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;+ const { workbook, worksheet, unitId } = target;const config = Tools.deepClone(worksheet.getConfig());config.name += localeService.t('sheets.sheetCopy'); // TODO: 文字国际化config.id = Tools.generateRandomId();diff --git a/packages/sheets/src/commands/commands/delete-range-move-left.command.ts b/packages/sheets/src/commands/commands/delete-range-move-left.command.tsindex 7991363b9e..a19ee4e42c 100644--- a/packages/sheets/src/commands/commands/delete-range-move-left.command.ts+++ b/packages/sheets/src/commands/commands/delete-range-move-left.command.ts@@ -30,6 +30,7 @@ import { SelectionManagerService } from '../../services/selection-manager.servicimport { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';import { getRemoveRangeMutations } from '../utils/handle-range-mutation';import { followSelectionOperation } from './utils/selection-utils';+import { getSheetCommandTarget } from './utils/target-util';export interface IDeleteRangeMoveLeftCommandParams {range: IRange;@@ -50,23 +51,17 @@ export const DeleteRangeMoveLeftCommand: ICommand = {const selectionManagerService = accessor.get(SelectionManagerService);const sheetInterceptorService = accessor.get(SheetInterceptorService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService- .getCurrentUniverSheetInstance()+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { worksheet, workbook, subUnitId, unitId } = target;- .getActiveSheet()- .getSheetId();let range = params?.range;if (!range) {range = selectionManagerService.getLast()?.range;}- if (!range) return false;-- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;+ if (!range) return false;const deleteRangeMutationParams: IDeleteRangeMutationParams = {range,subUnitId,diff --git a/packages/sheets/src/commands/commands/delete-range-move-up.command.ts b/packages/sheets/src/commands/commands/delete-range-move-up.command.tsindex f06052594b..a70e7543c0 100644--- a/packages/sheets/src/commands/commands/delete-range-move-up.command.ts+++ b/packages/sheets/src/commands/commands/delete-range-move-up.command.ts@@ -30,10 +30,12 @@ import { SelectionManagerService } from '../../services/selection-manager.servicimport { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';import { getRemoveRangeMutations } from '../utils/handle-range-mutation';import { followSelectionOperation } from './utils/selection-utils';+import { getSheetCommandTarget } from './utils/target-util';export interface IDeleteRangeMoveUpCommandParams {range: IRange;}+export const DeleteRangeMoveUpCommandId = 'sheet.command.delete-range-move-up';/*** The command to delete range.@@ -49,19 +51,16 @@ export const DeleteRangeMoveUpCommand: ICommand = {const selectionManagerService = accessor.get(SelectionManagerService);const sheetInterceptorService = accessor.get(SheetInterceptorService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId, workbook, worksheet } = target;let range = params?.range;if (!range) {range = selectionManagerService.getLast()?.range!;}if (!range) return false;- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;-const deleteRangeMutationParams: IDeleteRangeMutationParams = {range,subUnitId,@@ -83,7 +82,7 @@ export const DeleteRangeMoveUpCommand: ICommand = {redos.push(...sheetInterceptor.redos);redos.push(followSelectionOperation(range, workbook, worksheet));undos.push(...(sheetInterceptor.preUndos ?? []));- const result = await sequenceExecute(redos, commandService).result;+ const result = sequenceExecute(redos, commandService).result;if (result) {undoRedoService.pushUndoRedo({diff --git a/packages/sheets/src/commands/commands/insert-range-move-down.command.ts b/packages/sheets/src/commands/commands/insert-range-move-down.command.tsindex 907ef90fb9..5807e07b6f 100644--- a/packages/sheets/src/commands/commands/insert-range-move-down.command.ts+++ b/packages/sheets/src/commands/commands/insert-range-move-down.command.ts@@ -39,6 +39,7 @@ import { InsertRowMutation, InsertRowMutationUndoFactory } from '../mutations/inimport { RemoveRowMutation } from '../mutations/remove-row-col.mutation';import { getInsertRangeMutations } from '../utils/handle-range-mutation';import { followSelectionOperation } from './utils/selection-utils';+import { getSheetCommandTarget } from './utils/target-util';export interface InsertRangeMoveDownCommandParams {range: IRange;@@ -66,23 +67,17 @@ export const InsertRangeMoveDownCommand: ICommand = {return false;}- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService- .getCurrentUniverSheetInstance()+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId, worksheet, workbook } = target;- .getActiveSheet()- .getSheetId();let range = params?.range;if (!range) {range = selectionManagerService.getLast()?.range;}if (!range) return false;- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;-const redoMutations: IMutationInfo[] = [];const undoMutations: IMutationInfo[] = [];diff --git a/packages/sheets/src/commands/commands/insert-range-move-right.command.ts b/packages/sheets/src/commands/commands/insert-range-move-right.command.tsindex 6e445bd50d..af5462a495 100644--- a/packages/sheets/src/commands/commands/insert-range-move-right.command.ts+++ b/packages/sheets/src/commands/commands/insert-range-move-right.command.ts@@ -39,6 +39,7 @@ import { InsertColMutation, InsertColMutationUndoFactory } from '../mutations/inimport { RemoveColMutation } from '../mutations/remove-row-col.mutation';import { getInsertRangeMutations } from '../utils/handle-range-mutation';import { followSelectionOperation } from './utils/selection-utils';+import { getSheetCommandTarget } from './utils/target-util';export interface InsertRangeMoveRightCommandParams {range: IRange;@@ -65,19 +66,17 @@ export const InsertRangeMoveRightCommand: ICommand = {return false;}- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { workbook, worksheet, unitId, subUnitId } = target;+let range = params?.range;if (!range) {range = selectionManagerService.getLast()?.range;}if (!range) return false;- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;-const redoMutations: IMutationInfo[] = [];const undoMutations: IMutationInfo[] = [];@@ -147,7 +146,6 @@ export const InsertRangeMoveRightCommand: ICommand = {);redoMutations.push(...insertRangeRedo);-undoMutations.push(...insertRangeUndo);const sheetInterceptor = sheetInterceptorService.onCommandExecute({diff --git a/packages/sheets/src/commands/commands/insert-sheet.command.ts b/packages/sheets/src/commands/commands/insert-sheet.command.tsindex 93af3018e1..ec4935a7a3 100644--- a/packages/sheets/src/commands/commands/insert-sheet.command.ts+++ b/packages/sheets/src/commands/commands/insert-sheet.command.ts@@ -32,6 +32,7 @@ import type {} from '../../basics/interfaces/mutation-interface';import { InsertSheetMutation, InsertSheetUndoMutationFactory } from '../mutations/insert-sheet.mutation';import { RemoveSheetMutation } from '../mutations/remove-sheet.mutation';+import { getSheetCommandTargetWorkbook } from './utils/target-util';export interface IInsertSheetCommandParams {unitId?: string;@@ -51,11 +52,10 @@ export const InsertSheetCommand: ICommand = {const univerInstanceService = accessor.get(IUniverInstanceService);const localeService = accessor.get(LocaleService);- const unitId = params?.unitId ?? univerInstanceService.getCurrentUniverSheetInstance().getUnitId();-- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;+ const target = getSheetCommandTargetWorkbook(univerInstanceService, { unitId: params?.unitId });+ if (!target) return false;+ const { unitId, workbook } = target;let index = workbook.getSheets().length;let sheetConfig = mergeWorksheetSnapshotWithDefault({});if (params) {diff --git a/packages/sheets/src/commands/commands/move-range.command.ts b/packages/sheets/src/commands/commands/move-range.command.tsindex 6c53baea6c..9e1e4448bf 100644--- a/packages/sheets/src/commands/commands/move-range.command.ts+++ b/packages/sheets/src/commands/commands/move-range.command.ts@@ -36,6 +36,7 @@ import { MoveRangeMutation } from '../mutations/move-range.mutation';import type { ISetSelectionsOperationParams } from '../operations/selection.operation';import { SetSelectionsOperation } from '../operations/selection.operation';import { alignToMergedCellsBorders, getPrimaryForRange } from './utils/selection-utils';+import { getSheetCommandTarget } from './utils/target-util';export interface IMoveRangeCommandParams {toRange: IRange;@@ -50,11 +51,11 @@ export const MoveRangeCommand: ICommand = {const undoRedoService = accessor.get(IUndoRedoService);const univerInstanceService = accessor.get(IUniverInstanceService);const errorService = accessor.get(ErrorService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { worksheet, subUnitId, unitId } = target;const moveRangeMutations = getMoveRangeUndoRedoMutations(accessor,{ unitId, subUnitId, range: params.fromRange },diff --git a/packages/sheets/src/commands/commands/remove-row-col.command.ts b/packages/sheets/src/commands/commands/remove-row-col.command.tsindex 4f103f61b6..03c4d9d27a 100644--- a/packages/sheets/src/commands/commands/remove-row-col.command.ts+++ b/packages/sheets/src/commands/commands/remove-row-col.command.ts@@ -41,6 +41,7 @@ import {} from '../mutations/remove-row-col.mutation';import { type ISetRangeValuesMutationParams, SetRangeValuesMutation } from '../mutations/set-range-values.mutation';import { followSelectionOperation } from './utils/selection-utils';+import { getSheetCommandTarget } from './utils/target-util';export interface IRemoveRowColCommandParams {range: IRange;@@ -59,18 +60,14 @@ export const RemoveRowCommand: ICommand<IRemoveRowColCommandParams> = {const sheetInterceptorService = accessor.get(SheetInterceptorService);let range = params?.range;- if (!range) {- range = selectionManagerService.getLast()?.range;- }- if (!range) {- return false;- }+ if (!range) range = selectionManagerService.getLast()?.range;+ if (!range) return false;+const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();+ const { workbook, worksheet, subUnitId, unitId } = target;range = {...range,@@ -123,7 +120,9 @@ export const RemoveRowCommand: ICommand<IRemoveRowColCommandParams> = {return false;},};+export const RemoveColCommandId = 'sheet.command.remove-col';+/*** This command would remove the selected columns. These selected rows can be non-continuous.*/@@ -134,20 +133,16 @@ export const RemoveColCommand: ICommand = {handler: async (accessor: IAccessor, params?: IRemoveRowColCommandParams) => {const selectionManagerService = accessor.get(SelectionManagerService);const sheetInterceptorService = accessor.get(SheetInterceptorService);+let range = params?.range;- if (!range) {- range = selectionManagerService.getLast()?.range;- }- if (!range) {- return false;- }+ if (!range) range = selectionManagerService.getLast()?.range;+ if (!range) return false;const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();+ const { workbook, worksheet, subUnitId, unitId } = target;range = {...range,diff --git a/packages/sheets/src/commands/commands/remove-sheet.command.ts b/packages/sheets/src/commands/commands/remove-sheet.command.tsindex 5c840df7c9..5d255fa9dc 100644--- a/packages/sheets/src/commands/commands/remove-sheet.command.ts+++ b/packages/sheets/src/commands/commands/remove-sheet.command.ts@@ -32,6 +32,7 @@ import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-import { InsertSheetMutation } from '../mutations/insert-sheet.mutation';import { RemoveSheetMutation, RemoveSheetUndoMutationFactory } from '../mutations/remove-sheet.mutation';import type { ISetWorksheetActiveOperationParams } from '../operations/set-worksheet-active.operation';+import { getSheetCommandTarget } from './utils/target-util';export interface IRemoveSheetCommandParams {unitId?: string;@@ -49,22 +50,12 @@ export const RemoveSheetCommand: ICommand = {const undoRedoService = accessor.get(IUndoRedoService);const univerInstanceService = accessor.get(IUniverInstanceService);const sheetInterceptorService = accessor.get(SheetInterceptorService);- let unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- let subUnitId = univerInstanceService- .getCurrentUniverSheetInstance()- .getActiveSheet()- .getSheetId();+ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) return false;- if (params) {- unitId = params.unitId ?? unitId;- subUnitId = params.subUnitId ?? subUnitId;- }+ const { unitId, subUnitId, workbook, worksheet } = target;- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;if (workbook.getSheets().length <= 1) return false;const index = workbook.getSheetIndex(worksheet);diff --git a/packages/sheets/src/commands/commands/remove-worksheet-merge.command.ts b/packages/sheets/src/commands/commands/remove-worksheet-merge.command.tsindex 97debd4e84..043c373251 100644--- a/packages/sheets/src/commands/commands/remove-worksheet-merge.command.ts+++ b/packages/sheets/src/commands/commands/remove-worksheet-merge.command.ts@@ -29,6 +29,7 @@ import {RemoveWorksheetMergeMutation,} from '../mutations/remove-worksheet-merge.mutation';import { SetSelectionsOperation } from '../../commands/operations/selection.operation';+import { getSheetCommandTarget } from './utils/target-util';export const RemoveWorksheetMergeCommand: ICommand = {type: CommandType.COMMAND,@@ -41,18 +42,11 @@ export const RemoveWorksheetMergeCommand: ICommand = {const selections = selectionManagerService.getSelectionRanges();if (!selections?.length) return false;- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService- .getCurrentUniverSheetInstance()- .getActiveSheet()- .getSheetId();-- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;+ const { subUnitId, unitId, worksheet } = target;const removeMergeMutationParams: IRemoveWorksheetMergeMutationParams = {unitId,subUnitId,@@ -78,6 +72,7 @@ export const RemoveWorksheetMergeCommand: ICommand = {const nowSelections = selectionManagerService.getSelections();if (!nowSelections?.length) return false;+const undoSelections = Tools.deepClone(nowSelections);const redoSelections = Tools.deepClone(nowSelections);const redoLastSelection = redoSelections[redoSelections.length - 1];diff --git a/packages/sheets/src/commands/commands/set-border-command.ts b/packages/sheets/src/commands/commands/set-border-command.tsindex f8671faf3b..a95441b850 100644--- a/packages/sheets/src/commands/commands/set-border-command.ts+++ b/packages/sheets/src/commands/commands/set-border-command.ts@@ -39,7 +39,7 @@ import { BorderStyleManagerService, type IBorderInfo } from '../../services/bordimport { SelectionManagerService } from '../../services/selection-manager.service';import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';-import { getCommandTarget } from '../utils/get-target';+import { getSheetCommandTarget } from './utils/target-util';function forEach(range: IRange, action: (row: number, column: number) => void): void {const { startRow, startColumn, endRow, endColumn } = range;@@ -142,11 +142,10 @@ export const SetBorderCommand: ICommand = {const selectionManagerService = accessor.get(SelectionManagerService);const borderStyleManagerService = accessor.get(BorderStyleManagerService);- const { worksheet, unitId, subUnitId } = getCommandTarget(- univerInstanceService,- params?.unitId,- params?.subUnitId- );+ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) return false;++ const { worksheet, unitId, subUnitId } = target;const selections = selectionManagerService.getSelectionRanges();const mergeData = worksheet.getConfig().mergeData;if (!selections?.length) {diff --git a/packages/sheets/src/commands/commands/set-frozen-cancel.command.ts b/packages/sheets/src/commands/commands/set-frozen-cancel.command.tsindex 929cf1c776..9e7fbf8a83 100644--- a/packages/sheets/src/commands/commands/set-frozen-cancel.command.ts+++ b/packages/sheets/src/commands/commands/set-frozen-cancel.command.ts@@ -20,6 +20,7 @@ import type { IAccessor } from '@wendellhu/redi';import type { ISetFrozenMutationParams } from '../mutations/set-frozen.mutation';import { SetFrozenMutation, SetFrozenMutationFactory } from '../mutations/set-frozen.mutation';+import { getSheetCommandTarget } from './utils/target-util';export const SetFrozenCancelCommand: ICommand = {type: CommandType.COMMAND,@@ -29,18 +30,10 @@ export const SetFrozenCancelCommand: ICommand = {const undoRedoService = accessor.get(IUndoRedoService);const univerInstanceService = accessor.get(IUniverInstanceService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService- .getCurrentUniverSheetInstance()-- .getActiveSheet()- .getSheetId();-- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;+ const { unitId, subUnitId } = target;const redoMutationParams: ISetFrozenMutationParams = {unitId,subUnitId,diff --git a/packages/sheets/src/commands/commands/set-frozen.command.ts b/packages/sheets/src/commands/commands/set-frozen.command.tsindex df18a6ab78..8fc325dfb6 100644--- a/packages/sheets/src/commands/commands/set-frozen.command.ts+++ b/packages/sheets/src/commands/commands/set-frozen.command.ts@@ -20,6 +20,7 @@ import type { IAccessor } from '@wendellhu/redi';import type { ISetFrozenMutationParams } from '../mutations/set-frozen.mutation';import { SetFrozenMutation, SetFrozenMutationFactory } from '../mutations/set-frozen.mutation';+import { getSheetCommandTarget } from './utils/target-util';interface ISetFrozenCommandParams {startRow: number;@@ -36,18 +37,10 @@ export const SetFrozenCommand: ICommand = {const undoRedoService = accessor.get(IUndoRedoService);const univerInstanceService = accessor.get(IUniverInstanceService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService- .getCurrentUniverSheetInstance()-- .getActiveSheet()- .getSheetId();-- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;+ const { unitId, subUnitId, worksheet } = target;const { startColumn, startRow, xSplit, ySplit } = params;if (diff --git a/packages/sheets/src/commands/commands/set-hide-gridlines.command.ts b/packages/sheets/src/commands/commands/set-hide-gridlines.command.tsindex 8beb1d717f..6801888e71 100644--- a/packages/sheets/src/commands/commands/set-hide-gridlines.command.ts+++ b/packages/sheets/src/commands/commands/set-hide-gridlines.command.ts@@ -23,6 +23,7 @@ import {SetHideGridlinesMutation,SetHideGridlinesUndoMutationFactory,} from '../mutations/set-hide-gridlines.mutatiom';+import { getSheetCommandTarget } from './utils/target-util';export interface ISetHideGridlinesCommandParams {hideGridlines?: BooleanNumber;@@ -39,17 +40,13 @@ export const SetHideGridlinesCommand: ICommand = {const undoRedoService = accessor.get(IUndoRedoService);const univerInstanceService = accessor.get(IUniverInstanceService);- let unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- let subUnitId = univerInstanceService- .getCurrentUniverSheetInstance()+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;- .getActiveSheet()- .getSheetId();+ const { unitId, subUnitId } = target;let hideGridlines = BooleanNumber.FALSE;if (params) {- unitId = params.unitId ?? unitId;- subUnitId = params.subUnitId ?? unitId;hideGridlines = params.hideGridlines ?? BooleanNumber.FALSE;}diff --git a/packages/sheets/src/commands/commands/set-range-values.command.ts b/packages/sheets/src/commands/commands/set-range-values.command.tsindex 2eb9ade045..1eae7cc491 100644--- a/packages/sheets/src/commands/commands/set-range-values.command.ts+++ b/packages/sheets/src/commands/commands/set-range-values.command.ts@@ -32,6 +32,7 @@ import { INTERCEPTOR_POINT } from '../../services/sheet-interceptor/interceptor-import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';import type { ISheetCommandSharedParams } from '../utils/interface';+import { getSheetCommandTarget } from './utils/target-util';export interface ISetRangeValuesCommandParams extends Partial<ISheetCommandSharedParams> {range?: IRange;@@ -56,12 +57,12 @@ export const SetRangeValuesCommand: ICommand = {const univerInstanceService = accessor.get(IUniverInstanceService);const selectionManagerService = accessor.get(SelectionManagerService);const sheetInterceptorService = accessor.get(SheetInterceptorService);- const {- value,- range,- unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId(),- subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId(),- } = params;++ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { subUnitId, unitId } = target;+ const { value, range } = params;const currentSelections = range ? [range] : selectionManagerService.getSelectionRanges();if (!currentSelections || !currentSelections.length) {return false;diff --git a/packages/sheets/src/commands/commands/set-row-visible.command.ts b/packages/sheets/src/commands/commands/set-row-visible.command.tsindex 33c4c3c862..5267418007 100644--- a/packages/sheets/src/commands/commands/set-row-visible.command.ts+++ b/packages/sheets/src/commands/commands/set-row-visible.command.ts@@ -36,6 +36,7 @@ import {import type { ISetSelectionsOperationParams } from '../operations/selection.operation';import { SetSelectionsOperation } from '../operations/selection.operation';import { getPrimaryForRange } from './utils/selection-utils';+import { getSheetCommandTarget } from './utils/target-util';export interface ISetSpecificRowsVisibleCommandParams {unitId: string;@@ -160,12 +161,10 @@ export const SetRowHiddenCommand: ICommand = {return false;}- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId, worksheet } = target;const redoMutationParams: ISetRowHiddenMutationParams = {unitId,diff --git a/packages/sheets/src/commands/commands/set-style.command.ts b/packages/sheets/src/commands/commands/set-style.command.tsindex 6fa24d675b..60ed486f60 100644--- a/packages/sheets/src/commands/commands/set-style.command.ts+++ b/packages/sheets/src/commands/commands/set-style.command.ts@@ -43,6 +43,7 @@ import { SelectionManagerService } from '../../services/selection-manager.servicimport { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';+import { getSheetCommandTarget } from './utils/target-util';export interface IStyleTypeValue<T> {type: keyof IStyleData;@@ -70,12 +71,11 @@ export const SetStyleCommand: ICommand<ISetStyleCommandParams<unknown>> = {handler: async <T> (accessor: IAccessor, params: ISetStyleCommandParams<T>) => {const univerInstanceService = accessor.get(IUniverInstanceService);- const {- unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId(),- subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId(),- range,- style,- } = params;+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;+ const { range, style } = params;const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);const selectionManagerService = accessor.get(SelectionManagerService);@@ -85,12 +85,6 @@ export const SetStyleCommand: ICommand<ISetStyleCommandParams<unknown>> = {return false;}- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- const worksheet = workbook?.getSheetBySheetId(subUnitId);- if (worksheet == null) {- return false;- }-const cellValue = new ObjectMatrix<ICellData>();if (Tools.isArray(style.value)) {@@ -171,12 +165,12 @@ export const SetBoldCommand: ICommand = {id: 'sheet.command.set-bold',handler: async (accessor) => {const selection = accessor.get(SelectionManagerService).getLast();+ if (!selection) return false;- if (!selection) {- return false;- }+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;- const worksheet = accessor.get(IUniverInstanceService).getCurrentUniverSheetInstance().getActiveSheet();+ const { worksheet } = target;const { actualRow, actualColumn } = selection.primary;const currentlyBold = worksheet.getRange(actualRow, actualColumn).getFontWeight() === FontWeight.BOLD;@@ -200,12 +194,12 @@ export const SetItalicCommand: ICommand = {id: 'sheet.command.set-italic',handler: async (accessor) => {const selection = accessor.get(SelectionManagerService).getLast();+ if (!selection) return false;- if (!selection) {- return false;- }+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;- const worksheet = accessor.get(IUniverInstanceService).getCurrentUniverSheetInstance().getActiveSheet();+ const { worksheet } = target;let currentlyItalic = true;if (selection.primary) {@@ -233,15 +227,13 @@ export const SetUnderlineCommand: ICommand = {id: 'sheet.command.set-underline',handler: async (accessor) => {const selection = accessor.get(SelectionManagerService).getLast();- if (!selection) {- return false;- }+ if (!selection) return false;- const worksheet = accessor- .get(IUniverInstanceService)- .getCurrentUniverSheetInstance()+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;++ const { worksheet } = target;- .getActiveSheet();let currentlyUnderline = true;if (selection.primary) {currentlyUnderline = !!worksheet@@ -270,15 +262,13 @@ export const SetStrikeThroughCommand: ICommand = {id: 'sheet.command.set-stroke',handler: async (accessor) => {const selection = accessor.get(SelectionManagerService).getLast();- if (!selection) {- return false;- }+ if (!selection) return false;++ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;- const worksheet = accessor- .get(IUniverInstanceService)- .getCurrentUniverSheetInstance()+ const { worksheet } = target;- .getActiveSheet();let currentlyStrokeThrough = true;if (selection.primary) {currentlyStrokeThrough = !!worksheet@@ -305,15 +295,12 @@ export const SetOverlineCommand: ICommand = {id: 'sheet.command.set-overline',handler: async (accessor) => {const selection = accessor.get(SelectionManagerService).getLast();- if (!selection) {- return false;- }+ if (!selection) return false;- const worksheet = accessor- .get(IUniverInstanceService)- .getCurrentUniverSheetInstance()+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;- .getActiveSheet();+ const { worksheet } = target;let currentlyOverline = true;if (selection.primary) {currentlyOverline = !!worksheetdiff --git a/packages/sheets/src/commands/commands/set-tab-color.command.ts b/packages/sheets/src/commands/commands/set-tab-color.command.tsindex bb4d6b83f9..d7a4ee9a16 100644--- a/packages/sheets/src/commands/commands/set-tab-color.command.ts+++ b/packages/sheets/src/commands/commands/set-tab-color.command.ts@@ -20,6 +20,7 @@ import type { IAccessor } from '@wendellhu/redi';import type { ISetTabColorMutationParams } from '../mutations/set-tab-color.mutation';import { SetTabColorMutation, SetTabColorUndoMutationFactory } from '../mutations/set-tab-color.mutation';+import { getSheetCommandTarget } from './utils/target-util';export interface ISetTabColorCommandParams {value: string;@@ -32,20 +33,11 @@ export const SetTabColorCommand: ICommand = {handler: async (accessor: IAccessor, params: ISetTabColorCommandParams) => {const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);- const univerInstanceService = accessor.get(IUniverInstanceService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService- .getCurrentUniverSheetInstance()-- .getActiveSheet()- .getSheetId();-- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;+ const { unitId, subUnitId } = target;const setTabColorMutationParams: ISetTabColorMutationParams = {color: params.value,unitId,diff --git a/packages/sheets/src/commands/commands/set-worksheet-activate.command.ts b/packages/sheets/src/commands/commands/set-worksheet-activate.command.tsindex 9c052ad049..41e6ff4c79 100644--- a/packages/sheets/src/commands/commands/set-worksheet-activate.command.ts+++ b/packages/sheets/src/commands/commands/set-worksheet-activate.command.ts@@ -20,6 +20,7 @@ import type { IAccessor } from '@wendellhu/redi';import type { ISetWorksheetActiveOperationParams } from '../operations/set-worksheet-active.operation';import { SetWorksheetActiveOperation } from '../operations/set-worksheet-active.operation';+import { getSheetCommandTarget } from './utils/target-util';export interface ISetWorksheetActivateCommandParams {unitId?: string;@@ -34,16 +35,11 @@ export const SetWorksheetActivateCommand: ICommand = {id: 'sheet.command.set-worksheet-activate',handler: (accessor: IAccessor, params?: ISetWorksheetActivateCommandParams, options?: IExecutionOptions) => {const commandService = accessor.get(ICommandService);- const univerInstanceService = accessor.get(IUniverInstanceService);- let unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- let subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();-- if (params) {- unitId = params.unitId ?? unitId;- subUnitId = params.subUnitId ?? subUnitId;- }+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);+ if (!target) return false;+ const { unitId, subUnitId } = target;return new Promise((resolve) => {setTimeout(() => {const result = commandService.syncExecuteCommand(SetWorksheetActiveOperation.id, {diff --git a/packages/sheets/src/commands/commands/set-worksheet-col-width.command.ts b/packages/sheets/src/commands/commands/set-worksheet-col-width.command.tsindex a6fa9ba2b3..f8191477a0 100644--- a/packages/sheets/src/commands/commands/set-worksheet-col-width.command.ts+++ b/packages/sheets/src/commands/commands/set-worksheet-col-width.command.ts@@ -33,6 +33,7 @@ import {SetWorksheetColWidthMutation,SetWorksheetColWidthMutationFactory,} from '../mutations/set-worksheet-col-width.mutation';+import { getSheetCommandTarget } from './utils/target-util';export interface IDeltaColumnWidthCommandParams {anchorCol: number;@@ -52,11 +53,11 @@ export const DeltaColumnWidthCommand: ICommand<IDeltaColumnWidthCommandParams> =const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);- const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();++ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;++ const { worksheet, unitId, subUnitId } = target;const { anchorCol, deltaX } = params;const anchorColWidth = worksheet.getColumnWidth(anchorCol);@@ -154,16 +155,14 @@ export const SetColWidthCommand: ICommand = {const selectionManagerService = accessor.get(SelectionManagerService);const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);- const univerInstanceService = accessor.get(IUniverInstanceService);const selections = selectionManagerService.getSelectionRanges();- if (!selections?.length) {- return false;- }+ if (!selections?.length) return false;- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;+ const { subUnitId, unitId } = target;const redoMutationParams: ISetWorksheetColWidthMutationParams = {subUnitId,unitId,diff --git a/packages/sheets/src/commands/commands/set-worksheet-hide.command.ts b/packages/sheets/src/commands/commands/set-worksheet-hide.command.tsindex 1865fea319..7fa43bd5a2 100644--- a/packages/sheets/src/commands/commands/set-worksheet-hide.command.ts+++ b/packages/sheets/src/commands/commands/set-worksheet-hide.command.ts@@ -27,6 +27,7 @@ import type { IAccessor } from '@wendellhu/redi';import type { ISetWorksheetHideMutationParams } from '../mutations/set-worksheet-hide.mutation';import { SetWorksheetHideMutation, SetWorksheetHideMutationFactory } from '../mutations/set-worksheet-hide.mutation';+import { getSheetCommandTarget } from './utils/target-util';export interface ISetWorksheetHiddenCommandParams {subUnitId?: string;@@ -39,21 +40,12 @@ export const SetWorksheetHideCommand: ICommand = {handler: async (accessor: IAccessor, params?: ISetWorksheetHiddenCommandParams) => {const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);- const univerInstanceService = accessor.get(IUniverInstanceService);const errorService = accessor.get(ErrorService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- let subUnitId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();-- if (params) {- subUnitId = params.subUnitId ?? subUnitId;- }-- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);+ if (!target) return false;+ const { workbook, worksheet, unitId, subUnitId } = target;const hidden = worksheet.getConfig().hidden;if (hidden === BooleanNumber.TRUE) return false;@@ -73,7 +65,6 @@ export const SetWorksheetHideCommand: ICommand = {}const result = commandService.syncExecuteCommand(SetWorksheetHideMutation.id, redoMutationParams);-if (result) {undoRedoService.pushUndoRedo({unitID: unitId,@@ -82,6 +73,7 @@ export const SetWorksheetHideCommand: ICommand = {});return true;}+return false;},};diff --git a/packages/sheets/src/commands/commands/set-worksheet-name.command.ts b/packages/sheets/src/commands/commands/set-worksheet-name.command.tsindex 237e55ab1c..6d7cc131aa 100644--- a/packages/sheets/src/commands/commands/set-worksheet-name.command.ts+++ b/packages/sheets/src/commands/commands/set-worksheet-name.command.ts@@ -27,6 +27,7 @@ import type { IAccessor } from '@wendellhu/redi';import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';import type { ISetWorksheetNameMutationParams } from '../mutations/set-worksheet-name.mutation';import { SetWorksheetNameMutation, SetWorksheetNameMutationFactory } from '../mutations/set-worksheet-name.mutation';+import { getSheetCommandTarget } from './utils/target-util';export interface ISetWorksheetNameCommandParams {name: string;@@ -44,13 +45,12 @@ export const SetWorksheetNameCommand: ICommand = {handler: async (accessor: IAccessor, params: ISetWorksheetNameCommandParams) => {const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);- const univerInstanceService = accessor.get(IUniverInstanceService);const sheetInterceptorService = accessor.get(SheetInterceptorService);- const unitId = params.unitId || univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId =- params.subUnitId || univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);+ if (!target) return false;+ const { unitId, subUnitId } = target;const redoMutationParams: ISetWorksheetNameMutationParams = {subUnitId,name: params.name,diff --git a/packages/sheets/src/commands/commands/set-worksheet-order.command.ts b/packages/sheets/src/commands/commands/set-worksheet-order.command.tsindex 4dc900393c..0aadde3bd5 100644--- a/packages/sheets/src/commands/commands/set-worksheet-order.command.ts+++ b/packages/sheets/src/commands/commands/set-worksheet-order.command.ts@@ -23,6 +23,7 @@ import {SetWorksheetOrderMutation,SetWorksheetOrderUndoMutationFactory,} from '../mutations/set-worksheet-order.mutation';+import { getSheetCommandTarget } from './utils/target-util';export interface ISetWorksheetOrderCommandParams {order: number;@@ -37,19 +38,12 @@ export const SetWorksheetOrderCommand: ICommand = {handler: async (accessor: IAccessor, params: ISetWorksheetOrderCommandParams) => {const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);- const univerInstanceService = accessor.get(IUniverInstanceService);- const unitId = params.unitId || univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const subUnitId =- params.subUnitId || univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();-- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);+ if (!target) return false;+ const { workbook, unitId, subUnitId } = target;const fromOrder = workbook.getConfig().sheetOrder.indexOf(subUnitId);-const setWorksheetOrderMutationParams: ISetWorksheetOrderMutationParams = {fromOrder,toOrder: params.order,diff --git a/packages/sheets/src/commands/commands/set-worksheet-right-to-left.command.ts b/packages/sheets/src/commands/commands/set-worksheet-right-to-left.command.tsindex f7a850186a..cfeecad362 100644--- a/packages/sheets/src/commands/commands/set-worksheet-right-to-left.command.ts+++ b/packages/sheets/src/commands/commands/set-worksheet-right-to-left.command.ts@@ -23,6 +23,7 @@ import {SetWorksheetRightToLeftMutation,SetWorksheetRightToLeftUndoMutationFactory,} from '../mutations/set-worksheet-right-to-left.mutation';+import { getSheetCommandTarget } from './utils/target-util';export interface ISetWorksheetRightToLeftCommandParams {rightToLeft?: BooleanNumber;@@ -37,27 +38,17 @@ export const SetWorksheetRightToLeftCommand: ICommand = {handler: async (accessor: IAccessor, params?: ISetWorksheetRightToLeftCommandParams) => {const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);- const univerInstanceService = accessor.get(IUniverInstanceService);- let unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- let subUnitId = univerInstanceService- .getCurrentUniverSheetInstance()+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);+ if (!target) return false;- .getActiveSheet()- .getSheetId();+ const { unitId, subUnitId } = target;let rightToLeft = BooleanNumber.FALSE;if (params) {- unitId = params.unitId ?? unitId;- subUnitId = params.subUnitId ?? subUnitId;rightToLeft = params.rightToLeft ?? BooleanNumber.FALSE;}- const workbook = univerInstanceService.getUniverSheetInstance(unitId);- if (!workbook) return false;- const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) return false;-const setWorksheetRightToLeftMutationParams: ISetWorksheetRightToLeftMutationParams = {rightToLeft,unitId,diff --git a/packages/sheets/src/commands/commands/set-worksheet-row-height.command.ts b/packages/sheets/src/commands/commands/set-worksheet-row-height.command.tsindex 4102771ca9..a149089a96 100644--- a/packages/sheets/src/commands/commands/set-worksheet-row-height.command.ts+++ b/packages/sheets/src/commands/commands/set-worksheet-row-height.command.ts@@ -39,6 +39,7 @@ import {SetWorksheetRowIsAutoHeightMutation,SetWorksheetRowIsAutoHeightMutationFactory,} from '../mutations/set-worksheet-row-height.mutation';+import { getSheetCommandTarget } from './utils/target-util';export interface IDeltaRowHeightCommand {anchorRow: number;@@ -55,11 +56,10 @@ export const DeltaRowHeightCommand: ICommand = {return false;}- const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const worksheet = workbook.getActiveSheet();- const unitId = workbook.getUnitId();- const subUnitId = worksheet.getSheetId();+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;++ const { worksheet, subUnitId, unitId } = target;const { anchorRow, deltaY } = params;const anchorRowHeight = worksheet.getRowHeight(anchorRow);@@ -195,9 +195,10 @@ export const SetRowHeightCommand: ICommand = {return false;}- const workbook = univerInstanceService.getCurrentUniverSheetInstance();- const unitId = workbook.getUnitId();- const subUnitId = workbook.getActiveSheet().getSheetId();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId } = target;const redoMutationParams: ISetWorksheetRowHeightMutationParams = {subUnitId,@@ -278,10 +279,11 @@ export const SetWorksheetRowIsAutoHeightCommand: ICommand = {const undoRedoService = accessor.get(IUndoRedoService);const selectionManagerService = accessor.get(SelectionManagerService);const univerInstanceService = accessor.get(IUniverInstanceService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const workSheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();- const subUnitId = workSheet.getSheetId();+ const target = getSheetCommandTarget(univerInstanceService);+ if (!target) return false;++ const { unitId, subUnitId, worksheet } = target;const { anchorRow } = params ?? {};const ranges =@@ -291,7 +293,7 @@ export const SetWorksheetRowIsAutoHeightCommand: ICommand = {startRow: anchorRow,endRow: anchorRow,startColumn: 0,- endColumn: workSheet.getMaxColumns() - 1,+ endColumn: worksheet.getMaxColumns() - 1,},]: selectionManagerService.getSelectionRanges();diff --git a/packages/sheets/src/commands/commands/set-worksheet-show.command.ts b/packages/sheets/src/commands/commands/set-worksheet-show.command.tsindex bf44527762..6aa6f82ce5 100644--- a/packages/sheets/src/commands/commands/set-worksheet-show.command.ts+++ b/packages/sheets/src/commands/commands/set-worksheet-show.command.ts@@ -25,6 +25,7 @@ import {SetWorksheetActiveOperation,// SetWorksheetUnActivateMutationFactory,} from '../operations/set-worksheet-active.operation';+import { getSheetCommandTarget } from './utils/target-util';export interface ISetWorksheetShowCommandParams {value?: string;@@ -39,17 +40,10 @@ export const SetWorksheetShowCommand: ICommand = {const undoRedoService = accessor.get(IUndoRedoService);const univerInstanceService = accessor.get(IUniverInstanceService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- let subUnitId = univerInstanceService- .getCurrentUniverSheetInstance()-- .getActiveSheet()- .getSheetId();-- if (params) {- subUnitId = params.value ?? subUnitId;- }+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));+ if (!target) return false;+ const { unitId, subUnitId } = target;const workbook = univerInstanceService.getUniverSheetInstance(unitId);if (!workbook) return false;const worksheet = workbook.getSheetBySheetId(subUnitId);diff --git a/packages/sheets/src/commands/commands/utils/target-util.ts b/packages/sheets/src/commands/commands/utils/target-util.tsnew file mode 100644index 0000000000..a551951528--- /dev/null+++ b/packages/sheets/src/commands/commands/utils/target-util.ts@@ -0,0 +1,89 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import type { IUniverInstanceService, Nullable, Workbook, Worksheet } from '@univerjs/core';++export function getSheetCommandTargetWorkbook(univerInstanceService: IUniverInstanceService, params: { unitId?: string }): Nullable<{+ workbook: Workbook;+ unitId: string;+}> {+ const { unitId } = params;+ const workbook = unitId+ ? univerInstanceService.getUniverSheetInstance(unitId)+ : univerInstanceService.getCurrentUniverSheetInstance();++ if (!workbook) return null;++ return {+ workbook,+ unitId: workbook.getUnitId(),+ };+}++interface IResult {+ workbook: Workbook;+ worksheet: Worksheet;+ unitId: string;+ subUnitId: string;+}++/**+ * Get targeted Workbook & Worksheet of a command. If `unitId` and `subUnitId` are given, the function would+ * try to get these instances. If not, it would try to get the current active instances.+ *+ * @param univerInstanceService+ * @param params - unitId and subUnitId+ * @param params.unitId - The unitId of the Workbook+ * @param params.subUnitId - The subUnitId of the Worksheet+ * @returns Targeted Workbook & Worksheet+ */+export function getSheetCommandTarget(univerInstanceService: IUniverInstanceService, params: { unitId?: string; subUnitId?: string } = {}): Nullable<IResult> {+ const { unitId, subUnitId } = params;++ const workbook = unitId+ ? univerInstanceService.getUniverSheetInstance(unitId)+ : univerInstanceService.getCurrentUniverSheetInstance();+ if (!workbook) return null;++ const worksheet = subUnitId ? workbook.getSheetBySheetId(subUnitId) : workbook.getActiveSheet();+ if (!worksheet) {+ return null;+ }++ return {+ worksheet,+ workbook,+ unitId: workbook.getUnitId(),+ subUnitId: worksheet.getSheetId(),+ };+}++export function getSheetMutationTarget(univerInstanceService: IUniverInstanceService, params: { unitId: string; subUnitId: string }): Nullable<Pick<IResult, 'workbook' | 'worksheet'>> {+ const { unitId, subUnitId } = params;++ const workbook = univerInstanceService.getUniverSheetInstance(unitId);+ if (!workbook) return null;++ const worksheet = workbook.getSheetBySheetId(subUnitId);+ if (!worksheet) {+ return null;+ }++ return {+ worksheet,+ workbook,+ };+}diff --git a/packages/sheets/src/commands/mutations/__tests__/remove-row-col.mutation.spec.ts b/packages/sheets/src/commands/mutations/__tests__/remove-row-col.mutation.spec.tsindex 2714504afe..35f4484a1f 100644--- a/packages/sheets/src/commands/mutations/__tests__/remove-row-col.mutation.spec.ts+++ b/packages/sheets/src/commands/mutations/__tests__/remove-row-col.mutation.spec.ts@@ -35,13 +35,13 @@ describe('Test moving rows & cols', () => {let get: Injector['get'];const getWorksheet = () => {const univerInstanceService = get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return worksheet;};const getId = () => {const univerInstanceService = get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const worksheet = workbook.getActiveSheet();return {unitId: workbook.getUnitId(),diff --git a/packages/sheets/src/commands/mutations/remove-sheet.mutation.ts b/packages/sheets/src/commands/mutations/remove-sheet.mutation.tsindex 619a344acb..11f1a43f89 100644--- a/packages/sheets/src/commands/mutations/remove-sheet.mutation.ts+++ b/packages/sheets/src/commands/mutations/remove-sheet.mutation.ts@@ -22,6 +22,7 @@ import type {IInsertSheetMutationParams,IRemoveSheetMutationParams,} from '../../basics/interfaces/mutation-interface';+import { getSheetMutationTarget } from '../commands/utils/target-util';/*** Generate undo mutation of a `RemoveSheetMutation`@@ -35,10 +36,16 @@ export const RemoveSheetUndoMutationFactory = (params: IRemoveSheetMutationParams): IInsertSheetMutationParams => {const univerInstanceService = accessor.get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();const { subUnitId, unitId } = params;- const sheet = workbook.getSheetBySheetId(subUnitId)!.getConfig();- const config = workbook!.getConfig();++ const target = getSheetMutationTarget(univerInstanceService, params);+ if (!target) {+ throw new Error('[RemoveSheetUndoMutationFactory]: Worksheet is null error!');+ }++ const { workbook, worksheet } = target;+ const sheet = worksheet.getConfig();+ const config = workbook.getConfig();const index = config.sheetOrder.findIndex((id) => id === subUnitId);return {diff --git a/packages/sheets/src/commands/mutations/set-worksheet-hide.mutation.ts b/packages/sheets/src/commands/mutations/set-worksheet-hide.mutation.tsindex 56f2cd7b5e..64fe17b697 100644--- a/packages/sheets/src/commands/mutations/set-worksheet-hide.mutation.ts+++ b/packages/sheets/src/commands/mutations/set-worksheet-hide.mutation.ts@@ -17,6 +17,7 @@import type { BooleanNumber, IMutation } from '@univerjs/core';import { CommandType, IUniverInstanceService } from '@univerjs/core';import type { IAccessor } from '@wendellhu/redi';+import { getSheetMutationTarget } from '../commands/utils/target-util';export interface ISetWorksheetHideMutationParams {hidden: BooleanNumber;@@ -28,11 +29,12 @@ export const SetWorksheetHideMutationFactory = (accessor: IAccessor,params: ISetWorksheetHideMutationParams): ISetWorksheetHideMutationParams => {- const universheet = accessor.get(IUniverInstanceService).getCurrentUniverSheetInstance();- const worksheet = universheet.getSheetBySheetId(params.subUnitId);- if (worksheet == null) {- throw new Error('worksheet is null error!');+ const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);+ if (!target) {+ throw new Error('[SetWorksheetHideMutationFactory]: worksheet is null error!');}++ const { worksheet } = target;return {hidden: worksheet.isSheetHidden(),unitId: params.unitId,diff --git a/packages/sheets/src/commands/mutations/set-worksheet-name.mutation.ts b/packages/sheets/src/commands/mutations/set-worksheet-name.mutation.tsindex 00b7549b60..5f562cae5f 100644--- a/packages/sheets/src/commands/mutations/set-worksheet-name.mutation.ts+++ b/packages/sheets/src/commands/mutations/set-worksheet-name.mutation.ts@@ -17,6 +17,7 @@import type { IMutation } from '@univerjs/core';import { CommandType, IUniverInstanceService } from '@univerjs/core';import type { IAccessor } from '@wendellhu/redi';+import { getSheetMutationTarget } from '../commands/utils/target-util';export interface ISetWorksheetNameMutationParams {name: string;@@ -28,11 +29,12 @@ export const SetWorksheetNameMutationFactory = (accessor: IAccessor,params: ISetWorksheetNameMutationParams): ISetWorksheetNameMutationParams => {- const universheet = accessor.get(IUniverInstanceService).getCurrentUniverSheetInstance();- const worksheet = universheet.getSheetBySheetId(params.subUnitId);- if (worksheet == null) {- throw new Error('worksheet is null error!');+ const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);+ if (!target) {+ throw new Error('[SetWorksheetNameMutationFactory]: worksheet is null error!');}++ const { worksheet } = target;return {unitId: params.unitId,name: worksheet.getName(),diff --git a/packages/sheets/src/commands/utils/get-target.ts b/packages/sheets/src/commands/utils/get-target.tsdeleted file mode 100644index 7ce695e5d8..0000000000--- a/packages/sheets/src/commands/utils/get-target.ts+++ /dev/null@@ -1,52 +0,0 @@-/**- * Copyright 2023-present DreamNum Inc.- *- * Licensed under the Apache License, Version 2.0 (the "License");- * you may not use this file except in compliance with the License.- * You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */--import type { IUniverInstanceService, Workbook, Worksheet } from '@univerjs/core';--/**- * Get target workbook and worksheet.- * @param univerInstanceService- * @param unitId- * @param subUnitId- *- * @internal- *- * @returns- */-export function getCommandTarget(- univerInstanceService: IUniverInstanceService,- unitId?: string,- subUnitId?: string-): { workbook: Workbook; worksheet: Worksheet; unitId: string; subUnitId: string } {- const workbook = unitId- ? univerInstanceService.getUniverSheetInstance(unitId)- : univerInstanceService.getCurrentUniverSheetInstance();- if (!workbook) {- throw new Error();- }-- const worksheet = subUnitId ? workbook.getSheetBySheetId(subUnitId) : workbook.getActiveSheet();- if (!worksheet) {- throw new Error();- }-- return {- workbook,- worksheet,- unitId: unitId ?? workbook.getUnitId(),- subUnitId: subUnitId ?? worksheet.getSheetId(),- };-}diff --git a/packages/sheets/src/commands/utils/handle-range-mutation.ts b/packages/sheets/src/commands/utils/handle-range-mutation.tsindex a8fc0bebdb..fc5cb5d4ae 100644--- a/packages/sheets/src/commands/utils/handle-range-mutation.ts+++ b/packages/sheets/src/commands/utils/handle-range-mutation.ts@@ -30,6 +30,7 @@ import {SetRangeValuesMutation,SetRangeValuesUndoMutationFactory,} from '../mutations/set-range-values.mutation';+import { getSheetMutationTarget } from '../commands/utils/target-util';/*** Generate undo mutation of a `InsertRangeMutation`@@ -348,10 +349,11 @@ export const DeleteRangeUndoMutationFactory = (params: IDeleteRangeMutationParams): Nullable<IInsertRangeMutationParams> => {const univerInstanceService = accessor.get(IUniverInstanceService);- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getSheetBySheetId(params.subUnitId);- if (!worksheet) return null;- const cellMatrix = worksheet.getCellMatrix();+ const target = getSheetMutationTarget(univerInstanceService, params);+ if (!target) return null;+ const { worksheet } = target;+ const cellMatrix = worksheet.getCellMatrix();const undoData = new ObjectMatrix<ICellData>();const lastEndRow = worksheet.getConfig().rowCount;const lastEndColumn = worksheet.getConfig().columnCount;diff --git a/packages/sheets/src/controllers/defined-name-data.controller.ts b/packages/sheets/src/controllers/defined-name-data.controller.tsindex 760c302861..585697a28f 100644--- a/packages/sheets/src/controllers/defined-name-data.controller.ts+++ b/packages/sheets/src/controllers/defined-name-data.controller.ts@@ -87,7 +87,10 @@ export class DefinedNameDataController extends Disposable {})));- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();- handleWorkbookAdd(workbook);++ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;+ if (workbook) {+ handleWorkbookAdd(workbook);+ }}}diff --git a/packages/sheets/src/controllers/feature-calculation.controller.ts b/packages/sheets/src/controllers/feature-calculation.controller.tsindex cb91d2fbeb..4ad2ab555b 100644--- a/packages/sheets/src/controllers/feature-calculation.controller.ts+++ b/packages/sheets/src/controllers/feature-calculation.controller.ts@@ -31,7 +31,7 @@ export class FeatureCalculationController extends Disposable {@ICommandService private readonly _commandService: ICommandService,@IFeatureCalculationManagerServiceprivate readonly _featureCalculationManagerService: IFeatureCalculationManagerService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@Inject(FormulaDataModel) private readonly _formulaDataModel: FormulaDataModel) {super();diff --git a/packages/sheets/src/controllers/merge-cell.controller.ts b/packages/sheets/src/controllers/merge-cell.controller.tsindex a074991ba2..87a39b9287 100644--- a/packages/sheets/src/controllers/merge-cell.controller.ts+++ b/packages/sheets/src/controllers/merge-cell.controller.ts@@ -140,7 +140,8 @@ export class MergeCellController extends Disposable {switch (commandInfo.id) {case ClearSelectionAllCommand.id:case ClearSelectionFormatCommand.id: {- const workbook = self._univerInstanceService.getCurrentUniverSheetInstance();+ // TODO@Gggpound: get by unit id and subUnitId+ const workbook = self._univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const worksheet = workbook.getActiveSheet();const subUnitId = worksheet.getSheetId();@@ -274,9 +275,11 @@ export class MergeCellController extends Disposable {}));- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();- const sheet = workbook.getActiveSheet();- registerRefRange(workbook.getUnitId(), sheet.getSheetId());+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;+ if (workbook) {+ const sheet = workbook.getActiveSheet();+ registerRefRange(workbook.getUnitId(), sheet.getSheetId());+ }}private _handleMoveRowsCommand(params: IMoveRowsCommandParams, unitId: string, subUnitId: string) {@@ -1178,7 +1181,7 @@ function getWorkbook(univerInstanceService: IUniverInstanceService, unitId?: strif (unitId) {return univerInstanceService.getUniverSheetInstance(unitId);}- return univerInstanceService.getCurrentUniverSheetInstance();+ return univerInstanceService.getCurrentUniverSheetInstance()!;}function getWorksheet(workbook: Workbook, subUnitId?: string) {diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.tsindex 1ca25eed9a..b2d1a2b169 100644--- a/packages/sheets/src/index.ts+++ b/packages/sheets/src/index.ts@@ -42,6 +42,7 @@ export {SELECTION_CONTROL_BORDER_BUFFER_WIDTH,transformCellDataToSelectionData,} from './basics/selection';+export { getSheetCommandTarget, getSheetCommandTargetWorkbook, getSheetMutationTarget } from './commands/commands/utils/target-util';export { alignToMergedCellsBorders, getCellAtRowCol, setEndForRange } from './commands/commands/utils/selection-utils';export { MAX_CELL_PER_SHEET_KEY } from './controllers/config/config';export { BorderStyleManagerService, type IBorderInfo } from './services/border-style-manager.service';diff --git a/packages/sheets/src/services/__tests__/numfmt.service.test.ts b/packages/sheets/src/services/__tests__/numfmt.service.test.tsindex bceb1a2ee0..f90f099809 100644--- a/packages/sheets/src/services/__tests__/numfmt.service.test.ts+++ b/packages/sheets/src/services/__tests__/numfmt.service.test.ts@@ -46,7 +46,7 @@ describe('test numfmt service', () => {it('model set', () => {const univerInstanceService = get(IUniverInstanceService);const numfmtService = get(INumfmtService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const sheet = workbook.getActiveSheet();const unitId = workbook.getUnitId();const subUnitId = sheet.getSheetId();@@ -71,7 +71,7 @@ describe('test numfmt service', () => {it('model delete', () => {const univerInstanceService = get(IUniverInstanceService);const numfmtService = get(INumfmtService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const sheet = workbook.getActiveSheet();const unitId = workbook.getUnitId();const subUnitId = sheet.getSheetId();@@ -102,7 +102,7 @@ describe('test numfmt service', () => {it('ref model set', () => {const univerInstanceService = get(IUniverInstanceService);const numfmtService = get(INumfmtService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const sheet = workbook.getActiveSheet();const unitId = workbook.getUnitId();const subUnitId = sheet.getSheetId();@@ -128,7 +128,7 @@ describe('test numfmt service', () => {it('model delete', () => {const univerInstanceService = get(IUniverInstanceService);const numfmtService = get(INumfmtService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const sheet = workbook.getActiveSheet();const unitId = workbook.getUnitId();const subUnitId = sheet.getSheetId();@@ -158,7 +158,7 @@ describe('test numfmt service', () => {it('model delete', () => {const univerInstanceService = get(IUniverInstanceService);const numfmtService = get(INumfmtService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const unitId = workbook.getUnitId();const sheets = workbook.getSheets();const pattern = 'asdws';diff --git a/packages/sheets/src/services/__tests__/ref-range.setvice.spec.ts b/packages/sheets/src/services/__tests__/ref-range.setvice.spec.tsindex a25b902c32..2a82de4932 100644--- a/packages/sheets/src/services/__tests__/ref-range.setvice.spec.ts+++ b/packages/sheets/src/services/__tests__/ref-range.setvice.spec.ts@@ -65,7 +65,7 @@ describe('Test ref-range.service', () => {);const univerInstanceService = get(IUniverInstanceService);- workbook = univerInstanceService.getCurrentUniverSheetInstance();+ workbook = univerInstanceService.getCurrentUniverSheetInstance()!;worksheet = workbook.getActiveSheet();});afterEach(() => {diff --git a/packages/sheets/src/services/__tests__/sheet-permission.spec.ts b/packages/sheets/src/services/__tests__/sheet-permission.spec.tsindex 4bbe81cd8d..88d397bff5 100644--- a/packages/sheets/src/services/__tests__/sheet-permission.spec.ts+++ b/packages/sheets/src/services/__tests__/sheet-permission.spec.ts@@ -52,7 +52,7 @@ describe('test sheet permission service', () => {const univerInstanceService = get(IUniverInstanceService);const univerPermissionService = get(UniverPermissionService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const sheet = workbook.getActiveSheet();const sheetPermissionService = get(SheetPermissionService);expect(sheetPermissionService.getSheetEditable(workbook.getUnitId(), sheet.getSheetId())).toBe(@@ -72,7 +72,7 @@ describe('test sheet permission service', () => {const univerInstanceService = get(IUniverInstanceService);const univerPermissionService = get(UniverPermissionService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const sheet = workbook.getActiveSheet();const sheetPermissionService = get(SheetPermissionService);expect(sheetPermissionService.getSheetEditable(workbook.getUnitId(), sheet.getSheetId())).toBe(@@ -91,7 +91,7 @@ describe('test sheet permission service', () => {it('test setRangeValue commands', async () => {const univerPermissionService = get(UniverPermissionService);const univerInstanceService = get(IUniverInstanceService);- const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = univerInstanceService.getCurrentUniverSheetInstance()!;const commandService = get(ICommandService);univerPermissionService.setEditable(workbook.getUnitId(), false);diff --git a/packages/sheets/src/services/permission/sheet-permission.service.ts b/packages/sheets/src/services/permission/sheet-permission.service.tsindex fefedc9044..fe19217182 100644--- a/packages/sheets/src/services/permission/sheet-permission.service.ts+++ b/packages/sheets/src/services/permission/sheet-permission.service.ts@@ -46,7 +46,9 @@ export class SheetPermissionService extends Disposable {}private _init() {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;+ if (!workbook) return;+const unitId = workbook.getUnitId();workbook.getSheets().forEach((worksheet) => {const subUnitId = worksheet.getSheetId();@@ -78,7 +80,7 @@ export class SheetPermissionService extends Disposable {this._sheetInterceptorService.intercept(INTERCEPTOR_POINT.PERMISSION, {priority: 99,handler: (_value, commandInfo, next) => {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;const sheet = workbook?.getActiveSheet();const unitId = workbook?.getUnitId();const sheetId = sheet?.getSheetId();@@ -96,8 +98,13 @@ export class SheetPermissionService extends Disposable {);}+ // TODO@Gggpound: should get by unitId instead of the current onegetEditable$(unitId?: string, sheetId?: string) {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;+ if (!workbook) {+ throw new Error('[SheetPermissionService]: trying to get permission from a non-existing sheet.');+ }+const _unitId = unitId || workbook.getUnitId();const sheet = workbook.getActiveSheet();const _sheetId = sheetId || sheet.getSheetId();@@ -114,7 +121,9 @@ export class SheetPermissionService extends Disposable {}getSheetEditable(unitId?: string, sheetId?: string) {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;+ if (!workbook) return false;+const _unitId = unitId || workbook.getUnitId();const sheet = workbook.getActiveSheet();const _sheetId = sheetId || sheet.getSheetId();@@ -125,7 +134,9 @@ export class SheetPermissionService extends Disposable {}setSheetEditable(v: boolean, unitId?: string, sheetId?: string) {- const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;+ if (!workbook) return;+const _unitId = unitId || workbook.getUnitId();const sheet = workbook.getActiveSheet();const _sheetId = sheetId || sheet.getSheetId();diff --git a/packages/sheets/src/services/permission/tool.ts b/packages/sheets/src/services/permission/tool.tsindex e964351f7a..ced3fee7d5 100644--- a/packages/sheets/src/services/permission/tool.ts+++ b/packages/sheets/src/services/permission/tool.ts@@ -23,8 +23,9 @@ import { SheetPermissionService } from './sheet-permission.service';export function getCurrentSheetDisabled$(accessor: IAccessor) {const univerInstanceService = accessor.get(IUniverInstanceService);const sheetPermissionService = accessor.get(SheetPermissionService);- const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();- const sheetId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();++ const unitId = univerInstanceService.getCurrentUniverSheetInstance()?.getUnitId();+ const sheetId = univerInstanceService.getCurrentUniverSheetInstance()?.getActiveSheet().getSheetId();return new Observable<boolean>((subscriber) => {const permission$ = sheetPermissionService.getEditable$(unitId, sheetId)?.subscribe((e) => {diff --git a/packages/sheets/src/services/ref-range/ref-range.service.ts b/packages/sheets/src/services/ref-range/ref-range.service.tsindex aa9f7773a3..1125e6242a 100644--- a/packages/sheets/src/services/ref-range/ref-range.service.ts+++ b/packages/sheets/src/services/ref-range/ref-range.service.ts@@ -144,16 +144,16 @@ export class RefRangeService extends Disposable {private _onRefRangeChange = () => {this._sheetInterceptorService.interceptCommand({getMutations: (command: EffectRefRangeParams) => {- const workSheet = this._univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();- const unitId = getunitId(this._univerInstanceService);- const subUnitId = getsubUnitId(this._univerInstanceService);+ const worksheet = this._univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet();+ const unitId = getUnitId(this._univerInstanceService);+ const subUnitId = getSubUnitId(this._univerInstanceService);const getEffectsCbList = () => {switch (command.id) {case EffectRefRangId.MoveColsCommandId: {const params = command.params!;const startColumn = Math.min(params.fromRange.startColumn, params.toRange.startColumn);return this._checkRange(- [{ ...params.fromRange, startColumn, endColumn: workSheet.getColumnCount() - 1 }],+ [{ ...params.fromRange, startColumn, endColumn: worksheet.getColumnCount() - 1 }],unitId,subUnitId);@@ -163,7 +163,7 @@ export class RefRangeService extends Disposable {const startRow = Math.min(params.fromRange.startRow, params.toRange.startRow);return this._checkRange(- [{ ...params.fromRange, startRow, endRow: workSheet.getRowCount() - 1 }],+ [{ ...params.fromRange, startRow, endRow: worksheet.getRowCount() - 1 }],unitId,subUnitId);@@ -181,9 +181,9 @@ export class RefRangeService extends Disposable {const rowStart = params.params!.range.startRow;const range: IRange = {startRow: rowStart,- endRow: workSheet.getRowCount() - 1,+ endRow: worksheet.getRowCount() - 1,startColumn: 0,- endColumn: workSheet.getColumnCount() - 1,+ endColumn: worksheet.getColumnCount() - 1,};return this._checkRange([range], unitId, subUnitId);}@@ -192,9 +192,9 @@ export class RefRangeService extends Disposable {const colStart = params.params!.range.startColumn;const range: IRange = {startRow: 0,- endRow: workSheet.getRowCount() - 1,+ endRow: worksheet.getRowCount() - 1,startColumn: colStart,- endColumn: workSheet.getColumnCount() - 1,+ endColumn: worksheet.getColumnCount() - 1,};return this._checkRange([range], unitId, subUnitId);}@@ -204,9 +204,9 @@ export class RefRangeService extends Disposable {const rowStart = params.params!.range.startRow;const range: IRange = {startRow: rowStart,- endRow: workSheet.getRowCount() - 1,+ endRow: worksheet.getRowCount() - 1,startColumn: 0,- endColumn: workSheet.getColumnCount() - 1,+ endColumn: worksheet.getColumnCount() - 1,};return this._checkRange([range], unitId, subUnitId);}@@ -215,9 +215,9 @@ export class RefRangeService extends Disposable {const colStart = params.params!.range.startColumn;const range: IRange = {startRow: 0,- endRow: workSheet.getRowCount() - 1,+ endRow: worksheet.getRowCount() - 1,startColumn: colStart,- endColumn: workSheet.getColumnCount() - 1,+ endColumn: worksheet.getColumnCount() - 1,};return this._checkRange([range], unitId, subUnitId);}@@ -229,7 +229,7 @@ export class RefRangeService extends Disposable {startRow: range.startRow,startColumn: range.startColumn,endColumn: range.endColumn,- endRow: workSheet.getRowCount() - 1,+ endRow: worksheet.getRowCount() - 1,};return this._checkRange([effectRange], unitId, subUnitId);}@@ -240,7 +240,7 @@ export class RefRangeService extends Disposable {const effectRange = {startRow: range.startRow,startColumn: range.startColumn,- endColumn: workSheet.getColumnCount() - 1,+ endColumn: worksheet.getColumnCount() - 1,endRow: range.endRow,};return this._checkRange([effectRange], unitId, subUnitId);@@ -333,8 +333,8 @@ export class RefRangeService extends Disposable {_unitId?: string,_subUnitId?: string): IDisposable => {- const unitId = _unitId || getunitId(this._univerInstanceService);- const subUnitId = _subUnitId || getsubUnitId(this._univerInstanceService);+ const unitId = _unitId || getUnitId(this._univerInstanceService);+ const subUnitId = _subUnitId || getSubUnitId(this._univerInstanceService);const refRangeManagerId = getRefRangId(unitId, subUnitId);const rangeString = this._serializer.serialize(range);@@ -365,12 +365,12 @@ export class RefRangeService extends Disposable {};}-function getunitId(univerInstanceService: IUniverInstanceService) {- return univerInstanceService.getCurrentUniverSheetInstance().getUnitId();+function getUnitId(univerInstanceService: IUniverInstanceService) {+ return univerInstanceService.getCurrentUniverSheetInstance()!.getUnitId();}-function getsubUnitId(univerInstanceService: IUniverInstanceService) {- return univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+function getSubUnitId(univerInstanceService: IUniverInstanceService) {+ return univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet().getSheetId();}function getSelectionRanges(selectionManagerService: SelectionManagerService) {diff --git a/packages/sheets/src/services/sheet-interceptor/__tests__/sheet-interceptor.service.spec.ts b/packages/sheets/src/services/sheet-interceptor/__tests__/sheet-interceptor.service.spec.tsindex b957e39175..6a1243631c 100644--- a/packages/sheets/src/services/sheet-interceptor/__tests__/sheet-interceptor.service.spec.ts+++ b/packages/sheets/src/services/sheet-interceptor/__tests__/sheet-interceptor.service.spec.ts@@ -40,7 +40,7 @@ describe('Test SheetInterceptorService', () => {function getCell(row: number, col: number): Nullable<ICellData> {const cus = get(IUniverInstanceService);- const sheet = cus.getCurrentUniverSheetInstance().getActiveSheet()!;+ const sheet = cus.getCurrentUniverSheetInstance()!.getActiveSheet()!;return sheet.getCell(row, col);}@@ -157,12 +157,12 @@ describe('Test SheetInterceptorService', () => {// it('Test SheetInterceptorService', () => {// it('getLastRowWithContent', () => {// const univerInstanceService = get(IUniverInstanceService);- // const sheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet()!;+ // const sheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet()!;// expect(sheet.getLastRowWithContent()).toBe(3);// });// it('getLastColumnWithContent', () => {// const univerInstanceService = get(IUniverInstanceService);- // const sheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet()!;+ // const sheet = univerInstanceService.getCurrentUniverSheetInstance()!.getActiveSheet()!;// expect(sheet.getLastColumnWithContent()).toBe(3);// });// });diff --git a/packages/sheets/src/services/sheet-interceptor/sheet-interceptor.service.ts b/packages/sheets/src/services/sheet-interceptor/sheet-interceptor.service.tsindex 3a5ecdf371..20ed257965 100644--- a/packages/sheets/src/services/sheet-interceptor/sheet-interceptor.service.ts+++ b/packages/sheets/src/services/sheet-interceptor/sheet-interceptor.service.ts@@ -55,21 +55,21 @@ export class SheetInterceptorService extends Disposable {private readonly _workbookDisposables = new Map<string, IDisposable>();private readonly _worksheetDisposables = new Map<string, IDisposable>();- constructor(@IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService) {+ constructor(@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService) {super();// When a workbook is created or a worksheet is added after when workbook is created,// `SheetInterceptorService` inject interceptors to worksheet instances to it.this.disposeWithMe(toDisposable(- this._currentUniverService.sheetAdded$.subscribe((workbook) => {+ this._univerInstanceService.sheetAdded$.subscribe((workbook) => {this._interceptWorkbook(workbook);})));this.disposeWithMe(toDisposable(- this._currentUniverService.sheetDisposed$.subscribe((workbook) =>+ this._univerInstanceService.sheetDisposed$.subscribe((workbook) =>this._disposeWorkbookInterceptor(workbook)))diff --git a/packages/slides-ui/src/slides-ui-plugin.ts b/packages/slides-ui/src/slides-ui-plugin.tsindex a1e3fdfd0c..ceb770586a 100644--- a/packages/slides-ui/src/slides-ui-plugin.ts+++ b/packages/slides-ui/src/slides-ui-plugin.ts@@ -30,7 +30,7 @@ export class UniverSlidesUIPlugin extends Plugin {_config: unknown,@Inject(Injector) override readonly _injector: Injector,@Inject(LocaleService) private readonly _localeService: LocaleService,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService) {super(SLIDE_UI_PLUGIN_NAME);}@@ -48,10 +48,10 @@ export class UniverSlidesUIPlugin extends Plugin {}private _markSlideAsFocused() {- const currentService = this._currentUniverService;+ const currentService = this._univerInstanceService;try {- const c = currentService.getCurrentUniverSlideInstance();- currentService.focusUniverInstance(c.getUnitId());+ const slide = currentService.getCurrentUniverSlideInstance()!;+ currentService.focusUniverInstance(slide.getUnitId());} catch (e) {}}diff --git a/packages/slides/src/slides-plugin.ts b/packages/slides/src/slides-plugin.tsindex 9b9282418a..af56f3022e 100644--- a/packages/slides/src/slides-plugin.ts+++ b/packages/slides/src/slides-plugin.ts@@ -40,7 +40,7 @@ export class UniverSlidesPlugin extends Plugin {constructor(config: Partial<IUniverSlidesConfig> = {},- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@Inject(LocaleService) private readonly _localeService: LocaleService,@Inject(Injector) override readonly _injector: Injector) {diff --git a/packages/slides/src/views/render/canvas-view.ts b/packages/slides/src/views/render/canvas-view.tsindex 2f3ef4e96e..9a858d8f01 100644--- a/packages/slides/src/views/render/canvas-view.ts+++ b/packages/slides/src/views/render/canvas-view.ts@@ -43,7 +43,7 @@ export class CanvasView extends RxDisposable {private _activePageId: string = '';constructor(- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@Inject(Injector) private readonly _injector: Injector,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService) {@@ -57,17 +57,17 @@ export class CanvasView extends RxDisposable {this._create(unitId);});- this._currentUniverService.currentSlide$.pipe(takeUntil(this.dispose$)).subscribe((slideModel) => {+ this._univerInstanceService.currentSlide$.pipe(takeUntil(this.dispose$)).subscribe((slideModel) => {this._create(slideModel?.getUnitId());});- this._currentUniverService.getAllUniverSlidesInstance().forEach((slideModel) => {+ this._univerInstanceService.getAllUniverSlidesInstance().forEach((slideModel) => {this._create(slideModel.getUnitId());});}activePage(pageId?: string) {- const model = this._currentUniverService.getCurrentUniverSlideInstance();+ const model = this._univerInstanceService.getCurrentUniverSlideInstance()!;let page: Nullable<ISlidePage>;if (pageId) {page = model.getPage(pageId);@@ -122,7 +122,7 @@ export class CanvasView extends RxDisposable {return;}- const model = this._currentUniverService.getUniverSlideInstance(unitId);+ const model = this._univerInstanceService.getUniverSlideInstance(unitId);if (model == null) {return;@@ -134,12 +134,12 @@ export class CanvasView extends RxDisposable {}private _currentRender() {- const slideDataModel = this._currentUniverService.getCurrentUniverSlideInstance();+ const slideDataModel = this._univerInstanceService.getCurrentUniverSlideInstance()!;return this._renderManagerService.getRenderById(slideDataModel.getUnitId());}private _addNewRender(unitId: string) {- const slideDataModel = this._currentUniverService.getUniverSlideInstance(unitId);+ const slideDataModel = this._univerInstanceService.getUniverSlideInstance(unitId);if (slideDataModel == null) {return;@@ -259,7 +259,7 @@ export class CanvasView extends RxDisposable {}, 300);private _createSlide(mainScene: Scene) {- const model = this._currentUniverService.getCurrentUniverSlideInstance();+ const model = this._univerInstanceService.getCurrentUniverSlideInstance()!;const { width: sceneWidth, height: sceneHeight } = mainScene;@@ -285,7 +285,7 @@ export class CanvasView extends RxDisposable {}private _addBackgroundRect(scene: Scene, fill: IColorStyle) {- const model = this._currentUniverService.getCurrentUniverSlideInstance();+ const model = this._univerInstanceService.getCurrentUniverSlideInstance()!;const pageSize = model.getPageSize();@@ -377,7 +377,7 @@ export class CanvasView extends RxDisposable {}createThumbs() {- const slideDataModel = this._currentUniverService.getCurrentUniverSlideInstance();+ const slideDataModel = this._univerInstanceService.getCurrentUniverSlideInstance()!;const pageOrder = slideDataModel.getPageOrder();diff --git a/packages/ui/src/components/range-selector/RangeSelector.tsx b/packages/ui/src/components/range-selector/RangeSelector.tsxindex 7a37608898..c25a8777f3 100644--- a/packages/ui/src/components/range-selector/RangeSelector.tsx+++ b/packages/ui/src/components/range-selector/RangeSelector.tsx@@ -81,7 +81,7 @@ export function RangeSelector(props: IRangeSelectorProps) {const rangeSelectorService = useDependency(IRangeSelectorService);- const currentUniverService = useDependency(IUniverInstanceService);+ const univerInstanceService = useDependency(IUniverInstanceService);const [selectorVisible, setSelectorVisible] = useState(false);@@ -247,7 +247,7 @@ export function RangeSelector(props: IRangeSelectorProps) {}function getSheetIdByName(name: string) {- return currentUniverService.getCurrentUniverSheetInstance().getSheetBySheetName(name)?.getSheetId() || '';+ return univerInstanceService.getCurrentUniverSheetInstance()?.getSheetBySheetName(name)?.getSheetId() || '';}function handleTextValueChange(value: Nullable<string>) {diff --git a/packages/ui/src/services/editor/editor.service.ts b/packages/ui/src/services/editor/editor.service.tsindex f97a85c9a3..6809524baa 100644--- a/packages/ui/src/services/editor/editor.service.ts+++ b/packages/ui/src/services/editor/editor.service.ts@@ -254,10 +254,10 @@ export interface IEditorService {*/setOperationSheetUnitId(unitId: Nullable<string>): void;getOperationSheetUnitId(): Nullable<string>;- /**- * The sub-table within the sheet currently being operated on- * will determine whether to include subUnitId information in the ref.- */+ /**+ * The sub-table within the sheet currently being operated on+ * will determine whether to include subUnitId information in the ref.+ */setOperationSheetSubUnitId(sheetId: Nullable<string>): void;getOperationSheetSubUnitId(): Nullable<string>;@@ -353,7 +353,7 @@ export class EditorService extends Disposable implements IEditorService, IDisposprivate _spreadsheetFocusState: boolean = false;constructor(- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,@Inject(LexerTreeBuilder) private readonly _lexerTreeBuilder: LexerTreeBuilder,@IContextService private readonly _contextService: IContextService@@ -385,7 +385,11 @@ export class EditorService extends Disposable implements IEditorService, IDispos}closeRangePrompt() {- const documentDataModel = this._currentUniverService.getCurrentUniverDocInstance();+ const documentDataModel = this._univerInstanceService.getCurrentUniverDocInstance();+ if (!documentDataModel) {+ return;+ }+const editorUnitId = documentDataModel.getUnitId();this._contextService.setContextValue(EDITOR_ACTIVATED, false);@@ -434,7 +438,7 @@ export class EditorService extends Disposable implements IEditorService, IDispos}selectionChangingState() {- // const documentDataModel = this._currentUniverService.getCurrentUniverDocInstance();+ // const documentDataModel = this._univerInstanceService.getCurrentUniverDocInstance();const editorUnitId = this.getFocusId();if (editorUnitId == null) {return true;@@ -478,17 +482,20 @@ export class EditorService extends Disposable implements IEditorService, IDisposfocus(editorUnitId?: string) {if (editorUnitId == null) {- const documentDataModel = this._currentUniverService.getCurrentUniverDocInstance();+ const documentDataModel = this._univerInstanceService.getCurrentUniverDocInstance();+ if (!documentDataModel) {+ return;+ }+editorUnitId = documentDataModel.getUnitId();}const editor = this.getEditor(editorUnitId);-if (editor == null) {return;}- this._currentUniverService.setCurrentUniverDocInstance(editorUnitId);+ this._univerInstanceService.setCurrentUniverDocInstance(editorUnitId);const valueCount = editor.getValue().length;@@ -518,9 +525,12 @@ export class EditorService extends Disposable implements IEditorService, IDispos}setValueNoRefresh(val: string, editorUnitId: string) {- this._setValue$.next({ body: {- dataStream: val,- }, editorUnitId });+ this._setValue$.next({+ body: {+ dataStream: val,+ },+ editorUnitId,+ });this.resize(editorUnitId);}@@ -606,7 +616,7 @@ export class EditorService extends Disposable implements IEditorService, IDisposregister(config: IEditorConfigParam, container: HTMLDivElement): IDisposable {const { initialSnapshot, editorUnitId, canvasStyle = {} } = config;- const documentDataModel = this._currentUniverService.createDoc(initialSnapshot || this._getBlank(editorUnitId));+ const documentDataModel = this._univerInstanceService.createDoc(initialSnapshot || this._getBlank(editorUnitId));let render = this._renderManagerService.getRenderById(editorUnitId);@@ -647,16 +657,16 @@ export class EditorService extends Disposable implements IEditorService, IDisposthis._editors.delete(editorUnitId);- this._currentUniverService.disposeDocument(editorUnitId);+ this._univerInstanceService.disposeDocument(editorUnitId);/*** Compatible with the editor in the sheet scenario,* it is necessary to refocus back to the current sheet when unloading.*/- const sheets = this._currentUniverService.getAllUniverSheetsInstance();+ const sheets = this._univerInstanceService.getAllUniverSheetsInstance();if (sheets.length > 0) {- const current = this._currentUniverService.getCurrentUniverSheetInstance();- this._currentUniverService.focusUniverInstance(current.getUnitId());+ const current = this._univerInstanceService.getCurrentUniverSheetInstance()!;+ this._univerInstanceService.focusUniverInstance(current.getUnitId());}}@@ -713,7 +723,7 @@ export class EditorService extends Disposable implements IEditorService, IDispos}private _getCurrentEditorUnitId() {- const current = this._currentUniverService.getCurrentUniverDocInstance();+ const current = this._univerInstanceService.getCurrentUniverDocInstance()!;return current.getUnitId();}