tiny-markdown-editor 0.2.31 → 0.2.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -128,6 +128,9 @@ class CommandBar {
128
128
  this.buttons = {};
129
129
  this.state = {};
130
130
  this.hotkeys = [];
131
+ this.cleanupFns = [];
132
+ this.detachEditor = null;
133
+ this.destroyed = false;
131
134
  this.e = null;
132
135
  this.editor = null;
133
136
  this.commands = {};
@@ -167,7 +170,7 @@ class CommandBar {
167
170
  "insertLink",
168
171
  "insertImage",
169
172
  ]);
170
- document.addEventListener("keydown", (e) => this.handleKeydown(e));
173
+ this.addListener(document, "keydown", (e) => this.handleKeydown(e));
171
174
  if (props.editor)
172
175
  this.setEditor(props.editor);
173
176
  }
@@ -274,7 +277,7 @@ class CommandBar {
274
277
  this.buttons[commandName].className = "TMCommandButton TMCommandButton_Disabled";
275
278
  this.buttons[commandName].title = title;
276
279
  this.buttons[commandName].innerHTML = this.commands[commandName].innerHTML || "?";
277
- this.buttons[commandName].addEventListener("mousedown", (e) => this.handleClick(commandName, e));
280
+ this.addListener(this.buttons[commandName], "mousedown", (e) => this.handleClick(commandName, e));
278
281
  this.e.appendChild(this.buttons[commandName]);
279
282
  }
280
283
  }
@@ -304,8 +307,12 @@ class CommandBar {
304
307
  }
305
308
  }
306
309
  setEditor(editor) {
310
+ if (this.detachEditor)
311
+ this.detachEditor();
307
312
  this.editor = editor;
308
- editor.addEventListener("selection", (e) => this.handleSelection(e));
313
+ const handler = (e) => this.handleSelection(e);
314
+ editor.addEventListener("selection", handler);
315
+ this.detachEditor = () => editor.removeEventListener("selection", handler);
309
316
  }
310
317
  handleSelection(event) {
311
318
  if (event.commandState) {
@@ -351,6 +358,33 @@ class CommandBar {
351
358
  }
352
359
  }
353
360
  }
361
+ addListener(target, type, handler) {
362
+ target.addEventListener(type, handler);
363
+ this.cleanupFns.push(() => target.removeEventListener(type, handler));
364
+ }
365
+ /**
366
+ * Removes all listeners registered by this instance, including the document-level keydown
367
+ * listener and the selection listener on its editor, and detaches the command bar element.
368
+ * Safe to call multiple times.
369
+ */
370
+ destroy() {
371
+ if (this.destroyed)
372
+ return;
373
+ this.destroyed = true;
374
+ if (this.detachEditor) {
375
+ this.detachEditor();
376
+ this.detachEditor = null;
377
+ }
378
+ for (const cleanup of this.cleanupFns)
379
+ cleanup();
380
+ this.cleanupFns = [];
381
+ if (this.e && this.e.parentNode) {
382
+ this.e.parentNode.removeChild(this.e);
383
+ }
384
+ this.editor = null;
385
+ this.e = null;
386
+ this.buttons = {};
387
+ }
354
388
  }
355
389
  exports.CommandBar = CommandBar;
356
390
  exports.default = CommandBar;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiny-markdown-editor",
3
- "version": "0.2.31",
3
+ "version": "0.2.33",
4
4
  "description": "TinyMDE: A tiny, ultra low dependency, embeddable HTML/JavaScript Markdown editor.",
5
5
  "main": "lib/index.js",
6
6
  "files": [