Skip to the content
v1.0.1-beta.17

Templates

Premium add-on. This page covers a premium add-on, included in Scriviamo Pro and Pro + Collaboration. See pricing

The templates add-on adds templates: reusable content, from a few blocks to a whole document, that people insert from the "/" chooser, the Insert menu, command search or the Templates tool.

import "scriviamo/dist/addons/scriviamo.templates.js"     // and scriviamo.templates.css

Giving editors templates

Templates live in a TemplateLibrary. Every editor uses Templates.library unless it is given its own:

import {Templates, TemplateLibrary} from "scriviamo/dist/addons/scriviamo.templates.js"

Templates.library.load(await (await fetch("/templates")).json())

// Or one editor's own library:
Templates.useLibrary(editor, new TemplateLibrary({templates: [...]}))

A template is plain data, so it can come from your server, a file or code:

Templates.library.add({
  name: "Meeting notes",
  description: "Agenda, decisions and actions",
  icon: "bi bi-people",                       // a Bootstrap Icons class
  content: {nodes: [                          // a document's JSON, or an array of node JSON
    new HeadingNode("Meeting with {{Client}}", 2).toJSON(),
    new TextNode("{{date}} · Attendees: {{Client}}, {{Owner}}").toJSON()
  ]}
})

A document an editor saved with editor.toJSON() works as content as it is, so any document can become a template.

Fields

Text in a template can hold fields in double braces, such as {{Client}}. When the template is inserted, the editor asks for each field once, and fills in every place it appears, keeping its formatting. {{date}} and {{time}} fill themselves in with today's date and the time; add your own automatic fields to Templates.automaticFields.

Saving templates

The Templates tool's Save as Template saves the selected blocks, or the whole document when nothing is selected, under a name the user gives. It is added to the editor's library, which raises changed, and the editor raises templateSaved with {template}. Store the library when it changes:

Templates.library.addEventListener("changed", () => {
  fetch("/templates", {method: "PUT", body: JSON.stringify(Templates.library.toJSON())})
})

Or give a library a storageKey, and it keeps its templates in the browser's local storage:

Templates.library = new TemplateLibrary({storageKey: "my-app-templates"})

In code

MemberDoes
Templates.insert(editor, template, replace, values)Inserts a template after the node being worked on, or at the end, as one undo step. values fills fields without asking. Resolves with the inserted nodes.
Templates.saveSelection(editor, name, description)Saves the selection, or the document, as a template; asks for the name when it isn't given
Templates.showPicker(editor)Opens the Templates tool's picker
template.fieldsThe fields a template asks for
library.add(template), remove(id), item(id), items(), load(list), toJSON()Manage a library

Adding to the insertion chooser

The add-on lists templates in the insertion chooser through an editor extension's insertionChoices(editor), which any extension can use to offer more there and in command search: each choice has a caption, an icon, keywords, and choose(replace).