Skip to content

Commit

Permalink
feat(editor): add custom actions and update mapping for Tiptap Editor
Browse files Browse the repository at this point in the history
This commit introduces the ability to add custom actions to the Tiptap Editor and updates the actions map to include these custom slash commands. It enhances the editor's functionality and user experience by enabling the execution of custom actions within the editor interface.
  • Loading branch information
phodal committed Nov 1, 2024
1 parent 2294ac4 commit c1c5d5a
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,67 @@ const BubbleMenu: PromptAction[] = [
];
```

Custom Smamples:


```tsx
const actionExecutor: AiActionExecutor = new AiActionExecutor();
actionExecutor.setEndpointUrl("/api/chat");

const instance = PromptsManager.getInstance();
const map = customSlashActions?.map((action) => {
return {
name: action.name,
i18Name: false,
template: `123125`,
facetType: FacetType.SLASH_COMMAND,
outputForm: OutputForm.STREAMING,
action: async (editor: Editor) => {
if (action.action) {
await action.action(editor);
}
},
};
}) || [];

instance.updateActionsMap("article", ArticlePrompts.concat(map));

const editor = useEditor({
extensions: setupExtensions(instance, actionExecutor).concat([
Markdown.configure({
transformPastedText: true,
transformCopiedText: false,
}),
]),
content: md.render(value),
immediatelyRender: false,
editorProps: {
attributes: {
class: "prose lg:prose-xl bb-editor-inner",
},
},
onUpdate: ({ editor }) => {
if (onChange) {
const schema = editor.state.schema;
try {
const serializer = DOMSerializer.fromSchema(schema);
const serialized: HTMLElement | DocumentFragment = serializer.serializeFragment(editor.state.doc.content);

const html: string = Array.from(serialized.childNodes)
.map((node: ChildNode) => (node as HTMLElement).outerHTML)
.join("");

const turndownService = new TurndownService();
const markdown = turndownService.turndown(html);
onChange(markdown);
} catch (e) {
console.error(e);
}
}
},
});
```

## Refs

### Tiptap Editor extensions
Expand Down

0 comments on commit c1c5d5a

Please sign in to comment.