mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 13:25:12 +00:00
feat(editor): add start-with-ai button for empty doc (#9836)
Close [BS-2391](https://linear.app/affine-design/issue/BS-2391/bs-ai-toolbar-空状态下添加-actions-列表) https://github.com/user-attachments/assets/cbded517-2d3d-4a75-b144-644e2b03f68a
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { handleInlineAskAIAction } from '@affine/core/blocksuite/presets/ai';
|
||||
import { pageAIGroups } from '@affine/core/blocksuite/presets/ai/_common/config';
|
||||
import { DocsService } from '@affine/core/modules/doc';
|
||||
import { EditorService } from '@affine/core/modules/editor';
|
||||
import { FeatureFlagService } from '@affine/core/modules/feature-flag';
|
||||
@@ -5,6 +7,7 @@ import { TemplateDocService } from '@affine/core/modules/template-doc';
|
||||
import { TemplateListMenu } from '@affine/core/modules/template-doc/view/template-list-menu';
|
||||
import { useI18n } from '@affine/i18n';
|
||||
import track from '@affine/track';
|
||||
import { PageRootBlockComponent } from '@blocksuite/affine/blocks';
|
||||
import type { Store } from '@blocksuite/affine/store';
|
||||
import {
|
||||
AiIcon,
|
||||
@@ -62,6 +65,7 @@ const StarterBarNotEmpty = ({ doc }: { doc: Store }) => {
|
||||
[doc.id, templateDocService.list]
|
||||
)
|
||||
);
|
||||
const enableAI = useLiveData(featureFlagService.flags.enable_ai.$);
|
||||
const enableTemplateDoc = useLiveData(
|
||||
featureFlagService.flags.enable_template_doc.$
|
||||
);
|
||||
@@ -85,10 +89,29 @@ const StarterBarNotEmpty = ({ doc }: { doc: Store }) => {
|
||||
setTemplateMenuOpen(open);
|
||||
}, []);
|
||||
|
||||
const showAI = false;
|
||||
const startWithAI = useCallback(() => {
|
||||
const std = editorService.editor.editorContainer$.value?.std;
|
||||
if (!std) return;
|
||||
|
||||
const rootBlockId = std.host.doc.root?.id;
|
||||
if (!rootBlockId) return;
|
||||
|
||||
const rootComponent = std.view.getBlock(rootBlockId);
|
||||
if (!(rootComponent instanceof PageRootBlockComponent)) return;
|
||||
|
||||
const { id, created } = rootComponent.focusFirstParagraph();
|
||||
if (created) {
|
||||
std.view.viewUpdated.once(v => {
|
||||
if (v.id === id) handleInlineAskAIAction(std.host, pageAIGroups);
|
||||
});
|
||||
} else {
|
||||
handleInlineAskAIAction(std.host, pageAIGroups);
|
||||
}
|
||||
}, [editorService.editor]);
|
||||
|
||||
const showTemplate = !isTemplate && enableTemplateDoc;
|
||||
|
||||
if (!showAI && !showTemplate) {
|
||||
if (!enableAI && !showTemplate) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -96,10 +119,12 @@ const StarterBarNotEmpty = ({ doc }: { doc: Store }) => {
|
||||
<div className={styles.root}>
|
||||
{t['com.affine.page-starter-bar.start']()}
|
||||
<ul className={styles.badges}>
|
||||
{showAI ? (
|
||||
{enableAI ? (
|
||||
<Badge
|
||||
data-testid="start-with-ai-badge"
|
||||
icon={<AiIcon />}
|
||||
text={t['com.affine.page-starter-bar.ai']()}
|
||||
onClick={startWithAI}
|
||||
/>
|
||||
) : null}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user