@@ -3,6 +3,9 @@ import {actions} from './actions';
3
3
4
4
export class TiptapWidget {
5
5
6
+ /**
7
+ * @param {HTMLElement } context - DOM context for initialization.
8
+ */
6
9
static initialize ( context ) {
7
10
$ ( 'div.tiptap-editor' , context ) . each ( function ( ) {
8
11
let elem = $ ( this ) ;
@@ -18,6 +21,13 @@ export class TiptapWidget {
18
21
} ) ;
19
22
}
20
23
24
+ /**
25
+ * @param {jQuery } elem - The jQuery element representing the Tiptap widget.
26
+ * @param {Object } [opts={}] - Configuration options for the Tiptap widget.
27
+ * @param {Array } opts.actions - An array of actions to be used in the editor.
28
+ * @param {Array } opts.colors - An array of colors to be used in the editor.
29
+ * @param {string } opts.helpLink - A link to help resources related to the editor.
30
+ */
21
31
constructor ( elem , opts = { } ) {
22
32
elem . data ( 'yafowil-tiptap' , this ) ;
23
33
elem . attr ( 'spellcheck' , false ) ;
@@ -60,7 +70,7 @@ export class TiptapWidget {
60
70
let container = $ ( '<div />' )
61
71
. addClass ( 'btn-group me-2' )
62
72
. appendTo ( this . controls ) ;
63
- act . forEach ( name => this . add_button ( name , container ) ) ;
73
+ act . forEach ( name => this . add_button ( name , container ) ) ;
64
74
} else {
65
75
this . add_button ( act , this . controls ) ;
66
76
}
@@ -72,13 +82,19 @@ export class TiptapWidget {
72
82
this . editor . on ( 'selectionUpdate' , this . on_selection_update ) ;
73
83
}
74
84
85
+ /**
86
+ * Cleans up the widget and removes event listeners.
87
+ */
75
88
destroy ( ) {
76
89
this . unload_all ( ) ;
77
90
this . editor . destroy ( ) ;
78
91
this . elem . empty ( ) ;
79
92
this . buttons = null ;
80
93
}
81
94
95
+ /**
96
+ * Unloads all action buttons in the widget.
97
+ */
82
98
unload_all ( ) {
83
99
for ( let btn in this . buttons ) {
84
100
if ( this . buttons [ btn ] . unload ) {
@@ -87,14 +103,26 @@ export class TiptapWidget {
87
103
}
88
104
}
89
105
106
+ /**
107
+ * Adds a button to the control panel.
108
+ *
109
+ * @param {string } name - The name of the action to create a button for.
110
+ * @param {jQuery } container - The jQuery container to append the button to.
111
+ */
90
112
add_button ( name , container ) {
91
- let factory = actions [ name ] ,
92
- btn = new factory ( this , this . editor , {
93
- container_elem : container
94
- } ) ;
113
+ let factory = actions [ name ] ;
114
+ let btn = new factory ( this , this . editor , {
115
+ container_elem : container
116
+ } ) ;
95
117
this . buttons [ name ] = btn ;
96
118
}
97
119
120
+ /**
121
+ * Parses the provided actions and returns a structured array.
122
+ *
123
+ * @param {Array } acs - An array of action names.
124
+ * @returns {Array } A structured array of parsed actions.
125
+ */
98
126
parse_actions ( acs ) {
99
127
let ret = [ ] ;
100
128
function parse ( ret_arr , acts ) {
@@ -107,12 +135,18 @@ export class TiptapWidget {
107
135
} else {
108
136
ret_arr . push ( action ) ;
109
137
}
110
- } )
138
+ } ) ;
111
139
}
112
140
parse ( ret , acs ) ;
113
141
return ret ;
114
142
}
115
143
144
+ /**
145
+ * Parses the provided actions and returns a set of corresponding extensions.
146
+ *
147
+ * @param {Array } acs - An array of action names.
148
+ * @returns {Set } A set of extensions associated with the actions.
149
+ */
116
150
parse_extensions ( acs ) {
117
151
let extensions = new Set ( [
118
152
tiptap . Document ,
@@ -129,6 +163,9 @@ export class TiptapWidget {
129
163
return extensions ;
130
164
}
131
165
166
+ /**
167
+ * Updates the state of all buttons and the textarea value.
168
+ */
132
169
on_update ( ) {
133
170
for ( let btn in this . buttons ) {
134
171
if ( this . buttons [ btn ] . on_update ) {
@@ -138,6 +175,9 @@ export class TiptapWidget {
138
175
this . textarea . val ( this . editor . getHTML ( ) ) ;
139
176
}
140
177
178
+ /**
179
+ * Updates the state of all buttons based on the current selection.
180
+ */
141
181
on_selection_update ( ) {
142
182
for ( let btn in this . buttons ) {
143
183
if ( this . buttons [ btn ] . on_selection_update ) {
@@ -147,18 +187,23 @@ export class TiptapWidget {
147
187
}
148
188
}
149
189
150
-
151
190
//////////////////////////////////////////////////////////////////////////////
152
191
// yafowil.widget.array integration
153
192
//////////////////////////////////////////////////////////////////////////////
154
193
194
+ /**
195
+ * Re-initializes widget on array add event.
196
+ */
155
197
function tiptap_on_array_add ( inst , context ) {
156
198
TiptapWidget . initialize ( context ) ;
157
199
}
158
200
201
+ /**
202
+ * Registers subscribers to yafowil array events.
203
+ */
159
204
export function register_array_subscribers ( ) {
160
205
if ( window . yafowil_array === undefined ) {
161
206
return ;
162
207
}
163
208
window . yafowil_array . on_array_event ( 'on_add' , tiptap_on_array_add ) ;
164
- }
209
+ }
0 commit comments