tsx
import 'prosekit/basic/style.css'
import { createEditor } from 'prosekit/core'
import { ProseKit } from 'prosekit/react'
import { useMemo } from 'react'
import { defaultContent } from './default-doc'
import { defineExtension } from './extension'
import Toolbar from './toolbar'
export default function Editor() {
const editor = useMemo(() => {
const extension = defineExtension()
return createEditor({ extension, defaultContent })
}, [])
return (
<ProseKit editor={editor}>
<div className='box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 shadow dark:border-zinc-700 flex flex-col bg-white dark:bg-neutral-900'>
<Toolbar />
<div className='relative w-full flex-1 box-border overflow-y-scroll'>
<div ref={editor.mount} className='ProseMirror box-border min-h-full px-[max(4rem,_calc(50%-20rem))] py-8 outline-none outline-0 [&_span[data-mention="user"]]:text-blue-500 [&_span[data-mention="tag"]]:text-violet-500 [&_pre]:text-white [&_pre]:bg-zinc-800'></div>
</div>
</div>
</ProseKit>
)
}
ts
import { defineBasicExtension } from 'prosekit/basic'
import { union } from 'prosekit/core'
import { defineCodeBlock } from 'prosekit/extensions/code-block'
export function defineExtension() {
return union(defineBasicExtension(), defineCodeBlock())
}
export type EditorExtension = ReturnType<typeof defineExtension>
tsx
import { ThemeSelector } from './theme-selector'
export default function Toolbar() {
return (
<div className='z-2 box-border border-zinc-200 dark:border-zinc-800 border-solid border-l-0 border-r-0 border-t-0 border-b flex flex-wrap gap-1 p-2 items-center'>
<ThemeSelector />
</div>
)
}
ts
import type { NodeJSON } from 'prosekit/core'
const js = `async function main() {\n while (true) {\n await sleep();\n await eat();\n await code('JavaScript!');\n }\n}`
const py = `async def main():\n while True:\n await sleep()\n await eat()\n await code("Python!")`
const go = `func main() {\n\tfor {\n\t\tsleep()\n\t\teat()\n\t\tcode("Go!")\n\t}\n}`
export const defaultContent: NodeJSON = {
type: 'doc',
content: [
{
type: 'codeBlock',
attrs: { language: 'javascript' },
content: [{ type: 'text', text: js }],
},
{
type: 'codeBlock',
attrs: { language: 'python' },
content: [{ type: 'text', text: py }],
},
{
type: 'codeBlock',
attrs: { language: 'go' },
content: [{ type: 'text', text: go }],
},
],
}
tsx
import {
defineCodeBlockShiki,
shikiBundledThemesInfo,
type ShikiBundledTheme,
} from 'prosekit/extensions/code-block'
import { useExtension } from 'prosekit/react'
import { useMemo, useState } from 'react'
export function ThemeSelector() {
const [theme, setTheme] = useState('github-dark')
const extension = useMemo(() => {
return defineCodeBlockShiki({ themes: [theme as ShikiBundledTheme] })
}, [theme])
useExtension(extension)
return (
<>
<label htmlFor="code-block-theme-selector">Theme</label>
<select
id="code-block-theme-selector"
value={theme}
onChange={(event) => setTheme(event.target.value)}
className='outline-unset focus-visible:outline-unset flex items-center justify-center rounded-md p-2 font-medium transition focus-visible:ring-2 text-sm focus-visible:ring-zinc-900 dark:focus-visible:ring-zinc-300 disabled:pointer-events-none min-w-9 min-h-9 disabled:opacity-50 hover:disabled:opacity-50 bg-transparent hover:bg-zinc-100 dark:hover:bg-zinc-800 data-[state=on]:bg-gray-200 dark:data-[state=on]:bg-gray-700'
>
{shikiBundledThemesInfo.map((info) => (
<option key={info.id} value={info.id}>
{info.id}
</option>
))}
</select>
</>
)
}