-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(editor): restructure extension setup and exports
- Refactored the setup of editor extensions to use a function and updated exports for better organization and clarity.
- Loading branch information
Showing
4 changed files
with
140 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |