Skip to content

Commit 9cf9516

Browse files
author
Farzad Hayat
authored
DOC-2151: (6 Docs) Improve events.adoc explanation event handling (#3395)
* DOC-2151: Improve events.adoc explanation event handling * DOC-2151: Expand upon the "`+setup+` and `+init_instance_callback+` options" section
1 parent 94902ae commit 9cf9516

File tree

1 file changed

+68
-7
lines changed

1 file changed

+68
-7
lines changed

modules/ROOT/pages/events.adoc

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,88 @@
22
:navtitle: Available Events
33
:description_short: List of common editor events
44
:description: List of common editor events
5-
:keywords: Click, DblClick, MouseDown, MouseUp, MouseMove, MouseOver, MouseOut, MouseEnter, MouseLeave, KeyDown, KeyPress, KeyUp, ContextMenu, Paste, Init, Focus, Blur, BeforeSetContent, SetContent, GetContent, PreProcess, PostProcess, NodeChange, Undo, Redo, Change, Dirty, Remove, ExecCommand, PastePreProcess, PastePostProcess, AutocompleterStart, AutocompleterUpdate, AutocompleterEnd
6-
7-
{productname} supports some browser-native events, and provides additional events for working with the editor and plugins.
5+
:keywords: Click, DblClick, MouseDown, MouseUp, MouseMove, MouseOver, MouseOut, MouseEnter, MouseLeave, KeyDown, KeyPress, KeyUp, ContextMenu, Paste, Init, Focus, Blur, BeforeSetContent, SetContent, GetContent, PreProcess, PostProcess, NodeChange, Undo, Redo, Change, Dirty, Remove, ExecCommand, PastePreProcess, PastePostProcess, AutocompleterStart, AutocompleterUpdate, AutocompleterEnd, setup, init, init_instance_callback, AddEditor, RemoveEditor, BeforeUnload, browser-native events, editor-core events, plugin events, editor-manager events, event handlers, event binding
86

7+
[[handling-editor-events]]
98
== Handling Editor events
109

11-
The following examples illustrate how to use supported native events, editor core events, and plugin events with {productname}.
10+
{productname} supports several types of events for working with the editor and plugins.
11+
12+
* xref:browser-native-events[Supported browser-native events]
13+
* xref:editor-core-events[Editor core events]
14+
* xref:plugin-events[Plugin events]
15+
* xref:editor-manager-events[Editor Manager events]
16+
17+
The following examples illustrate how to use supported native events, editor core events, and plugin events with {productname}. Editor Manager events are handled separately using `+tinymce.on+`, rather than `+editor.on+`.
18+
19+
=== Defining an event handler
20+
21+
Event handlers can be initialized anywhere using the `+editor.on+` method, as shown in the following example:
22+
23+
[source,js]
24+
----
25+
tinymce.activeEditor.on('click', (e) => {
26+
console.log('Editor was clicked at: ' + e.pageX + ', ' + e.pageY);
27+
});
28+
----
29+
30+
=== `+setup+` and `+init_instance_callback+` options
31+
32+
A common use case for setting up events is to use the `+setup+` and `+init_instance_callback+` options. These options allow you to bind events before and after the editor instance is initialized.
33+
34+
The `+setup+` option can be used to bind an event **before** the editor instance is initialized.
35+
36+
The `+init_instance_callback+` option can be used to bind an event **after** the editor instance is initialized.
37+
38+
TIP: The `+init_instance_callback+` is functionally equivalent to binding an event listener to the `init` event on the `+setup+` option.
1239

13-
The following example uses a function to create a console log entry when the editor has initialized. This is also an example of handling an event which does not return any data.
40+
For example, the following code:
1441

1542
[source,js]
1643
----
1744
tinymce.init({
1845
selector: 'textarea',
1946
setup: (editor) => {
2047
editor.on('init', (e) => {
21-
console.log('The Editor has initialized.');
48+
console.log('Editor is initialized.');
2249
});
2350
}
2451
});
2552
----
2653

27-
The following example uses a function to create a console log entry when a command is executed in the editor. This is also an example of handling an event that returns data.
54+
Is equivalent to:
55+
56+
[source,js]
57+
----
58+
tinymce.init({
59+
selector: 'textarea',
60+
init_instance_callback: (editor) => {
61+
console.log('Editor is initialized.');
62+
}
63+
});
64+
----
65+
66+
For more information on these options, see xref:editor-important-options.adoc#setup[setup] and xref:editor-important-options.adoc#init_instance_callback[init_instance_callback].
67+
68+
=== Binding an event before editor initialization
69+
70+
The following example uses the `+setup+` option to bind an event before the editor instance is initialized. This is an example of handling an event that does not return data, such as the `+ResizeEditor+` editor event. Therefore, the `+e+` parameter is not used.
71+
72+
[source,js]
73+
----
74+
tinymce.init({
75+
selector: 'textarea',
76+
setup: (editor) => {
77+
editor.on('ResizeEditor', (e) => {
78+
console.log('Editor was resized.');
79+
});
80+
}
81+
});
82+
----
83+
84+
=== Binding an event after editor initialization
85+
86+
The following example uses the `+init_instance_callback+` option to bind an event after the editor instance is initialized. This is an example of handling an event that returns data, such as the `+ExecCommand+` event. Therefore, we can use the `+e+` parameter to access the data returned by the event.
2887

2988
[source,js]
3089
----
@@ -38,6 +97,7 @@ tinymce.init({
3897
});
3998
----
4099

100+
[[browser-native-events]]
41101
== Supported browser-native events
42102

43103
{productname} supports the following browser-native events. Click the event name for details (links open https://developer.mozilla.org/[MDN Web Docs]).
@@ -176,6 +236,7 @@ The following events are provided by the {productname} editor.
176236
|FontFamilyTextUpdate |`+{ value: string }+` |Fired after the visible text label of the `fontfamily` bespoke toolbar button is updated. `value` refers to the updated visible text label.
177237
|===
178238

239+
[[plugin-events]]
179240
== Plugin events
180241

181242
The following plugins provide events.

0 commit comments

Comments
 (0)