Skip to content

Commit b57388f

Browse files
committed
feat: block slides insert before image load finished (#7948)
1 parent 5e555b3 commit b57388f

File tree

3 files changed

+50
-100
lines changed

3 files changed

+50
-100
lines changed

packages/frontend/core/src/blocksuite/presets/ai/actions/edgeless-response.ts

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ import {
1818
fitContent,
1919
ImageBlockModel,
2020
InsertBelowIcon,
21+
LightLoadingIcon,
2122
NoteDisplayMode,
2223
ResetIcon,
2324
} from '@blocksuite/blocks';
2425
import { 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

2729
import { AIPenIcon, ChatWithAIIcon } from '../_common/icons';
2830
import { 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+
};
101106
export 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

124154
export 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: '',

packages/frontend/core/src/blocksuite/presets/ai/chat-panel/utils.ts

Lines changed: 0 additions & 84 deletions
This file was deleted.

packages/frontend/core/src/blocksuite/presets/ai/messages/slides-renderer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ export class AISlidesRenderer extends WithDisposable(LitElement) {
8585
contents: res.contents,
8686
images: res.images,
8787
});
88+
// refresh loading menu item
89+
getAIPanel(this.host)
90+
.shadowRoot?.querySelector('ai-panel-answer')
91+
?.requestUpdate();
8892
}
8993
})
9094
.catch(console.error);

0 commit comments

Comments
 (0)