import classNames from 'classnames'; import { atom, useAtom } from 'jotai'; import { useRef, useState } from 'react'; import Markdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; import { Button } from './core/Button'; import type { EditorProps } from './core/Editor/Editor'; import { Editor } from './core/Editor/Editor'; import { HStack, VStack } from './core/Stacks'; import { Prose } from './Prose'; type ViewMode = 'edit' | 'preview'; interface Props extends Pick { placeholder: string; className?: string; defaultValue: string; onChange: (value: string) => void; name: string; defaultMode?: ViewMode; doneButtonLabel?: string; } const viewModeAtom = atom>({}); export function MarkdownEditor({ className, defaultValue, onChange, name, defaultMode = 'preview', doneButtonLabel = 'Save', ...editorProps }: Props) { const containerRef = useRef(null); const [rawViewMode, setViewMode] = useAtom(viewModeAtom); const viewMode = rawViewMode[name] ?? defaultMode; const [value, setValue] = useState(defaultValue); const editor = ( ); const preview = defaultValue.length === 0 ? (

No description

) : ( { if (href && !href.match(/https?:\/\//)) { href = `http://${href}`; } return ( {children} ); }, }} > {value} ); const contents = viewMode === 'preview' ? preview : editor; return (
{contents}
{viewMode === 'preview' && ( )} {viewMode === 'edit' && ( )}
); }