import { $generateNodesFromSerializedNodes } from '@lexical/clipboard' import { BaseSerializedNode } from '@lexical/clipboard/clipboard' import { InitialEditorStateType } from '@lexical/react/LexicalComposer' import * as Dialog from '@radix-ui/react-dialog' import { $insertNodes, LexicalEditor } from 'lexical' import { X } from 'lucide-react' import { Notice } from 'obsidian' import { useRef, useState } from 'react' import { useDatabase } from '../../contexts/DatabaseContext' import { useDialogContainer } from '../../contexts/DialogContext' import { DuplicateTemplateException } from '../../database/exception' import LexicalContentEditable from './chat-input/LexicalContentEditable' /* * This component must be used inside * The modal={false} prop is required because modal mode blocks pointer events across the entire page, * which would conflict with lexical editor popovers */ export default function CreateTemplateDialogContent({ selectedSerializedNodes, onClose, }: { selectedSerializedNodes?: BaseSerializedNode[] | null onClose: () => void }) { const container = useDialogContainer() const { getTemplateManager } = useDatabase() const [templateName, setTemplateName] = useState('') const editorRef = useRef(null) const contentEditableRef = useRef(null) const initialEditorState: InitialEditorStateType = ( editor: LexicalEditor, ) => { if (!selectedSerializedNodes) return editor.update(() => { const parsedNodes = $generateNodesFromSerializedNodes( selectedSerializedNodes, ) $insertNodes(parsedNodes) }) } const onSubmit = async () => { try { if (!editorRef.current) return const serializedEditorState = editorRef.current.toJSON() const nodes = serializedEditorState.editorState.root.children if (nodes.length === 0) { new Notice('Please enter a content for your template') return } if (templateName.trim().length === 0) { new Notice('Please enter a name for your template') return } await ( await getTemplateManager() ).createTemplate({ name: templateName, content: { nodes }, }) new Notice(`Template created: ${templateName}`) setTemplateName('') onClose() } catch (error) { if (error instanceof DuplicateTemplateException) { new Notice('A template with this name already exists') } else { console.error(error) new Notice('Failed to create template') } } } return (
Create template Create template from selected content
setTemplateName(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') { e.stopPropagation() e.preventDefault() onSubmit() } }} />
) }