import useSize from '@react-hook/size'; import classNames from 'classnames'; import { useRef } from 'react'; import Markdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; import { useKeyValue } from '../hooks/useKeyValue'; import type { EditorProps } from './core/Editor/Editor'; import { Editor } from './core/Editor/Editor'; import { IconButton } from './core/IconButton'; import { SplitLayout } from './core/SplitLayout'; import { VStack } from './core/Stacks'; import { Prose } from './Prose'; interface Props extends Pick { placeholder: string; className?: string; defaultValue: string; onChange: (value: string) => void; name: string; } export function MarkdownEditor({ className, defaultValue, onChange, name, placeholder, heightMode, stateKey, }: Props) { const containerRef = useRef(null); const [width] = useSize(containerRef.current); const wideEnoughForSplit = width > 600; const { set: setViewMode, value: rawViewMode } = useKeyValue<'edit' | 'preview' | 'both'>({ namespace: 'global', key: ['md_view', name], fallback: 'edit', }); if (rawViewMode == null) return null; let viewMode = rawViewMode; if (rawViewMode === 'both' && !wideEnoughForSplit) { viewMode = 'edit'; } const editor = ( ); const preview = defaultValue.length === 0 ? (

No description

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