Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -140,13 +140,16 @@ function addAIChatBlock(
return;
}

const { doc } = host;
const surfaceBlock = doc
.getBlocks()
.find(block => block.flavour === 'affine:surface');
if (!surfaceBlock) {
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
Expand All @@ -158,6 +161,7 @@ function addAIChatBlock(
{
xywh: bound.serialize(),
messages: JSON.stringify(messages),
index: layer.generateIndex('affine:embed-ai-chat'),
donteatfriedrice marked this conversation as resolved.
Show resolved Hide resolved
sessionId,
},
surfaceBlock.id
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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: [],
Expand Down
Loading