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: change message and notification API (#2332)

Wenzhao Hu5cc688177941
diff --git a/packages/design/src/components/message/Message.tsx b/packages/design/src/components/message/Message.tsx
index 60ee2dee27..8409e158b1 100644
--- a/packages/design/src/components/message/Message.tsx
+++ b/packages/design/src/components/message/Message.tsx
@@ -32,14 +32,15 @@ export enum MessageType {
}
export interface IMessageProps {
- key: number;
+ key: string;
type: MessageType;
- content?: string;
+ content: string;
}
-export interface IMessageMethodOptions {
- content: string;
- delay?: number;
+export interface IMessageOptions extends
+ Partial<Pick<IMessageProps, 'key' | 'type' >>,
+ Pick<IMessageProps, 'content'> {
+ duration?: number;
}
const iconMap = {
@@ -99,9 +100,9 @@ export class Message {
this.render();
}
- append(type: MessageType, options: IMessageMethodOptions): IDisposable {
- const { content, delay = 3000 } = options;
- const key = Date.now();
+ append(type: MessageType, options: IMessageOptions): IDisposable {
+ const { content, duration = 3000 } = options;
+ const key = `${Date.now()}`;
this._messages.push({
key,
@@ -111,11 +112,11 @@ export class Message {
this.render();
- setTimeout(() => this.teardown(key), delay);
+ setTimeout(() => this.teardown(key), duration);
return { dispose: () => this.teardown(key) };
}
- teardown(key: number) {
+ teardown(key: string) {
this._messages = this._messages.filter((message) => message.key !== key);
this.render();
@@ -125,19 +126,19 @@ export class Message {
render(<MessageContainer messages={this._messages} />, this._container);
}
- success(options: IMessageMethodOptions): IDisposable {
+ success(options: IMessageOptions): IDisposable {
return this.append(MessageType.Success, options);
}
- info(options: IMessageMethodOptions): IDisposable {
+ info(options: IMessageOptions): IDisposable {
return this.append(MessageType.Info, options);
}
- warning(options: IMessageMethodOptions): IDisposable {
+ warning(options: IMessageOptions): IDisposable {
return this.append(MessageType.Warning, options);
}
- error(options: IMessageMethodOptions): IDisposable {
+ error(options: IMessageOptions): IDisposable {
return this.append(MessageType.Error, options);
}
}
diff --git a/packages/design/src/components/message/index.ts b/packages/design/src/components/message/index.ts
index 68e1e74128..a6382e1a6c 100644
--- a/packages/design/src/components/message/index.ts
+++ b/packages/design/src/components/message/index.ts
@@ -14,4 +14,4 @@
* limitations under the License.
*/
-export { type IMessageMethodOptions, type IMessageProps, Message, MessageType } from './Message';
+export { type IMessageOptions, type IMessageProps, Message, MessageType } from './Message';
diff --git a/packages/design/src/index.ts b/packages/design/src/index.ts
index 2cf01369ea..7217146033 100644
--- a/packages/design/src/index.ts
+++ b/packages/design/src/index.ts
@@ -45,7 +45,7 @@ export { FormDualColumnLayout, type IFormDualColumnLayoutProps, FormLayout, type
export { type IInputProps, type IInputWithSlotProps, Input, InputWithSlot } from './components/input';
export { type IInputNumberProps, InputNumber } from './components/input-number';
export { type MenuRef, Menu, MenuItem, MenuItemGroup, SubMenu } from './components/menu';
-export { type IMessageMethodOptions, type IMessageProps, Message, MessageType } from './components/message';
+export { type IMessageOptions, type IMessageProps, Message, MessageType } from './components/message';
export { type IPagerProps, Pager } from './components/pager';
export { type IPopupProps, Popup, RectPopup, type IRectPopupProps } from './components/popup';
export { type IRadioProps, Radio } from './components/radio';
diff --git a/packages/find-replace/src/views/dialog/FindReplaceDialog.tsx b/packages/find-replace/src/views/dialog/FindReplaceDialog.tsx
index 605842863d..383b6760a3 100644
--- a/packages/find-replace/src/views/dialog/FindReplaceDialog.tsx
+++ b/packages/find-replace/src/views/dialog/FindReplaceDialog.tsx
@@ -168,7 +168,7 @@ export const ReplaceDialog = forwardRef(function ReplaceDIalogImpl(_props, ref)
messageDisposable = messageService.show({
content: localeService.t('find-replace.dialog.no-match'),
type: MessageType.Warning,
- delay: 5000,
+ duration: 5000,
});
}
diff --git a/packages/ui/src/components/notification/Notification.tsx b/packages/ui/src/components/notification/Notification.tsx
index 8589b2c00d..47c85020fb 100644
--- a/packages/ui/src/components/notification/Notification.tsx
+++ b/packages/ui/src/components/notification/Notification.tsx
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+/* eslint-disable react-refresh/only-export-components */
+
import { ConfigContext } from '@univerjs/design';
import { CloseSingle, ErrorSingle, SuccessSingle, WarningSingle } from '@univerjs/icons';
import clsx from 'clsx';
@@ -34,6 +36,10 @@ const iconMap = {
};
export interface INotificationOptions {
+ /**
+ * Key of the notification.
+ */
+ key?: string;
/**
* Component type, optional success, warning, error
*/
@@ -88,8 +94,7 @@ export const PureContent = (props: INotificationOptions) => {
export function Notification() {
const { mountContainer } = useContext(ConfigContext);
-
- if (!mountContainer) return <></>;
+ if (!mountContainer) return null;
const [api, contextHolder] = useNotification({
prefixCls: styles.notification,
@@ -117,6 +122,7 @@ export function Notification() {
lines={options.lines}
/>
),
+ key: options.key,
placement: options.placement ?? 'topRight',
duration: options.duration ?? 4.5,
closable: options.closable ?? true,
@@ -126,7 +132,7 @@ export function Notification() {
return () => {
subscription.unsubscribe();
};
- }, []);
+ }, [api]);
return <>{contextHolder}</>;
}
diff --git a/packages/ui/src/services/message/__testing__/mock-message.service.ts b/packages/ui/src/services/message/__testing__/mock-message.service.ts
index da3d59181f..b31673589b 100644
--- a/packages/ui/src/services/message/__testing__/mock-message.service.ts
+++ b/packages/ui/src/services/message/__testing__/mock-message.service.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { IMessageMethodOptions, IMessageProps } from '@univerjs/design';
+import type { IMessageOptions, IMessageProps } from '@univerjs/design';
import type { IDisposable } from '@wendellhu/redi';
import { toDisposable } from '@univerjs/core';
@@ -24,7 +24,7 @@ import type { IMessageService } from '../message.service';
* This is a mocked message service for testing purposes.
*/
export class MockMessageService implements IMessageService {
- show(_options: IMessageMethodOptions & Omit<IMessageProps, 'key'>): IDisposable {
+ show(_options: IMessageOptions & Omit<IMessageProps, 'key'>): IDisposable {
return toDisposable(() => { /* empty */ });
}
diff --git a/packages/ui/src/services/message/desktop-message.service.ts b/packages/ui/src/services/message/desktop-message.service.ts
index d13e609440..2a51c0a569 100644
--- a/packages/ui/src/services/message/desktop-message.service.ts
+++ b/packages/ui/src/services/message/desktop-message.service.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { IMessageMethodOptions, IMessageProps } from '@univerjs/design';
+import type { IMessageOptions, IMessageProps } from '@univerjs/design';
import { Message } from '@univerjs/design';
import type { IDisposable } from '@wendellhu/redi';
@@ -29,7 +29,7 @@ export class DesktopMessageService implements IMessageService {
this.message = new Message(container);
}
- show(options: IMessageMethodOptions & Omit<IMessageProps, 'key'>): IDisposable {
+ show(options: IMessageOptions & Omit<IMessageProps, 'key'>): IDisposable {
if (!this.message) {
throw new Error('[DesktopMessageService]: no message implementation!');
}
diff --git a/packages/ui/src/services/message/message.service.ts b/packages/ui/src/services/message/message.service.ts
index bad2e0da7e..89b1db965f 100644
--- a/packages/ui/src/services/message/message.service.ts
+++ b/packages/ui/src/services/message/message.service.ts
@@ -14,14 +14,14 @@
* limitations under the License.
*/
-import type { IMessageMethodOptions, IMessageProps } from '@univerjs/design';
+import type { IMessageOptions } from '@univerjs/design';
import type { IDisposable } from '@wendellhu/redi';
import { createIdentifier } from '@wendellhu/redi';
-export const IMessageService = createIdentifier<IMessageService>('univer.message-service');
+export const IMessageService = createIdentifier<IMessageService>('ui.message.service');
export interface IMessageService {
- show(options: IMessageMethodOptions & Omit<IMessageProps, 'key'>): IDisposable;
+ show(options: IMessageOptions): IDisposable;
setContainer(container: HTMLElement): void;
}
diff --git a/packages/ui/src/services/notification/notification.service.ts b/packages/ui/src/services/notification/notification.service.ts
index fed86de828..d7ccb68e29 100644
--- a/packages/ui/src/services/notification/notification.service.ts
+++ b/packages/ui/src/services/notification/notification.service.ts
@@ -19,7 +19,7 @@ import { createIdentifier } from '@wendellhu/redi';
import type { INotificationOptions } from '../../components/notification/Notification';
-export const INotificationService = createIdentifier<INotificationService>('univer.notification-service');
+export const INotificationService = createIdentifier<INotificationService>('ui.notification.service');
export interface INotificationService {
show(params: INotificationOptions): IDisposable;