Skip to content

Commit b718e4f

Browse files
committed
properly unload widget to prevent memory leaks
1 parent 39dd682 commit b718e4f

File tree

4 files changed

+62
-15
lines changed

4 files changed

+62
-15
lines changed

js/src/bootstrap5/buttons.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class Action {
3838
if (opts.tooltip) {
3939
this.container.attr('data-bs-toggle', 'tooltip')
4040
.attr('data-bs-title', opts.tooltip);
41-
new bootstrap.Tooltip(this.container);
41+
this.tooltip = new bootstrap.Tooltip(this.container);
4242
}
4343

4444
if (opts.icon) {
@@ -85,6 +85,13 @@ export class Action {
8585
on_click(e) {
8686
e.preventDefault();
8787
}
88+
89+
unload() {
90+
if (this.tooltip) {
91+
this.tooltip.dispose();
92+
}
93+
this.elem.off();
94+
}
8895
}
8996

9097
export class Button extends Action {
@@ -162,6 +169,13 @@ export class DropdownButton extends Button {
162169
*/
163170
unload() {
164171
$(window).off('resize', this.on_resize);
172+
if (this.submit_elem) {
173+
this.submit_elem.off();
174+
}
175+
for (let child of this.children) {
176+
child.unload();
177+
}
178+
super.unload();
165179
}
166180

167181
/**

js/src/bootstrap5/widget.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,33 @@ export class TiptapWidget {
9292
* Cleans up the widget and removes event listeners.
9393
*/
9494
destroy() {
95+
if (this.editor) {
96+
this.editor.off();
97+
this.editor.destroy();
98+
}
99+
95100
this.unload_all();
96-
this.editor.destroy();
97-
this.elem.empty();
98-
this.buttons = null;
101+
102+
if (this.helpLink && typeof this.helpLink.unload === 'function') {
103+
this.helpLink.unload();
104+
}
105+
106+
// remove from jQuery data cache
107+
this.elem.removeData('yafowil-tiptap');
108+
109+
// remove event listeners
110+
this.editarea.off();
111+
this.controls.off();
112+
this.textarea.off();
113+
this.elem.off();
99114
}
100115

101116
/**
102117
* Unloads all action buttons in the widget.
103118
*/
104119
unload_all() {
105120
for (let btn in this.buttons) {
106-
if (this.buttons[btn].unload) {
107-
this.buttons[btn].unload();
108-
}
121+
this.buttons[btn].unload();
109122
}
110123
}
111124

src/yafowil/widget/tiptap/resources/bootstrap5/widget.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var yafowil_tiptap = (function (exports, $, bootstrap) {
2020
if (opts.tooltip) {
2121
this.container.attr('data-bs-toggle', 'tooltip')
2222
.attr('data-bs-title', opts.tooltip);
23-
new bootstrap.Tooltip(this.container);
23+
this.tooltip = new bootstrap.Tooltip(this.container);
2424
}
2525
if (opts.icon) {
2626
this.icon = $('<i />')
@@ -47,6 +47,12 @@ var yafowil_tiptap = (function (exports, $, bootstrap) {
4747
on_click(e) {
4848
e.preventDefault();
4949
}
50+
unload() {
51+
if (this.tooltip) {
52+
this.tooltip.dispose();
53+
}
54+
this.elem.off();
55+
}
5056
}
5157
class Button extends Action {
5258
compile(opts) {
@@ -90,6 +96,13 @@ var yafowil_tiptap = (function (exports, $, bootstrap) {
9096
}
9197
unload() {
9298
$(window).off('resize', this.on_resize);
99+
if (this.submit_elem) {
100+
this.submit_elem.off();
101+
}
102+
for (let child of this.children) {
103+
child.unload();
104+
}
105+
super.unload();
93106
}
94107
on_resize(e) {
95108
this.active = false;
@@ -627,16 +640,23 @@ var yafowil_tiptap = (function (exports, $, bootstrap) {
627640
}
628641
}
629642
destroy() {
643+
if (this.editor) {
644+
this.editor.off();
645+
this.editor.destroy();
646+
}
630647
this.unload_all();
631-
this.editor.destroy();
632-
this.elem.empty();
633-
this.buttons = null;
648+
if (this.helpLink && typeof this.helpLink.unload === 'function') {
649+
this.helpLink.unload();
650+
}
651+
this.elem.removeData('yafowil-tiptap');
652+
this.editarea.off();
653+
this.controls.off();
654+
this.textarea.off();
655+
this.elem.off();
634656
}
635657
unload_all() {
636658
for (let btn in this.buttons) {
637-
if (this.buttons[btn].unload) {
638-
this.buttons[btn].unload();
639-
}
659+
this.buttons[btn].unload();
640660
}
641661
}
642662
add_button(name, container) {

0 commit comments

Comments
 (0)