TextEditor
TextEditor
is a powerful (rich-text) inline text editor that can be used on any SVG text element.
Learn more about the TextEditor plugin.
constructorβ
constructor(options?: TextEditor.Options);
The TextEditor constructor accepts several parameters for advanced use only:
textβ
[optional] SVG <text>
element. It is recommended to use the ui.TextEditor.getTextElement(el)
method to find the
container <text>
SVG element wrapping all the lines. You can use this method directly on the evt.target
element in
your event handlers, and just check if the returned value is truthy.
placeholderβ
[optional] Set to true
to show a default placeholder (Enter text...) when the text is empty. Set it to a string to
show a custom placeholder. The placeholder visual look can be styled in CSS as follows:
.text-editor .caret.placeholder:before { // The caret.
background-color: red;
}
.text-editor .caret.placeholder:after { // The placeholder text.
font-style: italic;
color: blue;
}
Configurationβ
The ui.TextEditor.edit()
method accepts several parameters. This is the recommended way to start
your text editor.
ui.TextEditor.edit(el?: SVGElement, opt?: TextEditor.Options): TextEditor;
elβ
[optional] SVG <text>
element or any of its descendants. Usually, you pass the event target object when the user
doubleclicks your JointJS element/link.
opt.cellViewβ
[optional] The view for your cell. Usually, you either have direct access to this view (in the paper triggered
'cell:pointerclick'
or 'cell:pointerdblclick'
events), but you can always find a view for any JointJS element or link
by using paper.findViewByModel(cell)
.
opt.textPropertyβ
[optional] The path to the text property that causes the element/link to re-render the text inside it.
(e.g. 'attrs/label/text'
).
opt.annotationsPropertyβ
[optional] The path to the property that holds the text annotations. (e.g. 'attrs/label/annotations'
). (Rich-text specific.)
opt.placeholderβ
[optional] Set to true
to show a placeholder when the text is empty. The placeholder text and visual look can be
styled in CSS as follows:
.text-editor .caret.placeholder:before { // The caret.
background-color: red;
}
.text-editor .caret.placeholder:after { // The placeholder text.
content: 'Enter text...';
font-style: italic;
color: blue;
}
opt.annotationsβ
[optional] Annotations that will be set on the ui.TextEditor
instance. (Rich-text specific.)
opt.annotateUrlsβ
[optional] Set to true
to enable the text editor to automatically detect URL hyperlinks, and annotate them using
the default urlAnnotation
.
opt.urlAnnotationβ
[optional] The default annotation for detected URL hyperlinks:
{
attrs: {
'class': 'url-annotation',
fill: 'lightblue',
'text-decoration': 'underline'
}
}
It could be defined as an object, or a function that returns an object.
function(url) {
return {
attrs: {
'data-url': url,
'fill': 'blue',
'text-decoration': 'underline'
}
}
}
opt.useNativeSelectionβ
[optional] ui.TextEditor
uses native selections for
selecting text. This can be disabled by setting useNativeSelection
to false
in which case ui.TextEditor
renders
its own selections. This is an advanced feature that is useful only in very rare occasions. For example, if you, for
any reason, need to select text in two different elements at the same time, you should set the useNativeSelection
to
false
. This is because if native selections are used, two or more text elements cannot have focus at the same time.
opt.textareaAttributesβ
[optional] Set attributes on the underlying (invisible) textarea that is used internally by the text editor to handle keyboard text input. This is an advanced option, and is useful in very rare situations. The default value is:
textareaAttributes: {
autocorrect: 'off',
autocomplete: 'off',
autocapitalize: 'off',
spellcheck: 'false',
tabindex: '0'
}
Some jQuery UI users reported an issue with the autocomplete
attribute being set on the text area, resulting in the
following error thrown in the jQuery UI:
Uncaught Error: cannot call methods on autocomplete prior to initialization; attempted to call method 'off'
. In this
case, you might want to set your own textareaAttributes
to overcome this issue (in this specific case, omitting
the autocomplete
attribute).
opt.onKeydownβ
[optional] The callback is triggered when a key is pressed. It is possible to stop the event from propagating to the
text editor by calling evt.stopPropagation()
.
onKeydown: (evt: KeyboardEvent) => {
if (evt.code === 'Enter') {
evt.stopPropagation();
TextEditor.close();
}
}
opt.onOutsidePointerdownβ
[optional] The callback is triggered when the user clicks/touches outside the text editor.
onOutsidePointerdown: (evt: PointerEvent) => {
TextEditor.close();
}
Methodsβ
render()β
textEditor.render(root?: HTMLElement): this;
Render the Text Editor. Call this method right after you have constructed the text editor instance, and you're ready to
display the inline text editor. If root
(HTML DOM element) is specified, the text editor HTML container will be
appended to that element instead of to the document.body
. This is useful if you want the text editor to scroll with
some other container.
remove()β
textEditor.remove(): this;
Remove the Text Editor. Call this when you're done with inline text editing.
selectAll()β
textEditor.selectAll(): this;
Programmatically select all the text inside the text editor.
select()β
textEditor.select(selectionStart: number, selectionEnd?: number): this
Programmatically select a portion of the text inside the text editor starting at selectionStart
, and ending at
selectionEnd
. This method automatically swaps selectionStart
and selectionEnd
if they are in a wrong order.
deselect()β
Programmatically deselect all the selected text inside the text editor.
textEditor.deselect(): this;
getSelectionStart()β
textEditor.getSelectionStart(): number | null;
Return the start character position of the current selection.
getSelectionEnd()β
textEditor.getSelectionEnd(): number | null;
Return the end character position of the current selection.
getSelectionRange()β
textEditor.getSelectionRange(): TextEditor.Selection;
Return an object of the form { start: Number, end: Number }
containing the start and end position of the current
selection. Note that the start and end positions are returned normalized. This means that the start index will always
be lower than the end index even though the user started selecting the text from the end back to the start.
getSelectionLength()β
textEditor.getSelectionLength(): number;
Return the number of characters in the current selection.
getSelection()β
textEditor.getSelection(): string;
Return the selected text.
startSelecting()β
textEditor.startSelecting(): void;
Starts the text selection (i.e. updates the selection range based on the mouse coordinates until the user releases the mouse).
findAnnotationsUnderCursor()β
textEditor.findAnnotationsUnderCursor(
annotations: Vectorizer.TextAnnotation,
selectionStart: number
): Array<Vectorizer.TextAnnotation>;
Find all the annotations in the annotations
array that the cursor at selectionStart
position falls into.
findAnnotationsInSelection()β
textEditor.findAnnotationsInSelection(
annotations: Vectorizer.TextAnnotation,
selectionStart: number,
selectionEnd: number
): Array<Vectorizer.TextAnnotation>;
Find all the annotations that fall into the selection range specified by selectionStart
and selectionEnd
. This
method assumes the selection range is normalized.
setCaret()β
textEditor.setCaret(charNum: number, opt?: { [key: string]: any }): this;
Programmatically set the caret position. If opt.silent
is true, the text editor will not trigger the 'caret:change'
event.
hideCaret()β
textEditor.hideCaret(): this;
Programmatically hide the caret.
updateCaret()β
textEditor.updateCaret(): void;
Manually update the visual attributes (e.g. size, color) of the caret. It's useful when the containing SVG was transformed.
getTextContent()β
textEditor.getTextContent(): string;
Return the text content (including new line characters) inside the text editor.
getWordBoundary()β
textEditor.getWordBoundary(charNum: number): [number, number] | undefined;
Return the start and end character positions for a word under charNum
character position.
getURLBoundary()β
textEditor.getURLBoundary(charNum: number): [number, number] | undefined;
Return the start and end character positions for a URL under charNum
character position. Return undefined
if there
was no URL recognized at the charNum
index.
getNumberOfChars()β
textEditor.getNumberOfChars(): number;
Return the number of characters in the text.