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.

feat: sidebar-service supports scroll-event subscription (#2724)

YuHong2da550df5950
diff --git a/packages/ui/src/services/sidebar/desktop-sidebar.service.ts b/packages/ui/src/services/sidebar/desktop-sidebar.service.ts
index 714adf5627..6fafe6e316 100644
--- a/packages/ui/src/services/sidebar/desktop-sidebar.service.ts
+++ b/packages/ui/src/services/sidebar/desktop-sidebar.service.ts
@@ -25,6 +25,8 @@ export class DesktopSidebarService implements ISidebarService {
private _sidebarOptions: ISidebarMethodOptions = {};
readonly sidebarOptions$ = new Subject<ISidebarMethodOptions>();
+ readonly scrollEvent$ = new Subject<Event>();
+
open(params: ISidebarMethodOptions): IDisposable {
this._sidebarOptions = {
...params,
diff --git a/packages/ui/src/services/sidebar/sidebar.service.ts b/packages/ui/src/services/sidebar/sidebar.service.ts
index 4b6303a7c7..6fb05b6d4e 100644
--- a/packages/ui/src/services/sidebar/sidebar.service.ts
+++ b/packages/ui/src/services/sidebar/sidebar.service.ts
@@ -24,6 +24,7 @@ export const ISidebarService = createIdentifier<ISidebarService>('univer.sidebar
export interface ISidebarService {
readonly sidebarOptions$: Subject<ISidebarMethodOptions>;
+ readonly scrollEvent$: Subject<Event>;
open(params: ISidebarMethodOptions): IDisposable;
close(): void;
diff --git a/packages/ui/src/views/components/sidebar/Sidebar.tsx b/packages/ui/src/views/components/sidebar/Sidebar.tsx
index 1d85768f22..6005bf8d48 100644
--- a/packages/ui/src/views/components/sidebar/Sidebar.tsx
+++ b/packages/ui/src/views/components/sidebar/Sidebar.tsx
@@ -17,7 +17,7 @@
import { CloseSingle } from '@univerjs/icons';
import { useDependency } from '@wendellhu/redi/react-bindings';
import clsx from 'clsx';
-import React, { useMemo } from 'react';
+import React, { useEffect, useMemo, useRef } from 'react';
import { CustomLabel } from '../../../components/custom-label/CustomLabel';
import { ISidebarService } from '../../../services/sidebar/sidebar.service';
import { useObservable } from '../../../components/hooks/observable';
@@ -27,6 +27,7 @@ import type { ISidebarMethodOptions } from './interface';
export function Sidebar() {
const sidebarService = useDependency(ISidebarService);
const sidebarOptions = useObservable<ISidebarMethodOptions>(sidebarService.sidebarOptions$);
+ const scrollRef = useRef<HTMLDivElement>(null);
const options = useMemo(() => {
if (!sidebarOptions) {
@@ -54,6 +55,20 @@ export function Sidebar() {
return copy;
}, [sidebarOptions]);
+ useEffect(() => {
+ const handleScroll = (e: Event) => {
+ sidebarService.scrollEvent$.next(e);
+ };
+ const scrollElement = scrollRef.current;
+ if (scrollElement) {
+ scrollElement.addEventListener('scroll', handleScroll);
+ }
+
+ return () => {
+ scrollElement?.removeEventListener('scroll', handleScroll);
+ };
+ }, [sidebarService]);
+
const _className = clsx(styles.sidebar, {
[styles.sidebarOpen]: options?.visible,
});
@@ -79,7 +94,7 @@ export function Sidebar() {
}
return (
<section className={_className} style={{ width }}>
- <section className={styles.sidebarContainer}>
+ <section className={styles.sidebarContainer} ref={scrollRef}>
<header className={styles.sidebarHeader}>
{options?.header}