TextEditor
import { TextEditor } from 'pixi-silk/ui/textedit';
Experimental
Text you can click and edit on the canvas. It draws a SilkText plus a caret and a selection drawn with silk.
Typing goes through a hidden <textarea> that follows the caret on screen. Input methods (Japanese,
Chinese, dead keys), mobile keyboards, autocorrect, the emoji picker and the clipboard all work.
- Click to place the caret, drag to select, Shift + click to extend. Double click selects a word, triple click a paragraph. On touch, tap places the caret and a long press selects a word.
- Editing keys follow the platform. Escape leaves the editor.
- Undo merges consecutive typing. Pass
historyto share the app’s undo stack.
Events: focus, blur, input (every edit), change (on commit, when the text changed), submit
(Enter in a single-line editor) and tab. The same values are on TextEditor.state.
Rich text (mixed styles in one editor) and right-to-left text are not supported.
Experimental: the API may change in a minor release.
Example#
const name = new TextEditor({ text: 'Untitled', fontSize: 14, multiline: false, width: 200 });
name.on('change', (text: string) => console.log('renamed to', text));
world.addChild(name);
Extends#
Container
Constructors#
Constructor#
new TextEditor(options?: TextEditorOptions): TextEditor;
Experimental
Parameters#
| Parameter | Type |
|---|---|
options | TextEditorOptions |
Returns#
TextEditor
Overrides#
Container.constructor
Properties#
| Property | Modifier | Type | Default value | Description | Defined in |
|---|---|---|---|---|---|
active | static | TextEditor | null | null | Experimental The editor that has keyboard focus, or null. | ui/textedit.ts:207 |
caretWidth | public | number | undefined | Experimental Caret width in screen pixels, whatever the zoom. Call TextEditor.redraw after changing it. | ui/textedit.ts:248 |
content | readonly | Container | undefined | Experimental Holds the text, caret and selection. A single-line editor scrolls it. | ui/textedit.ts:240 |
enter | public | "blur" | "newline" | "commit" | undefined | Experimental What Enter does. See the enter option. | ui/textedit.ts:244 |
maxLength | public | number | undefined | Experimental Longest text allowed, in UTF-16 code units. | ui/textedit.ts:242 |
placeholderObject | readonly | SilkText | undefined | Experimental The placeholder, shown while the text is empty. | ui/textedit.ts:234 |
state | readonly | TextEditorState | undefined | Experimental Signals of the text, focus and selection. | ui/textedit.ts:236 |
tab | public | "insert" | "next" | undefined | Experimental What Tab does. See the tab option. | ui/textedit.ts:246 |
textObject | readonly | SilkText | undefined | Experimental The text being edited. Style it as any SilkText. | ui/textedit.ts:232 |
theme | readonly | TextEditorTheme | undefined | Experimental Colours. Call TextEditor.redraw after changing them. | ui/textedit.ts:238 |
Accessors#
composition#
Get Signature#
get composition():
| {
end: number;
start: number;
}
| null;
Experimental
Range of the text an input method is composing, or null.
Returns
Type Literal
{
end: number;
start: number;
}
| Name | Type | Description | Defined in |
|---|---|---|---|
end | number | Index after the last composed character. | ui/textedit.ts:462 |
start | number | Index of the first composed character. | ui/textedit.ts:460 |
null
field#
Get Signature#
get field(): HTMLTextAreaElement | null;
Experimental
The hidden text field while it exists (created on first focus).
Returns
HTMLTextAreaElement | null
focused#
Get Signature#
get focused(): boolean;
Experimental
True while the editor has keyboard focus.
Returns
boolean
multiline#
Get Signature#
get multiline(): boolean;
Experimental
True for a multi-line editor.
Returns
boolean
readOnly#
Get Signature#
get readOnly(): boolean;
Experimental
Selectable and copyable, but not editable.
Returns
boolean
Set Signature#
set readOnly(v: boolean): void;
Experimental
Sets TextEditor.readOnly.
Parameters
| Parameter | Type |
|---|---|
v | boolean |
Returns
void
selectable#
Get Signature#
get selectable(): boolean;
Experimental
Whether pointer input places a caret and selects.
Returns
boolean
Set Signature#
set selectable(v: boolean): void;
Experimental
Sets TextEditor.selectable.
Parameters
| Parameter | Type |
|---|---|
v | boolean |
Returns
void
selectedText#
Get Signature#
get selectedText(): string;
Experimental
The selected text.
Returns
string
selectionEnd#
Get Signature#
get selectionEnd(): number;
Experimental
End of the selection (the larger offset).
Returns
number
selectionStart#
Get Signature#
get selectionStart(): number;
Experimental
Start of the selection (the smaller offset).
Returns
number
value#
Get Signature#
get value(): string;
Experimental
The text. Setting it replaces the text and puts the caret at the end, without an undo step.
Returns
string
Set Signature#
set value(v: string): void;
Experimental
Sets TextEditor.value.
Parameters
| Parameter | Type |
|---|---|
v | string |
Returns
void
Methods#
blur()#
blur(): this;
Experimental
Takes keyboard focus away and commits the text.
Returns#
this
chain()#
static chain(editors: readonly TextEditor[]): () => void;
Experimental
Links editors in Tab order, like fields of a form: Tab moves to the next one, Shift + Tab back. Returns the unlink function.
Parameters#
| Parameter | Type |
|---|---|
editors | readonly TextEditor[] |
Returns#
() => void
deleteBackward()#
deleteBackward(unit?: "grapheme" | "word" | "line"): boolean;
Experimental
Deletes the selection, or the grapheme, word or line before the caret.
Parameters#
| Parameter | Type | Default value |
|---|---|---|
unit | "grapheme" | "word" | "line" | 'grapheme' |
Returns#
boolean
deleteForward()#
deleteForward(unit?: "grapheme" | "word" | "line"): boolean;
Experimental
Deletes the selection, or the grapheme, word or line after the caret.
Parameters#
| Parameter | Type | Default value |
|---|---|---|
unit | "grapheme" | "word" | "line" | 'grapheme' |
Returns#
boolean
destroy()#
destroy(options?: DestroyOptions): void;
Experimental
Blurs (which commits the text), removes the hidden text field and stops listening for input.
Parameters#
| Parameter | Type |
|---|---|
options? | DestroyOptions |
Returns#
void
Overrides#
Container.destroy
focus()#
focus(selectAll?: boolean): this;
Experimental
Gives the editor keyboard focus. selectAll selects the whole text, as tabbing into a field does.
Parameters#
| Parameter | Type | Default value |
|---|---|---|
selectAll | boolean | false |
Returns#
this
handleKey()#
handleKey(e: Pick<KeyboardEvent, "key" | "altKey" | "ctrlKey" | "metaKey" | "shiftKey"> & {
isComposing?: boolean;
keyCode?: number;
}): boolean;
Experimental
Handles a key press the way the hidden field receives it. Returns true when the editor used the key. You only need it to drive the editor from your own key handling.
Parameters#
| Parameter | Type |
|---|---|
e | Pick<KeyboardEvent, "key" | "altKey" | "ctrlKey" | "metaKey" | "shiftKey"> & { isComposing?: boolean; keyCode?: number; } |
Returns#
boolean
hitTest()#
hitTest(global: PointData): CaretHit;
Experimental
The caret position under a point in global (screen) coordinates.
Parameters#
| Parameter | Type |
|---|---|
global | PointData |
Returns#
insert()#
insert(text: string, kind?: TextEditKind): boolean;
Experimental
Replaces the selection with text, as typing does. Returns false when nothing changed.
Parameters#
| Parameter | Type | Default value |
|---|---|---|
text | string | undefined |
kind | TextEditKind | 'type' |
Returns#
boolean
redo()#
redo(): void;
Experimental
Redoes the last undone edit.
Returns#
void
redraw()#
redraw(): void;
Experimental
Draws the caret, selection and placeholder again. Happens on its own after every change.
Returns#
void
select()#
select(anchor: number, focus?: number): this;
Experimental
Selects from anchor to focus (a caret when focus is left out). Offsets snap to grapheme boundaries.
Parameters#
| Parameter | Type | Default value |
|---|---|---|
anchor | number | undefined |
focus | number | anchor |
Returns#
this
selectAll()#
selectAll(): this;
Experimental
Selects the whole text.
Returns#
this
tick()#
tick(dt: number): boolean;
Experimental
Advances the caret blink and animations by dt seconds. Returns true while it still moves.
Parameters#
| Parameter | Type |
|---|---|
dt | number |
Returns#
boolean
undo()#
undo(): void;
Experimental
Undoes the last text edit (in the app’s history when it was given one with undo()).
Returns#
void