diff --git a/bun.lock b/bun.lock index 11a67669..e47f01e4 100644 --- a/bun.lock +++ b/bun.lock @@ -33,7 +33,7 @@ }, "apps/agent": { "name": "@shumai/agent-app", - "version": "0.3.6", + "version": "0.3.7", "bin": { "shumai-agent": "./src/index.ts", }, @@ -45,7 +45,7 @@ }, "apps/cli": { "name": "@shumai/cli", - "version": "0.3.6", + "version": "0.3.7", "bin": { "shumai-cli": "./src/index.ts", }, @@ -60,7 +60,7 @@ }, "apps/transcode": { "name": "@shumai/transcode-app", - "version": "0.3.6", + "version": "0.3.7", "bin": { "shumai-transcode": "./src/index.ts", }, @@ -72,7 +72,7 @@ }, "apps/web": { "name": "@shumai/web-app", - "version": "0.3.6", + "version": "0.3.7", "dependencies": { "@shumai/agent": "workspace:*", "@shumai/api": "workspace:*", @@ -92,7 +92,7 @@ }, "packages/agent": { "name": "@shumai/agent", - "version": "0.3.6", + "version": "0.3.7", "dependencies": { "@anthropic-ai/sandbox-runtime": "0.0.66", "@earendil-works/pi-agent-core": "0.82.1", @@ -117,7 +117,7 @@ }, "packages/api": { "name": "@shumai/api", - "version": "0.3.6", + "version": "0.3.7", "dependencies": { "@hono/zod-validator": "0.7.6", "@shumai/core": "workspace:*", @@ -131,7 +131,7 @@ }, "packages/core": { "name": "@shumai/core", - "version": "0.3.6", + "version": "0.3.7", "dependencies": { "@earendil-works/pi-agent-core": "0.82.1", "@earendil-works/pi-ai": "0.82.1", @@ -166,7 +166,7 @@ }, "packages/db": { "name": "@shumai/db", - "version": "0.3.6", + "version": "0.3.7", "dependencies": { "@earendil-works/pi-agent-core": "0.82.1", "@prisma/adapter-pg": "7.8.0", @@ -182,7 +182,7 @@ }, "packages/dtos": { "name": "@shumai/dtos", - "version": "0.3.6", + "version": "0.3.7", "dependencies": { "@earendil-works/pi-agent-core": "0.82.1", "zod": "4.4.3", @@ -190,7 +190,7 @@ }, "packages/e2e": { "name": "@shumai/e2e", - "version": "0.3.6", + "version": "0.3.7", "dependencies": { "@earendil-works/pi-agent-core": "0.82.1", "@google/genai": "1.52.0", @@ -205,11 +205,11 @@ }, "packages/tableprinter": { "name": "@shumai/tableprinter", - "version": "0.3.6", + "version": "0.3.7", }, "packages/transcode": { "name": "@shumai/transcode", - "version": "0.3.6", + "version": "0.3.7", "dependencies": { "@shumai/core": "workspace:*", "@shumai/db": "workspace:*", @@ -223,7 +223,7 @@ }, "packages/webui": { "name": "@shumai/webui", - "version": "0.3.6", + "version": "0.3.7", "dependencies": { "@base-ui/react": "1.5.0", "@dnd-kit/abstract": "0.4.0", @@ -321,7 +321,7 @@ }, "packages/workflow-core": { "name": "@shumai/workflow-core", - "version": "0.3.6", + "version": "0.3.7", "dependencies": { "@shumai/core": "workspace:*", "@shumai/db": "workspace:*", diff --git a/packages/webui/components/lexical/plugins/ToolbarPlugin.tsx b/packages/webui/components/lexical/plugins/ToolbarPlugin.tsx deleted file mode 100644 index 53ac3533..00000000 --- a/packages/webui/components/lexical/plugins/ToolbarPlugin.tsx +++ /dev/null @@ -1,324 +0,0 @@ -import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' -import { useCallback, useEffect, useState } from 'react' -import { - SELECTION_CHANGE_COMMAND, - FORMAT_TEXT_COMMAND, - $getSelection, - $isRangeSelection, - COMMAND_PRIORITY_CRITICAL, - $createParagraphNode, -} from 'lexical' -import { - $isListNode, - ListNode, - INSERT_ORDERED_LIST_COMMAND, - INSERT_UNORDERED_LIST_COMMAND, - REMOVE_LIST_COMMAND, -} from '@lexical/list' -import { - $createHeadingNode, - $createQuoteNode, - $isHeadingNode, - type HeadingTagType, -} from '@lexical/rich-text' -import { $createCodeNode } from '@lexical/code' -import { $setBlocksType } from '@lexical/selection' -import { $getNearestNodeOfType } from '@lexical/utils' -import { - Bold, - Italic, - Underline, - Strikethrough, - Code, - Quote, - List, - ListOrdered, - Type, - Heading1, - Heading2, - Heading3, -} from 'lucide-react' -import { Toggle } from '@/ui/components/ui/toggle' -import { Separator } from '@/ui/components/ui/separator' -import { - Select, - SelectContent, - SelectItem, - SelectTrigger, - SelectValue, -} from '@/ui/components/ui/select' - -const blockTypeToBlockName = { - paragraph: 'Normal', - h1: 'Heading 1', - h2: 'Heading 2', - h3: 'Heading 3', - bullet: 'Bullet List', - number: 'Numbered List', - quote: 'Quote', - code: 'Code Block', -} - -export function ToolbarPlugin() { - const [editor] = useLexicalComposerContext() - const [activeEditor, setActiveEditor] = useState(editor) - const [isBold, setIsBold] = useState(false) - const [isItalic, setIsItalic] = useState(false) - const [isUnderline, setIsUnderline] = useState(false) - const [isStrikethrough, setIsStrikethrough] = useState(false) - const [isCode, setIsCode] = useState(false) - const [blockType, setBlockType] = useState('paragraph') - - const $updateToolbar = useCallback(() => { - const selection = $getSelection() - if ($isRangeSelection(selection)) { - setIsBold(selection.hasFormat('bold')) - setIsItalic(selection.hasFormat('italic')) - setIsUnderline(selection.hasFormat('underline')) - setIsStrikethrough(selection.hasFormat('strikethrough')) - setIsCode(selection.hasFormat('code')) - - const anchorNode = selection.anchor.getNode() - const element = - anchorNode.getKey() === 'root' ? anchorNode : anchorNode.getTopLevelElementOrThrow() - const elementKey = element.getKey() - const elementDom = activeEditor.getElementByKey(elementKey) - - if (elementDom !== null) { - if ($isListNode(element)) { - const parentList = $getNearestNodeOfType(anchorNode, ListNode) - const type = parentList ? parentList.getListType() : element.getListType() - setBlockType(type as keyof typeof blockTypeToBlockName) - } else { - const type = $isHeadingNode(element) ? element.getTag() : element.getType() - if (type in blockTypeToBlockName) { - setBlockType(type as keyof typeof blockTypeToBlockName) - } - } - } - } - }, [activeEditor]) - - useEffect(() => { - return editor.registerCommand( - SELECTION_CHANGE_COMMAND, - (_payload, newEditor) => { - $updateToolbar() - setActiveEditor(newEditor) - return false - }, - COMMAND_PRIORITY_CRITICAL, - ) - }, [editor, $updateToolbar]) - - useEffect(() => { - return editor.registerUpdateListener(({ editorState }) => { - editorState.read(() => { - $updateToolbar() - }) - }) - }, [editor, $updateToolbar]) - - const formatParagraph = () => { - if (blockType !== 'paragraph') { - activeEditor.update(() => { - const selection = $getSelection() - if ($isRangeSelection(selection)) { - $setBlocksType(selection, () => $createParagraphNode()) - } - }) - } - } - - const formatHeading = (headingSize: HeadingTagType) => { - if (blockType !== headingSize) { - activeEditor.update(() => { - const selection = $getSelection() - if ($isRangeSelection(selection)) { - $setBlocksType(selection, () => $createHeadingNode(headingSize)) - } - }) - } - } - - const formatBulletList = () => { - if (blockType !== 'bullet') { - activeEditor.dispatchCommand(INSERT_UNORDERED_LIST_COMMAND, undefined) - } else { - activeEditor.dispatchCommand(REMOVE_LIST_COMMAND, undefined) - } - } - - const formatNumberedList = () => { - if (blockType !== 'number') { - activeEditor.dispatchCommand(INSERT_ORDERED_LIST_COMMAND, undefined) - } else { - activeEditor.dispatchCommand(REMOVE_LIST_COMMAND, undefined) - } - } - - const formatQuote = () => { - if (blockType !== 'quote') { - activeEditor.update(() => { - const selection = $getSelection() - if ($isRangeSelection(selection)) { - $setBlocksType(selection, () => $createQuoteNode()) - } - }) - } else { - formatParagraph() - } - } - - const formatCode = () => { - if (blockType !== 'code') { - activeEditor.update(() => { - const selection = $getSelection() - if ($isRangeSelection(selection)) { - $setBlocksType(selection, () => $createCodeNode()) - } - }) - } else { - formatParagraph() - } - } - - const handleBlockTypeChange = (value: string) => { - switch (value) { - case 'paragraph': - formatParagraph() - break - case 'h1': - formatHeading('h1') - break - case 'h2': - formatHeading('h2') - break - case 'h3': - formatHeading('h3') - break - case 'bullet': - formatBulletList() - break - case 'number': - formatNumberedList() - break - case 'quote': - formatQuote() - break - case 'code': - formatCode() - break - } - } - - return ( -
- - - - - { - activeEditor.dispatchCommand(FORMAT_TEXT_COMMAND, 'bold') - }} - aria-label="Format Bold" - > - - - { - activeEditor.dispatchCommand(FORMAT_TEXT_COMMAND, 'italic') - }} - aria-label="Format Italic" - > - - - { - activeEditor.dispatchCommand(FORMAT_TEXT_COMMAND, 'underline') - }} - aria-label="Format Underline" - > - - - { - activeEditor.dispatchCommand(FORMAT_TEXT_COMMAND, 'strikethrough') - }} - aria-label="Format Strikethrough" - > - - - { - activeEditor.dispatchCommand(FORMAT_TEXT_COMMAND, 'code') - }} - aria-label="Insert Code" - > - - -
- ) -} diff --git a/packages/webui/components/markdown-editor.css b/packages/webui/components/markdown-editor.css deleted file mode 100644 index 17bf933a..00000000 --- a/packages/webui/components/markdown-editor.css +++ /dev/null @@ -1,176 +0,0 @@ -.editor-container { - border-radius: 8px; - background: transparent; - color: inherit; - position: relative; - line-height: 1.5; - font-weight: 400; - text-align: left; - height: 100%; -} - -.editor-inner { - background: transparent; - position: relative; - height: 100%; -} - -.editor-input { - height: 100%; - min-height: 150px; - resize: none; - font-size: 15px; - caret-color: inherit; - position: relative; - tab-size: 1; - outline: 0; - padding: 15px 10px; - overflow-y: auto; -} - -.editor-placeholder { - color: #999; - overflow: hidden; - position: absolute; - text-overflow: ellipsis; - top: 15px; - left: 10px; - font-size: 15px; - user-select: none; - display: inline-block; - pointer-events: none; -} - -.editor-paragraph { - margin: 0 0 15px 0; - position: relative; -} - -.editor-paragraph:last-child { - margin-bottom: 0; -} - -.editor-heading-h1 { - font-size: 24px; - color: inherit; - font-weight: 400; - margin: 0; - padding: 0; -} - -.editor-heading-h2 { - font-size: 15px; - color: inherit; - font-weight: 700; - margin: 0; - padding: 0; - text-transform: uppercase; -} - -.editor-quote { - margin: 0; - margin-left: 20px; - font-size: 15px; - color: inherit; - border-left-color: #cecece; - border-left-width: 4px; - border-left-style: solid; - padding-left: 16px; -} - -.editor-list-ol { - padding: 0; - margin: 0; - margin-left: 16px; -} - -.editor-list-ul { - padding: 0; - margin: 0; - margin-left: 16px; -} - -.editor-listitem { - margin: 8px 32px 8px 32px; -} - -.editor-nested-listitem { - list-style-type: none; -} - -pre { - line-height: 1.5; -} - -.editor-code { - background-color: rgba(0, 0, 0, 0.05); - font-family: Menlo, Consolas, Monaco, monospace; - display: block; - padding: 8px 8px 8px 52px; - line-height: 1.53; - font-size: 13px; - margin: 0; - margin-top: 8px; - margin-bottom: 8px; - tab-size: 2; - overflow-x: auto; - position: relative; -} - -.dark .editor-code { - background-color: rgba(255, 255, 255, 0.05); -} - -.editor-code:before { - content: attr(data-gutter); - position: absolute; - background-color: transparent; - left: 0; - top: 0; - border-right: 1px solid transparent; - padding: 8px; - color: #777; - white-space: pre-wrap; - text-align: right; - min-width: 25px; -} - -.editor-text-bold { - font-weight: bold; -} - -.editor-text-italic { - font-style: italic; -} - -.editor-text-underline { - text-decoration: underline; -} - -.editor-text-strikethrough { - text-decoration: line-through; -} - -.editor-text-underlineStrikethrough { - text-decoration: underline line-through; -} - -.editor-text-code { - background-color: rgba(0, 0, 0, 0.05); - padding: 1px 0.25rem; - font-family: Menlo, Consolas, Monaco, monospace; - font-size: 94%; -} -.dark .editor-text-code { - background-color: rgba(255, 255, 255, 0.05); -} - -.editor-link { - color: rgb(33, 111, 219); - text-decoration: none; -} - -.editor-link:hover { - text-decoration: underline; - cursor: pointer; -} diff --git a/packages/webui/components/markdown-editor.tsx b/packages/webui/components/markdown-editor.tsx deleted file mode 100644 index e8b48629..00000000 --- a/packages/webui/components/markdown-editor.tsx +++ /dev/null @@ -1,180 +0,0 @@ -import { CodeHighlightNode, CodeNode } from '@lexical/code' -import { AutoLinkNode, LinkNode } from '@lexical/link' -import { ListItemNode, ListNode } from '@lexical/list' -import { - $convertFromMarkdownString, - $convertToMarkdownString, - TRANSFORMERS, -} from '@lexical/markdown' -import { AutoFocusPlugin } from '@lexical/react/LexicalAutoFocusPlugin' -import { LexicalComposer } from '@lexical/react/LexicalComposer' -import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' -import { ContentEditable } from '@lexical/react/LexicalContentEditable' -import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary' -import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin' -import { MarkdownShortcutPlugin } from '@lexical/react/LexicalMarkdownShortcutPlugin' -import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin' -import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin' -import { HeadingNode, QuoteNode } from '@lexical/rich-text' -import { TableCellNode, TableNode, TableRowNode } from '@lexical/table' -import { useEffect, useRef } from 'react' -import { ToolbarPlugin } from './lexical/plugins/ToolbarPlugin' -import './markdown-editor.css' - -const theme = { - ltr: 'ltr', - rtl: 'rtl', - placeholder: 'editor-placeholder', - paragraph: 'editor-paragraph', - quote: 'editor-quote', - heading: { - h1: 'editor-heading-h1', - h2: 'editor-heading-h2', - h3: 'editor-heading-h3', - h4: 'editor-heading-h4', - h5: 'editor-heading-h5', - h6: 'editor-heading-h6', - }, - list: { - nested: { - listitem: 'editor-nested-listitem', - }, - ol: 'editor-list-ol', - ul: 'editor-list-ul', - listitem: 'editor-listitem', - }, - image: 'editor-image', - link: 'editor-link', - text: { - bold: 'editor-text-bold', - italic: 'editor-text-italic', - overflowed: 'editor-text-overflowed', - hashtag: 'editor-text-hashtag', - underline: 'editor-text-underline', - strikethrough: 'editor-text-strikethrough', - underlineStrikethrough: 'editor-text-underlineStrikethrough', - code: 'editor-text-code', - }, - code: 'editor-code', - codeHighlight: { - atrule: 'editor-tokenAttr', - attr: 'editor-tokenAttr', - boolean: 'editor-tokenProperty', - builtin: 'editor-tokenSelector', - cdata: 'editor-tokenComment', - char: 'editor-tokenSelector', - class: 'editor-tokenFunction', - 'class-name': 'editor-tokenFunction', - comment: 'editor-tokenComment', - constant: 'editor-tokenProperty', - deleted: 'editor-tokenProperty', - doctype: 'editor-tokenComment', - entity: 'editor-tokenOperator', - function: 'editor-tokenFunction', - important: 'editor-tokenVariable', - inserted: 'editor-tokenSelector', - keyword: 'editor-tokenAttr', - namespace: 'editor-tokenVariable', - number: 'editor-tokenProperty', - operator: 'editor-tokenOperator', - prolog: 'editor-tokenComment', - property: 'editor-tokenProperty', - punctuation: 'editor-tokenPunctuation', - regex: 'editor-tokenVariable', - selector: 'editor-tokenSelector', - string: 'editor-tokenSelector', - symbol: 'editor-tokenProperty', - tag: 'editor-tokenProperty', - url: 'editor-tokenOperator', - variable: 'editor-tokenVariable', - }, -} - -function OnChangeMarkdownPlugin({ onChange }: { onChange: (markdown: string) => void }) { - const [editor] = useLexicalComposerContext() - return ( - { - editor.getEditorState().read(() => { - const markdown = $convertToMarkdownString(TRANSFORMERS) - onChange(markdown) - }) - }} - /> - ) -} - -function InitialContentPlugin({ content }: { content: string }) { - const [editor] = useLexicalComposerContext() - const isFirstRender = useRef(true) - - useEffect(() => { - if (isFirstRender.current) { - isFirstRender.current = false - editor.update(() => { - $convertFromMarkdownString(content, TRANSFORMERS) - }) - } - }, [editor, content]) - - return null -} - -export interface MarkdownEditorProps { - initialContent?: string - onChange?: (content: string) => void - readOnly?: boolean -} - -export function MarkdownEditor({ - initialContent = '', - onChange, - readOnly = false, -}: MarkdownEditorProps) { - const initialConfig = { - namespace: 'MarkdownEditor', - theme, - editable: !readOnly, - onError: (error: Error) => { - console.error(error) - }, - nodes: [ - HeadingNode, - ListNode, - ListItemNode, - QuoteNode, - CodeNode, - CodeHighlightNode, - TableNode, - TableCellNode, - TableRowNode, - AutoLinkNode, - LinkNode, - ], - } - - return ( - -
- {!readOnly && } -
- } - placeholder={ -
- {readOnly ? 'No content.' : 'Enter some text...'} -
- } - ErrorBoundary={LexicalErrorBoundary} - /> - - - - {onChange && } - -
-
-
- ) -} diff --git a/packages/webui/components/markdown-editor/index.ts b/packages/webui/components/markdown-editor/index.ts new file mode 100644 index 00000000..8a31eff8 --- /dev/null +++ b/packages/webui/components/markdown-editor/index.ts @@ -0,0 +1,2 @@ +export { MarkdownEditor, type MarkdownEditorProps } from './markdown-editor' +export { default } from './markdown-editor' diff --git a/packages/webui/components/markdown-editor/markdown-editor.css b/packages/webui/components/markdown-editor/markdown-editor.css new file mode 100644 index 00000000..d620e669 --- /dev/null +++ b/packages/webui/components/markdown-editor/markdown-editor.css @@ -0,0 +1,275 @@ +.shumai-editor-wrapper { + display: flex; + flex-direction: column; + border: 1px solid var(--border, #e2e8f0); + border-radius: 8px; + background-color: var(--background, #ffffff); + color: var(--foreground, #0f172a); + position: relative; + width: 100%; + box-sizing: border-box; + overflow: hidden; + font-family: inherit; +} + +.shumai-editor-toolbar { + display: flex; + align-items: center; + flex-wrap: wrap; + gap: 4px; + padding: 6px 8px; + border-bottom: 1px solid var(--border, #e2e8f0); + background-color: var(--muted, #f8fafc); + user-select: none; +} + +.shumai-editor-toolbar button { + display: inline-flex; + align-items: center; + justify-content: center; + width: 30px; + height: 30px; + border-radius: 6px; + border: none; + background: transparent; + color: var(--muted-foreground, #64748b); + cursor: pointer; + transition: all 0.15s ease; + padding: 0; +} + +.shumai-editor-toolbar button:hover:not(:disabled) { + background-color: var(--accent, #e2e8f0); + color: var(--accent-foreground, #0f172a); +} + +.shumai-editor-toolbar button:disabled { + opacity: 0.35; + cursor: not-allowed; +} + +.shumai-editor-toolbar button.active { + background-color: var(--primary, #0f172a); + color: var(--primary-foreground, #ffffff); +} + +.shumai-editor-toolbar select { + height: 30px; + padding: 0 8px; + border-radius: 6px; + border: 1px solid var(--border, #e2e8f0); + background-color: var(--background, #ffffff); + color: var(--foreground, #0f172a); + font-size: 13px; + cursor: pointer; + outline: none; +} + +.shumai-editor-toolbar .toolbar-divider { + width: 1px; + height: 20px; + background-color: var(--border, #e2e8f0); + margin: 0 4px; +} + +.shumai-editor-content-area { + position: relative; + min-height: 240px; + flex: 1; + width: 100%; + box-sizing: border-box; +} + +.editor-input { + min-height: 240px; + padding: 16px; + outline: none; + font-size: 15px; + line-height: 1.7; + color: inherit; + width: 100%; + box-sizing: border-box; +} + +.editor-placeholder { + position: absolute; + top: 16px; + left: 16px; + color: var(--muted-foreground, #94a3b8); + pointer-events: none; + user-select: none; + font-size: 15px; +} + +/* Typography styles in WYSIWYG mode */ +.editor-paragraph { + margin: 0 0 12px 0; + position: relative; +} + +.editor-heading-h1 { + display: block; + font-size: 26px; + font-weight: 700; + margin: 20px 0 12px 0; + line-height: 1.3; + color: inherit; +} + +.editor-heading-h2 { + display: block; + font-size: 20px; + font-weight: 600; + margin: 18px 0 10px 0; + line-height: 1.35; + color: inherit; +} + +.editor-heading-h3 { + display: block; + font-size: 17px; + font-weight: 600; + margin: 14px 0 8px 0; + line-height: 1.4; + color: inherit; +} + +.editor-quote { + display: block; + margin: 12px 0; + padding: 8px 16px; + border-left: 4px solid var(--primary, #3b82f6); + background-color: var(--muted, rgba(0, 0, 0, 0.03)); + border-radius: 0 6px 6px 0; + color: var(--muted-foreground, #64748b); + font-style: italic; +} + +.editor-list-ul { + padding: 0 0 0 24px; + margin: 8px 0 12px 0; + list-style-type: disc; +} + +.editor-list-ol { + padding: 0 0 0 24px; + margin: 8px 0 12px 0; + list-style-type: decimal; +} + +.editor-listitem { + margin: 4px 0; +} + +.editor-nested-listitem { + list-style-type: none; + margin: 0; +} + +/* Checklist / Task list */ +.editor-checklist { + list-style-type: none; + padding-left: 0; + margin: 8px 0 12px 0; +} + +.editor-listitem-checked, +.editor-listitem-unchecked { + position: relative; + list-style-type: none; + padding-left: 28px; + margin: 6px 0; + outline: none; + cursor: default; +} + +.editor-listitem-checked:before, +.editor-listitem-unchecked:before { + content: ''; + position: absolute; + left: 4px; + top: 4px; + width: 16px; + height: 16px; + border: 1.5px solid var(--border, #cbd5e1); + border-radius: 4px; + background-color: var(--background, #ffffff); + cursor: pointer; + box-sizing: border-box; +} + +.editor-listitem-checked:before { + background-color: var(--primary, #2563eb); + border-color: var(--primary, #2563eb); + background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 16 16' fill='white' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M12.207 4.793a1 1 0 010 1.414l-5 5a1 1 0 01-1.414 0l-2-2a1 1 0 011.414-1.414L6.5 9.086l4.293-4.293a1 1 0 011.414 0z'/%3E%3C/svg%3E"); + background-size: 12px; + background-position: center; + background-repeat: no-repeat; +} + +.editor-listitem-checked { + text-decoration: line-through; + opacity: 0.75; +} + +/* Inline text formats */ +.editor-text-bold { + font-weight: 600; +} + +.editor-text-italic { + font-style: italic; +} + +.editor-text-underline { + text-decoration: underline; +} + +.editor-text-strikethrough { + text-decoration: line-through; +} + +.editor-text-code { + font-family: + ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', + monospace; + font-size: 0.9em; + padding: 2px 6px; + border-radius: 4px; + background-color: var(--muted, #f1f5f9); + border: 1px solid var(--border, #e2e8f0); +} + +/* Code block (full-row block display) */ +code.editor-code-block, +.editor-code-block { + display: block !important; + width: 100% !important; + box-sizing: border-box !important; + font-family: + ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', + monospace; + font-size: 13.5px; + line-height: 1.6; + background-color: var(--muted, #f1f5f9); + color: var(--foreground, #0f172a); + border: 1px solid var(--border, #e2e8f0); + padding: 12px 16px; + border-radius: 6px; + margin: 12px 0; + overflow-x: auto; + white-space: pre-wrap; + word-break: break-all; + tab-size: 2; + position: relative; +} + +.editor-link { + color: var(--primary, #2563eb); + text-decoration: underline; + cursor: pointer; +} + +.editor-link:hover { + opacity: 0.8; +} diff --git a/packages/webui/components/markdown-editor/markdown-editor.test.tsx b/packages/webui/components/markdown-editor/markdown-editor.test.tsx new file mode 100644 index 00000000..09c7131c --- /dev/null +++ b/packages/webui/components/markdown-editor/markdown-editor.test.tsx @@ -0,0 +1,23 @@ +// @vitest-environment happy-dom +import { describe, it, expect } from 'vitest' +import { render, screen } from '@testing-library/react' +import React from 'react' +import { MarkdownEditor } from './markdown-editor' + +describe('MarkdownEditor', () => { + it('renders toolbar buttons and content editable area', () => { + render() + + expect(screen.getByRole('textbox')).toBeDefined() + expect(screen.getByRole('button', { name: /bold/i })).toBeDefined() + expect(screen.getByRole('button', { name: /italic/i })).toBeDefined() + expect(screen.getByRole('button', { name: /undo/i })).toBeDefined() + }) + + it('renders in readOnly mode without toolbar', () => { + const { container } = render() + + expect(container.querySelector('.shumai-editor-toolbar')).toBeNull() + expect(container.querySelector('.read-only')).toBeDefined() + }) +}) diff --git a/packages/webui/components/markdown-editor/markdown-editor.tsx b/packages/webui/components/markdown-editor/markdown-editor.tsx new file mode 100644 index 00000000..3ffbfaa9 --- /dev/null +++ b/packages/webui/components/markdown-editor/markdown-editor.tsx @@ -0,0 +1,150 @@ +import React, { useEffect, useRef } from 'react' +import { LexicalComposer, type InitialConfigType } from '@lexical/react/LexicalComposer' +import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin' +import { ContentEditable } from '@lexical/react/LexicalContentEditable' +import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin' +import { ListPlugin } from '@lexical/react/LexicalListPlugin' +import { CheckListPlugin } from '@lexical/react/LexicalCheckListPlugin' +import { TabIndentationPlugin } from '@lexical/react/LexicalTabIndentationPlugin' +import { LinkPlugin } from '@lexical/react/LexicalLinkPlugin' +import { AutoFocusPlugin } from '@lexical/react/LexicalAutoFocusPlugin' +import { MarkdownShortcutPlugin } from '@lexical/react/LexicalMarkdownShortcutPlugin' +import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin' +import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary' +import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' + +import { $convertFromMarkdownString, $convertToMarkdownString } from '@lexical/markdown' + +import { HeadingNode, QuoteNode } from '@lexical/rich-text' +import { ListNode, ListItemNode } from '@lexical/list' +import { CodeNode } from '@lexical/code' +import { TableNode, TableCellNode, TableRowNode } from '@lexical/table' +import { AutoLinkNode, LinkNode } from '@lexical/link' + +import { ToolbarPlugin } from './toolbar' +import { editorTheme } from './theme' +import { EDITOR_TRANSFORMERS } from './transformers' +import './markdown-editor.css' + +function MarkdownSyncPlugin({ + value, + initialContent = '', + onChange, +}: { + value?: string + initialContent?: string + onChange?: (markdown: string) => void +}) { + const [editor] = useLexicalComposerContext() + const isInitializedRef = useRef(false) + const lastEmittedMarkdownRef = useRef('') + + useEffect(() => { + if (!isInitializedRef.current) { + isInitializedRef.current = true + const startingContent = value !== undefined ? value : initialContent + if (startingContent) { + editor.update(() => { + $convertFromMarkdownString(startingContent, EDITOR_TRANSFORMERS) + }) + lastEmittedMarkdownRef.current = startingContent + } + } + }, [editor, initialContent, value]) + + useEffect(() => { + if ( + isInitializedRef.current && + value !== undefined && + value !== lastEmittedMarkdownRef.current + ) { + editor.update(() => { + $convertFromMarkdownString(value, EDITOR_TRANSFORMERS) + }) + lastEmittedMarkdownRef.current = value + } + }, [editor, value]) + + return ( + { + editorState.read(() => { + const markdown = $convertToMarkdownString(EDITOR_TRANSFORMERS) + if (markdown !== lastEmittedMarkdownRef.current) { + lastEmittedMarkdownRef.current = markdown + onChange?.(markdown) + } + }) + }} + /> + ) +} + +export interface MarkdownEditorProps { + initialContent?: string + value?: string + onChange?: (markdown: string) => void + placeholder?: string + readOnly?: boolean + autoFocus?: boolean + className?: string + hideToolbar?: boolean +} + +export function MarkdownEditor({ + initialContent = '', + value, + onChange, + placeholder = 'Write your markdown content here...', + readOnly = false, + autoFocus = false, + className = '', + hideToolbar = false, +}: MarkdownEditorProps) { + const initialConfig: InitialConfigType = { + namespace: 'ShumaiWysiwygMarkdownEditor', + theme: editorTheme, + editable: !readOnly, + onError: (error: Error) => { + console.error('[MarkdownEditor] Error:', error) + }, + nodes: [ + HeadingNode, + QuoteNode, + ListNode, + ListItemNode, + CodeNode, + TableNode, + TableCellNode, + TableRowNode, + AutoLinkNode, + LinkNode, + ], + } + + return ( +
+ + {!readOnly && !hideToolbar && } +
+ } + placeholder={
{placeholder}
} + ErrorBoundary={LexicalErrorBoundary} + /> + + + + + + + + {autoFocus && } +
+
+
+ ) +} + +export default MarkdownEditor diff --git a/packages/webui/components/markdown-editor/theme.ts b/packages/webui/components/markdown-editor/theme.ts new file mode 100644 index 00000000..6baae9e2 --- /dev/null +++ b/packages/webui/components/markdown-editor/theme.ts @@ -0,0 +1,32 @@ +import type { EditorThemeClasses } from 'lexical' + +export const editorTheme: EditorThemeClasses = { + paragraph: 'editor-paragraph', + quote: 'editor-quote', + heading: { + h1: 'editor-heading-h1', + h2: 'editor-heading-h2', + h3: 'editor-heading-h3', + }, + list: { + checklist: 'editor-checklist', + listitem: 'editor-listitem', + listitemChecked: 'editor-listitem-checked', + listitemUnchecked: 'editor-listitem-unchecked', + nested: { + listitem: 'editor-nested-listitem', + }, + ol: 'editor-list-ol', + ul: 'editor-list-ul', + }, + link: 'editor-link', + text: { + bold: 'editor-text-bold', + italic: 'editor-text-italic', + underline: 'editor-text-underline', + strikethrough: 'editor-text-strikethrough', + underlineStrikethrough: 'editor-text-underlineStrikethrough', + code: 'editor-text-code', + }, + code: 'editor-code-block', +} diff --git a/packages/webui/components/markdown-editor/toolbar.tsx b/packages/webui/components/markdown-editor/toolbar.tsx new file mode 100644 index 00000000..2244853c --- /dev/null +++ b/packages/webui/components/markdown-editor/toolbar.tsx @@ -0,0 +1,489 @@ +import React, { useCallback, useEffect, useState } from 'react' +import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' +import { + $getSelection, + $isRangeSelection, + $createParagraphNode, + FORMAT_TEXT_COMMAND, + SELECTION_CHANGE_COMMAND, + UNDO_COMMAND, + REDO_COMMAND, + CAN_UNDO_COMMAND, + CAN_REDO_COMMAND, + COMMAND_PRIORITY_CRITICAL, + COMMAND_PRIORITY_LOW, + type RangeSelection, + type TextNode, + type ElementNode, +} from 'lexical' +import { + $isListNode, + ListNode, + INSERT_ORDERED_LIST_COMMAND, + INSERT_UNORDERED_LIST_COMMAND, + INSERT_CHECK_LIST_COMMAND, + REMOVE_LIST_COMMAND, +} from '@lexical/list' +import { + $createHeadingNode, + $createQuoteNode, + $isHeadingNode, + type HeadingTagType, +} from '@lexical/rich-text' +import { $createCodeNode } from '@lexical/code' +import { $isLinkNode, TOGGLE_LINK_COMMAND } from '@lexical/link' +import { $isAtNodeEnd, $setBlocksType } from '@lexical/selection' +import { $getNearestNodeOfType } from '@lexical/utils' +import { + Bold, + Italic, + Underline, + Strikethrough, + Code, + Quote, + List, + ListOrdered, + ListTodo, + Heading1, + Heading2, + Heading3, + Link as LinkIcon, + RemoveFormatting, + Undo2, + Redo2, +} from 'lucide-react' + +const blockTypes = { + paragraph: 'Normal', + h1: 'Heading 1', + h2: 'Heading 2', + h3: 'Heading 3', + bullet: 'Bullet List', + number: 'Numbered List', + check: 'Check List', + quote: 'Quote', + code: 'Code Block', +} as const + +type BlockType = keyof typeof blockTypes + +function getSelectedNode(selection: RangeSelection): TextNode | ElementNode { + const anchor = selection.anchor + const focus = selection.focus + const anchorNode = selection.anchor.getNode() + const focusNode = selection.focus.getNode() + if (anchorNode === focusNode) { + return anchorNode + } + const isBackward = selection.isBackward() + if (isBackward) { + return $isAtNodeEnd(focus) ? anchorNode : focusNode + } else { + return $isAtNodeEnd(anchor) ? focusNode : anchorNode + } +} + +export function ToolbarPlugin() { + const [editor] = useLexicalComposerContext() + const [canUndo, setCanUndo] = useState(false) + const [canRedo, setCanRedo] = useState(false) + const [blockType, setBlockType] = useState('paragraph') + const [isBold, setIsBold] = useState(false) + const [isItalic, setIsItalic] = useState(false) + const [isUnderline, setIsUnderline] = useState(false) + const [isStrikethrough, setIsStrikethrough] = useState(false) + const [isCode, setIsCode] = useState(false) + const [isLink, setIsLink] = useState(false) + + const updateToolbar = useCallback(() => { + const selection = $getSelection() + if ($isRangeSelection(selection)) { + setIsBold(selection.hasFormat('bold')) + setIsItalic(selection.hasFormat('italic')) + setIsUnderline(selection.hasFormat('underline')) + setIsStrikethrough(selection.hasFormat('strikethrough')) + setIsCode(selection.hasFormat('code')) + + // Update link state + const node = getSelectedNode(selection) + const parent = node.getParent() + setIsLink($isLinkNode(parent) || $isLinkNode(node)) + + // Update block type + const anchorNode = selection.anchor.getNode() + const element = + anchorNode.getKey() === 'root' ? anchorNode : anchorNode.getTopLevelElementOrThrow() + const elementKey = element.getKey() + const elementDom = editor.getElementByKey(elementKey) + + if (elementDom !== null) { + if ($isListNode(element)) { + const parentList = $getNearestNodeOfType(anchorNode, ListNode) + const type = parentList ? parentList.getListType() : element.getListType() + setBlockType(type as BlockType) + } else if ($isHeadingNode(element)) { + setBlockType(element.getTag() as BlockType) + } else { + const type = element.getType() + if (type in blockTypes) { + setBlockType(type as BlockType) + } else { + setBlockType('paragraph') + } + } + } + } + }, [editor]) + + useEffect(() => { + return editor.registerCommand( + SELECTION_CHANGE_COMMAND, + () => { + updateToolbar() + return false + }, + COMMAND_PRIORITY_CRITICAL, + ) + }, [editor, updateToolbar]) + + useEffect(() => { + return editor.registerUpdateListener(({ editorState }) => { + editorState.read(() => { + updateToolbar() + }) + }) + }, [editor, updateToolbar]) + + useEffect(() => { + const unregisterUndo = editor.registerCommand( + CAN_UNDO_COMMAND, + (payload: boolean) => { + setCanUndo(payload) + return false + }, + COMMAND_PRIORITY_LOW, + ) + const unregisterRedo = editor.registerCommand( + CAN_REDO_COMMAND, + (payload: boolean) => { + setCanRedo(payload) + return false + }, + COMMAND_PRIORITY_LOW, + ) + return () => { + unregisterUndo() + unregisterRedo() + } + }, [editor]) + + const formatParagraph = () => { + editor.update(() => { + const selection = $getSelection() + if ($isRangeSelection(selection)) { + $setBlocksType(selection, () => $createParagraphNode()) + } + }) + } + + const formatHeading = (headingSize: HeadingTagType) => { + editor.update(() => { + const selection = $getSelection() + if ($isRangeSelection(selection)) { + $setBlocksType(selection, () => $createHeadingNode(headingSize)) + } + }) + } + + const formatBulletList = () => { + if (blockType !== 'bullet') { + editor.dispatchCommand(INSERT_UNORDERED_LIST_COMMAND, undefined) + } else { + editor.dispatchCommand(REMOVE_LIST_COMMAND, undefined) + } + } + + const formatNumberedList = () => { + if (blockType !== 'number') { + editor.dispatchCommand(INSERT_ORDERED_LIST_COMMAND, undefined) + } else { + editor.dispatchCommand(REMOVE_LIST_COMMAND, undefined) + } + } + + const formatCheckList = () => { + if (blockType !== 'check') { + editor.dispatchCommand(INSERT_CHECK_LIST_COMMAND, undefined) + } else { + editor.dispatchCommand(REMOVE_LIST_COMMAND, undefined) + } + } + + const formatQuote = () => { + if (blockType !== 'quote') { + editor.update(() => { + const selection = $getSelection() + if ($isRangeSelection(selection)) { + $setBlocksType(selection, () => $createQuoteNode()) + } + }) + } else { + formatParagraph() + } + } + + const formatCodeBlock = () => { + if (blockType !== 'code') { + editor.update(() => { + const selection = $getSelection() + if ($isRangeSelection(selection)) { + $setBlocksType(selection, () => $createCodeNode()) + } + }) + } else { + formatParagraph() + } + } + + const insertLink = useCallback(() => { + if (!isLink) { + const url = prompt('Enter link URL (e.g., https://example.com):') + if (url) { + editor.dispatchCommand(TOGGLE_LINK_COMMAND, url) + } + } else { + editor.dispatchCommand(TOGGLE_LINK_COMMAND, null) + } + }, [editor, isLink]) + + const clearFormatting = useCallback(() => { + editor.update(() => { + const selection = $getSelection() + if ($isRangeSelection(selection)) { + selection.setFormat(0) + $setBlocksType(selection, () => $createParagraphNode()) + } + }) + }, [editor]) + + const handleBlockTypeChange = (e: React.ChangeEvent) => { + const value = e.target.value as BlockType + switch (value) { + case 'paragraph': + formatParagraph() + break + case 'h1': + case 'h2': + case 'h3': + formatHeading(value) + break + case 'bullet': + formatBulletList() + break + case 'number': + formatNumberedList() + break + case 'check': + formatCheckList() + break + case 'quote': + formatQuote() + break + case 'code': + formatCodeBlock() + break + } + } + + return ( +
+ {/* Undo / Redo */} + + + +
+ + {/* Block Type Dropdown */} + + +
+ + {/* Inline Styles */} + + + + + + + +
+ + {/* Quick Insert / Block shortcuts */} + + + + + + + + +
+ + {/* Clear formatting */} + +
+ ) +} diff --git a/packages/webui/components/markdown-editor/transformers.ts b/packages/webui/components/markdown-editor/transformers.ts new file mode 100644 index 00000000..ccbf9da3 --- /dev/null +++ b/packages/webui/components/markdown-editor/transformers.ts @@ -0,0 +1,16 @@ +import { + CHECK_LIST, + ELEMENT_TRANSFORMERS, + MULTILINE_ELEMENT_TRANSFORMERS, + TEXT_FORMAT_TRANSFORMERS, + TEXT_MATCH_TRANSFORMERS, + type Transformer, +} from '@lexical/markdown' + +export const EDITOR_TRANSFORMERS: Transformer[] = [ + CHECK_LIST, + ...ELEMENT_TRANSFORMERS, + ...MULTILINE_ELEMENT_TRANSFORMERS, + ...TEXT_FORMAT_TRANSFORMERS, + ...TEXT_MATCH_TRANSFORMERS, +]