Getting started
Early development
Writeon is in early development (0.x). The API may still change before 1.0.
Install
sh
npm install @writeon/core
# and the adapter for your framework, if you use one
npm install @writeon/react # React 18+
npm install @writeon/vue # Vue 3.3+sh
pnpm add @writeon/core
# and the adapter for your framework, if you use one
pnpm add @writeon/react # React 18+
pnpm add @writeon/vue # Vue 3.3+sh
# Yarn does not install peer dependencies: list them
yarn add @writeon/core prosemirror-commands prosemirror-history prosemirror-inputrules \
prosemirror-keymap prosemirror-model prosemirror-schema-list \
prosemirror-state prosemirror-transform prosemirror-view
# and the adapter for your framework, if you use one
yarn add @writeon/react # React 18+
yarn add @writeon/vue # Vue 3.3+sh
bun add @writeon/core
# and the adapter for your framework, if you use one
bun add @writeon/react # React 18+
bun add @writeon/vue # Vue 3.3+ProseMirror packages are peer dependencies, so your app has one copy of each. npm (7+), pnpm and bun install them for you; with Yarn, add them yourself as shown.
Your first editor
An editor is made of extensions. basic() gives paragraphs, headings, bold, italic, underline, strike, inline code, links, lists, blockquotes, line breaks and undo/redo.
Every editor needs a label: the accessible name screen readers announce for the editable area.
ts
import { createEditor } from '@writeon/core'
import { toggleBold } from '@writeon/core/commands'
import { basic } from '@writeon/core/presets'
// <div id="editor"></div> and <button id="bold">Bold</button>
const element = document.querySelector<HTMLElement>('#editor')
if (!element) throw new Error('No #editor element')
const editor = createEditor({
element,
extensions: basic(),
// Required: screen readers announce the editor by this name.
label: 'Article text',
placeholder: 'Start writing…',
onChange: (editor) => console.log(editor.getJSON()),
})
// Commands are factories: call toggleBold(), then run the result.
document.querySelector('#bold')?.addEventListener('click', () => {
editor.run(toggleBold())
})tsx
import type { DocJSON } from '@writeon/core'
import { toggleBold } from '@writeon/core/commands'
import { basic } from '@writeon/core/presets'
import {
EditorContent,
useEditor,
useEditorState,
} from '@writeon/react'
// Create the extensions once, outside components.
const extensions = basic()
interface Props {
initial: DocJSON
onSave: (doc: DocJSON) => void
}
export function ArticleEditor({ initial, onSave }: Props) {
const editor = useEditor({
extensions,
content: initial,
label: 'Article text',
onChange: (editor) => onSave(editor.getJSON()),
})
// Re-renders only when this value changes.
const isBold = useEditorState(editor, (s) => s.marks.has('bold'))
return (
<>
<button
type="button"
aria-pressed={isBold}
onClick={() => editor.run(toggleBold())}
>
Bold
</button>
<EditorContent editor={editor} className="article-editor" />
</>
)
}vue
<script setup lang="ts">
import type { DocJSON } from '@writeon/core'
import { toggleBold } from '@writeon/core/commands'
import { basic } from '@writeon/core/presets'
import { EditorContent, useEditor, useEditorState } from '@writeon/vue'
const props = defineProps<{ initial: DocJSON }>()
const emit = defineEmits<{ save: [doc: DocJSON] }>()
const editor = useEditor({
extensions: basic(),
content: props.initial,
label: 'Article text',
onChange: (editor) => emit('save', editor.getJSON()),
})
// Updates only when this value changes.
const isBold = useEditorState(editor, (s) => s.marks.has('bold'))
</script>
<template>
<button
type="button"
:aria-pressed="isBold"
@click="editor.run(toggleBold())"
>
Bold
</button>
<EditorContent :editor="editor" class="article-editor" />
</template>Styles
The core ships no styles. ProseMirror needs this one rule so spaces and line breaks show correctly:
css
.ProseMirror {
white-space: pre-wrap;
word-wrap: break-word;
}Or use the default kit: a themeable toolbar and content styles in one CSS file.
Next
- Core concepts: documents, extensions, commands and state.
- Saving and loading: store JSON, convert to HTML or Markdown.
- Security: validate on the server before you store or render.