Skip to content

Commit

Permalink
refactor(editor): extract smart actions to function and add custom ac…
Browse files Browse the repository at this point in the history
…tions 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.
  • Loading branch information
phodal committed Nov 1, 2024
1 parent 6e10d3e commit 13583ff
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions web/core/lib/editor/menu/menu-bubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<PromptAction[]>([]);
const [smartMenus, setSmartMenus] = React.useState<PromptAction[]>(customActions ? innerSmartActions().concat(customActions) : innerSmartActions);
const [menus, setMenus] = React.useState<any[]>([]);

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]);

Expand Down

0 comments on commit 13583ff

Please sign in to comment.