React and Next.js
scriviamo-react puts the editor in a React app as a component. This page walks through
setting it up, keeping and saving the document, and rendering documents on the server with
Next.js. The package's README lists every prop.
Installing
npm install scriviamo scriviamo-react bootstrap bootstrap-icons
React 18 or later. Load the stylesheets once, such as in your app's root component or layout: Bootstrap's CSS and Bootstrap Icons, which Scriviamo's interface uses, then Scriviamo's own and those of the add-ons you use.
import "bootstrap/dist/css/bootstrap.min.css"
import "bootstrap-icons/font/bootstrap-icons.min.css"
import "scriviamo/dist/scriviamo.css"
import "scriviamo/dist/addons/scriviamo.callouts.css"
Add-ons register their node types when they're imported. Import them in one module, and import that module wherever documents are edited or rendered, so every editor knows every node type:
// lib/scriviamo.ts
import "scriviamo/addons/callouts"
import "scriviamo/addons/math"
export {Editor} from "scriviamo"
An editor
import {useState} from "react"
import {ScriviamoEditor} from "scriviamo-react"
import "./lib/scriviamo"
export function NotesPage() {
const [doc, setDoc] = useState("# Notes\n\nStart writing.")
return <ScriviamoEditor value={doc} format="markdown" onChange={setDoc} statusBar style={{height: 500}} />
}
Give the editor a height, or set adaptiveHeight to let it grow with the document.
valueandonChangekeep the document in your state, informat:"json"(the default, which keeps everything),"html"or"markdown". Changingvaluefrom outside replaces the document; passing the editor's own changes back in doesn't reload anything.- Leave
valueout for an editor that keeps its own document, and read it from the editor when you need it. changeDelayreports changes only once typing pauses, for large documents.- Every other prop is an editor setting, applied as it changes:
readOnly,toolbarVisible,localeand the others in the wrappers README.
Saving
autosave saves the document a moment after each change, and the status bar shows whether it
has:
const autosave = useMemo(() => ({
save: async (editor: Editor) => {
let response = await fetch(`/api/documents/${id}`, {method: "PUT", headers: {"Content-Type": "application/json"}, body: editor.toJSON()})
if (!response.ok) throw new Error(`Saving failed: ${response.status}`)
},
warnBeforeLeaving: true
}), [id])
<ScriviamoEditor value={initial} autosave={autosave} statusBar />
Keep the object the same between renders, with useMemo, as here. See
Autosave for its settings.
The editor itself
For anything the props don't cover, use the Editor: onReady gives it, and so does a ref.
const ref = useRef<ScriviamoEditorHandle>(null)
<ScriviamoEditor ref={ref} />
// later
ref.current?.editor?.print()
events listens to the editor's events by name, such as {linkPressed: event => …}. See
The editor for what it offers.
The inspector
ScriviamoInspector, from scriviamo-react/inspector, shows the settings of the selected blocks,
or of the document. It needs the premium inspector add-on, imported first:
import "@scriviamo-pro/pro/addons/inspector"
import "@scriviamo-pro/pro/dist/addons/scriviamo.inspector.css"
import {ScriviamoInspector} from "scriviamo-react/inspector"
const [editor, setEditor] = useState<Editor | null>(null)
<ScriviamoEditor onReady={setEditor} />
<ScriviamoInspector editor={editor} style={{width: 320}} />
See Getting started for installing the Pro packages.
Next.js
The editor works in the browser, so the component that shows it is a client component
("use client"). On the server it renders only the empty element the editor fills, and the
editor is created once the page is running.
Documents render on the server without the editor's interface: a headless editor, made with
new Editor(null), loads the saved JSON and returns its HTML and the stylesheet it needs, in a
server component or a route handler. With a schema, it also checks a document before saving it, whatever the browser sent:
import {Editor} from "@/lib/scriviamo"
export default async function ViewPage({params}) {
let document = await loadDocument((await params).id)
let editor = new Editor(null, {content: document.content})
return (
<>
<style>{editor.exportCSS()}</style>
<article dangerouslySetInnerHTML={{__html: editor.toHTML()}} />
</>
)
}
See Server rendering for what a headless editor can do. The Next.js starter is a complete app with all of this: a list of documents, an editor page with autosave, a save endpoint, and server-rendered pages.