Skip to content

Commit

Permalink
feat: support MessageOptions.detail (#4135)
Browse files Browse the repository at this point in the history
* feat: support MessageOptions.detail

* feat: change the message open method to an object parameter

* chore: optimized code

* style: adjust the dialog-content font size

---------

Co-authored-by: hacke2 <[email protected]>
  • Loading branch information
bk1012 and hacke2 authored Nov 4, 2024
1 parent b47d8ed commit c24cd87
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 92 deletions.
10 changes: 6 additions & 4 deletions packages/components/src/dialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface IDialogProps extends IOverlayProps {
type?: ModalType;
icon?: IconDesc;
message: string | React.ReactNode;
detail?: string;
buttons?: JSX.Element[] | JSX.Element;
closable?: boolean;
onOk?: () => void;
Expand All @@ -46,11 +47,11 @@ const DefaultButtons = ({ onCancel, onOk, cancelText, okText }) => (
export const DialogContent: React.FC<IDialogProps> = ({
onClose,
closable,
type = 'confirm',
messageType = MessageType.Info,
icon,
message,
buttons,
type = 'confirm',
title,
onOk,
onCancel,
Expand All @@ -69,8 +70,8 @@ export const DialogContent: React.FC<IDialogProps> = ({
/>
)}
<div className={'kt-dialog-content_area'}>
{type !== 'basic' && title && <p className={'kt-dialog-content_title'}>{title}</p>}
<span className={'kt-dialog-message'}>{message}</span>
<p className={'kt-dialog-content_title'}>{title}</p>
{message && <span className={'kt-dialog-message'}>{message}</span>}
</div>
{closable && type !== 'basic' && (
<button className={cls('kt-dialog-closex', getIcon('close'))} onClick={onClose}></button>
Expand All @@ -97,6 +98,7 @@ export const Dialog: React.FC<IDialogProps> = ({
messageType,
icon,
message,
detail,
buttons,
type = 'confirm',
title,
Expand All @@ -123,6 +125,6 @@ export const Dialog: React.FC<IDialogProps> = ({
afterClose={afterClose}
{...restProps}
>
<DialogContent message={message} buttons={buttons} visible={visible} {...restProps}></DialogContent>
<DialogContent title={message} message={detail} buttons={buttons} visible={visible} icon={icon} {...restProps} />
</Overlay>
);
2 changes: 1 addition & 1 deletion packages/components/src/dialog/styles.less
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

.@{prefix}-dialog-content_title {
line-height: 22px;
font-size: 14px;
font-size: 15px;
margin: 0px 0px 8px 0px;
}
}
Expand Down
10 changes: 5 additions & 5 deletions packages/editor/src/browser/fs-resource/fs-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,11 @@ export class FileSystemResourceProvider extends WithEventBus implements IResourc
[localize('file.prompt.save', 'Save')]: AskSaveResult.SAVE,
[localize('file.prompt.cancel', 'Cancel')]: AskSaveResult.CANCEL,
};
const selection = await this.dialogService.open(
formatLocalize('saveChangesMessage', resource.name),
MessageType.Info,
Object.keys(buttons),
);
const selection = await this.dialogService.open({
message: formatLocalize('saveChangesMessage', resource.name),
type: MessageType.Info,
buttons: Object.keys(buttons),
});
const result = buttons[selection!];
return this.close(resource, result);
}
Expand Down
10 changes: 5 additions & 5 deletions packages/editor/src/browser/untitled-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,11 @@ export class UntitledSchemeResourceProvider extends WithEventBus implements IRes
[localize('file.prompt.save', 'Save')]: AskSaveResult.SAVE,
[localize('file.prompt.cancel', 'Cancel')]: AskSaveResult.CANCEL,
};
const selection = await this.dialogService.open(
formatLocalize('saveChangesMessage', resource.name),
MessageType.Info,
Object.keys(buttons),
);
const selection = await this.dialogService.open({
message: formatLocalize('saveChangesMessage', resource.name),
type: MessageType.Info,
buttons: Object.keys(buttons),
});
return await this.close(resource, buttons[selection!]);
}
}
10 changes: 5 additions & 5 deletions packages/editor/src/browser/workbench-editor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,11 @@ export class WorkbenchEditorServiceImpl extends WithEventBus implements Workbenc
filesDetail += formatLocalize('file.prompt.more.number', toClose.length - MAX_CONFIRM_RESOURCES);
}
}
const selection = await this.dialogService.open(
toMarkdown(formatLocalize('saveNFilesChangesMessage', toClose.length, filesDetail), this.openner),
MessageType.Info,
Object.keys(buttons),
);
const selection = await this.dialogService.open({
message: toMarkdown(formatLocalize('saveNFilesChangesMessage', toClose.length, filesDetail), this.openner),
type: MessageType.Info,
buttons: Object.keys(buttons),
});
const result = buttons[selection!];
if (result === AskSaveResult.SAVE) {
await Promise.all(toClose.map((v) => this.resourceService.close?.(v.resource, AskSaveResult.SAVE)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export class MainThreadFileSystemEvent extends Disposable {
localize('refactoring-changes.msg.skipChanges'),
];
// edit#metadata.needsConfirmation#true --> show dialog
const answer = await this.dialogService.open(message, MessageType.Info, choices);
const answer = await this.dialogService.open({ message, type: MessageType.Info, buttons: choices });
showPreview = 'show';
if (answer === choices[1]) {
// Skip changes
Expand All @@ -211,7 +211,7 @@ export class MainThreadFileSystemEvent extends Disposable {
localize('refactoring-changes.msg.skipChanges'),
localize('component.modal.okText'),
];
const answer = await this.dialogService.open(message, MessageType.Info, choices);
const answer = await this.dialogService.open({ message, type: MessageType.Info, buttons: choices });
if (answer === choices[1]) {
// Skip changes
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export class MainThreadMessage implements IMainThreadMessage {
from,
): Promise<number | undefined> {
const action = options.modal
? await this.dialogService.open(message, type, actions)
: await this.messageService.open(message, type, actions, true, from);
? await this.dialogService.open({ message, type, buttons: actions, options })
: await this.messageService.open({ message, type, buttons: actions, closable: true, from });
return action ? actions.indexOf(action) : undefined;
}
}
4 changes: 4 additions & 0 deletions packages/extension/src/hosted/api/vscode/ext.host.message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class ExtHostMessage implements IExtHostMessage {
} else {
if ('modal' in optionsOrFirstItem) {
options.modal = optionsOrFirstItem.modal;

if ('detail' in optionsOrFirstItem) {
options.detail = optionsOrFirstItem.detail;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,13 @@ export class WindowDialogServiceImpl implements IWindowDialogService {
}
await fileTreeDialogService.whenReady;
const model = FileTreeDialogModel.createModel(this.injector, fileTreeDialogService);
const res = await this.dialogService.open<string[]>(
<FileDialog model={model} options={{ ...defaultOptions, ...options }} isOpenDialog={true} />,
MessageType.Empty,
[],
true,
undefined,
{ className: styles.file_dialog_wrapper },
);
const res = await this.dialogService.open<string[]>({
message: <FileDialog model={model} options={{ ...defaultOptions, ...options }} isOpenDialog={true} />,
type: MessageType.Empty,
buttons: [],
closable: true,
props: { className: styles.file_dialog_wrapper },
});
this.dialogService.reset();
if (res && res.length > 0) {
const files = res.map((r) => URI.file(r));
Expand Down Expand Up @@ -180,14 +179,13 @@ export class WindowDialogServiceImpl implements IWindowDialogService {
}
await fileTreeDialogService.whenReady;
const model = FileTreeDialogModel.createModel(this.injector, fileTreeDialogService);
const res = await this.dialogService.open<string[]>(
<FileDialog model={model} options={options} isOpenDialog={false} />,
MessageType.Empty,
[],
true,
undefined,
{ className: styles.file_dialog_wrapper },
);
const res = await this.dialogService.open<string[]>({
message: <FileDialog model={model} options={options} isOpenDialog={false} />,
type: MessageType.Empty,
buttons: [],
closable: true,
props: { className: styles.file_dialog_wrapper },
});
this.dialogService.reset();
if (res && res.length > 0) {
const file = URI.file(res[0]);
Expand Down
23 changes: 15 additions & 8 deletions packages/overlay/src/browser/dialog.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Autowired, Injectable } from '@opensumi/di';
import { Deferred, Emitter, MessageType } from '@opensumi/ide-core-common';

import { AbstractMessageService, IDialogService, Icon } from '../common';
import { AbstractMessageService, IDialogService, Icon, OpenMessageOptions } from '../common';

import { DialogContextKey } from './dialog.contextkey';

Expand All @@ -18,6 +18,8 @@ export class DialogService extends AbstractMessageService implements IDialogServ

protected message: string | React.ReactNode = '';

protected detail: string | undefined;

protected title = '';

public closable = true;
Expand All @@ -36,17 +38,18 @@ export class DialogService extends AbstractMessageService implements IDialogServ
return this._visible;
}

open<T = string>(
message: string | React.ReactNode,
type: MessageType,
buttons?: any[],
open<T = string>({
message,
type,
buttons,
options,
closable = true,
_?: string,
props?: Record<string, any>,
): Promise<T | undefined> {
props,
}: OpenMessageOptions): Promise<T | undefined> {
this.deferred = new Deferred<string>();
this.type = type;
this.message = message;
this.detail = options?.detail;
this._visible = true;
this.closable = closable;
this.props = props ?? {};
Expand Down Expand Up @@ -75,6 +78,10 @@ export class DialogService extends AbstractMessageService implements IDialogServ
return this.message;
}

getDetail(): string | undefined {
return this.detail;
}

getType(): MessageType | undefined {
return this.type;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/overlay/src/browser/dialog.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const Dialog: FC = () => {
const dialogService = useInjectable<IDialogService>(IDialogService);
const icon = dialogService.getIcon();
const message = dialogService.getMessage();
const detail = dialogService.getDetail();
const buttons = dialogService.getButtons();
const type = dialogService.getType();

Expand Down Expand Up @@ -48,6 +49,7 @@ export const Dialog: FC = () => {
closable={dialogService.closable ?? true}
afterClose={afterClose}
message={message}
detail={detail}
type='confirm'
messageType={type}
icon={icon}
Expand Down
16 changes: 9 additions & 7 deletions packages/overlay/src/browser/message.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { parseWithoutEscape } from '@opensumi/ide-components/lib/utils';
import { IOpenerService, toMarkdown } from '@opensumi/ide-core-browser';
import { MayCancelablePromise, MessageType, localize, uuid } from '@opensumi/ide-core-common';

import { AbstractMessageService, IMessageService, MAX_MESSAGE_LENGTH } from '../common';
import { AbstractMessageService, IMessageService, MAX_MESSAGE_LENGTH, OpenMessageOptions } from '../common';

import type vscode from 'vscode';

@Injectable()
export class MessageService extends AbstractMessageService implements IMessageService {
Expand Down Expand Up @@ -37,13 +39,13 @@ export class MessageService extends AbstractMessageService implements IMessageSe
* @param closable true | false
* @param from from extension
*/
open<T = string>(
rawMessage: string | React.ReactNode,
type: MessageType,
buttons?: string[],
open<T = string>({
type,
buttons,
from,
closable = true,
from?: string,
): MayCancelablePromise<T | undefined> {
message: rawMessage,
}: OpenMessageOptions): MayCancelablePromise<T | undefined> {
if (!rawMessage) {
return Promise.resolve(undefined);
}
Expand Down
36 changes: 17 additions & 19 deletions packages/overlay/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@ import React from 'react';

import { Event, MayCancelablePromise, MessageType, URI } from '@opensumi/ide-core-common';

import type vscode from 'vscode';

export const IMessageService = Symbol('IMessageService');

export interface OpenMessageOptions {
message: string | React.ReactNode;
type: MessageType;
buttons?: any[];
options?: vscode.MessageOptions;
closable?: boolean;
from?: string;
props?: Record<string, any>;
}
export interface IMessageService {
info(
message: string | React.ReactNode,
Expand All @@ -23,14 +34,7 @@ export interface IMessageService {
closable?: boolean,
props?: Record<string, any>,
): MayCancelablePromise<string | undefined>;
open<T = string>(
message: string | React.ReactNode,
type: MessageType,
buttons?: string[],
closable?: boolean,
from?: string,
props?: Record<string, any>,
): MayCancelablePromise<T | undefined>;
open<T = string>(options: OpenMessageOptions): MayCancelablePromise<T | undefined>;
hide<T = string>(value?: T): void;
}

Expand All @@ -47,6 +51,7 @@ export interface IDialogService extends IMessageService {
visible: boolean;
onDidDialogVisibleChange: Event<boolean>;
getMessage(): string | React.ReactNode;
getDetail(): string | undefined;
getIcon(): Icon | undefined;
getButtons(): string[] | undefined;
getType(): MessageType | undefined;
Expand All @@ -61,7 +66,7 @@ export abstract class AbstractMessageService implements IMessageService {
closable?: boolean,
props?: Record<string, any>,
): Promise<string | undefined> {
return this.open(message, MessageType.Info, buttons, closable, undefined, props);
return this.open({ message, type: MessageType.Info, buttons, closable, props });
}

warning(
Expand All @@ -70,7 +75,7 @@ export abstract class AbstractMessageService implements IMessageService {
closable?: boolean,
props?: Record<string, any>,
): Promise<string | undefined> {
return this.open(message, MessageType.Warning, buttons, closable, undefined, props);
return this.open({ message, type: MessageType.Warning, buttons, closable, props });
}

error(
Expand All @@ -79,16 +84,9 @@ export abstract class AbstractMessageService implements IMessageService {
closable?: boolean,
props?: Record<string, any>,
): Promise<string | undefined> {
return this.open(message, MessageType.Error, buttons, closable, undefined, props);
return this.open({ message, type: MessageType.Error, buttons, closable, props });
}
abstract open<T = string>(
message: string | React.ReactNode,
type: MessageType,
buttons?: any[],
closable?: boolean,
from?: string,
props?: Record<string, any>,
): Promise<T | undefined>;
abstract open<T = string>(options: OpenMessageOptions): Promise<T | undefined>;
abstract hide<T = string>(value?: T): void;
}

Expand Down
10 changes: 5 additions & 5 deletions packages/search/src/browser/replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ export async function replaceAll(
[localize('search.replace.buttonCancel')]: false,
[localize('search.replace.buttonOK')]: true,
};
const selection = await dialogService!.open(
formatLocalize(
const selection = await dialogService!.open({
message: formatLocalize(
'search.removeAll.occurrences.files.confirmation.message',
String(resultTotal.resultNum),
String(resultTotal.fileNum),
replaceText,
),
MessageType.Warning,
Object.keys(buttons),
);
type: MessageType.Warning,
buttons: Object.keys(buttons),
});
if (!buttons[selection!]) {
return buttons[selection!];
}
Expand Down
Loading

1 comment on commit c24cd87

@opensumi
Copy link
Contributor

@opensumi opensumi bot commented on c24cd87 Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Next publish successful!

3.4.6-next-1730692409.0

Please sign in to comment.