From 9101e7d690a95382222c8c603ffd82b736c11e7e Mon Sep 17 00:00:00 2001 From: xiange Date: Thu, 16 May 2024 22:33:32 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20show=20error=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/chili-core/src/foundation/pubsub.ts | 1 + packages/chili-ui/src/mainWindow.ts | 3 ++- packages/chili-ui/src/toast/toast.module.css | 13 ++++++++++++- packages/chili-ui/src/toast/toast.ts | 18 +++++++++++++++--- packages/chili/src/services/commandService.ts | 1 + 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/packages/chili-core/src/foundation/pubsub.ts b/packages/chili-core/src/foundation/pubsub.ts index 27ef3e99..5e44586c 100644 --- a/packages/chili-core/src/foundation/pubsub.ts +++ b/packages/chili-core/src/foundation/pubsub.ts @@ -35,6 +35,7 @@ export interface PubSubEventMap { displayHome: (show: boolean) => void; showProperties(document: IDocument, nodes: INode[]): void; showToast: (message: I18nKeys, ...args: any[]) => void; + displayError: (message: string) => void; showPermanent: (action: () => Promise, message: I18nKeys, ...args: any[]) => void; showDialog: (title: I18nKeys, context: IPropertyChanged, callback: () => void) => void; viewCursor: (cursor: CursorType) => void; diff --git a/packages/chili-ui/src/mainWindow.ts b/packages/chili-ui/src/mainWindow.ts index 23256444..5da8e362 100644 --- a/packages/chili-ui/src/mainWindow.ts +++ b/packages/chili-ui/src/mainWindow.ts @@ -30,7 +30,8 @@ export class MainWindow implements IWindow { private _initSubs(app: IApplication) { const displayHome = debounce(this.displayHome, 100); - PubSub.default.sub("showToast", Toast.show); + PubSub.default.sub("showToast", Toast.info); + PubSub.default.sub("displayError", Toast.error); PubSub.default.sub("showDialog", Dialog.show); PubSub.default.sub("showPermanent", Permanent.show); PubSub.default.sub("activeViewChanged", (view) => displayHome(app, view === undefined)); diff --git a/packages/chili-ui/src/toast/toast.module.css b/packages/chili-ui/src/toast/toast.module.css index f83a4a82..02ffc413 100644 --- a/packages/chili-ui/src/toast/toast.module.css +++ b/packages/chili-ui/src/toast/toast.module.css @@ -6,7 +6,18 @@ z-index: 10000; border-radius: 0.8em; background-color: rgba(0, 0, 0, 0.75); - color: #fff; font-size: 1.2em; padding: 1em; } + +.info { + color: #fff; +} + +.error { + color: #ff0000; +} + +.warning { + color: #ffc107; +} diff --git a/packages/chili-ui/src/toast/toast.ts b/packages/chili-ui/src/toast/toast.ts index a20882ea..f53f26d0 100644 --- a/packages/chili-ui/src/toast/toast.ts +++ b/packages/chili-ui/src/toast/toast.ts @@ -7,13 +7,25 @@ import style from "./toast.module.css"; export class Toast { private static _lastToast: [number, HTMLElement] | undefined; - static readonly show = (message: I18nKeys, ...args: any[]) => { + static readonly info = (message: I18nKeys, ...args: any[]) => { + Toast.display(style.info, I18n.translate(message, ...args)); + }; + + static readonly error = (message: string) => { + Toast.display(style.error, message); + }; + + static readonly warn = (message: string) => { + Toast.display(style.warning, message); + }; + + private static display(type: string, message: string) { if (this._lastToast) { clearTimeout(this._lastToast[0]); this._lastToast[1].remove(); } - const toast = label({ className: style.toast, textContent: I18n.translate(message, ...args) }); + const toast = label({ className: `${style.toast} ${type}`, textContent: message }); document.body.appendChild(toast); this._lastToast = [ window.setTimeout(() => { @@ -22,5 +34,5 @@ export class Toast { }, 2000), toast, ]; - }; + } } diff --git a/packages/chili/src/services/commandService.ts b/packages/chili/src/services/commandService.ts index d6503d93..d1821a3b 100644 --- a/packages/chili/src/services/commandService.ts +++ b/packages/chili/src/services/commandService.ts @@ -54,6 +54,7 @@ export class CommandService implements IService { await command .execute(this.app) .catch((err) => { + PubSub.default.pub("displayError", err); Logger.error(err); }) .finally(() => {