import { Button, cn, InputArea, Switch, Tabs } from "@cloudflare/kumo"; import { ArrowsIn, ArrowsOut } from "@phosphor-icons/react"; import { EditorContent } from "@tiptap/react"; import { lazy, Suspense, useRef, useState } from "react"; import { DirectiveContext } from "./editor/directives.tsx"; import { FormatToolbar } from "./editor/FormatToolbar.tsx"; import { OutlineButton } from "./editor/Outline.tsx"; import { SelectionMenu, selectionBlocks } from "./editor/SelectionMenu.tsx"; import { SlashMenu } from "./editor/SlashMenu.tsx"; import type { SlashState } from "./editor/slash.ts"; import { readSpotlight, writeSpotlight } from "./editor/spotlight.ts"; import type { MarkdownEditorMode, MarkdownEditorProps } from "./editor/types.ts"; import { useFocusMode } from "./editor/useFocusMode.ts"; import { useMarkdownTiptap } from "./editor/useMarkdownTiptap.ts"; export { type OutlineEntry, outlineOf } from "./editor/Outline.tsx"; export { looksLikeMarkdown, pasteMarkdown } from "./editor/paste.ts"; export { selectionBlocks } from "./editor/SelectionMenu.tsx"; export type { AiSelection, MarkdownEditorLabels, MarkdownEditorMode, MarkdownEditorProps, PickedImage, } from "./editor/types.ts"; // CodeMirror loads with the first switch to Markdown mode, not with the rich editor. const SourceEditor = lazy(() => import("./editor/SourceEditor.tsx").then((m) => ({ default: m.SourceEditor })), ); /** * The markdown body editor (RFC-0014 phase 1): rich editing on Tiptap (MIT), a Markdown mode for * the source, and markdown as the only thing that is ever stored. * * - **Minimal diff:** blocks nobody touched are saved with their original bytes; only edited or * new blocks are written anew (`editor/markdown-blocks.ts`). * - **Nothing silently dropped:** a block the editor would change the meaning of (raw HTML, link * definitions, syntax it does not model) is shown read-only as a source block and saved as is; * Markdown mode edits it. * - The schema follows what the site renders (`marked` with GFM): headings, lists, task lists, * tables, code, quotes, links (the site's link rules plus `asset:`), images. * - `/` opens a menu of blocks, a selection gets a small formatting toolbar, pasted markdown * arrives as blocks, and Markdown mode is CodeMirror (loaded on first use). */ export function MarkdownEditor({ value, onChange, label, labels, placeholder, resolveSrc, defaultMode = "rich", size = "base", readOnly = false, className, onPickImage, locale = "en", onAskAi, blocks, focusActions, previewSource, }: MarkdownEditorProps) { const [mode, setMode] = useState(defaultMode); const [spotlight, setSpotlight] = useState(readSpotlight); const rootRef = useRef(null); // The text a source block asked to open in Markdown mode (selected there once). const [sourceFocus, setSourceFocus] = useState(); const [slash, setSlash] = useState(null); const { editor, editorRef, codec, insertImage, askRef } = useMarkdownTiptap({ value, onChange, label, labels, placeholder, resolveSrc, size, readOnly, onPickImage, locale, onAskAi, blocks, previewSource, setMode, setSourceFocus, setSlash, }); const canFocus = labels.focus !== undefined; const { focused, focusButton, toggleFocus } = useFocusMode({ rootRef, editorRef, canFocus, slash, }); return (
{mode === "rich" && editor ? ( void insertImage(editor) : undefined} /> ) : ( )}
{focused && focusActions} {focused && labels.spotlight && mode === "rich" && ( { setSpotlight(on); writeSpotlight(on); }} /> )} {mode === "rich" && editor && } { setSourceFocus(undefined); setMode(v as MarkdownEditorMode); }} tabs={[ { value: "rich", label: labels.rich }, { value: "markdown", label: labels.markdown }, ]} /> {canFocus && ( )}
{/* The rich editor stays mounted in Markdown mode, so its undo history survives a switch. */} {mode === "markdown" && ( onChange(v)} autoResize minRows={size === "sm" ? 6 : 12} maxRows={40} readOnly={readOnly} className="w-full font-mono" /> } > )} {slash && mode === "rich" && }
); }