From 52a95af82883fc533fae9a5dec60b1ad48159c68 Mon Sep 17 00:00:00 2001 From: donteatfriedrice Date: Wed, 31 Jul 2024 03:16:33 +0000 Subject: [PATCH] fix: ai chat block added later should be with higher index (#7660) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [BS-1000](https://linear.app/affine-design/issue/BS-1000/更好地处理新增内容的层级) --- .../blocksuite/presets/ai/_common/chat-actions-handle.ts | 8 ++++++-- .../presets/ai/peek-view/chat-block-peek-view.ts | 4 ++-- 2 files changed, 8 insertions(+), 4 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 8da0b3a21ac8..02dd36883715 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 @@ -131,7 +131,7 @@ function getViewportCenter( // Add AI chat block and focus on it function addAIChatBlock( - doc: Doc, + host: EditorHost, messages: ChatMessage[], sessionId: string, viewportCenter: { x: number; y: number } @@ -140,6 +140,7 @@ function addAIChatBlock( return; } + const { doc } = host; const surfaceBlock = doc .getBlocks() .find(block => block.flavour === 'affine:surface'); @@ -147,6 +148,8 @@ 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 @@ -158,6 +161,7 @@ function addAIChatBlock( { xywh: bound.serialize(), messages: JSON.stringify(messages), + index: layer.generateIndex('affine:embed-ai-chat'), sessionId, }, surfaceBlock.id @@ -329,7 +333,7 @@ const SAVE_CHAT_TO_BLOCK_ACTION: ChatAction = { // After switching to edgeless mode, the user can save the chat to a block const blockId = addAIChatBlock( - host.doc, + host, messages, newSessionId, viewportCenter diff --git a/packages/frontend/core/src/blocksuite/presets/ai/peek-view/chat-block-peek-view.ts b/packages/frontend/core/src/blocksuite/presets/ai/peek-view/chat-block-peek-view.ts index ecd4713356e5..a3e792088506 100644 --- a/packages/frontend/core/src/blocksuite/presets/ai/peek-view/chat-block-peek-view.ts +++ b/packages/frontend/core/src/blocksuite/presets/ai/peek-view/chat-block-peek-view.ts @@ -180,7 +180,8 @@ export class AIChatBlockPeekView extends LitElement { return; } - const aiChatBlockId = doc.addBlock( + const edgelessService = this._rootService as EdgelessRootService; + const aiChatBlockId = edgelessService.addBlock( 'affine:embed-ai-chat' as keyof BlockSuite.BlockModels, { xywh: bound.serialize(), @@ -197,7 +198,6 @@ export class AIChatBlockPeekView extends LitElement { this.updateContext({ currentChatBlockId: aiChatBlockId }); // Connect the parent chat block to the AI chat block - const edgelessService = this._rootService as EdgelessRootService; edgelessService.addElement(CanvasElementType.CONNECTOR, { mode: ConnectorMode.Curve, controllers: [],