From 94c5effdd58a04dd5f4baab3eb021e4f09d610c8 Mon Sep 17 00:00:00 2001 From: donteatfriedrice Date: Fri, 2 Aug 2024 03:53:21 +0000 Subject: [PATCH] fix: should save chat to block when doc mode (#7697) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [BS-1033](https://linear.app/affine-design/issue/BS-1033/在page模式下尝试存chat-block,被告知失败,预期是切换到白板模式直接操作成功) --- .../presets/ai/_common/chat-actions-handle.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/frontend/core/src/blocksuite/presets/ai/_common/chat-actions-handle.ts b/packages/frontend/core/src/blocksuite/presets/ai/_common/chat-actions-handle.ts index aab3e39747..453638786a 100644 --- a/packages/frontend/core/src/blocksuite/presets/ai/_common/chat-actions-handle.ts +++ b/packages/frontend/core/src/blocksuite/presets/ai/_common/chat-actions-handle.ts @@ -110,7 +110,7 @@ function getViewportCenter( mode: DocMode, rootService: PageRootService | EdgelessRootService ) { - const center = { x: 0, y: 0 }; + const center = { x: 400, y: 50 }; if (mode === 'page') { const viewport = rootService.editPropsStore.getStorage('viewport'); if (viewport) { @@ -138,7 +138,8 @@ function addAIChatBlock( host: EditorHost, messages: ChatMessage[], sessionId: string, - viewportCenter: { x: number; y: number } + viewportCenter: { x: number; y: number }, + index: string ) { if (!messages.length || !sessionId) { return; @@ -152,8 +153,6 @@ function addAIChatBlock( return; } - const surfaceService = host.spec.getService('affine:surface'); - const { layer } = surfaceService; // Add AI chat block to the center of the viewport const width = 300; // AI_CHAT_BLOCK_WIDTH = 300 const height = 320; // AI_CHAT_BLOCK_HEIGHT = 320 @@ -165,7 +164,7 @@ function addAIChatBlock( { xywh: bound.serialize(), messages: JSON.stringify(messages), - index: layer.generateIndex('affine:embed-ai-chat'), + index, sessionId, rootWorkspaceId: doc.collection.id, rootDocId: doc.id, @@ -300,11 +299,14 @@ const SAVE_CHAT_TO_BLOCK_ACTION: ChatAction = { } const rootService = host.spec.getService('affine:page'); - if (!rootService) return false; + const surfaceService = host.spec.getService('affine:surface'); + if (!rootService || !surfaceService) return false; const { docModeService, notificationService } = rootService; + const { layer } = surfaceService; const curMode = docModeService.getMode(); const viewportCenter = getViewportCenter(curMode, rootService); + const newBlockIndex = layer.generateIndex('affine:embed-ai-chat'); // If current mode is not edgeless, switch to edgeless mode first if (curMode !== 'edgeless') { // Set mode to edgeless @@ -342,7 +344,8 @@ const SAVE_CHAT_TO_BLOCK_ACTION: ChatAction = { host, messages, newSessionId, - viewportCenter + viewportCenter, + newBlockIndex ); if (!blockId) { return false;