@@ -18,11 +18,13 @@ import {
1818 fitContent ,
1919 ImageBlockModel ,
2020 InsertBelowIcon ,
21+ LightLoadingIcon ,
2122 NoteDisplayMode ,
2223 ResetIcon ,
2324} from '@blocksuite/blocks' ;
2425import { assertExists , Bound } from '@blocksuite/global/utils' ;
25- import type { TemplateResult } from 'lit' ;
26+ import { html , type TemplateResult } from 'lit' ;
27+ import { styleMap } from 'lit/directives/style-map.js' ;
2628
2729import { AIPenIcon , ChatWithAIIcon } from '../_common/icons' ;
2830import { insertFromMarkdown } from '../_common/markdown-utils' ;
@@ -98,27 +100,55 @@ export function retry(panel: AffineAIPanelWidget): AIItemConfig {
98100 } ;
99101}
100102
103+ const extraConditions : Record < string , ( data : any ) => boolean > = {
104+ createSlides : data => ! ! data . contents ,
105+ } ;
101106export function createInsertResp < T extends keyof BlockSuitePresets . AIActions > (
102107 id : T ,
103108 handler : ( host : EditorHost , ctx : CtxRecord ) => void ,
104109 host : EditorHost ,
105110 ctx : CtxRecord ,
106111 buttonText : string = 'Insert below'
107- ) : AIItemConfig {
108- return {
109- name : buttonText ,
110- icon : InsertBelowIcon ,
111- showWhen : ( ) => {
112- const panel = getAIPanel ( host ) ;
113- return ! EXCLUDING_INSERT_ACTIONS . includes ( id ) && ! ! panel . answer ;
112+ ) : AIItemConfig [ ] {
113+ const extraCondition = extraConditions [ id ] || ( ( _ : any ) => true ) ;
114+ return [
115+ {
116+ name : `${ buttonText } - Loading...` ,
117+ icon : html `< div style =${ styleMap ( { height : '20px' , width : '20px' } ) } >
118+ ${ LightLoadingIcon }
119+ </ div > ` ,
120+ showWhen : ( ) => {
121+ const panel = getAIPanel ( host ) ;
122+ const data = ctx . get ( ) ;
123+ return (
124+ ! EXCLUDING_INSERT_ACTIONS . includes ( id ) &&
125+ ! ! panel . answer &&
126+ // required data for insert
127+ ! extraCondition ( data )
128+ ) ;
129+ } ,
114130 } ,
115- handler : ( ) => {
116- reportResponse ( 'result:insert' ) ;
117- handler ( host , ctx ) ;
118- const panel = getAIPanel ( host ) ;
119- panel . hide ( ) ;
131+ {
132+ name : buttonText ,
133+ icon : InsertBelowIcon ,
134+ showWhen : ( ) => {
135+ const panel = getAIPanel ( host ) ;
136+ const data = ctx . get ( ) ;
137+ return (
138+ ! EXCLUDING_INSERT_ACTIONS . includes ( id ) &&
139+ ! ! panel . answer &&
140+ // required data for insert
141+ ! ! extraCondition ( data )
142+ ) ;
143+ } ,
144+ handler : ( ) => {
145+ reportResponse ( 'result:insert' ) ;
146+ handler ( host , ctx ) ;
147+ const panel = getAIPanel ( host ) ;
148+ panel . hide ( ) ;
149+ } ,
120150 } ,
121- } ;
151+ ] ;
122152}
123153
124154export function asCaption < T extends keyof BlockSuitePresets . AIActions > (
@@ -555,7 +585,7 @@ export function actionToResponse<T extends keyof BlockSuitePresets.AIActions>(
555585 panel . hide ( ) ;
556586 } ,
557587 } ,
558- getInsertAndReplaceHandler ( id , host , ctx , variants ) ,
588+ ... getInsertAndReplaceHandler ( id , host , ctx , variants ) ,
559589 asCaption ( id , host ) ,
560590 retry ( getAIPanel ( host ) ) ,
561591 discard ( getAIPanel ( host ) , getEdgelessCopilotWidget ( host ) ) ,
@@ -603,7 +633,7 @@ export function actionToErrorResponse<
603633 responses : [
604634 {
605635 name : 'Response' ,
606- items : [ getInsertAndReplaceHandler ( id , host , ctx , variants ) ] ,
636+ items : getInsertAndReplaceHandler ( id , host , ctx , variants ) ,
607637 } ,
608638 {
609639 name : '' ,
0 commit comments