Skip to the content
v1.0.1-beta.17

The toolbar

The toolbar is a list of ToolbarItems in editor.toolbarItems. Users can rearrange it with the customizer, and applications can add their own items.

Built-in tools

Each tool has an identifier, which activateToolbarItem runs and saved toolbar states list.

IdentifierTool
insertInsert a block, from the same list as the "/" chooser. With at least editor.insertMenuSearchThreshold block types to offer (5 by default), it has a search field that filters it by name and keywords. The arrow keys and Enter choose either way
tableInsert a table, choosing its size in a grid: point at a size, or move with the arrow keys, then click or press Enter. Without a size, as from activateToolbarItem("table"), the table is 3 × 3.
undo, redo
blocktypeBlock type: paragraph, heading levels, quote, code and so on
font, sizeFont and size menus
bold, italic, underline, strike
inlineA menu of the other inline formats
code, highlight, keyboard, superscript, subscriptThose formats as separate buttons
textcolor, backgroundText and background colors
linkAdd, change or remove a link
alignmentText or node alignment
lineheight, linespacingLine height and extra line spacing
list:0, list:1, list:2Bulleted, numbered and checklist lists
outdent, indentList levels
clearformatting
commentAdd a comment
documentbackgroundThe document's background color
find, replaceThe search bar
print
searchCommand search
separator, flexiblespaceA divider, and space that pushes the following items to the far end

The default toolbar shows most of these. code, highlight, keyboard, superscript, subscript, comment, documentbackground, find and replace are offered in the customizer. Editor.catalogIdentifiers lists every tool, including those registered by extensions.

editor.activateToolbarItem("bold")

Adding your own items

Add a ToolbarItem to editor.toolbarItems. The editor shows it, and clicking it raises toolbarItemPressed:

import {ToolbarItem, ToolbarItemKinds, ToolbarMenuItem} from "scriviamo"

editor.toolbarItems.add(new ToolbarItem("save", "Save", "bi bi-floppy"))

let exportItem = new ToolbarItem("export", "Export", "bi bi-box-arrow-up", ToolbarItemKinds.menu)
exportItem.menuItems.push(new ToolbarMenuItem("pdf", "PDF"), new ToolbarMenuItem("docx", "Word"))
editor.toolbarItems.add(exportItem)

editor.addEventListener("toolbarItemPressed", event => {
  let {item, menuItem} = event.detail
  if (item.identifier == "save") save()
  if (item.identifier == "export") exportAs(menuItem.identifier)
})

Items you add appear in the customizer too, so users can move or remove them. resetToolbar() removes them; add them again after calling it.

editor.toolbarItems has add, addAt, item(identifier), remove, removeAll, and setItems(items) to replace every item at once. Adding a ToolbarItem.separator() or ToolbarItem.flexibleSpace() adds a divider or space.

ToolbarItem

MemberMeaning
new ToolbarItem(identifier, caption, icon, kind, tooltip)
iconA Bootstrap Icons class list, such as "bi bi-type-bold"
kindToolbarItemKinds.button, toggle, menu, color, separator or flexibleSpace
menuItemsA menu's items
enabled, active, visibleactive shows a toggle as on
tooltip, setTooltip(value)
alwaysShowCaptionShows the caption even when the toolbar shows icons only
setDisplayCaption(value)Changes the caption a menu button shows, such as the current choice
tagData of your own
makePopupFor a menu that picks its value in a popup of its own, such as the table size grid: choose => popup, where the popup calls choose with a ToolbarMenuItem carrying the value

A color item opens a color palette, and its value arrives as a DocumentColor, or null for Default.

ToolbarMenuItem

MemberMeaning
new ToolbarMenuItem(identifier, caption, icon)
checked, enabled
valueData delivered with the choice; the identifier by default
captionStyleCSS for the caption, such as a font family in a font menu
ToolbarMenuItem.submenu(identifier, caption, icon, children)An item that opens a submenu
ToolbarMenuItem.separator()A divider

The same classes make node toolbars and context menus; see Writing node types.

Display mode

editor.toolbarDisplayMode is ToolbarDisplayModes.iconOnly, iconAndText or textOnly. Users can change it by right-clicking the toolbar.

Customizing and saving the arrangement

Users open the customizer by right-clicking the toolbar and choosing Customize Toolbar, or you open it with editor.showToolbarCustomizer(). They drag items into, around and off the toolbar, or drag the default set back.

Save each user's arrangement and restore it later:

editor.addEventListener("toolbarCustomized", event => localStorage.toolbar = event.detail.state)

if (localStorage.toolbar) editor.loadToolbarState(localStorage.toolbar)

The state is JSON with a version, the display mode, and the item identifiers in order:

{"version": 1, "displayMode": 0, "items": ["insert", "separator", "undo", "redo", "flexiblespace", "search", "save"]}

loadToolbarState skips identifiers the editor doesn't know, and returns false for state it can't read. To restore your own items, add them before loading the state.

resetToolbar() puts back the default tools, removing your own items and any customization. It keeps the display mode, which is the user's choice.

Editor extensions

An extension adds tools to every editor, with their default positions, state and keyboard shortcuts. See Editor extensions.