Example: change-tracking
Install this example with
shadcn:npx shadcn@latest add @prosekit/react-example-change-trackingnpx shadcn@latest add @prosekit/preact-example-change-trackingnpx shadcn@latest add @prosekit/svelte-example-change-trackingnpx shadcn@latest add @prosekit/vue-example-change-trackingimport { useMemo } from 'preact/hooks'
import { defineBasicExtension } from 'prosekit/basic'
import {
createEditor,
union,
} from 'prosekit/core'
import {
defineCommitViewer,
type Commit,
} from 'prosekit/extensions/commit'
import { defineReadonly } from 'prosekit/extensions/readonly'
import { ProseKit } from 'prosekit/preact'
export default function EditorDiff(props: { commit: Commit }) {
const editor = useMemo(() => {
const extension = union(
defineBasicExtension(),
defineReadonly(),
defineCommitViewer(props.commit),
)
return createEditor({ extension })
}, [props.commit])
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 dark:border-gray-700 shadow-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
<div className="relative w-full flex-1 box-border overflow-y-auto">
<div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
</div>
</div>
</ProseKit>
)
}import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import { useMemo } from 'preact/hooks'
import { defineBasicExtension } from 'prosekit/basic'
import {
createEditor,
union,
type NodeJSON,
} from 'prosekit/core'
import {
defineCommitRecorder,
type CommitRecorder,
} from 'prosekit/extensions/commit'
import { ProseKit } from 'prosekit/preact'
export default function EditorMain(props: {
commitRecorder: CommitRecorder
defaultContent?: NodeJSON
}) {
const editor = useMemo(() => {
const extension = union(
defineBasicExtension(),
defineCommitRecorder(props.commitRecorder),
)
return createEditor({ extension, defaultContent: props.defaultContent })
}, [props.commitRecorder, props.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 dark:border-gray-700 shadow-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
<div className="relative w-full flex-1 box-border overflow-y-auto">
<div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
</div>
</div>
</ProseKit>
)
}import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import {
useCallback,
useMemo,
useState,
} from 'preact/hooks'
import type { NodeJSON } from 'prosekit/core'
import {
CommitRecorder,
type Commit,
} from 'prosekit/extensions/commit'
import EditorDiff from './editor-diff'
import EditorMain from './editor-main'
export default function Editor() {
const [commits, setCommits] = useState<
{ id: string; date: Date; commit: Commit }[]
>([])
const [key, setKey] = useState(0)
const [defaultContent, setDefaultContent] = useState<NodeJSON | undefined>()
const commitRecorder = useMemo(() => new CommitRecorder(), [])
const handleCommit = useCallback(() => {
const commit = commitRecorder.commit()
if (!commit) return
const id = Math.random().toString(36).slice(2, 9)
setCommits((existing) => [{ id, date: new Date(), commit }, ...existing])
}, [commitRecorder])
const handleRestore = useCallback(
(id: string) => {
const index = commits.findIndex((commit) => commit.id === id)
const commit = commits[index]
if (index === -1 || !commit) return
const doc = commit.commit.doc
setDefaultContent(doc)
setCommits((commits) => commits.slice(index))
setKey((key) => key + 1)
},
[commits],
)
return (
<div className="grid grid-cols-2 gap-2">
<div className="flex flex-col gap-4">
<div className="max-h-md">
<EditorMain
key={key}
defaultContent={defaultContent}
commitRecorder={commitRecorder}
/>
</div>
<button onClick={handleCommit} className="inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-white dark:ring-offset-gray-950 transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-gray-900 dark:focus-visible:ring-gray-300 focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 border-0 bg-gray-900 dark:bg-gray-50 text-gray-50 dark:text-gray-900 hover:bg-gray-900/90 dark:hover:bg-gray-50/90 h-10 px-4 py-2">
Save
</button>
</div>
<div className="flex flex-col gap-4">
{commits.map((commit) => (
<div key={commit.id}>
<div className="max-h-md">
<EditorDiff commit={commit.commit} />
</div>
<div className="w-full inline-flex justify-between p-1 text-sm">
<span className="opacity-50">
{commit.date.toLocaleTimeString()}
</span>
<button
className="underline opacity-50 hover:opacity-100"
onClick={() => handleRestore(commit.id)}
>
Restore
</button>
</div>
</div>
))}
</div>
</div>
)
}export { default as ExampleEditor } from './editor'import { defineBasicExtension } from 'prosekit/basic'
import {
createEditor,
union,
} from 'prosekit/core'
import {
defineCommitViewer,
type Commit,
} from 'prosekit/extensions/commit'
import { defineReadonly } from 'prosekit/extensions/readonly'
import { ProseKit } from 'prosekit/react'
import { useMemo } from 'react'
export default function EditorDiff(props: { commit: Commit }) {
const editor = useMemo(() => {
const extension = union(
defineBasicExtension(),
defineReadonly(),
defineCommitViewer(props.commit),
)
return createEditor({ extension })
}, [props.commit])
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 dark:border-gray-700 shadow-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
<div className="relative w-full flex-1 box-border overflow-y-auto">
<div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
</div>
</div>
</ProseKit>
)
}import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import { defineBasicExtension } from 'prosekit/basic'
import {
createEditor,
union,
type NodeJSON,
} from 'prosekit/core'
import {
defineCommitRecorder,
type CommitRecorder,
} from 'prosekit/extensions/commit'
import { ProseKit } from 'prosekit/react'
import { useMemo } from 'react'
export default function EditorMain(props: {
commitRecorder: CommitRecorder
defaultContent?: NodeJSON
}) {
const editor = useMemo(() => {
const extension = union(
defineBasicExtension(),
defineCommitRecorder(props.commitRecorder),
)
return createEditor({ extension, defaultContent: props.defaultContent })
}, [props.commitRecorder, props.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 dark:border-gray-700 shadow-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
<div className="relative w-full flex-1 box-border overflow-y-auto">
<div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
</div>
</div>
</ProseKit>
)
}import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import type { NodeJSON } from 'prosekit/core'
import {
CommitRecorder,
type Commit,
} from 'prosekit/extensions/commit'
import {
useCallback,
useMemo,
useState,
} from 'react'
import EditorDiff from './editor-diff'
import EditorMain from './editor-main'
export default function Editor() {
const [commits, setCommits] = useState<
{ id: string; date: Date; commit: Commit }[]
>([])
const [key, setKey] = useState(0)
const [defaultContent, setDefaultContent] = useState<NodeJSON | undefined>()
const commitRecorder = useMemo(() => new CommitRecorder(), [])
const handleCommit = useCallback(() => {
const commit = commitRecorder.commit()
if (!commit) return
const id = Math.random().toString(36).slice(2, 9)
setCommits((commits) => [{ id, date: new Date(), commit }, ...commits])
}, [commitRecorder])
const handleRestore = useCallback(
(id: string) => {
const index = commits.findIndex((commit) => commit.id === id)
const commit = commits[index]
if (index === -1 || !commit) return
const doc = commit.commit.doc
setDefaultContent(doc)
setCommits((commits) => commits.slice(index))
setKey((key) => key + 1)
},
[commits],
)
return (
<div className="grid grid-cols-2 gap-2">
<div className="flex flex-col gap-4">
<div className="max-h-md">
<EditorMain
key={key}
defaultContent={defaultContent}
commitRecorder={commitRecorder}
/>
</div>
<button onClick={handleCommit} className="inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-white dark:ring-offset-gray-950 transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-gray-900 dark:focus-visible:ring-gray-300 focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 border-0 bg-gray-900 dark:bg-gray-50 text-gray-50 dark:text-gray-900 hover:bg-gray-900/90 dark:hover:bg-gray-50/90 h-10 px-4 py-2">
Save
</button>
</div>
<div className="flex flex-col gap-4">
{commits.map((commit) => (
<div key={commit.id}>
<div className="max-h-md">
<EditorDiff commit={commit.commit} />
</div>
<div className="w-full inline-flex justify-between p-1 text-sm">
<span className="opacity-50">
{commit.date.toLocaleTimeString()}
</span>
<button
className="underline opacity-50 hover:opacity-100"
onClick={() => handleRestore(commit.id)}
>
Restore
</button>
</div>
</div>
))}
</div>
</div>
)
}'use client'
export { default as ExampleEditor } from './editor'<script lang="ts">
import { defineBasicExtension } from 'prosekit/basic'
import {
createEditor,
union,
} from 'prosekit/core'
import {
defineCommitViewer,
type Commit,
} from 'prosekit/extensions/commit'
import { defineReadonly } from 'prosekit/extensions/readonly'
import { ProseKit } from 'prosekit/svelte'
interface Props {
commit: Commit
}
const props: Props = $props()
let extension = $derived(union(
defineBasicExtension(),
defineReadonly(),
defineCommitViewer(props.commit),
))
let editor = $derived(createEditor({ extension }))
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 dark:border-gray-700 shadow-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
<div class="relative w-full flex-1 box-border overflow-y-auto">
<div use:mount class="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
</div>
</div>
</ProseKit><script lang="ts">
import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import { defineBasicExtension } from 'prosekit/basic'
import {
createEditor,
union,
type NodeJSON,
} from 'prosekit/core'
import {
defineCommitRecorder,
type CommitRecorder,
} from 'prosekit/extensions/commit'
import { ProseKit } from 'prosekit/svelte'
interface Props {
commitRecorder: CommitRecorder
defaultContent?: NodeJSON
key?: number
}
const props: Props = $props()
let extension = $derived(union(
defineBasicExtension(),
defineCommitRecorder(props.commitRecorder),
))
let editor = $derived(createEditor({ extension, defaultContent: props.defaultContent }))
const mount = (element: HTMLElement) => {
editor.mount(element)
return { destroy: () => editor.unmount() }
}
</script>
{#key props.key}
<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 dark:border-gray-700 shadow-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
<div class="relative w-full flex-1 box-border overflow-y-auto">
<div use:mount class="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
</div>
</div>
</ProseKit>
{/key}<script lang="ts">
import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import type { NodeJSON } from 'prosekit/core'
import {
CommitRecorder,
type Commit,
} from 'prosekit/extensions/commit'
import EditorDiff from './editor-diff.svelte'
import EditorMain from './editor-main.svelte'
let commits: { id: string; date: Date; commit: Commit }[] = []
let key = 0
let defaultContent: NodeJSON | undefined = undefined
const commitRecorder = new CommitRecorder()
function handleCommit() {
const commit = commitRecorder.commit()
if (!commit) return
const id = Math.random().toString(36).slice(2, 9)
commits = [{ id, date: new Date(), commit }, ...commits]
}
function handleRestore(id: string) {
const index = commits.findIndex((commit) => commit.id === id)
const commit = commits[index]
if (index === -1 || !commit) return
const doc = commit.commit.doc
defaultContent = doc
commits = commits.slice(index)
key = key + 1
}
</script>
<div class="grid grid-cols-2 gap-2">
<div class="flex flex-col gap-4">
<div class="max-h-md">
<EditorMain {key} {defaultContent} {commitRecorder} />
</div>
<button onclick={handleCommit} class="inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-white dark:ring-offset-gray-950 transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-gray-900 dark:focus-visible:ring-gray-300 focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 border-0 bg-gray-900 dark:bg-gray-50 text-gray-50 dark:text-gray-900 hover:bg-gray-900/90 dark:hover:bg-gray-50/90 h-10 px-4 py-2">
Save
</button>
</div>
<div class="flex flex-col gap-4">
{#each commits as commit (commit.id)}
<div>
<div class="max-h-md">
<EditorDiff commit={commit.commit} />
</div>
<div class="w-full inline-flex justify-between p-1 text-sm">
<span class="opacity-50">
{commit.date.toLocaleTimeString()}
</span>
<button
class="underline opacity-50 hover:opacity-100"
onclick={() => handleRestore(commit.id)}
>
Restore
</button>
</div>
</div>
{/each}
</div>
</div>export { default as ExampleEditor } from './editor.svelte'<script setup lang="ts">
import { defineBasicExtension } from 'prosekit/basic'
import {
createEditor,
union,
} from 'prosekit/core'
import {
defineCommitViewer,
type Commit,
} from 'prosekit/extensions/commit'
import { defineReadonly } from 'prosekit/extensions/readonly'
import { ProseKit } from 'prosekit/vue'
import {
ref,
watchPostEffect,
} from 'vue'
const props = defineProps<{ commit: Commit }>()
const editor = createEditor({
extension: union(
defineBasicExtension(),
defineReadonly(),
defineCommitViewer(props.commit),
),
})
const editorRef = ref<HTMLDivElement | null>(null)
watchPostEffect((onCleanup) => {
editor.mount(editorRef.value)
onCleanup(() => editor.unmount())
})
</script>
<template>
<ProseKit :editor="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 dark:border-gray-700 shadow-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
<div class="relative w-full flex-1 box-border overflow-y-auto">
<div ref="editorRef" class="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
</div>
</div>
</ProseKit>
</template><script setup lang="ts">
import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import { defineBasicExtension } from 'prosekit/basic'
import {
createEditor,
union,
type NodeJSON,
} from 'prosekit/core'
import {
defineCommitRecorder,
type CommitRecorder,
} from 'prosekit/extensions/commit'
import { ProseKit } from 'prosekit/vue'
import {
ref,
watchPostEffect,
} from 'vue'
const props = defineProps<{
commitRecorder: CommitRecorder
defaultContent?: NodeJSON
}>()
const editor = createEditor({
extension: union(
defineBasicExtension(),
defineCommitRecorder(props.commitRecorder),
),
defaultContent: props.defaultContent,
})
const editorRef = ref<HTMLDivElement | null>(null)
watchPostEffect((onCleanup) => {
editor.mount(editorRef.value)
onCleanup(() => editor.unmount())
})
</script>
<template>
<ProseKit :editor="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 dark:border-gray-700 shadow-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
<div class="relative w-full flex-1 box-border overflow-y-auto">
<div ref="editorRef" class="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
</div>
</div>
</ProseKit>
</template><script setup lang="ts">
import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import type { NodeJSON } from 'prosekit/core'
import {
CommitRecorder,
type Commit,
} from 'prosekit/extensions/commit'
import { ref } from 'vue'
import EditorDiff from './editor-diff.vue'
import EditorMain from './editor-main.vue'
const commits = ref<{ id: string; date: Date; commit: Commit }[]>([])
const key = ref(0)
const defaultContent = ref<NodeJSON | undefined>()
const commitRecorder = new CommitRecorder()
function handleCommit() {
const commit = commitRecorder.commit()
if (!commit) return
const id = Math.random().toString(36).slice(2, 9)
commits.value = [{ id, date: new Date(), commit }, ...commits.value]
}
function handleRestore(id: string) {
const index = commits.value.findIndex((commit) => commit.id === id)
const commit = commits.value[index]
if (index === -1 || !commit) return
const doc = commit.commit.doc
defaultContent.value = doc
commits.value = commits.value.slice(index)
key.value = key.value + 1
}
</script>
<template>
<div class="grid grid-cols-2 gap-2">
<div class="flex flex-col gap-4">
<div class="max-h-md">
<EditorMain
:key="key"
:default-content="defaultContent"
:commit-recorder="commitRecorder"
/>
</div>
<button class="inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-white dark:ring-offset-gray-950 transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-gray-900 dark:focus-visible:ring-gray-300 focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 border-0 bg-gray-900 dark:bg-gray-50 text-gray-50 dark:text-gray-900 hover:bg-gray-900/90 dark:hover:bg-gray-50/90 h-10 px-4 py-2" @click="handleCommit">
Save
</button>
</div>
<div class="flex flex-col gap-4">
<div v-for="commit in commits" :key="commit.id">
<div class="max-h-md">
<EditorDiff :commit="commit.commit" />
</div>
<div class="w-full inline-flex justify-between p-1 text-sm">
<span class="opacity-50">
{{ commit.date.toLocaleTimeString() }}
</span>
<button
class="underline opacity-50 hover:opacity-100"
@click="() => handleRestore(commit.id)"
>
Restore
</button>
</div>
</div>
</div>
</div>
</template>export { default as ExampleEditor } from './editor.vue'