refactor(editor): ai slash menu config extension (#10680)

This commit is contained in:
L-Sun
2025-03-07 09:05:03 +00:00
parent 264f0dd2be
commit d37868d97a
9 changed files with 26 additions and 165 deletions
@@ -41,7 +41,7 @@ export const defaultSlashMenuConfig: SlashMenuConfig = {
disableWhen: ({ model }) => {
return model.flavour === 'affine:code';
},
items: [
items: ({ std, model }) => [
{
name: 'New Doc',
description: 'Start a new document.',
@@ -103,7 +103,7 @@ export const defaultSlashMenuConfig: SlashMenuConfig = {
},
// ---------------------------------------------------------
({ std, model }) => {
...(() => {
const { host } = std;
const surfaceModel = getSurfaceBlock(host.doc);
@@ -152,10 +152,10 @@ export const defaultSlashMenuConfig: SlashMenuConfig = {
}));
return [...frameItems, ...groupItems];
},
})(),
// ---------------------------------------------------------
() => {
...((): SlashMenuActionItem[] => {
const now = new Date();
const tomorrow = new Date();
const yesterday = new Date();
@@ -209,7 +209,7 @@ export const defaultSlashMenuConfig: SlashMenuConfig = {
},
},
];
},
})(),
// ---------------------------------------------------------
// { groupName: 'Actions' },
@@ -2,4 +2,5 @@ export { AFFINE_SLASH_MENU_WIDGET } from './consts';
// TODO(@L-Sun): narrow the scope of the exported symbols
export * from './extensions';
export * from './types';
// TODO(@L-Sun): remove this when refactoring quick search
export * from './widget';
@@ -51,7 +51,7 @@ export type SlashMenuConfig = {
/**
* The items in the slash menu. It can be generated dynamically with the context.
*/
items: (SlashMenuItem | ((ctx: SlashMenuContext) => SlashMenuItem[]))[];
items: SlashMenuItem[] | ((ctx: SlashMenuContext) => SlashMenuItem[]);
/**
* Slash menu will not be triggered when the condition is true.
@@ -73,11 +73,14 @@ export function mergeSlashMenuConfigs(
configs: Map<string, SlashMenuConfig>
): SlashMenuConfig {
return {
items: Array.from(configs.values().flatMap(config => config.items)),
items: ctx =>
Array.from(configs.values()).flatMap(({ items }) =>
typeof items === 'function' ? items(ctx) : items
),
disableWhen: ctx =>
configs
.values()
.map(config => config.disableWhen?.(ctx) ?? false)
.map(({ disableWhen }) => disableWhen?.(ctx) ?? false)
.some(Boolean),
};
}
@@ -48,14 +48,7 @@ const showSlashMenu = debounce(
disposables.add(() => slashMenu.remove());
slashMenu.context = context;
slashMenu.items = buildSlashMenuItems(
config.items
.map(item => {
if (typeof item === 'function') {
return item(context);
}
return item;
})
.flat(),
typeof config.items === 'function' ? config.items(context) : config.items,
context,
configItemTransform
);