|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { |
| 4 | + CONTENT_BLOCKS_DATA_TYPE, |
| 5 | + type ContentBlock, |
| 6 | + ContentBlocksPlugin, |
| 7 | + FilePlugin, |
| 8 | + ImagePlugin, |
| 9 | + type MediaLists, |
| 10 | + extractMediaLists, |
| 11 | +} from '@lobehub/editor'; |
| 12 | +import { CodeEditor, Collapse, Highlighter } from '@lobehub/ui'; |
| 13 | +import { debounce } from 'es-toolkit'; |
| 14 | +import { useMemo, useState } from 'react'; |
| 15 | + |
| 16 | +import { DEFAULT_HEADLESS_EDITOR_PLUGINS, createHeadlessEditor } from '@/headless'; |
| 17 | + |
| 18 | +import sample from './data.json'; |
| 19 | + |
| 20 | +const noopUpload = async (): Promise<{ url: string }> => ({ url: '' }); |
| 21 | + |
| 22 | +const buildHeadless = () => |
| 23 | + createHeadlessEditor({ |
| 24 | + additionalPlugins: [ |
| 25 | + [ImagePlugin, { handleUpload: noopUpload, renderImage: () => null }], |
| 26 | + [FilePlugin, { decorator: () => null, handleUpload: noopUpload }], |
| 27 | + ContentBlocksPlugin, |
| 28 | + ], |
| 29 | + plugins: DEFAULT_HEADLESS_EDITOR_PLUGINS, |
| 30 | + }); |
| 31 | + |
| 32 | +interface Result { |
| 33 | + blocks: ContentBlock[]; |
| 34 | + error?: string; |
| 35 | + media: MediaLists; |
| 36 | +} |
| 37 | + |
| 38 | +const extract = (jsonText: string): Result => { |
| 39 | + let parsed: unknown; |
| 40 | + try { |
| 41 | + parsed = JSON.parse(jsonText); |
| 42 | + } catch (error) { |
| 43 | + return { |
| 44 | + blocks: [], |
| 45 | + error: `Invalid JSON: ${(error as Error).message}`, |
| 46 | + media: { fileList: [], imageList: [] }, |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + const headless = buildHeadless(); |
| 51 | + try { |
| 52 | + headless.hydrate({ content: parsed, type: 'json' }); |
| 53 | + const blocks = headless.kernel.getDocument( |
| 54 | + CONTENT_BLOCKS_DATA_TYPE, |
| 55 | + ) as unknown as ContentBlock[]; |
| 56 | + return { blocks: blocks ?? [], media: extractMediaLists(blocks ?? []) }; |
| 57 | + } catch (error) { |
| 58 | + return { |
| 59 | + blocks: [], |
| 60 | + error: (error as Error).message, |
| 61 | + media: { fileList: [], imageList: [] }, |
| 62 | + }; |
| 63 | + } finally { |
| 64 | + headless.destroy(); |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +const initialInput = JSON.stringify(sample, null, 2); |
| 69 | + |
| 70 | +export default () => { |
| 71 | + const [input, setInput] = useState(initialInput); |
| 72 | + const [result, setResult] = useState<Result>(() => extract(initialInput)); |
| 73 | + |
| 74 | + const debouncedExtract = useMemo( |
| 75 | + () => debounce((value: string) => setResult(extract(value)), 200), |
| 76 | + [], |
| 77 | + ); |
| 78 | + |
| 79 | + const handleChange = (next: string) => { |
| 80 | + setInput(next); |
| 81 | + debouncedExtract(next); |
| 82 | + }; |
| 83 | + |
| 84 | + return ( |
| 85 | + <Collapse |
| 86 | + defaultActiveKey={['input', 'blocks', 'media']} |
| 87 | + items={[ |
| 88 | + { |
| 89 | + children: ( |
| 90 | + <CodeEditor |
| 91 | + language="json" |
| 92 | + onValueChange={handleChange} |
| 93 | + style={{ fontSize: 12 }} |
| 94 | + value={input} |
| 95 | + variant="borderless" |
| 96 | + /> |
| 97 | + ), |
| 98 | + key: 'input', |
| 99 | + label: 'Editor JSON (input)', |
| 100 | + }, |
| 101 | + { |
| 102 | + children: ( |
| 103 | + <Highlighter language="json" style={{ fontSize: 12, padding: 16 }} variant="borderless"> |
| 104 | + {result.error ? `// ${result.error}` : JSON.stringify(result.blocks, null, 2)} |
| 105 | + </Highlighter> |
| 106 | + ), |
| 107 | + key: 'blocks', |
| 108 | + label: `content-blocks (${result.blocks.length})`, |
| 109 | + }, |
| 110 | + { |
| 111 | + children: ( |
| 112 | + <Highlighter language="json" style={{ fontSize: 12, padding: 16 }} variant="borderless"> |
| 113 | + {JSON.stringify(result.media, null, 2)} |
| 114 | + </Highlighter> |
| 115 | + ), |
| 116 | + key: 'media', |
| 117 | + label: `extractMediaLists → ${result.media.imageList.length} image · ${result.media.fileList.length} file`, |
| 118 | + }, |
| 119 | + ]} |
| 120 | + padding={{ body: 0 }} |
| 121 | + style={{ border: 'none', borderRadius: 0, width: '100%' }} |
| 122 | + variant="outlined" |
| 123 | + /> |
| 124 | + ); |
| 125 | +}; |
0 commit comments