Skip to content

Commit

Permalink
refactor(editor): restructure extension setup and exports
Browse files Browse the repository at this point in the history
- Refactored the setup of editor extensions to use a function and updated exports for better organization and clarity.
  • Loading branch information
phodal committed Sep 23, 2024
1 parent 8673551 commit 72c0ce1
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 137 deletions.
184 changes: 93 additions & 91 deletions web/core/lib/editor/action/custom-editor-commands.ts
Original file line number Diff line number Diff line change
@@ -1,99 +1,101 @@
import { Commands, Extension } from "@tiptap/react";
import { Editor } from "@tiptap/core";
import { Transaction } from "prosemirror-state";
import { DefinedVariable, FacetType, OutputForm, PromptAction, } from "@/editor/defs/custom-action.type";
import { PromptsManager } from "@/editor/prompts/prompts-manager";
import { ARTICLE_TYPE_OPTIONS, TypeOptions } from "@/editor/defs/type-options.type";
import { AiActionExecutor } from "@/editor/action/AiActionExecutor";
import { Commands, Extension } from '@tiptap/react';
import { Editor } from '@tiptap/core';
import { Transaction } from 'prosemirror-state';
import { DefinedVariable, FacetType, OutputForm, PromptAction } from '@/editor/defs/custom-action.type';
import { PromptsManager } from '@/editor/prompts/prompts-manager';
import { ARTICLE_TYPE_OPTIONS, TypeOptions } from '@/editor/defs/type-options.type';
import { AiActionExecutor } from '@/editor/action/AiActionExecutor';

declare module "@tiptap/core" {
interface Commands<ReturnType> {
callLlm: {
callLlm: (action: PromptAction) => string | undefined;
};
getAiActions: {
getAiActions: (facet: FacetType) => PromptAction[];
};
callQuickAction: {
callQuickAction: (text: string) => ReturnType;
}
runAiAction: {
runAiAction: (action: PromptAction) => ReturnType;
};
replaceRange: {
replaceRange: (text: string) => ReturnType;
}
setBackgroundContext: () => ReturnType,
getArticleType: {
getArticleType: () => TypeOptions,
}
setArticleType: {
setArticleType: (articleType: TypeOptions) => ReturnType
},
}
declare module '@tiptap/core' {
interface Commands<ReturnType> {
callLlm: {
callLlm: (action: PromptAction) => string | undefined;
};
getAiActions: {
getAiActions: (facet: FacetType) => PromptAction[];
};
callQuickAction: {
callQuickAction: (text: string) => ReturnType;
}
runAiAction: {
runAiAction: (action: PromptAction) => ReturnType;
};
replaceRange: {
replaceRange: (text: string) => ReturnType;
}
setBackgroundContext: () => ReturnType,
getArticleType: {
getArticleType: () => TypeOptions,
}
setArticleType: {
setArticleType: (articleType: TypeOptions) => ReturnType
},
}
}

let articleType = ARTICLE_TYPE_OPTIONS[0];

export const CustomEditorCommands = Extension.create({
name: "commandFunctions",
export const CustomEditorCommands = (promptsManager: PromptsManager = PromptsManager.getInstance()) => {
return Extension.create({
name: 'commandFunctions',

// @ts-ignore
addCommands: () => {
return {
getArticleType:
() =>
({ tr }: { tr: Transaction }) => {
return articleType
},
setArticleType:
(type: TypeOptions) =>
({ editor, tr, dispatch }: { editor: Editor, tr: Transaction, dispatch: any }) => {
articleType = type;
},
// @ts-ignore
addCommands: () => {
return {
getArticleType:
() =>
({ tr }: { tr: Transaction }) => {
return articleType;
},
setArticleType:
(type: TypeOptions) =>
({ editor, tr, dispatch }: { editor: Editor, tr: Transaction, dispatch: any }) => {
articleType = type;
},

callLlm:
(action: PromptAction) =>
async ({ tr, commands, editor }: { tr: Transaction; commands: Commands, editor: Editor }) => {
let actionHandler = new AiActionExecutor(editor);
return await actionHandler.execute(action)
},
getAiActions:
(facet: FacetType) =>
({ editor }: { editor: Editor }) => {
let articleType = editor.commands.getArticleType();
return PromptsManager.getInstance().getActions(facet, articleType);
},
runAiAction:
(action: PromptAction) =>
({ editor }: { editor: Editor }) => {
editor.commands.callLlm(action);
},
callQuickAction:
(text: string) =>
({ editor }: { editor: Editor }) => {
editor.setEditable(false);
editor.commands.callLlm(<PromptAction>{
name: text,
template: `You are an assistant to help user write article. Here is user command:` + text + `\n Here is some content ###Article title: {{${DefinedVariable.TITLE}}}, Before Content: {{${DefinedVariable.BEFORE_CURSOR}}}###`,
facetType: FacetType.QUICK_INSERT,
outputForm: OutputForm.STREAMING,
});
callLlm:
(action: PromptAction) =>
async ({ tr, commands, editor }: { tr: Transaction; commands: Commands, editor: Editor }) => {
const actionHandler = new AiActionExecutor(editor);
return await actionHandler.execute(action);
},
getAiActions:
(facet: FacetType) =>
({ editor }: { editor: Editor }) => {
const articleType = editor.commands.getArticleType();
return promptsManager.getActions(facet, articleType);
},
runAiAction:
(action: PromptAction) =>
({ editor }: { editor: Editor }) => {
editor.commands.callLlm(action);
},
callQuickAction:
(text: string) =>
({ editor }: { editor: Editor }) => {
editor.setEditable(false);
editor.commands.callLlm(<PromptAction>{
name: text,
template: `You are an assistant to help user write article. Here is user command:` + text + `\n Here is some content ###Article title: {{${DefinedVariable.TITLE}}}, Before Content: {{${DefinedVariable.BEFORE_CURSOR}}}###`,
facetType: FacetType.QUICK_INSERT,
outputForm: OutputForm.STREAMING
});

editor.setEditable(true);
},
replaceRange:
(text: string) =>
({ editor, tr, dispatch }: { editor: Editor, tr: Transaction, dispatch: any }) => {
const { from, to } = editor.state.selection;
tr.replaceRangeWith(from, to, editor.state.schema.text(text));
dispatch(tr);
},
setBackgroundContext:
(context: string) =>
({ editor }: { editor: Editor }) => {
PromptsManager.getInstance().saveBackgroundContext(context);
},
};
},
});
editor.setEditable(true);
},
replaceRange:
(text: string) =>
({ editor, tr, dispatch }: { editor: Editor, tr: Transaction, dispatch: any }) => {
const { from, to } = editor.state.selection;
tr.replaceRangeWith(from, to, editor.state.schema.text(text));
dispatch(tr);
},
setBackgroundContext:
(context: string) =>
({ editor }: { editor: Editor }) => {
promptsManager.saveBackgroundContext(context);
}
};
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PluginKey } from '@tiptap/pm/state';
import { FacetType } from '@/editor/defs/custom-action.type';
import { PromptsManager } from '@/editor/prompts/prompts-manager';

export const createSlashExtension = (extensionName: string) => {
export const createSlashExtension = (extensionName: string, promptsManager: PromptsManager = PromptsManager.getInstance()) => {
return Node.create({
name: "slash-command",
addOptions() {
Expand Down Expand Up @@ -42,7 +42,7 @@ export const createSlashExtension = (extensionName: string) => {
},
items: () => {
const articleType = this.editor.commands.getArticleType();
return (PromptsManager.getInstance().getActions(FacetType.SLASH_COMMAND, articleType) || []);
return (promptsManager.getActions(FacetType.SLASH_COMMAND, articleType) || []);
},
render: () => {
let component: ReactRenderer<unknown, SlashView>;
Expand Down
77 changes: 40 additions & 37 deletions web/core/lib/editor/live-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,55 @@ import { Advice } from "@/editor/extensions/advice/advice";
import { AdviceManager } from "@/editor/extensions/advice/advice-manager";
import { AdviceView } from "@/editor/extensions/advice/advice-view";
import { Settings } from "@/editor/components/settings";
import { PromptsManager } from '@/editor/prompts/prompts-manager.ts';


const md = new MarkdownIt()

export const extensions = [
// we define all commands here
CustomEditorCommands,
AdviceExtension.configure({
HTMLAttributes: {
class: "my-advice",
},
setAdviceCommand: (advice: Advice) => {
AdviceManager.getInstance().addAdvice(advice);
},
onAdviceActivated: (adviceId) => {
if (adviceId) AdviceManager.getInstance().setActiveId(adviceId);
},
}),
InlineCompletion,
StarterKit.configure({
bulletList: {
keepMarks: true,
keepAttributes: false,
},
orderedList: {
keepMarks: true,
keepAttributes: false,
},
}),
createSlashExtension('ai-slash'),
createQuickBox(),
CharacterCount.configure({}),
Color.configure({ types: [TextStyle.name, ListItem.name] }),
// @ts-ignore
TextStyle.configure({ types: [ListItem.name] }),
Table,
TableRow,
TableCell,
TableHeader
]
export const setupExtensions = (promptsManager: PromptsManager = PromptsManager.getInstance()) => {
return [
// we define all commands here
CustomEditorCommands(promptsManager),
AdviceExtension.configure({
HTMLAttributes: {
class: "my-advice",
},
setAdviceCommand: (advice: Advice) => {
AdviceManager.getInstance().addAdvice(advice);
},
onAdviceActivated: (adviceId) => {
if (adviceId) AdviceManager.getInstance().setActiveId(adviceId);
},
}),
InlineCompletion,
StarterKit.configure({
bulletList: {
keepMarks: true,
keepAttributes: false,
},
orderedList: {
keepMarks: true,
keepAttributes: false,
},
}),
createSlashExtension('ai-slash', promptsManager),
createQuickBox(),
CharacterCount.configure({}),
Color.configure({ types: [TextStyle.name, ListItem.name] }),
// @ts-ignore
TextStyle.configure({ types: [ListItem.name] }),
Table,
TableRow,
TableCell,
TableHeader
]
}

const LiveEditor = () => {
const { t } = useTranslation();

const editor = useEditor({
extensions,
extensions: setupExtensions(PromptsManager.getInstance()),
content: md.render(t('Editor Placeholder')),
editorProps: {
attributes: {
Expand Down
12 changes: 5 additions & 7 deletions web/core/lib/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export * from "@/editor/live-editor";
export * from '@/editor/live-editor';

export {
extensions,
default as LiveEditor
} from "@/editor/live-editor";

export { ToolbarMenu } from "@/editor/menu/toolbar-menu";
export { default as LiveEditor } from '@/editor/live-editor';
export { setupExtensions } from '@/editor/live-editor';
export { ToolbarMenu } from '@/editor/menu/toolbar-menu';
export { PromptsManager } from '@/editor/prompts/prompts-manager.ts';

0 comments on commit 72c0ce1

Please sign in to comment.