Example: page
Page layout with configurable paper settings.
Install this example with
shadcn: npx shadcn@latest add @prosekit/react-example-pagenpx shadcn@latest add @prosekit/svelte-example-pageimport 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import 'prosekit/extensions/page/style.css'
import './zoom.css'
import { clsx, createEditor, type NodeJSON } from 'prosekit/core'
import { ProseKit } from 'prosekit/react'
import { useMemo, useState } from 'react'
import { sampleContent } from '../../sample/sample-doc-page'
import { defineExtension } from './extension'
import PaperController from './paper-controller'
interface EditorProps {
initialContent?: NodeJSON
}
export default function Editor(props: EditorProps) {
const defaultContent = props.initialContent ?? sampleContent
const editor = useMemo(() => {
const extension = defineExtension()
return createEditor({
extension,
defaultContent,
})
}, [defaultContent])
const [zoom, setZoom] = useState(50)
return (
<ProseKit editor={editor}>
<div className="relative w-max min-w-full flex flex-col flex-1 box-border overflow-auto">
<PaperController zoom={zoom} setZoom={setZoom} />
<div
data-editor-zoom="true"
style={{ '--zoom': zoom / 100 } as React.CSSProperties}
ref={editor.mount}
className={clsx('ProseMirror', 'self-center box-border min-h-full m-0 p-10 print:p-0 outline-hidden')}
/>
</div>
</ProseKit>
)
}import { defineBasicExtension } from 'prosekit/basic'
import { union } from 'prosekit/core'
import { definePageBreak } from 'prosekit/extensions/page'
export function defineExtension() {
return union(
defineBasicExtension(),
definePageBreak(),
)
}
export type EditorExtension = ReturnType<typeof defineExtension>'use client'
export { default as ExampleEditor } from './editor'import { definePageRendering, type PageRenderingOptions } from 'prosekit/extensions/page'
import { useExtension } from 'prosekit/react'
import { useEffect, useId, useMemo, useState } from 'react'
// Paper sizes in pixels at 96 DPI
const PAPER_SIZES = {
A3: { short: 1123, long: 1587 },
A4: { short: 794, long: 1123 },
A5: { short: 559, long: 794 },
B4: { short: 945, long: 1334 },
B5: { short: 665, long: 945 },
letter: { short: 816, long: 1056 },
} as const
type PaperSize = keyof typeof PAPER_SIZES
type Orientation = 'portrait' | 'landscape'
export default function PaperController({
zoom,
setZoom,
}: {
zoom: number
setZoom: (zoom: number) => void
}) {
const id = useId()
const [paperSize, setPaperSize] = useState<PaperSize>('A4')
const [orientation, setOrientation] = useState<Orientation>('portrait')
const [margin, setMargin] = useState(70)
const [enablePageLayout, setEnablePageLayout] = useState(true)
const pageRenderingOptions: PageRenderingOptions = useMemo(() => {
const { short, long } = PAPER_SIZES[paperSize]
const pageWidth = orientation === 'portrait' ? short : long
const pageHeight = orientation === 'portrait' ? long : short
return {
pageWidth,
pageHeight,
marginTop: margin,
marginRight: margin,
marginBottom: margin,
marginLeft: margin,
}
}, [paperSize, orientation, margin])
useEffect(() => {
const styleId = 'print-page-style'
let style = document.getElementById(styleId) as HTMLStyleElement | null
if (!style) {
style = document.createElement('style')
style.id = styleId
document.head.appendChild(style)
}
style.textContent = `@page { size: ${paperSize} ${orientation}; margin: 0; }`
return () => {
style.textContent = ''
}
}, [paperSize, orientation])
const extension = useMemo(() => {
return enablePageLayout ? definePageRendering(pageRenderingOptions) : null
}, [pageRenderingOptions, enablePageLayout])
useExtension(extension)
return (
<div
data-paper-controller={paperSize}
className="grid grid-cols-[auto_1fr] gap-2 w-min border p-2 bg-[Canvas] sticky top-2 left-2 z-10 print:hidden text-xs"
>
<label htmlFor={`${id}-page`}>Page</label>
<select
id={`${id}-page`}
value={enablePageLayout ? 'Enabled' : 'Disabled'}
onChange={(e) => setEnablePageLayout(e.target.value === 'Enabled')}
className="rounded border disabled:opacity-50"
>
<option value="Enabled">Enabled</option>
<option value="Disabled">Disabled</option>
</select>
<label htmlFor={`${id}-paper`}>Paper Size</label>
<select
id={`${id}-paper`}
value={paperSize}
onChange={(e) => setPaperSize(e.target.value as PaperSize)}
disabled={!enablePageLayout}
className="rounded border disabled:opacity-50"
>
<option value="A3">A3</option>
<option value="A4">A4</option>
<option value="A5">A5</option>
<option value="B4">B4</option>
<option value="B5">B5</option>
<option value="letter">Letter</option>
</select>
<label htmlFor={`${id}-orientation`}>Orientation</label>
<select
id={`${id}-orientation`}
value={orientation}
onChange={(e) => setOrientation(e.target.value as Orientation)}
disabled={!enablePageLayout}
className="rounded border disabled:opacity-50"
>
<option value="portrait">Portrait</option>
<option value="landscape">Landscape</option>
</select>
<label htmlFor={`${id}-margin`}>Margin</label>
<select
id={`${id}-margin`}
value={String(margin)}
onChange={(e) => setMargin(Number.parseInt(e.target.value, 10))}
disabled={!enablePageLayout}
className="rounded border disabled:opacity-50"
>
<option value="30">Narrow</option>
<option value="70">Normal</option>
<option value="120">Wide</option>
</select>
<label htmlFor={`${id}-zoom`}>Zoom</label>
<select
id={`${id}-zoom`}
value={String(zoom)}
onChange={(e) => setZoom(Number.parseInt(e.target.value, 10))}
className="rounded border disabled:opacity-50"
>
<option value="25">25%</option>
<option value="50">50%</option>
<option value="75">75%</option>
<option value="100">100%</option>
<option value="125">125%</option>
<option value="150">150%</option>
<option value="200">200%</option>
</select>
</div>
)
}div[data-editor-zoom] {
zoom: var(--zoom);
}
@media print {
div[data-editor-zoom] {
zoom: 1;
}
}import type { NodeJSON } from 'prosekit/core'
export const sampleContent: NodeJSON = {
type: 'doc',
content: [
{
type: 'heading',
attrs: { level: 1 },
content: [{ type: 'text', text: 'Page Layout Demo' }],
},
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'This is the first page.',
},
],
},
{
type: 'paragraph',
content: [
{
type: 'text',
text:
'The content below will be on a new page because of a page break. You can insert a page break by pressing Command+Enter on Mac or Ctrl+Enter on Windows and Linux.',
},
],
},
{ type: 'pageBreak' },
{
type: 'heading',
attrs: { level: 1 },
content: [{ type: 'text', text: 'Page 2' }],
},
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'This is the second page.',
},
],
},
{
type: 'paragraph',
content: [
{
type: 'text',
text:
'When the content on a page exceeds the available height, it will automatically flow to the next page. This is similar to how traditional word processors like Microsoft Word handle pagination.',
},
],
},
{ type: 'image', attrs: { src: 'https://placehold.co/600x500' } },
{ type: 'image', attrs: { src: 'https://placehold.co/600x500' } },
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'The images above exceed the available height on the second page, so they automatically flow to the next page.',
},
],
},
{
type: 'heading',
attrs: { level: 2 },
content: [{ type: 'text', text: 'Known Limitation' }],
},
{
type: 'paragraph',
content: [
{
type: 'text',
text:
'Page breaks only occur between block elements. A single block taller than the remaining space on the page will overflow to the next page rather than split. In other words, you cannot split a node like paragraph or a table across pages. The paragraph below demonstrates this.',
},
],
},
{
type: 'paragraph',
content: [
{
type: 'text',
marks: [{ type: 'bold' }],
text: 'This is a very long paragraph that demonstrates the limitation of block-level pagination.',
},
{
type: 'text',
text: ' ',
},
{
type: 'text',
marks: [{ type: 'italic' }],
text:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.',
},
],
},
],
}<script lang="ts">
import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import 'prosekit/extensions/page/style.css'
import './zoom.css'
import { clsx, createEditor, type NodeJSON } from 'prosekit/core'
import { ProseKit } from 'prosekit/svelte'
import { untrack } from 'svelte'
import { sampleContent } from '../../sample/sample-doc-page'
import { defineExtension } from './extension'
import PaperController from './paper-controller.svelte'
const props: {
initialContent?: NodeJSON
} = $props()
const extension = defineExtension()
const defaultContent = untrack(() => props.initialContent ?? sampleContent)
const editor = createEditor({ extension, defaultContent })
let zoom = $state(50)
</script>
<ProseKit {editor}>
<div class="relative w-max min-w-full flex flex-col flex-1 box-border overflow-auto">
<PaperController bind:zoom />
<div
data-editor-zoom="true"
style:--zoom={zoom / 100}
{@attach editor.mount}
class={clsx('ProseMirror', 'self-center box-border min-h-full m-0 p-10 print:p-0 outline-hidden')}
>
</div>
</div>
</ProseKit>import { defineBasicExtension } from 'prosekit/basic'
import { union } from 'prosekit/core'
import { definePageBreak } from 'prosekit/extensions/page'
export function defineExtension() {
return union(
defineBasicExtension(),
definePageBreak(),
)
}
export type EditorExtension = ReturnType<typeof defineExtension>export { default as ExampleEditor } from './editor.svelte'<script lang="ts">
import { definePageRendering, type PageRenderingOptions } from 'prosekit/extensions/page'
import { useExtension } from 'prosekit/svelte'
import { toStore } from 'svelte/store'
// Paper sizes in pixels at 96 DPI
const PAPER_SIZES = {
A3: { short: 1123, long: 1587 },
A4: { short: 794, long: 1123 },
A5: { short: 559, long: 794 },
B4: { short: 945, long: 1334 },
B5: { short: 665, long: 945 },
letter: { short: 816, long: 1056 },
} as const
type PaperSize = keyof typeof PAPER_SIZES
type Orientation = 'portrait' | 'landscape'
let {
zoom = $bindable(50),
}: {
zoom: number
} = $props()
let id = $props.id()
let paperSize = $state<PaperSize>('A4')
let orientation = $state<Orientation>('portrait')
let margin = $state(70)
let enablePageLayout = $state(true)
const pageRenderingOptions: PageRenderingOptions = $derived.by(() => {
const { short, long } = PAPER_SIZES[paperSize]
const pageWidth = orientation === 'portrait' ? short : long
const pageHeight = orientation === 'portrait' ? long : short
return {
pageWidth,
pageHeight,
marginTop: margin,
marginRight: margin,
marginBottom: margin,
marginLeft: margin,
}
})
$effect(() => {
const styleId = 'print-page-style'
let style = document.getElementById(styleId) as HTMLStyleElement | null
if (!style) {
style = document.createElement('style')
style.id = styleId
document.head.appendChild(style)
}
style.textContent = `@page { size: ${paperSize} ${orientation}; margin: 0; }`
return () => {
style.textContent = ''
}
})
const extension = $derived(enablePageLayout ? definePageRendering(pageRenderingOptions) : null)
const extensionStore = toStore(() => extension)
useExtension(extensionStore)
</script>
<div
data-paper-controller={paperSize}
class="grid grid-cols-[auto_1fr] gap-2 w-min border p-2 bg-[Canvas] sticky top-2 left-2 z-10 print:hidden text-xs"
>
<label for="{id}-page">Page</label>
<select
id="{id}-page"
value={enablePageLayout ? 'Enabled' : 'Disabled'}
onchange={(e) => {
enablePageLayout = e.currentTarget.value === 'Enabled'
}}
class="rounded border disabled:opacity-50"
>
<option value="Enabled">Enabled</option>
<option value="Disabled">Disabled</option>
</select>
<label for="{id}-paper">Paper Size</label>
<select
id="{id}-paper"
value={paperSize}
onchange={(e) => {
paperSize = e.currentTarget.value as PaperSize
}}
disabled={!enablePageLayout}
class="rounded border disabled:opacity-50"
>
<option value="A3">A3</option>
<option value="A4">A4</option>
<option value="A5">A5</option>
<option value="B4">B4</option>
<option value="B5">B5</option>
<option value="letter">Letter</option>
</select>
<label for="{id}-orientation">Orientation</label>
<select
id="{id}-orientation"
value={orientation}
onchange={(e) => {
orientation = e.currentTarget.value as Orientation
}}
disabled={!enablePageLayout}
class="rounded border disabled:opacity-50"
>
<option value="portrait">Portrait</option>
<option value="landscape">Landscape</option>
</select>
<label for="{id}-margin">Margin</label>
<select
id="{id}-margin"
value={String(margin)}
onchange={(e) => {
margin = Number.parseInt(e.currentTarget.value, 10)
}}
disabled={!enablePageLayout}
class="rounded border disabled:opacity-50"
>
<option value="30">Narrow</option>
<option value="70">Normal</option>
<option value="120">Wide</option>
</select>
<label for="{id}-zoom">Zoom</label>
<select
id="{id}-zoom"
value={String(zoom)}
onchange={(e) => {
zoom = Number.parseInt(e.currentTarget.value, 10)
}}
class="rounded border disabled:opacity-50"
>
<option value="25">25%</option>
<option value="50">50%</option>
<option value="75">75%</option>
<option value="100">100%</option>
<option value="125">125%</option>
<option value="150">150%</option>
<option value="200">200%</option>
</select>
</div>div[data-editor-zoom] {
zoom: var(--zoom);
}
@media print {
div[data-editor-zoom] {
zoom: 1;
}
}import type { NodeJSON } from 'prosekit/core'
export const sampleContent: NodeJSON = {
type: 'doc',
content: [
{
type: 'heading',
attrs: { level: 1 },
content: [{ type: 'text', text: 'Page Layout Demo' }],
},
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'This is the first page.',
},
],
},
{
type: 'paragraph',
content: [
{
type: 'text',
text:
'The content below will be on a new page because of a page break. You can insert a page break by pressing Command+Enter on Mac or Ctrl+Enter on Windows and Linux.',
},
],
},
{ type: 'pageBreak' },
{
type: 'heading',
attrs: { level: 1 },
content: [{ type: 'text', text: 'Page 2' }],
},
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'This is the second page.',
},
],
},
{
type: 'paragraph',
content: [
{
type: 'text',
text:
'When the content on a page exceeds the available height, it will automatically flow to the next page. This is similar to how traditional word processors like Microsoft Word handle pagination.',
},
],
},
{ type: 'image', attrs: { src: 'https://placehold.co/600x500' } },
{ type: 'image', attrs: { src: 'https://placehold.co/600x500' } },
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'The images above exceed the available height on the second page, so they automatically flow to the next page.',
},
],
},
{
type: 'heading',
attrs: { level: 2 },
content: [{ type: 'text', text: 'Known Limitation' }],
},
{
type: 'paragraph',
content: [
{
type: 'text',
text:
'Page breaks only occur between block elements. A single block taller than the remaining space on the page will overflow to the next page rather than split. In other words, you cannot split a node like paragraph or a table across pages. The paragraph below demonstrates this.',
},
],
},
{
type: 'paragraph',
content: [
{
type: 'text',
marks: [{ type: 'bold' }],
text: 'This is a very long paragraph that demonstrates the limitation of block-level pagination.',
},
{
type: 'text',
text: ' ',
},
{
type: 'text',
marks: [{ type: 'italic' }],
text:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.',
},
],
},
],
}