fix: should toggle empty input when generate an image (#8643)

This commit is contained in:
donteatfriedrice
2024-11-01 10:45:32 +00:00
parent ecbf5a95fa
commit aacfb82819
4 changed files with 42 additions and 11 deletions

View File

@@ -13,6 +13,7 @@ import {
} from '@blocksuite/affine/blocks'; } from '@blocksuite/affine/blocks';
import type { TemplateResult } from 'lit'; import type { TemplateResult } from 'lit';
import { TOGGLE_EMPTY_INPUT_ACTIONS } from '../actions/consts';
import { actionToHandler } from '../actions/doc-handler'; import { actionToHandler } from '../actions/doc-handler';
import { actionToHandler as edgelessActionToHandler } from '../actions/edgeless-handler'; import { actionToHandler as edgelessActionToHandler } from '../actions/edgeless-handler';
import { import {
@@ -251,10 +252,17 @@ function edgelessHandler<T extends keyof BlockSuitePresets.AIActions>(
if (!edgeless) { if (!edgeless) {
AIProvider.slots.requestRunInEdgeless.emit({ host }); AIProvider.slots.requestRunInEdgeless.emit({ host });
} else { } else {
const selectedElements = edgeless.service.selection.selectedElements;
if (!selectedElements.length) return;
edgeless.gfx.tool.setTool({ type: 'copilot' }); edgeless.gfx.tool.setTool({ type: 'copilot' });
const currentController = const currentController =
edgeless.gfx.tool.currentTool$.peek() as CopilotTool; edgeless.gfx.tool.currentTool$.peek() as CopilotTool;
const selectedElements = edgeless.service.selection.selectedElements; if (!currentController) {
edgeless.gfx.tool.setTool({ type: 'default' });
return;
}
currentController.updateDragPointsWith(selectedElements, 10); currentController.updateDragPointsWith(selectedElements, 10);
currentController.draggingAreaUpdated.emit(false); // do not show edgeless panel currentController.draggingAreaUpdated.emit(false); // do not show edgeless panel
@@ -276,7 +284,8 @@ function edgelessHandler<T extends keyof BlockSuitePresets.AIActions>(
content: sendAttachments ? '' : markdown, content: sendAttachments ? '' : markdown,
}; };
}, },
trackerOptions trackerOptions,
TOGGLE_EMPTY_INPUT_ACTIONS.includes(id)
)(host); )(host);
} }
}; };

View File

@@ -27,3 +27,4 @@ export const generatingStages: {
}; };
export const INSERT_ABOVE_ACTIONS = ['createHeadings']; export const INSERT_ABOVE_ACTIONS = ['createHeadings'];
export const TOGGLE_EMPTY_INPUT_ACTIONS = ['createImage', 'makeItReal'];

View File

@@ -16,6 +16,7 @@ import {
} from '@blocksuite/affine/blocks'; } from '@blocksuite/affine/blocks';
import { assertExists } from '@blocksuite/affine/global/utils'; import { assertExists } from '@blocksuite/affine/global/utils';
import { Slice } from '@blocksuite/affine/store'; import { Slice } from '@blocksuite/affine/store';
import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx';
import { AIChatBlockModel } from '@toeverything/infra'; import { AIChatBlockModel } from '@toeverything/infra';
import type { TemplateResult } from 'lit'; import type { TemplateResult } from 'lit';
@@ -216,7 +217,8 @@ function actionToStream<T extends keyof BlockSuitePresets.AIActions>(
attachments?: (string | Blob)[]; attachments?: (string | Blob)[];
seed?: string; seed?: string;
} | void>, } | void>,
trackerOptions?: BlockSuitePresets.TrackerOptions trackerOptions?: BlockSuitePresets.TrackerOptions,
panelInput?: string
) { ) {
const action = AIProvider.actions[id]; const action = AIProvider.actions[id];
@@ -233,7 +235,7 @@ function actionToStream<T extends keyof BlockSuitePresets.AIActions>(
const options = { const options = {
...variants, ...variants,
signal, signal,
input: '', input: panelInput ?? '',
stream: true, stream: true,
control, control,
where, where,
@@ -243,6 +245,12 @@ function actionToStream<T extends keyof BlockSuitePresets.AIActions>(
workspaceId: host.doc.collection.id, workspaceId: host.doc.collection.id,
} as Parameters<typeof action>[0]; } as Parameters<typeof action>[0];
const content = ctx.get().content;
if (typeof content === 'string' && !content.length && panelInput) {
ctx.set({
content: panelInput,
});
}
const data = await extract(host, ctx); const data = await extract(host, ctx);
if (data) { if (data) {
Object.assign(options, data); Object.assign(options, data);
@@ -305,6 +313,7 @@ function actionToGeneration<T extends keyof BlockSuitePresets.AIActions>(
) { ) {
return (host: EditorHost, ctx: CtxRecord) => { return (host: EditorHost, ctx: CtxRecord) => {
return ({ return ({
input,
signal, signal,
update, update,
finish, finish,
@@ -324,7 +333,8 @@ function actionToGeneration<T extends keyof BlockSuitePresets.AIActions>(
signal, signal,
variants, variants,
extract, extract,
trackerOptions trackerOptions,
input
)?.(host, ctx); )?.(host, ctx);
if (!stream) return; if (!stream) return;
@@ -416,7 +426,8 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(
attachments?: (string | Blob)[]; attachments?: (string | Blob)[];
seed?: string; seed?: string;
} | void>, } | void>,
trackerOptions?: BlockSuitePresets.TrackerOptions trackerOptions?: BlockSuitePresets.TrackerOptions,
toggleEmptyInput?: boolean
) { ) {
return (host: EditorHost) => { return (host: EditorHost) => {
const aiPanel = getAIPanel(host); const aiPanel = getAIPanel(host);
@@ -466,14 +477,20 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(
} else if (!isEmpty) { } else if (!isEmpty) {
const lastSelected = selectedElements.at(-1)?.id; const lastSelected = selectedElements.at(-1)?.id;
assertExists(lastSelected); assertExists(lastSelected);
referenceElement = getSelectedNoteAnchor(host, lastSelected); const noteAnchor = getSelectedNoteAnchor(host, lastSelected);
referenceElement = noteAnchor;
} }
if (!referenceElement) return; if (!referenceElement) {
const gfx = host.std.get(GfxControllerIdentifier);
gfx?.tool.setTool({ type: 'default' });
edgelessCopilot.lockToolbar(false);
return;
}
if (isCreateImageAction || isMakeItRealAction) { if (isCreateImageAction || isMakeItRealAction) {
togglePanel = async () => { togglePanel = async () => {
if (isEmpty) return true; if (isEmpty || toggleEmptyInput) return true;
const { notes, shapes, images, edgelessTexts, embedSyncedDocs } = const { notes, shapes, images, edgelessTexts, embedSyncedDocs } =
BlocksUtils.splitElements(selectedElements); BlocksUtils.splitElements(selectedElements);
const blocks = [ const blocks = [
@@ -494,7 +511,11 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(
togglePanel() togglePanel()
.then(isEmpty => { .then(isEmpty => {
aiPanel.toggle(referenceElement, isEmpty ? undefined : 'placeholder'); aiPanel.toggle(
referenceElement,
isEmpty ? undefined : 'placeholder',
false
);
}) })
.catch(console.error); .catch(console.error);
}; };

View File

@@ -269,7 +269,7 @@ export const getSelectedImagesAsBlobs = async (host: EditorHost) => {
}; };
export const getSelectedNoteAnchor = (host: EditorHost, id: string) => { export const getSelectedNoteAnchor = (host: EditorHost, id: string) => {
return host.querySelector(`[data-portal-block-id="${id}"] .note-background`); return host.querySelector(`affine-edgeless-note[data-block-id="${id}"]`);
}; };
export function getCopilotSelectedElems( export function getCopilotSelectedElems(