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(ui): invoke onOpen callback when sidebar is opened (#6636)

刘义瑞804206b8aa78
diff --git a/packages/ui/src/services/sidebar/desktop-sidebar.service.ts b/packages/ui/src/services/sidebar/desktop-sidebar.service.ts
index 798bbf2853..a700374f46 100644
--- a/packages/ui/src/services/sidebar/desktop-sidebar.service.ts
+++ b/packages/ui/src/services/sidebar/desktop-sidebar.service.ts
@@ -17,16 +17,17 @@
import type { IDisposable } from '@univerjs/core';
import type { ISidebarMethodOptions } from '../../views/components/sidebar/Sidebar';
import type { ISidebarService } from './sidebar.service';
-import { toDisposable } from '@univerjs/core';
+import { Disposable, toDisposable } from '@univerjs/core';
import { Subject } from 'rxjs';
-export class DesktopSidebarService implements ISidebarService {
+export class DesktopSidebarService extends Disposable implements ISidebarService {
private _sidebarOptions: ISidebarMethodOptions = {};
readonly sidebarOptions$ = new Subject<ISidebarMethodOptions>();
readonly scrollEvent$ = new Subject<Event>();
private _container?: HTMLElement;
+ private _openAnimationFrameId: number | null = null;
get visible(): boolean {
return this._sidebarOptions.visible || false;
@@ -36,6 +37,20 @@ export class DesktopSidebarService implements ISidebarService {
return this._sidebarOptions;
}
+ override dispose(): void {
+ super.dispose();
+ this.close();
+ this.sidebarOptions$.complete();
+ this.scrollEvent$.complete();
+ }
+
+ private _clearPendingOpenFrame() {
+ if (this._openAnimationFrameId !== null) {
+ cancelAnimationFrame(this._openAnimationFrameId);
+ this._openAnimationFrameId = null;
+ }
+ }
+
open(params: ISidebarMethodOptions): IDisposable {
this._sidebarOptions = {
...params,
@@ -44,6 +59,11 @@ export class DesktopSidebarService implements ISidebarService {
};
this.sidebarOptions$.next(this._sidebarOptions);
+ this._clearPendingOpenFrame();
+ this._openAnimationFrameId = requestAnimationFrame(() => {
+ this._openAnimationFrameId = null;
+ this._sidebarOptions.onOpen && this._sidebarOptions.onOpen();
+ });
return toDisposable(() => {
this.close();
@@ -54,12 +74,14 @@ export class DesktopSidebarService implements ISidebarService {
if (id && this._sidebarOptions.id !== id) {
return;
}
+
+ this._clearPendingOpenFrame();
this._sidebarOptions = {
...this._sidebarOptions,
visible: false,
};
this.sidebarOptions$.next(this._sidebarOptions);
- this._sidebarOptions.onClose && this._sidebarOptions.onClose();
+ this._sidebarOptions.onClose?.();
}
getContainer() {
diff --git a/packages/ui/src/views/components/sidebar/Sidebar.tsx b/packages/ui/src/views/components/sidebar/Sidebar.tsx
index 728d682c35..600371c970 100644
--- a/packages/ui/src/views/components/sidebar/Sidebar.tsx
+++ b/packages/ui/src/views/components/sidebar/Sidebar.tsx
@@ -34,7 +34,7 @@ export interface ISidebarMethodOptions {
width?: number | string;
- onClose?: () => void;
+ onClose?: (id?: string) => void;
onOpen?: () => void;
}
@@ -103,15 +103,9 @@ export function Sidebar() {
}, [options]);
function handleClose() {
- const options = {
- ...sidebarOptions,
- visible: false,
- };
-
- sidebarService.options.visible = false;
- sidebarService.sidebarOptions$.next(options);
- options?.onClose?.();
+ sidebarService.close(sidebarOptions?.id);
}
+
return (
<section
data-u-comp="sidebar"