feat: support chatting in center peek (#7601)

This commit is contained in:
donteatfriedrice
2024-07-26 09:36:26 +00:00
parent 6bc5337307
commit 1575472a3f
26 changed files with 2058 additions and 645 deletions

View File

@@ -1,4 +1,4 @@
import type { BlockComponent } from '@blocksuite/block-std';
import type { BlockComponent, EditorHost } from '@blocksuite/block-std';
import {
AffineReference,
type EmbedLinkedDocModel,
@@ -7,6 +7,7 @@ import {
type SurfaceRefBlockComponent,
type SurfaceRefBlockModel,
} from '@blocksuite/blocks';
import type { AIChatBlockModel } from '@blocksuite/presets';
import type { BlockModel } from '@blocksuite/store';
import { type DocMode, Entity, LiveData } from '@toeverything/infra';
import type { TemplateResult } from 'lit';
@@ -36,6 +37,13 @@ export type ImagePeekViewInfo = {
blockId: string;
};
export type AIChatBlockPeekViewInfo = {
type: 'ai-chat-block';
docId: string;
host: EditorHost;
model: AIChatBlockModel;
};
export type CustomTemplatePeekViewInfo = {
type: 'template';
template: TemplateResult;
@@ -43,7 +51,11 @@ export type CustomTemplatePeekViewInfo = {
export type ActivePeekView = {
target: PeekViewTarget;
info: DocPeekViewInfo | ImagePeekViewInfo | CustomTemplatePeekViewInfo;
info:
| DocPeekViewInfo
| ImagePeekViewInfo
| CustomTemplatePeekViewInfo
| AIChatBlockPeekViewInfo;
};
const EMBED_DOC_FLAVOURS = [
@@ -69,6 +81,12 @@ const isSurfaceRefModel = (
return blockModel.flavour === 'affine:surface-ref';
};
const isAIChatBlockModel = (
blockModel: BlockModel
): blockModel is AIChatBlockModel => {
return blockModel.flavour === 'affine:embed-ai-chat';
};
function resolvePeekInfoFromPeekTarget(
peekTarget: PeekViewTarget,
template?: TemplateResult
@@ -113,6 +131,13 @@ function resolvePeekInfoFromPeekTarget(
docId: blockModel.doc.id,
blockId: blockModel.id,
};
} else if (isAIChatBlockModel(blockModel)) {
return {
type: 'ai-chat-block',
docId: blockModel.doc.id,
model: blockModel,
host: peekTarget.host,
};
}
} else if (peekTarget instanceof HTMLAnchorElement) {
const maybeDoc = resolveLinkToDoc(peekTarget.href);

View File

@@ -1,4 +1,5 @@
import { toReactNode } from '@affine/component';
import { AIChatBlockPeekViewTemplate } from '@affine/core/blocksuite/presets/ai';
import { BlockComponent } from '@blocksuite/block-std';
import { useLiveData, useService } from '@toeverything/infra';
import { useEffect, useMemo } from 'react';
@@ -35,6 +36,11 @@ function renderPeekView({ info }: ActivePeekView) {
return <ImagePreviewPeekView docId={info.docId} blockId={info.blockId} />;
}
if (info.type === 'ai-chat-block') {
const template = AIChatBlockPeekViewTemplate(info.model, info.host);
return toReactNode(template);
}
return null; // unreachable
}