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(design): add `cancelText` & `confirmText` props for `Confirm` (#648)

白熱59ac2f33af3e
diff --git a/apps/docs/docusaurus.config.ts b/apps/docs/docusaurus.config.ts
index 8d6f441a80..1816a41fc5 100644
--- a/apps/docs/docusaurus.config.ts
+++ b/apps/docs/docusaurus.config.ts
@@ -59,7 +59,7 @@ const config: Config = {
id: `api/${name}`,
entryPoints: [`../../packages/${name}/src/index.ts`],
tsconfig: `../../packages/${name}/tsconfig.json`,
- exclude: ['node_modules/**/*', "**/*+(.spec|.e2e|.test).ts"],
+ exclude: ['node_modules/**/*', '**/*+(.spec|.e2e|.test).ts'],
excludePrivate: true,
excludeExternals: true,
excludeInternal: true,
@@ -82,7 +82,7 @@ const config: Config = {
sidebarPath: './sidebars.ts',
// Please change this to your repo.
// Remove this to remove the "edit this page" links.
- editUrl: 'https://github.com/dream-num/univer/edit/dev/apps/docs/docs/',
+ editUrl: 'https://github.com/dream-num/univer/edit/dev/apps/docs/',
},
// blog: {
// showReadingTime: true,
diff --git a/examples/sheets/src/sheets-plugin-debugger/commands/operations/confirm.operation.ts b/examples/sheets/src/sheets-plugin-debugger/commands/operations/confirm.operation.ts
index ff18dceffa..c506f6727f 100644
--- a/examples/sheets/src/sheets-plugin-debugger/commands/operations/confirm.operation.ts
+++ b/examples/sheets/src/sheets-plugin-debugger/commands/operations/confirm.operation.ts
@@ -1,6 +1,7 @@
+import type { ICommand } from '@univerjs/core';
+import { CommandType } from '@univerjs/core';
import { IConfirmService } from '@univerjs/ui';
-import { CommandType, ICommand } from '@univerjs/core';
-import { IAccessor } from '@wendellhu/redi';
+import type { IAccessor } from '@wendellhu/redi';
export interface IUIComponentCommandParams {
value: string;
@@ -16,6 +17,8 @@ export const ConfirmOperation: ICommand = {
id: 'confirm1',
children: { title: 'Confirm Content' },
title: { title: 'Confirm Title' },
+ confirmText: 'hello',
+ cancelText: 'world',
onClose() {
confirmService.close('confirm1');
},
diff --git a/examples/sheets/src/sheets-plugin-debugger/controllers/debugger.controller.ts b/examples/sheets/src/sheets-plugin-debugger/controllers/debugger.controller.ts
index 7ad7f2a573..73de71f40e 100644
--- a/examples/sheets/src/sheets-plugin-debugger/controllers/debugger.controller.ts
+++ b/examples/sheets/src/sheets-plugin-debugger/controllers/debugger.controller.ts
@@ -1,5 +1,5 @@
-import { IMenuService } from '@univerjs/ui';
import { Disposable, ICommandService } from '@univerjs/core';
+import { IMenuService } from '@univerjs/ui';
import { Inject, Injector } from '@wendellhu/redi';
import { ConfirmOperation } from '../commands/operations/confirm.operation';
diff --git a/examples/sheets/src/sheets-plugin-debugger/controllers/menu.ts b/examples/sheets/src/sheets-plugin-debugger/controllers/menu.ts
index 6b950001d3..b75d1d88f5 100644
--- a/examples/sheets/src/sheets-plugin-debugger/controllers/menu.ts
+++ b/examples/sheets/src/sheets-plugin-debugger/controllers/menu.ts
@@ -1,7 +1,8 @@
-import { IMenuSelectorItem, MenuItemType, MenuPosition } from '@univerjs/ui';
import { LocaleType } from '@univerjs/core';
import { defaultTheme, greenTheme } from '@univerjs/design';
-import { IAccessor } from '@wendellhu/redi';
+import type { IMenuSelectorItem } from '@univerjs/ui';
+import { MenuItemType, MenuPosition } from '@univerjs/ui';
+import type { IAccessor } from '@wendellhu/redi';
import { ConfirmOperation } from '../commands/operations/confirm.operation';
import { DialogOperation } from '../commands/operations/dialog.operation';
diff --git a/packages/design/src/components/confirm/Confirm.tsx b/packages/design/src/components/confirm/Confirm.tsx
index 6cef6f8b34..9246817f7b 100644
--- a/packages/design/src/components/confirm/Confirm.tsx
+++ b/packages/design/src/components/confirm/Confirm.tsx
@@ -19,6 +19,16 @@ export interface IConfirmProps {
*/
title?: React.ReactNode;
+ /**
+ * The text of the Confirm's confirm button.
+ */
+ cancelText?: string;
+
+ /**
+ * The text of the Confirm's cancel button.
+ */
+ confirmText?: string;
+
/**
* Callback when the Confirm is closed.
*/
@@ -31,16 +41,16 @@ export interface IConfirmProps {
}
export function Confirm(props: IConfirmProps) {
- const { children, visible = false, title, onClose, onConfirm } = props;
+ const { children, visible = false, title, cancelText, confirmText, onClose, onConfirm } = props;
const { locale } = useContext(ConfigContext);
function Footer() {
return (
<footer className={styles.confirmFooter}>
- <Button onClick={onClose}>{locale.design.Confirm.cancel}</Button>
+ <Button onClick={onClose}>{cancelText ?? locale.design.Confirm.cancel}</Button>
<Button type="primary" onClick={onConfirm}>
- {locale.design.Confirm.confirm}
+ {confirmText ?? locale.design.Confirm.confirm}
</Button>
</footer>
);
diff --git a/packages/ui/src/services/confirm/confirm.service.ts b/packages/ui/src/services/confirm/confirm.service.ts
index 6aa23a93e6..35761f816b 100644
--- a/packages/ui/src/services/confirm/confirm.service.ts
+++ b/packages/ui/src/services/confirm/confirm.service.ts
@@ -7,9 +7,11 @@ import { type IConfirmPartMethodOptions } from '../../views/components/confirm-p
export const IConfirmService = createIdentifier<IConfirmService>('univer.confirm-service');
export interface IConfirmService {
+ readonly confirmOptions$: Subject<IConfirmPartMethodOptions[]>;
+
open(params: IConfirmPartMethodOptions): IDisposable;
- close(id: string): void;
+ confirm(params: IConfirmPartMethodOptions): Promise<boolean>;
- getObservableConfirm(): Subject<IConfirmPartMethodOptions[]>;
+ close(id: string): void;
}
diff --git a/packages/ui/src/services/confirm/desktop-confirm.service.ts b/packages/ui/src/services/confirm/desktop-confirm.service.ts
index 23f09c0fa5..4f0f03a4bc 100644
--- a/packages/ui/src/services/confirm/desktop-confirm.service.ts
+++ b/packages/ui/src/services/confirm/desktop-confirm.service.ts
@@ -6,38 +6,50 @@ import { type IConfirmPartMethodOptions } from '../../views/components/confirm-p
import type { IConfirmService } from './confirm.service';
export class DesktopConfirmService implements IConfirmService {
- private confirmOptions: IConfirmPartMethodOptions[] = [];
- private readonly confirmOptions$ = new Subject<IConfirmPartMethodOptions[]>();
+ private _confirmOptions: IConfirmPartMethodOptions[] = [];
+ readonly confirmOptions$ = new Subject<IConfirmPartMethodOptions[]>();
open(option: IConfirmPartMethodOptions): IDisposable {
- if (this.confirmOptions.find((item) => item.id === option.id)) {
- this.confirmOptions = this.confirmOptions.map((item) => ({
+ if (this._confirmOptions.find((item) => item.id === option.id)) {
+ this._confirmOptions = this._confirmOptions.map((item) => ({
...(item.id === option.id ? option : item),
visible: true,
}));
} else {
- this.confirmOptions.push({
+ this._confirmOptions.push({
...option,
visible: true,
});
}
- this.confirmOptions$.next(this.confirmOptions);
+ this.confirmOptions$.next(this._confirmOptions);
return toDisposable(() => {
- this.confirmOptions = [];
+ this._confirmOptions = [];
this.confirmOptions$.next([]);
});
}
+ confirm(params: IConfirmPartMethodOptions): Promise<boolean> {
+ return new Promise((resolve) => {
+ const disposeHandler = this.open({
+ ...params,
+ onConfirm: () => {
+ disposeHandler.dispose();
+ resolve(true);
+ },
+ onClose: () => {
+ disposeHandler.dispose();
+ resolve(false);
+ },
+ });
+ });
+ }
+
close(id: string): void {
- this.confirmOptions = this.confirmOptions.map((item) => ({
+ this._confirmOptions = this._confirmOptions.map((item) => ({
...item,
visible: item.id === id ? false : item.visible,
}));
- this.confirmOptions$.next([...this.confirmOptions]);
- }
-
- getObservableConfirm() {
- return this.confirmOptions$;
+ this.confirmOptions$.next([...this._confirmOptions]);
}
}
diff --git a/packages/ui/src/views/components/confirm-part/ConfirmPart.tsx b/packages/ui/src/views/components/confirm-part/ConfirmPart.tsx
index d2bcb59311..d8eb41bc62 100644
--- a/packages/ui/src/views/components/confirm-part/ConfirmPart.tsx
+++ b/packages/ui/src/views/components/confirm-part/ConfirmPart.tsx
@@ -13,8 +13,7 @@ export function ConfirmPart() {
const [confirmOptions, setConfirmOptions] = useState<IConfirmPartMethodOptions[]>([]);
useEffect(() => {
- const confirm$ = confirmService.getObservableConfirm();
- const subscribtion = confirm$.subscribe((options: IConfirmPartMethodOptions[]) => {
+ const subscribtion = confirmService.confirmOptions$.subscribe((options: IConfirmPartMethodOptions[]) => {
setConfirmOptions(options);
});