Skip to content

Commit 77d8f65

Browse files
committed
documentation
1 parent 7ec1947 commit 77d8f65

File tree

2 files changed

+126
-8
lines changed

2 files changed

+126
-8
lines changed

js/src/bootstrap5/buttons.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import bootstrap from 'bootstrap';
33

44
export class Action {
55

6+
/**
7+
* @param {Object} editor - The editor instance to which this action belongs.
8+
* @param {Object} [opts={}] - Options for configuring the action.
9+
* @param {HTMLElement} opts.container_elem - The container element for the action.
10+
* @param {string} opts.tooltip - The tooltip text to display on hover.
11+
* @param {string} opts.icon - The icon class to use (from Bootstrap Icons).
12+
* @param {string} opts.text - The text label for the action.
13+
* @param {Object} opts.css - CSS properties to apply to the action.
14+
* @param {boolean} opts.toggle - If true, the action can be toggled active/inactive.
15+
*/
616
constructor(editor, opts = {}) {
717
this.editor = editor;
818
this.editor_elem = $(editor.options.element);
@@ -13,6 +23,11 @@ export class Action {
1323
this.elem.on('click', this.on_click);
1424
}
1525

26+
/**
27+
* Compiles the action's UI elements based on the provided options.
28+
*
29+
* @param {Object} opts - The options used to configure the action.
30+
*/
1631
compile(opts) {
1732
this.container = $('<div />')
1833
.addClass('action')
@@ -41,22 +56,44 @@ export class Action {
4156
this.content = $('> *', this.elem);
4257
}
4358

59+
/**
60+
* Gets the active state of the action.
61+
*
62+
* @returns {boolean} True if the action is active, false otherwise.
63+
*/
4464
get active() {
4565
return this._active;
4666
}
67+
68+
/**
69+
* Sets the active state of the action.
70+
*
71+
* @param {boolean} active - The new active state.
72+
*/
4773
set active(active) {
4874
if (this.opts.toggle) {
4975
active ? this.elem.addClass('active') : this.elem.removeClass('active');
5076
}
5177
this._active = active;
5278
}
79+
80+
/**
81+
* Handles click events on the action element.
82+
*
83+
* @param {Event} e - The click event.
84+
*/
5385
on_click(e) {
5486
e.preventDefault();
5587
}
5688
}
5789

5890
export class Button extends Action {
5991

92+
/**
93+
* Compiles the button's UI elements, adding button-specific classes.
94+
*
95+
* @param {Object} opts - The options used to configure the button.
96+
*/
6097
compile(opts) {
6198
super.compile(opts);
6299
this.container.addClass('btn-group');
@@ -67,6 +104,13 @@ export class Button extends Action {
67104

68105
export class DropdownButton extends Button {
69106

107+
/**
108+
* Creates an instance of the DropdownButton class.
109+
*
110+
* @param {Object} editor - The editor instance to which this dropdown button belongs.
111+
* @param {Object} [opts={}] - Options for configuring the dropdown button.
112+
* @param {boolean} opts.submit - If true, includes a submit button in the dropdown.
113+
*/
70114
constructor(editor, opts = {}) {
71115
super(editor, opts);
72116
this.elem.addClass('drop_btn dropdown-toggle')
@@ -90,10 +134,20 @@ export class DropdownButton extends Button {
90134
$(window).on('resize', this.on_resize);
91135
}
92136

137+
/**
138+
* Gets the currently active item in the dropdown.
139+
*
140+
* @returns {Object} The currently active item.
141+
*/
93142
get active_item() {
94143
return this._active_item;
95144
}
96145

146+
/**
147+
* Sets the currently active item in the dropdown.
148+
*
149+
* @param {Object} item - The item to set as active.
150+
*/
97151
set active_item(item) {
98152
let clone = item.elem.children().clone();
99153
this.elem.empty().append(clone);
@@ -103,14 +157,25 @@ export class DropdownButton extends Button {
103157
this._active_item = item;
104158
}
105159

160+
/**
161+
* Cleans up the dropdown button by removing event listeners.
162+
*/
106163
unload() {
107164
$(window).off('resize', this.on_resize);
108165
}
109166

167+
/**
168+
* Handles the window resize event, resetting the active state.
169+
*
170+
* @param {Event} e - The resize event.
171+
*/
110172
on_resize(e) {
111173
this.active = false;
112174
}
113175

176+
/**
177+
* Sets the items in the dropdown menu.
178+
*/
114179
set_items() {
115180
for (let child of this.children) {
116181
child.container.addClass('dropdown-item');
@@ -122,11 +187,19 @@ export class DropdownButton extends Button {
122187
this.active_item = this.children[0];
123188
}
124189

190+
/**
191+
* Updates the dropdown button based on the current state.
192+
*/
125193
on_update() {
126194
// ...
127195
}
128196

129197
/* istanbul ignore next */
198+
/**
199+
* Handles the submit action for the dropdown.
200+
*
201+
* @param {Event} e - The submit event.
202+
*/
130203
submit(e) {
131204
e.preventDefault();
132205
}

js/src/bootstrap5/widget.js

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import {actions} from './actions';
33

44
export class TiptapWidget {
55

6+
/**
7+
* @param {HTMLElement} context - DOM context for initialization.
8+
*/
69
static initialize(context) {
710
$('div.tiptap-editor', context).each(function() {
811
let elem = $(this);
@@ -18,6 +21,13 @@ export class TiptapWidget {
1821
});
1922
}
2023

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+
*/
2131
constructor(elem, opts={}) {
2232
elem.data('yafowil-tiptap', this);
2333
elem.attr('spellcheck', false);
@@ -60,7 +70,7 @@ export class TiptapWidget {
6070
let container = $('<div />')
6171
.addClass('btn-group me-2')
6272
.appendTo(this.controls);
63-
act.forEach(name => this.add_button(name, container));
73+
act.forEach(name => this.add_button(name, container));
6474
} else {
6575
this.add_button(act, this.controls);
6676
}
@@ -72,13 +82,19 @@ export class TiptapWidget {
7282
this.editor.on('selectionUpdate', this.on_selection_update);
7383
}
7484

85+
/**
86+
* Cleans up the widget and removes event listeners.
87+
*/
7588
destroy() {
7689
this.unload_all();
7790
this.editor.destroy();
7891
this.elem.empty();
7992
this.buttons = null;
8093
}
8194

95+
/**
96+
* Unloads all action buttons in the widget.
97+
*/
8298
unload_all() {
8399
for (let btn in this.buttons) {
84100
if (this.buttons[btn].unload) {
@@ -87,14 +103,26 @@ export class TiptapWidget {
87103
}
88104
}
89105

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+
*/
90112
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+
});
95117
this.buttons[name] = btn;
96118
}
97119

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+
*/
98126
parse_actions(acs) {
99127
let ret = [];
100128
function parse(ret_arr, acts) {
@@ -107,12 +135,18 @@ export class TiptapWidget {
107135
} else {
108136
ret_arr.push(action);
109137
}
110-
})
138+
});
111139
}
112140
parse(ret, acs);
113141
return ret;
114142
}
115143

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+
*/
116150
parse_extensions(acs) {
117151
let extensions = new Set([
118152
tiptap.Document,
@@ -129,6 +163,9 @@ export class TiptapWidget {
129163
return extensions;
130164
}
131165

166+
/**
167+
* Updates the state of all buttons and the textarea value.
168+
*/
132169
on_update() {
133170
for (let btn in this.buttons) {
134171
if (this.buttons[btn].on_update) {
@@ -138,6 +175,9 @@ export class TiptapWidget {
138175
this.textarea.val(this.editor.getHTML());
139176
}
140177

178+
/**
179+
* Updates the state of all buttons based on the current selection.
180+
*/
141181
on_selection_update() {
142182
for (let btn in this.buttons) {
143183
if (this.buttons[btn].on_selection_update) {
@@ -147,18 +187,23 @@ export class TiptapWidget {
147187
}
148188
}
149189

150-
151190
//////////////////////////////////////////////////////////////////////////////
152191
// yafowil.widget.array integration
153192
//////////////////////////////////////////////////////////////////////////////
154193

194+
/**
195+
* Re-initializes widget on array add event.
196+
*/
155197
function tiptap_on_array_add(inst, context) {
156198
TiptapWidget.initialize(context);
157199
}
158200

201+
/**
202+
* Registers subscribers to yafowil array events.
203+
*/
159204
export function register_array_subscribers() {
160205
if (window.yafowil_array === undefined) {
161206
return;
162207
}
163208
window.yafowil_array.on_array_event('on_add', tiptap_on_array_add);
164-
}
209+
}

0 commit comments

Comments
 (0)