Commit
Track changes in the editor and display them in a diff view. This is built on top of prosemirror-changeset.
import { 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)
if (index === -1) return
const doc = commits[index].commit.doc
setDefaultContent(doc)
setCommits((existing) => existing.slice(index))
setKey((value) => value + 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)
if (index === -1) return
const doc = commits[index].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'To record changes, first create a CommitRecorder instance. Then, pass it to the defineCommitRecorder function.
import {
CommitRecorder class CommitRecorder ,
defineCommitRecorder function defineCommitRecorder(commitRecorder: CommitRecorder): PlainExtensionDefine an extension that can record the changes in the editor. ,
} from 'prosekit/extensions/commit'
const commitRecorder const commitRecorder: CommitRecorder = new CommitRecorder new CommitRecorder(): CommitRecorder ()
const extension const extension: PlainExtension = defineCommitRecorder function defineCommitRecorder(commitRecorder: CommitRecorder): PlainExtensionDefine an extension that can record the changes in the editor. (commitRecorder const commitRecorder: CommitRecorder )When you want to save the changes, call the commit method. This will return a JSON Commit object if there are any changes. You can then serialize and save this object to your database.
import type { Commit } from 'prosekit/extensions/commit'
const commit const commit: Commit | null = commitRecorder const commitRecorder: CommitRecorder .commit CommitRecorder.commit(): Commit | nullReturn a commit object including all changes since the last commit. `null`
will be returned if there is no change. ()To display the changes, create another editor instance and call defineCommitViewer with the Commit object.
import { defineCommitViewer function defineCommitViewer(commit: Commit): PlainExtensionDefine an extension to display the changes from the given commit in the editor. } from 'prosekit/extensions/commit'
const extension const extension: PlainExtension = defineCommitViewer function defineCommitViewer(commit: Commit): PlainExtensionDefine an extension to display the changes from the given commit in the editor. (commit const commit: Commit )To highlight the changes, you must load the style.css file or define your own styles for the .prosekit-commit-addition and .prosekit-commit-deletion classes.
import 'prosekit/extensions/commit/style.css'