fix: ai chat block added later should be with higher index (#7660)

[BS-1000](https://linear.app/affine-design/issue/BS-1000/更好地处理新增内容的层级)
This commit is contained in:
donteatfriedrice
2024-07-31 03:16:34 +00:00
parent ede576061d
commit 52a95af828
2 changed files with 8 additions and 4 deletions
@@ -131,7 +131,7 @@ function getViewportCenter(
// Add AI chat block and focus on it // Add AI chat block and focus on it
function addAIChatBlock( function addAIChatBlock(
doc: Doc, host: EditorHost,
messages: ChatMessage[], messages: ChatMessage[],
sessionId: string, sessionId: string,
viewportCenter: { x: number; y: number } viewportCenter: { x: number; y: number }
@@ -140,6 +140,7 @@ function addAIChatBlock(
return; return;
} }
const { doc } = host;
const surfaceBlock = doc const surfaceBlock = doc
.getBlocks() .getBlocks()
.find(block => block.flavour === 'affine:surface'); .find(block => block.flavour === 'affine:surface');
@@ -147,6 +148,8 @@ function addAIChatBlock(
return; return;
} }
const surfaceService = host.spec.getService('affine:surface');
const { layer } = surfaceService;
// Add AI chat block to the center of the viewport // Add AI chat block to the center of the viewport
const width = 300; // AI_CHAT_BLOCK_WIDTH = 300 const width = 300; // AI_CHAT_BLOCK_WIDTH = 300
const height = 320; // AI_CHAT_BLOCK_HEIGHT = 320 const height = 320; // AI_CHAT_BLOCK_HEIGHT = 320
@@ -158,6 +161,7 @@ function addAIChatBlock(
{ {
xywh: bound.serialize(), xywh: bound.serialize(),
messages: JSON.stringify(messages), messages: JSON.stringify(messages),
index: layer.generateIndex('affine:embed-ai-chat'),
sessionId, sessionId,
}, },
surfaceBlock.id 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 // After switching to edgeless mode, the user can save the chat to a block
const blockId = addAIChatBlock( const blockId = addAIChatBlock(
host.doc, host,
messages, messages,
newSessionId, newSessionId,
viewportCenter viewportCenter
@@ -180,7 +180,8 @@ export class AIChatBlockPeekView extends LitElement {
return; return;
} }
const aiChatBlockId = doc.addBlock( const edgelessService = this._rootService as EdgelessRootService;
const aiChatBlockId = edgelessService.addBlock(
'affine:embed-ai-chat' as keyof BlockSuite.BlockModels, 'affine:embed-ai-chat' as keyof BlockSuite.BlockModels,
{ {
xywh: bound.serialize(), xywh: bound.serialize(),
@@ -197,7 +198,6 @@ export class AIChatBlockPeekView extends LitElement {
this.updateContext({ currentChatBlockId: aiChatBlockId }); this.updateContext({ currentChatBlockId: aiChatBlockId });
// Connect the parent chat block to the AI chat block // Connect the parent chat block to the AI chat block
const edgelessService = this._rootService as EdgelessRootService;
edgelessService.addElement(CanvasElementType.CONNECTOR, { edgelessService.addElement(CanvasElementType.CONNECTOR, {
mode: ConnectorMode.Curve, mode: ConnectorMode.Curve,
controllers: [], controllers: [],