Skip to content
svelte
<script lang="ts">
import 'prosekit/basic/style.css'

import {
  createEditor,
  jsonFromNode,
  type NodeJSON,
} from 'prosekit/core'
import {
  ProseKit,
  useDocChange,
} from 'prosekit/svelte'
import { defineExtension } from './extension'

export let defaultContent: NodeJSON | undefined = undefined
export let onDocUpdate: ((doc: NodeJSON) => void) | undefined = undefined

const extension = defineExtension()
const editor = createEditor({ extension, defaultContent })

useDocChange((doc) => onDocUpdate?.(jsonFromNode(doc)), { editor })

const mount = (element: HTMLElement) => {
  editor.mount(element)
  return { destroy: () => editor.unmount() }
}
</script>

<ProseKit {editor}>
  <div class='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'>
    <div class='relative w-full flex-1 box-border overflow-y-scroll'>
      <div use:mount class='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 { defineEmojiEnterRule } from './emoji'

export function defineExtension() {
  return union(defineBasicExtension(), defineEmojiEnterRule())
}

export type EditorExtension = ReturnType<typeof defineExtension>
ts
import { defineEnterRule } from 'prosekit/extensions/enter-rule'

/**
 * Converts the text before the text cursor into an emoji when pressing `Enter`.
 */
export function defineEmojiEnterRule() {
  return defineEnterRule({
    regex: /:(apple|banana):$/,
    handler: ({ match, from, to, state }) => {
      const text = match[1] as 'apple' | 'banana'
      const emoji = text === 'apple' ? '🍎' : '🍌'
      return state.tr.replaceWith(from, to, state.schema.text(emoji))
    },
  })
}