Skip to the content
v1.0.1-beta.17

Locked blocks, fields and variables

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

Two premium add-ons for documents with a fixed structure, such as contracts, offers and forms made from templates:

  • locking locks blocks, so the people filling a document in can't change them, and adds fill-in fields: named blanks in the text that can be filled in even inside locked blocks.
  • variables adds live variables, such as {{customer.name}}, filled in from the application's data wherever the document is shown, exported, printed or rendered on a server.
import {Locking, Fields} from "./dist/addons/scriviamo.locking.js"
import {Variables} from "./dist/addons/scriviamo.variables.js"

Locked blocks

A locked block can't be edited, moved or deleted; the blocks inside it are locked too. Its fill-in fields can still be filled in.

Lock and unlock blocks with Lock Blocks and Unlock Blocks in the Locked Blocks tool, or in code. Each is one undo step.

Locking.lock(editor, [heading, terms])   // the selected blocks, or the one being edited, by default
Locking.unlock(editor, [terms])

Locking a block marks it fixed, which is saved with it in the document's JSON. While the document is protected, fixed blocks are locked; with protection off, they can be changed, and show with a dashed outline and a lock. Documents are protected when they open.

Locking.setProtected(editor, false)   // lets the document's author change its fixed blocks
editor.addEventListener("protectionChanged", event => console.log(event.detail.protected))

The Locked Blocks tool offers locking, unlocking, Insert Field… and Protect Locked Blocks to a document's authors. For the people filling a document in, turn them off, and the tool offers only Next Field:

Locking.canChangeLocks = false              // for every editor
Locking.setCanChangeLocks(editor, false)    // or for one
MemberMeaning
Locking.lock(editor, nodes), Locking.unlock(editor, nodes)Marks blocks fixed, or not; one undo step each
Locking.isProtected(editor), Locking.setProtected(editor, value)Whether fixed blocks are locked; not saved
Locking.protectedByDefaultWhether editors start protected; true by default
Locking.isFixed(node)Whether a block is fixed, or inside one that is
Locking.fixedNodes(editor)The fixed blocks, outermost only

Locking stops people changing blocks in the editor. It isn't a security boundary: someone with the document's JSON can change it. Check a document on your server before relying on what its locked blocks say, such as by comparing them with the template it was made from.

Fill-in fields

A field is a named blank in the text, such as a contract's client or start date. Click one to fill it in; Tab goes to the next field, Shift-Tab to the one before, and Enter keeps the value. Fields with the same name share their value, so a client's name typed once fills it everywhere.

Add one with Insert Field…, which asks for its name, or in code:

Fields.insert(editor, "Client")                  // at the caret
Fields.fill(editor, {Client: "Acme", Start: "May 1"})   // one undo step
let values = Fields.values(editor)               // {Client: "Acme", Start: "May 1"}
let missing = Fields.empty(editor)               // names still empty, such as before sending
Fields.next(editor)                              // opens the next empty field
editor.addEventListener("fieldChanged", event => console.log(event.detail.name, event.detail.value))

An empty field shows its name. In exports it's a blank line: HTML has <span class="field field-empty" data-field="Client">__________</span>, and a filled field <span class="field" data-field="Client">Acme</span>; both import back, as does Markdown, which has the same elements.

Live variables

A variable is a placeholder for the application's data, such as a customer's name or an invoice's total. It shows the value; one without a value shows its name, as {{name}}.

Variables.setValues(editor, {customer: {name: "Acme", city: "Rome"}, total: 1234.5})

Names with dots read through nested objects, so customer.city is "Rome". Numbers and dates are written in the page's language, and lists joined with commas; set Variables.format to write them another way. Variables.values holds values for every editor without their own.

Setting values fills every variable in the document again, without an undo step, and a document that's loaded is filled as it loads. A variable keeps the value it was last filled with as its text, so exports, printing, PDF, Word and EPUB files, and every other format have the values in them.

Insert variables with the Insert Variable tool, which lists the variables the application offers, then Other Variable… for any name. Typing {{total}} makes it a variable too, for the variables offered.

Variables.definitions = [{name: "customer.name", label: "Customer name"}, {name: "total", label: "Total"}]
Variables.setDefinitions(editor, [...])   // or for one editor
Variables.convertTyped = "all"            // typed {{names}} become variables for any name; "none" for none
MemberMeaning
Variables.setValues(editor, values), Variables.valuesFor(editor)An editor's values
Variables.valuesThe values for editors without their own
Variables.insert(editor, name)Puts a variable at the caret; one undo step
Variables.refresh(editor)Fills the variables again, such as after changing the values object in place
Variables.names(editor)The names the document uses
Variables.lookup(values, name)A value by its dotted name
Variables.format(value, name, editor) => text

In HTML and Markdown a variable is <span class="variable" data-variable="customer.name">Acme</span>, which imports back by its name.

On a server

A headless editor fills variables too, so a server can render a document with each customer's data:

let editor = new Editor(null, {content: savedJSON})
Variables.setValues(editor, await loadCustomer(id))
let html = editor.toHTML(true)

Templates, fields and variables together

A template's {{fields}} are asked for once, as the template is inserted, and become plain text. Fill-in fields stay in the document, to be filled in, and changed, by whoever completes it. Variables stay too, and follow the application's data. A contract template might ask for its title when it's inserted, hold the client's details as variables, and leave a fill-in field for the date it's signed.