Basics
First of all lets describe elements we will work with. Every Inline Tool must provide a button — HTML element with icon or some layout — for Inline Toolbar of the Editor. When button is pressed Inline Tool receives selected text range as JavaScript Range
object references to TextNode
on the page. Some Tools may also provide actions for additional interactions with the user.
Enough theory, lets do some practice! To start we need to define JavaScript class. To let Editor know that this Tool is inline we need to provide isInline
static getter:
Inline Tools must provide three methods to work with Editor: render
, surround
, and checkState
.
Render
method must return HTML element of the button for Inline Toolbar. When user selects some text Editor calls checkState
method of each Inline Tool with current Selection
to update the state if selected text contains some of the inline markup. Finally, when button is pressed Editor calls surround
method of the tool with Range
object as an argument:
Lets create the basic structure of the Tool and add primitive implementation for methods.
For now if highlight is applied you can't redo it. How to deal with that you can read at Using API article.