mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-15 05:37:32 +00:00
refactor(core): edgeless toolbar ai action config extension (#10884)
This commit is contained in:
@@ -4,6 +4,10 @@ import type {
|
||||
} from '@blocksuite/affine/blocks/root';
|
||||
import { noop } from '@blocksuite/affine/global/utils';
|
||||
import type { DocMode } from '@blocksuite/affine/model';
|
||||
import {
|
||||
ActionPlacement,
|
||||
type ToolbarModuleConfig,
|
||||
} from '@blocksuite/affine/shared/services';
|
||||
import { html } from 'lit';
|
||||
|
||||
import type { AIItemGroupConfig } from '../../components/ai-item/types';
|
||||
@@ -78,3 +82,70 @@ export function setupEdgelessElementToolbarAIEntry(
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function edgelessToolbarAIEntryConfig(): ToolbarModuleConfig {
|
||||
return {
|
||||
actions: [
|
||||
{
|
||||
placement: ActionPlacement.Start,
|
||||
id: 'A.ai',
|
||||
score: -1,
|
||||
when(ctx) {
|
||||
const models = ctx.getSurfaceModels();
|
||||
return models.length > 0 && !models.some(model => model.isLocked());
|
||||
},
|
||||
content: ({ host, chain }) => {
|
||||
const filteredGroups = edgelessAIGroups.reduce<AIItemGroupConfig[]>(
|
||||
(pre, group) => {
|
||||
const filtered = group.items.filter(item =>
|
||||
item.showWhen?.(chain, 'edgeless' as DocMode, host)
|
||||
);
|
||||
|
||||
if (filtered.length > 0) pre.push({ ...group, items: filtered });
|
||||
|
||||
return pre;
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
if (filteredGroups.every(group => group.items.length === 0))
|
||||
return null;
|
||||
|
||||
const handler = () => {
|
||||
const aiPanel = getAIPanelWidget(host);
|
||||
if (aiPanel.config) {
|
||||
aiPanel.config.generateAnswer = ({ finish, input }) => {
|
||||
finish('success');
|
||||
aiPanel.hide();
|
||||
extractSelectedContent(host)
|
||||
.then(context => {
|
||||
AIProvider.slots.requestSendWithChat.next({
|
||||
input,
|
||||
context,
|
||||
host,
|
||||
});
|
||||
})
|
||||
.catch(console.error);
|
||||
};
|
||||
aiPanel.config.inputCallback = text => {
|
||||
const copilotWidget = getEdgelessCopilotWidget(host);
|
||||
const panel = copilotWidget.shadowRoot?.querySelector(
|
||||
'edgeless-copilot-panel'
|
||||
);
|
||||
if (panel instanceof HTMLElement) {
|
||||
panel.style.visibility = text ? 'hidden' : 'visible';
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
return html`<edgeless-copilot-toolbar-entry
|
||||
.host=${host}
|
||||
.groups=${edgelessAIGroups}
|
||||
.onClick=${handler}
|
||||
></edgeless-copilot-toolbar-entry>`;
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import type { FrameworkProvider } from '@toeverything/infra';
|
||||
import { buildAIPanelConfig } from '../ai-panel';
|
||||
import { toolbarAIEntryConfig } from '../entries';
|
||||
import {
|
||||
edgelessToolbarAIEntryConfig,
|
||||
setupEdgelessCopilot,
|
||||
setupEdgelessElementToolbarAIEntry,
|
||||
} from '../entries/edgeless/index';
|
||||
@@ -37,10 +38,15 @@ export function createAIEdgelessRootBlockSpec(
|
||||
aiPanelWidget,
|
||||
edgelessCopilotWidget,
|
||||
getAIEdgelessRootWatcher(framework),
|
||||
// In note
|
||||
ToolbarModuleExtension({
|
||||
id: BlockFlavourIdentifier('custom:affine:note'),
|
||||
config: toolbarAIEntryConfig(),
|
||||
}),
|
||||
ToolbarModuleExtension({
|
||||
id: BlockFlavourIdentifier('custom:affine:surface:*'),
|
||||
config: edgelessToolbarAIEntryConfig(),
|
||||
}),
|
||||
AiSlashMenuConfigExtension(),
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user