From 13583ff075708df1ea8760288151c2993e37f358 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Fri, 1 Nov 2024 09:32:45 +0800 Subject: [PATCH] refactor(editor): extract smart actions to function and add custom actions support Extract the logic for creating smart menus into a separate function to improve readability and reusability. Additionally, the MenuBubble component now accepts custom actions to enhance its functionality. --- web/core/lib/editor/menu/menu-bubble.tsx | 48 +++++++++++++----------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/web/core/lib/editor/menu/menu-bubble.tsx b/web/core/lib/editor/menu/menu-bubble.tsx index 5d48441..7bfbedf 100644 --- a/web/core/lib/editor/menu/menu-bubble.tsx +++ b/web/core/lib/editor/menu/menu-bubble.tsx @@ -14,35 +14,39 @@ import { newAdvice } from '@/editor/extensions/advice/advice'; import { ToolbarMenu } from '@/editor/menu/toolbar-menu'; import { BounceLoader } from 'react-spinners'; -export const MenuBubble = ({ editor } : { - editor: Editor +function innerSmartActions() : PromptAction[] { + const innerSmartMenus: PromptAction[] = []; + + innerSmartMenus.push({ + name: '扩写', + template: `根据如下的内容扩写,只返回三句,限 100 字以内。###\${${DefinedVariable.SELECTION}}###。`, + facetType: FacetType.BUBBLE_MENU, + changeForm: ChangeForm.DIFF, + outputForm: OutputForm.TEXT + }); + + innerSmartMenus.push({ + name: '润色', + template: `请润色并重写如下的内容:###\${${DefinedVariable.SELECTION}}###`, + facetType: FacetType.BUBBLE_MENU, + changeForm: ChangeForm.DIFF, + outputForm: OutputForm.TEXT + }); + + return innerSmartMenus; +} + +export const MenuBubble = ({ editor, customActions } : { + editor: Editor, + customActions?: PromptAction[] }) => { const [loading, setLoading] = React.useState(false); const [isOpen, setIsOpen] = React.useState(false); - const [smartMenus, setSmartMenus] = React.useState([]); + const [smartMenus, setSmartMenus] = React.useState(customActions ? innerSmartActions().concat(customActions) : innerSmartActions); const [menus, setMenus] = React.useState([]); useEffect(() => { - const innerSmartMenus: PromptAction[] = []; - - innerSmartMenus.push({ - name: '扩写', - template: `根据如下的内容扩写,只返回三句,限 100 字以内。###\${${DefinedVariable.SELECTION}}###。`, - facetType: FacetType.BUBBLE_MENU, - changeForm: ChangeForm.DIFF, - outputForm: OutputForm.TEXT - }); - - innerSmartMenus.push({ - name: '润色', - template: `请润色并重写如下的内容:###\${${DefinedVariable.SELECTION}}###`, - facetType: FacetType.BUBBLE_MENU, - changeForm: ChangeForm.DIFF, - outputForm: OutputForm.TEXT - }); - - setSmartMenus(innerSmartMenus); setMenus(editor?.commands?.getAiActions(FacetType.BUBBLE_MENU) || []); }, [editor, isOpen]);