Placeholder
Show some placeholder text when the current text block is empty or the whole document is empty.
import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import {
useCallback,
useMemo,
} from 'preact/hooks'
import {
createEditor,
jsonFromNode,
type NodeJSON,
} from 'prosekit/core'
import type { ProseMirrorNode } from 'prosekit/pm/model'
import {
ProseKit,
useDocChange,
} from 'prosekit/preact'
import { defineExtension } from './extension'
export default function Editor(props: {
defaultContent?: NodeJSON
onDocUpdate?: (doc: NodeJSON) => void
}) {
const editor = useMemo(() => {
const extension = defineExtension()
return createEditor({ extension, defaultContent: props.defaultContent })
}, [props.defaultContent])
const handleDocChange = useCallback(
(doc: ProseMirrorNode) => props.onDocUpdate?.(jsonFromNode(doc)),
[props.onDocUpdate],
)
useDocChange(handleDocChange, { editor })
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 { defineBasicExtension } from 'prosekit/basic'
import { union } from 'prosekit/core'
import { definePlaceholder } from 'prosekit/extensions/placeholder'
export function defineExtension() {
return union(
defineBasicExtension(),
definePlaceholder({ placeholder: 'Type something...' }),
)
}
export type EditorExtension = ReturnType<typeof defineExtension>export { default as ExampleEditor } from './editor'import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import {
createEditor,
jsonFromNode,
type NodeJSON,
} from 'prosekit/core'
import type { ProseMirrorNode } from 'prosekit/pm/model'
import {
ProseKit,
useDocChange,
} from 'prosekit/react'
import {
useCallback,
useMemo,
} from 'react'
import { defineExtension } from './extension'
export default function Editor(props: {
defaultContent?: NodeJSON
onDocUpdate?: (doc: NodeJSON) => void
}) {
const editor = useMemo(() => {
const extension = defineExtension()
return createEditor({ extension, defaultContent: props.defaultContent })
}, [props.defaultContent])
const handleDocChange = useCallback(
(doc: ProseMirrorNode) => props.onDocUpdate?.(jsonFromNode(doc)),
[props.onDocUpdate],
)
useDocChange(handleDocChange, { editor })
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 { defineBasicExtension } from 'prosekit/basic'
import { union } from 'prosekit/core'
import { definePlaceholder } from 'prosekit/extensions/placeholder'
export function defineExtension() {
return union(
defineBasicExtension(),
definePlaceholder({ placeholder: 'Type something...' }),
)
}
export type EditorExtension = ReturnType<typeof defineExtension>export { default as ExampleEditor } from './editor'import { definePlaceholder function definePlaceholder(options: PlaceholderOptions): PlainExtensionAdd a placeholder text to the editor when the current block or document is
empty. } from 'prosekit/extensions/placeholder'
const extension const extension: PlainExtension = definePlaceholder function definePlaceholder(options: PlaceholderOptions): PlainExtensionAdd a placeholder text to the editor when the current block or document is
empty. ({ placeholder PlaceholderOptions.placeholder: string | ((state: EditorState) => string)The placeholder to use. It can be a static string or a function that
receives the current editor state and returns a string. : 'Type Something...' })You can also show dynamic placeholders based on the current state.
import type { HeadingAttrs } from 'prosekit/extensions/heading'
import { definePlaceholder function definePlaceholder(options: PlaceholderOptions): PlainExtensionAdd a placeholder text to the editor when the current block or document is
empty. } from 'prosekit/extensions/placeholder'
const extension const extension: PlainExtension = definePlaceholder function definePlaceholder(options: PlaceholderOptions): PlainExtensionAdd a placeholder text to the editor when the current block or document is
empty. ({
placeholder PlaceholderOptions.placeholder: string | ((state: EditorState) => string)The placeholder to use. It can be a static string or a function that
receives the current editor state and returns a string. : (state state: EditorState ) => {
// Get the current node at the text selection
const node const node: Node = state state: EditorState .selection EditorState.selection: SelectionThe selection. .$from Selection.$from: ResolvedPosThe resolved lower bound of the selection's main range. .node ResolvedPos.node(depth?: number | null): NodeThe ancestor node at the given level. `p.node(p.depth)` is the
same as `p.parent`. ()
// Show different placeholders based on the node type
if (node const node: Node .type Node.type: NodeTypeThe type of node that this is. .name NodeType.name: stringThe name the node type has in this schema. === 'heading') {
const attrs const attrs: HeadingAttrs = node const node: Node .attrs Node.attrs: AttrsAn object mapping attribute names to values. The kind of
attributes allowed and required are
[determined](https://prosemirror.net/docs/ref/#model.NodeSpec.attrs) by the node type. as HeadingAttrs
return `Heading ${attrs const attrs: HeadingAttrs .level HeadingAttrs.level: number }`
}
return 'Type something...'
},
})Note that you would need to import the style file for the extension to work.