From 53eb4aca8d1a0206b959ef7ed70da00e95065df3 Mon Sep 17 00:00:00 2001 From: donteatfriedrice Date: Tue, 16 Jul 2024 06:15:39 +0000 Subject: [PATCH] feat: add notification when chat panel action success (#7490) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [BS-318](https://linear.app/affine-design/issue/BS-318/右侧边栏-ai-title-位置不统一) [BS-418](https://linear.app/affine-design/issue/BS-418/chat-panel-response-成功后,添加-toast) --- .../ai/chat-panel/actions/actions-handle.ts | 16 ++++++++-- .../ai/chat-panel/actions/copy-more.ts | 30 ++++++++++++++++--- .../ai/chat-panel/chat-panel-messages.ts | 11 ++++++- .../blocksuite/presets/ai/chat-panel/index.ts | 3 +- .../presets/ai/utils/editor-actions.ts | 4 --- 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/actions/actions-handle.ts b/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/actions/actions-handle.ts index 3a857e5d83..0efd53a405 100644 --- a/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/actions/actions-handle.ts +++ b/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/actions/actions-handle.ts @@ -26,6 +26,7 @@ const CommonActions = [ { icon: ReplaceIcon, title: 'Replace selection', + toast: 'Successfully replaced', handler: async ( host: EditorHost, content: string, @@ -39,7 +40,7 @@ const CommonActions = [ currentBlockSelections, }) .run(); - if (!data.selectedBlocks) return; + if (!data.selectedBlocks) return false; reportResponse('result:replace'); @@ -52,7 +53,7 @@ const CommonActions = [ currentTextSelection.from.length, content ); - return; + return true; } } @@ -63,11 +64,13 @@ const CommonActions = [ data.selectedBlocks.map(block => block.model), currentTextSelection ); + return true; }, }, { icon: InsertBelowIcon, title: 'Insert below', + toast: 'Successfully inserted', handler: async ( host: EditorHost, content: string, @@ -83,13 +86,14 @@ const CommonActions = [ currentImageSelections, }) .run(); - if (!data.selectedBlocks) return; + if (!data.selectedBlocks) return false; reportResponse('result:insert'); await insertBelow( host, content, data.selectedBlocks[data.selectedBlocks?.length - 1] ); + return true; }, }, ]; @@ -99,6 +103,7 @@ export const PageEditorActions = [ { icon: CreateIcon, title: 'Create as a doc', + toast: 'New doc created', handler: (host: EditorHost, content: string) => { reportResponse('result:add-page'); const newDoc = host.doc.collection.createDoc(); @@ -122,6 +127,8 @@ export const PageEditorActions = [ complete = true; insertFromMarkdown(newHost, content, noteId, 0).catch(console.error); })(); + + return true; }, }, ]; @@ -131,6 +138,7 @@ export const EdgelessEditorActions = [ { icon: CreateIcon, title: 'Add to edgeless as note', + toast: 'New note created', handler: async (host: EditorHost, content: string) => { reportResponse('result:add-note'); const { doc } = host; @@ -157,6 +165,8 @@ export const EdgelessEditorActions = [ elements: [id], editing: false, }); + + return true; }, }, ]; diff --git a/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/actions/copy-more.ts b/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/actions/copy-more.ts index ef786ed5d9..e874b9de7c 100644 --- a/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/actions/copy-more.ts +++ b/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/actions/copy-more.ts @@ -110,6 +110,16 @@ export class ChatCopyMore extends WithDisposable(LitElement) { this._morePopper?.toggle(); } + private readonly _notifySuccess = (title: string) => { + const rootService = this.host.spec.getService('affine:page'); + const { notificationService } = rootService; + notificationService?.notify({ + title: title, + accent: 'success', + onClose: function (): void {}, + }); + }; + private async _retry() { const { doc } = this.host; try { @@ -180,7 +190,14 @@ export class ChatCopyMore extends WithDisposable(LitElement) {
${content - ? html`
copyText(host, content)}> + ? html`
{ + const success = await copyText(host, content); + if (success) { + this._notifySuccess('Copied to clipboard'); + } + }} + > ${CopyIcon} Copy
` @@ -205,13 +222,18 @@ export class ChatCopyMore extends WithDisposable(LitElement) { action => action.title, action => { return html`
- action.handler( + @click=${async () => { + const success = await action.handler( host, content, this.curTextSelection, this.curBlockSelections - )} + ); + + if (success) { + this._notifySuccess(action.toast); + } + }} > ${action.icon}
${action.title}
diff --git a/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/chat-panel-messages.ts b/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/chat-panel-messages.ts index 5af2592f15..fbff7ca715 100644 --- a/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/chat-panel-messages.ts +++ b/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/chat-panel-messages.ts @@ -545,13 +545,22 @@ export class ChatPanelMessages extends WithDisposable(ShadowlessElement) { return; } - await action.handler( + const success = await action.handler( host, content, this._currentTextSelection, this._currentBlockSelections, this._currentImageSelections ); + const rootService = host.spec.getService('affine:page'); + const { notificationService } = rootService; + if (success) { + notificationService?.notify({ + title: action.toast, + accent: 'success', + onClose: function (): void {}, + }); + } }} > ${action.title} diff --git a/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/index.ts b/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/index.ts index 519ad9ef22..fd2c16e1f5 100644 --- a/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/index.ts +++ b/packages/frontend/core/src/blocksuite/presets/ai/chat-panel/index.ts @@ -28,7 +28,8 @@ export class ChatPanel extends WithDisposable(ShadowlessElement) { .chat-panel-container { display: flex; flex-direction: column; - padding: 0 12px; + padding: 0 16px; + padding-top: 8px; height: 100%; } diff --git a/packages/frontend/core/src/blocksuite/presets/ai/utils/editor-actions.ts b/packages/frontend/core/src/blocksuite/presets/ai/utils/editor-actions.ts index 8684566167..7db08538b1 100644 --- a/packages/frontend/core/src/blocksuite/presets/ai/utils/editor-actions.ts +++ b/packages/frontend/core/src/blocksuite/presets/ai/utils/editor-actions.ts @@ -140,9 +140,5 @@ export const copyText = async (host: EditorHost, text: string) => { .flatMap(model => model.children); const slice = Slice.fromModels(previewDoc, models); await host.std.clipboard.copySlice(slice); - const { notificationService } = host.std.spec.getService('affine:page'); - if (notificationService) { - notificationService.toast('Copied to clipboard'); - } return true; };