Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(disposeUnit): fix the component reported an error after disposeUnit (#6476)

wpxp123456f4d7d03ec36f
diff --git a/packages/docs-ui/src/views/count-bar/ZoomSlider.tsx b/packages/docs-ui/src/views/count-bar/ZoomSlider.tsx
index b4664287c0..6a624e729c 100644
--- a/packages/docs-ui/src/views/count-bar/ZoomSlider.tsx
+++ b/packages/docs-ui/src/views/count-bar/ZoomSlider.tsx
@@ -36,7 +36,7 @@ export function ZoomSlider() {
return Math.round(currentZoom);
}, [documentDataModel]);
- const [zoom, setZoom] = useState(() => getCurrentZoom());
+ const [zoom, setZoom] = useState<number>(100);
useEffect(() => {
setZoom(getCurrentZoom());
diff --git a/packages/sheets-formula/src/controllers/defined-name.controller.ts b/packages/sheets-formula/src/controllers/defined-name.controller.ts
index ef988c4e16..685f1cdd92 100644
--- a/packages/sheets-formula/src/controllers/defined-name.controller.ts
+++ b/packages/sheets-formula/src/controllers/defined-name.controller.ts
@@ -65,9 +65,11 @@ export class DefinedNameController extends Disposable {
private _changeUnitListener() {
toDisposable(
- this._univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe(() => {
+ this._univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
this._unRegisterDescriptions();
- this._registerDescriptions();
+ if (workbook) {
+ this._registerDescriptions();
+ }
})
);
}
@@ -97,11 +99,10 @@ export class DefinedNameController extends Disposable {
}
private _registerDescription(param: ISetDefinedNameMutationParam) {
- const { unitId, sheetId } = this._getUnitIdAndSheetId();
+ const target = this._getUnitIdAndSheetId(param);
+ if (!target) return;
- if (unitId == null || sheetId == null) {
- return;
- }
+ const { sheetId } = target;
const { name, comment, formulaOrRefString, localSheetId } = param;
if (!this._descriptionService.hasDescription(name) && (localSheetId == null || localSheetId === SCOPE_WORKBOOK_VALUE_DEFINED_NAME || localSheetId === sheetId)) {
@@ -141,16 +142,18 @@ export class DefinedNameController extends Disposable {
this._preUnitId = null;
}
- private _getUnitIdAndSheetId() {
- const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
- if (workbook == null) {
- return {};
- }
- const worksheet = workbook.getActiveSheet();
+ private _getUnitIdAndSheetId(params: { unitId?: string; subUnitId?: string } = {}) {
+ const { unitId, subUnitId } = params;
- if (worksheet == null) {
- return {};
- }
+ const workbook = unitId
+ ? this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)
+ : this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET);
+ if (!workbook) return null;
+
+ const worksheet = subUnitId
+ ? workbook.getSheetBySheetId(subUnitId)
+ : workbook.getActiveSheet(true);
+ if (!worksheet) return null;
return {
unitId: workbook.getUnitId(),
@@ -159,11 +162,10 @@ export class DefinedNameController extends Disposable {
}
private _registerDescriptions() {
- const { unitId, sheetId } = this._getUnitIdAndSheetId();
+ const target = this._getUnitIdAndSheetId();
+ if (!target) return;
- if (unitId == null || sheetId == null) {
- return;
- }
+ const { unitId, sheetId } = target;
const definedNames = this._definedNamesService.getDefinedNameMap(unitId);
if (!definedNames) {
@@ -191,11 +193,10 @@ export class DefinedNameController extends Disposable {
}
private _unregisterDescriptionsForNotInSheetId() {
- const { unitId, sheetId } = this._getUnitIdAndSheetId();
+ const target = this._getUnitIdAndSheetId();
+ if (!target) return;
- if (unitId == null || sheetId == null) {
- return;
- }
+ const { unitId, sheetId } = target;
const definedNames = this._definedNamesService.getDefinedNameMap(unitId);
if (!definedNames) {
diff --git a/packages/sheets-formula/src/controllers/super-table.controller.ts b/packages/sheets-formula/src/controllers/super-table.controller.ts
index b12930cbba..828c973cca 100644
--- a/packages/sheets-formula/src/controllers/super-table.controller.ts
+++ b/packages/sheets-formula/src/controllers/super-table.controller.ts
@@ -65,9 +65,11 @@ export class SuperTableController extends Disposable {
private _changeUnitListener() {
toDisposable(
- this._univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe(() => {
+ this._univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
this._unRegisterDescriptions();
- this._registerDescriptions();
+ if (workbook) {
+ this._registerDescriptions();
+ }
})
);
}
@@ -97,11 +99,10 @@ export class SuperTableController extends Disposable {
}
private _registerDescription(param: ISetSuperTableMutationParam) {
- const { unitId, sheetId } = this._getUnitIdAndSheetId();
+ const target = this._getUnitIdAndSheetId(param);
+ if (!target) return;
- if (unitId == null || sheetId == null) {
- return;
- }
+ const { unitId } = target;
const { tableName, reference } = param;
if (!this._descriptionService.hasDescription(tableName)) {
@@ -142,16 +143,18 @@ export class SuperTableController extends Disposable {
this._preUnitId = null;
}
- private _getUnitIdAndSheetId() {
- const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
- if (workbook == null) {
- return {};
- }
- const worksheet = workbook.getActiveSheet();
+ private _getUnitIdAndSheetId(params: { unitId?: string; subUnitId?: string } = {}) {
+ const { unitId, subUnitId } = params;
- if (worksheet == null) {
- return {};
- }
+ const workbook = unitId
+ ? this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)
+ : this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET);
+ if (!workbook) return null;
+
+ const worksheet = subUnitId
+ ? workbook.getSheetBySheetId(subUnitId)
+ : workbook.getActiveSheet(true);
+ if (!worksheet) return null;
return {
unitId: workbook.getUnitId(),
@@ -160,11 +163,10 @@ export class SuperTableController extends Disposable {
}
private _registerDescriptions() {
- const { unitId, sheetId } = this._getUnitIdAndSheetId();
+ const target = this._getUnitIdAndSheetId();
+ if (!target) return;
- if (unitId == null || sheetId == null) {
- return;
- }
+ const { unitId } = target;
const superTables = this._superTableService.getTableMap(unitId);
if (!superTables) {
@@ -193,11 +195,10 @@ export class SuperTableController extends Disposable {
}
private _unregisterDescriptionsForNotInSheetId() {
- const { unitId, sheetId } = this._getUnitIdAndSheetId();
+ const target = this._getUnitIdAndSheetId();
+ if (!target) return;
- if (unitId == null || sheetId == null) {
- return;
- }
+ const { unitId } = target;
const superTables = this._superTableService.getTableMap(unitId);
if (!superTables) {
diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
index f942e9f597..1314e266ce 100644
--- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
+++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
@@ -172,14 +172,17 @@ export class EditingRenderController extends Disposable {
private _initEditorVisibilityListener(): void {
this.disposeWithMe(this._univerInstanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_SHEET).subscribe(async (unit) => {
- if (this._editingUnit && unit?.getUnitId() !== this._editingUnit && !this._editorBridgeService.isForceKeepVisible()) {
+ if (this._editingUnit && !this._editorBridgeService.isForceKeepVisible() && (!unit || unit.getUnitId() !== this._editingUnit)) {
this._commandService.syncExecuteCommand(SetCellEditVisibleOperation.id, {
visible: false,
eventType: DeviceInputEventType.Keyboard,
keycode: KeyCode.ESC,
unitId: this._editingUnit,
});
- const editorBridgeRenderController = this._renderManagerService.getRenderById(unit!.getUnitId())?.with(EditorBridgeRenderController);
+
+ if (!unit) return;
+
+ const editorBridgeRenderController = this._renderManagerService.getRenderById(unit.getUnitId())?.with(EditorBridgeRenderController);
if (editorBridgeRenderController) {
editorBridgeRenderController.refreshEditorPosition();
}
diff --git a/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx b/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
index ccf5c5da3e..5d9777bb17 100644
--- a/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
+++ b/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
@@ -35,18 +35,16 @@ import { DefinedNameInput } from './DefinedNameInput';
export const DefinedNameContainer = () => {
const commandService = useDependency(ICommandService);
const univerInstanceService = useDependency(IUniverInstanceService);
- const workbook = univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
const localeService = useDependency(LocaleService);
const definedNamesService = useDependency(IDefinedNamesService);
const selectionManagerService = useDependency(SheetsSelectionsService);
- if (workbook == null) {
- return;
- }
-
- const unitId = workbook.getUnitId();
-
+ const workbook = univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET);
+ const unitId = workbook?.getUnitId();
const getDefinedNameMap = () => {
+ if (!unitId) {
+ return [];
+ }
const definedNameMap = definedNamesService.getDefinedNameMap(unitId);
if (definedNameMap) {
return Array.from(Object.values(definedNameMap));
@@ -55,11 +53,13 @@ export const DefinedNameContainer = () => {
};
const [editState, setEditState] = useState(false);
- const [definedNames, setDefinedNames] = useState<IDefinedNamesServiceParam[]>(getDefinedNameMap());
+ const [definedNames, setDefinedNames] = useState<IDefinedNamesServiceParam[]>([]);
const [editorKey, setEditorKey] = useState<Nullable<string>>(null);
const [deleteConformKey, setDeleteConformKey] = useState<Nullable<string>>();
useEffect(() => {
+ setDefinedNames(getDefinedNameMap());
+
const definedNamesSubscription = definedNamesService.update$.subscribe(() => {
setDefinedNames(getDefinedNameMap());
});
@@ -69,6 +69,10 @@ export const DefinedNameContainer = () => {
};
}, []);
+ if (!workbook || !unitId) {
+ return;
+ }
+
const insertConfirm = (param: IDefinedNamesServiceParam) => {
const { name, formulaOrRefString, comment, localSheetId, hidden } = param;
@@ -93,6 +97,7 @@ export const DefinedNameContainer = () => {
}
function handleDeleteConfirm(id: string) {
+ if (!unitId) return;
const item = definedNamesService.getValueById(unitId, id);
commandService.executeCommand(RemoveDefinedNameCommand.id, { ...item, unitId });
setDeleteConformKey(null);