Creating a content object plugin

Introduction

This article will teach you how to go about creating a simple but complete content block object plugin. There are also inline content objects in the Writer. Even so, we normally talk about content objects and inline objects. A content object lives in the editable area.
Examples of content objects are images, videos, maps and embedded social media posts.
A map embedded as a content object
There are number of CSS classes and building blocks you need to use in order to create a Writer compliant content object. These building blocks will ensure that your content object follows the UX guidelines of the Writer as well as give it support for generic properties.
Generic properties are configurable key/values that can be attached to any content object through configuration independent of individual plugin configurations.

Overview

The Writer, and its plugins, are all built on a component model. There is a complete separation between the data a plugin handles and the UI used to visualize, or render, the data. As the UI is data driven this means that changes to data results in the UI being rerendered.
Even though the Writer is not based on React a good knowledge of React and how components, props and state works is very useful.
A simple content object require building a few different building blocks, of which some are tightly coupled through naming only. The parts required are:
  1. 1.
    Tool A small component rendering a menu option in the content menu.
  2. 2.
    Command Executed by clicking the tool in the content menu.
  3. 3.
    Node A schema that defines a data node. A data node is created by the command. (Data nodes can be inserted in other ways, e.g. drag’n drop, which is not part of this tutorial.)
  4. 4.
    Component Renders the data node into a visible UI object in the editors content area. Many plugins will have many sub components to maintain a good coding structure.
  5. 5.
    Converter Exports the data node to an articles xml and imports the xml from an article into a data node.
Parts of a content object plugin

The bits and pieces

The plugin package

What binds all the parts together is the package file. This is a simple object with a configure function which you provide to the Writer. The configure function is called whenever the Writer is ready to initialise the plugin.
The configure function has two parameters. First a configurator object which contains all the functions necessary to register the plugins different building blocks. The second parameter is the plugin configuration as a plain object.
1
const MyPodcastPackage = {
2
name: 'my-podcast',
3
configure(configurator, pluginConfig) {
4
// Registration of all the bits and pieces
5
}
6
}
7
8
export {MyPodcastPackage}
Copied!

The tool and the command

First you need a menu item in the content menu. A tool is a simple button component that can be rendered in the content menu (or other menus). Tools are normally very simple and there only to execute a command. The responsibility of the command is to perform the desired action. For example create and insert a data node.
The content menu displays tools which execute commands
To work as intended a tool must have a command registered with the same name. A command on the other hand can be be used independently.
The tool is a class that inherits from Tool found in Substance whereas the command inherits from WriterCommand found in Writer.
1
import {Tool} from 'substance'
2
import {api, UIIcon} from 'writer'
3
4
class PodcastTool extends Tool {
5
render($) {
6
let el = $('div').attr('title', this.getLabel('Insert my podcast'))
7
8
el.append([
9
$('button')
10
.addClass('se-tool')
11
.append($(UIIcon, {
12
name: 'microphone-bold'
13
}))
14
).on('click', this.onClick.bind(this))
15
])
16
17
return el
18
}
19
20
onClick() {
21
// Execute a named command
22
this.context.api.editorSession.executeCommand('insert-mypodcast', {})
23
}
24
}
25
26
export {InsertMyDataNodeTool} //shouldn't it be named {PodcastTool}?
Copied!
1
import {WriterCommand, idGenerator} from 'writer'
2
3
class PodcastCommand extends WriterCommand {
4
execute(params) {
5
params.editorSession.transaction(tx => {
6
// Perform actions in a transaction that become
7
// part of the undo/redo history
8
})
9
return true
10
}
11
}
12
13
export {PodcastCommand}
Copied!
Both the tool and the command is registered in the package configure function.
1
import {PodcastTool} from './tools/PodcastTool.js'
2
import {InsertPodcastCommand} from './commands/PodcastCommand.js'
3
4
const MyPodcastPackage = {
5
name: 'my-podcast',
6
configure(configurator, pluginConfig) {
7
configurator.addContentMenuTopTool('podcast.insert', PodcastTool)
8
configurator.addCommand('podcast.insert', PodcastCommand)
9
}
10
}
11
12
export {MyPodcastPackage}
Copied!

The node and the component

A block node is in this context is a simple schema. Its purpose is to describe the data structure of a content objects data node. The data node will later be handed to a component as props to be rendered in the Writers editor area or be used by the converter to convert the data to and from XML.
The node schema, which extends the BlockNode imported from the Writer, must have a property called type. The type is what ties the data node to a specific (named) converter and a specific (named) component.
1
import {BlockNode} from 'writer'
2
class PodcastNode extends BlockNode {}
3
4
PodcastNode.define({
5
type: 'mypodcastnode',
6
dataType: {type: 'string'},
7
id: {type: 'string'},
8
url: {type: 'string', optional: false, default: ''}
9
caption: {type: 'string', optional: true, default: ''}
10
})
11
12
export {PodcastNode}
Copied!
The component is what render the user interface your plugin displays to the user in the editor content area. Only one component is needed, but most plugins are split into many sub components to keep the project well structured.
A simple podcast object in the content area
A component without any bells and whistles could look like below.
1
import {Component} from 'substance'
2
3
class PodcastComponent extends Component {
4
render($) {
5
const el = $('div').addClass('im-blocknode__container')
6
7
// Build the html structure and render the node data
8
el.append([
9
$('strong').append(this.props.node.title),
10
$('div').append(this.props.node.url)
11
])
12
13
// Add support for generic properties
14
el.append($(GenericPropsComponent, {
15
pluginName: 'my-podcast',
16
node: this.props.node,
17
isolatedNodeState: this.props.isolatedNodeState
18
}).ref('genericProps'))
19
20
return el
21
}
22
}
23
24
export {PodcastComponent}
Copied!
And again both the node and the component must be added to the writer in the package configure function.
Note that we use the node type as the first parameter when we add the component to make sure the Writer know that these are connected.
1
import {PodcastTool} from './tools/PodcastTool.js'
2
import {PodcastCommand} from './commands/PodcastCommand.js'
3
import {PodcastNode} from './nodes/PodcastNode.js'
4
import {PodcastComponent} from './nodes/PodcastComponent.js'
5
6
const MyPodcastPackage = {
7
name: 'my-podcast',
8
configure(configurator, pluginConfig) {
9
configurator.addContentMenuTopTool('podcast.insert', PodcastTool)
10
configurator.addCommand('podcast.insert', PodcastCommand)
11
configurator.addNode(PodcastNode)
12
configurator.addComponent(PodcastNode.type, PodcastComponent)
13
}
14
}
15
16
export {MyPodcastPackage}
Copied!
Now that a node and component to render the node is defined you can go back to the command and actually create the node.
Note the type property of the node object which tells the Writer which node schema to use.
1
import {WriterCommand, idGenerator} from 'writer'
2
3
class PodcastCommand extends WriterCommand {
4
execute(params) {
5
params.editorSession.transaction(tx => {
6
tx.insertBlockNode({
7
id: idGenerator(),
8
type: 'mypodcastnode',
9
url: 'https://example.org/mypodcast/1',
10
title: 'My podcast #1'
11
})
12
})
13
return true
14
}
15
}
16
17
export {PodcastCommand}
Copied!

Converter

Now that the plugin can create and render a data node it is time to look into how to make sure the data is included when the article is saved. And of course that the plugin also picks it up when the article is opened in the Writer again.
For this a converter needs to be defined. A converter includes an export() function for converting the data node to XML and an import function to parse the XML into the data node again.
In addition two properties, type and tagName, as well as a matchElement() function is needed. The type property is what ties the converter to the node for exporting. The tagName defines the xml element name for the exported data. The matchElement() function on the other hand allows your code to investigate which xml elements you understand and wants to import.
Note the type which ties this converter to the specific node type.
1
const PodcastConverter = {
2
type: 'mypodcastnode',
3
tagName: 'object',
4
matchElement: function(el) {
5
return el.is('object[type="x-my/podcast"]')
6
},
7
8
import: function(el, node) {
9
node.id = el.attr('id')
10
node.url = el.find('url').text()
11
node.title = el.find('title').text()
12
},
13
14
export: function(node, el, converter) {
15
const $ = converter.$
16
el.attr({
17
id: node.id,
18
type: 'x-my/podcast',
19
})
20
21
el.append([
22
$('url').append(node.url),
23
$('title').append(node.title)
24
])
25
}
26
}
27
28
export {PodcastConverter}
Copied!
Adding the converter should then make the package file complete.
1
import {PodcastTool} from './tools/PodcastTool.js'
2
import {PodcastCommand} from './commands/PodcastCommand.js'
3
import {PodcastNode} from './nodes/PodcastNode.js'
4
import {PodcastComponent} from './nodes/PodcastComponent.js'
5
import {PodcastConverter} from './converters/PodcastConverter.js'
6
7
const MyPodcastPackage = {
8
name: 'my-podcast',
9
configure(configurator, pluginConfig) {
10
configurator.addContentMenuTopTool('podcast.insert', PodcastTool)
11
configurator.addCommand('podcast.insert', PodcastCommand)
12
configurator.addNode(PodcastNode)
13
configurator.addComponent(PodcastNode.type, PodcastComponent)
14
configurator.addConverter(PodcastConverter)
15
}
16
}
17
18
export {MyPodcastPackage}
Copied!

Registering the plugin

Finally it is time to wrap it up by registering the plugin package when the Writer loads the plugin. This is done through a self executing function that call the registerPlugin() function in the Writer. Usually this is done in a separate index.js file.
1
import {registerPlugin} from 'writer'
2
import {MyPodcastPackage} from './MyPodcastPackage';
3
4
(() => {
5
if (registerPlugin) {
6
registerPlugin(MyPodcastPackage)
7
}
8
else {
9
console.error('Register method not yet available')
10
}
11
})()
Copied!

Wrap up

These are the most basic building blocks needed to create a simple content object plugin in the Writer and it should get you started. Other things you might want to look into are drop handlers, events, macros and ways to add components to the top bar or the meta data column.