@lesjoursfr/edith 1.1.0 → 1.1.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lesjoursfr/edith",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Simple WYSIWYG editor.",
5
5
  "license": "MIT",
6
6
  "repository": "lesjoursfr/edith",
@@ -47,23 +47,23 @@
47
47
  "devDependencies": {
48
48
  "@babel/core": "^7.20.12",
49
49
  "@babel/preset-env": "^7.20.2",
50
- "@codemirror/lang-html": "^6.4.0",
50
+ "@codemirror/lang-html": "^6.4.1",
51
51
  "@fortawesome/fontawesome-free": "^6.2.1",
52
52
  "@popperjs/core": "^2.11.6",
53
- "ava": "^5.1.0",
53
+ "ava": "^5.1.1",
54
54
  "babel-loader": "^9.1.2",
55
55
  "codemirror": "^6.0.1",
56
56
  "css-loader": "^6.7.3",
57
- "eslint": "^8.31.0",
57
+ "eslint": "^8.32.0",
58
58
  "eslint-config-prettier": "^8.6.0",
59
59
  "eslint-config-standard": "^17.0.0",
60
- "eslint-plugin-import": "^2.26.0",
60
+ "eslint-plugin-import": "^2.27.4",
61
61
  "eslint-plugin-n": "^15.6.1",
62
62
  "eslint-plugin-promise": "^6.1.1",
63
63
  "jsdom": "^21.0.0",
64
64
  "mini-css-extract-plugin": "^2.7.2",
65
65
  "postcss": "^8.4.21",
66
- "prettier": "^2.8.2",
66
+ "prettier": "^2.8.3",
67
67
  "sass": "^1.57.1",
68
68
  "sass-loader": "^13.2.0",
69
69
  "style-loader": "^3.3.1",
@@ -75,7 +75,7 @@
75
75
  "webpack-dev-server": "^4.11.1"
76
76
  },
77
77
  "peerDependencies": {
78
- "@codemirror/lang-html": "^6.4.0",
78
+ "@codemirror/lang-html": "^6.4.1",
79
79
  "@fortawesome/fontawesome-free": "^6.2.1",
80
80
  "@popperjs/core": "^2.11.6",
81
81
  "codemirror": "^6.0.1"
package/src/core/dom.js CHANGED
@@ -92,6 +92,31 @@ export function textifyNode(node) {
92
92
  return newNode;
93
93
  }
94
94
 
95
+ /**
96
+ * Know if a tag si a self-closing tag
97
+ * @param {String} tagName
98
+ * @returns {Boolean}
99
+ */
100
+ export function isSelfClosing(tagName) {
101
+ return [
102
+ "AREA",
103
+ "BASE",
104
+ "BR",
105
+ "COL",
106
+ "EMBED",
107
+ "HR",
108
+ "IMG",
109
+ "INPUT",
110
+ "KEYGEN",
111
+ "LINK",
112
+ "META",
113
+ "PARAM",
114
+ "SOURCE",
115
+ "TRACK",
116
+ "WBR",
117
+ ].includes(tagName);
118
+ }
119
+
95
120
  /**
96
121
  * Remove all node's child nodes that pass the test implemented by the provided function.
97
122
  * @param {Node} node the node to process
package/src/core/edit.js CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  createNodeWith,
7
7
  unwrapNode,
8
8
  textifyNode,
9
+ isSelfClosing,
9
10
  removeNodes,
10
11
  removeEmptyTextNodes,
11
12
  removeCommentNodes,
@@ -170,7 +171,7 @@ export function wrapInsideTag(tag, options = {}) {
170
171
  range.insertNode(node);
171
172
 
172
173
  // Remove empty tags
173
- removeNodes(parent, (el) => el.textContent.length === 0);
174
+ removeNodes(parent, (el) => !isSelfClosing(el.tagName) && el.textContent.length === 0);
174
175
 
175
176
  // Return the node
176
177
  selectNodeContents(node);
package/src/ui/editor.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { EditorView, basicSetup } from "codemirror";
2
2
  import { html } from "@codemirror/lang-html";
3
- import { hasClass, hasTagName, createNodeWith, removeNodesRecursively } from "../core/dom.js";
3
+ import { hasClass, hasTagName, createNodeWith, isSelfClosing, removeNodesRecursively } from "../core/dom.js";
4
4
  import {
5
5
  wrapInsideTag,
6
6
  replaceSelectionByHtml,
@@ -32,9 +32,9 @@ function EdithEditor(ctx, options) {
32
32
 
33
33
  EdithEditor.prototype.render = function () {
34
34
  // Create a wrapper for the editor
35
- const editorWrapper = document.createElement("div");
36
- editorWrapper.setAttribute("class", "edith-editing-area");
37
- editorWrapper.setAttribute(
35
+ this.editors.wrapper = document.createElement("div");
36
+ this.editors.wrapper.setAttribute("class", "edith-editing-area");
37
+ this.editors.wrapper.setAttribute(
38
38
  "style",
39
39
  this.resizable ? `min-height: ${this.height}px; resize: vertical` : `height: ${this.height}px`
40
40
  );
@@ -44,12 +44,12 @@ EdithEditor.prototype.render = function () {
44
44
  this.editors.visual.setAttribute("class", "edith-visual");
45
45
  this.editors.visual.setAttribute("contenteditable", "true");
46
46
  this.editors.visual.innerHTML = this.content;
47
- editorWrapper.append(this.editors.visual);
47
+ this.editors.wrapper.append(this.editors.visual);
48
48
 
49
49
  // Create the code editor
50
50
  this.editors.code = document.createElement("div");
51
51
  this.editors.code.setAttribute("class", "edith-code edith-hidden");
52
- editorWrapper.append(this.editors.code);
52
+ this.editors.wrapper.append(this.editors.code);
53
53
 
54
54
  // Bind events
55
55
  const keyEventsListener = this.onKeyEvent.bind(this);
@@ -59,7 +59,7 @@ EdithEditor.prototype.render = function () {
59
59
  this.editors.visual.addEventListener("paste", pasteEventListener);
60
60
 
61
61
  // Return the wrapper
62
- return editorWrapper;
62
+ return this.editors.wrapper;
63
63
  };
64
64
 
65
65
  EdithEditor.prototype.getVisualEditorElement = function () {
@@ -103,7 +103,10 @@ EdithEditor.prototype.getContent = function () {
103
103
 
104
104
  // Remove empty tags
105
105
  const placeholder = createNodeWith("div", { innerHTML: code });
106
- removeNodesRecursively(placeholder, (el) => el.nodeType === Node.ELEMENT_NODE && el.textContent.length === 0);
106
+ removeNodesRecursively(
107
+ placeholder,
108
+ (el) => el.nodeType === Node.ELEMENT_NODE && !isSelfClosing(el.tagName) && el.textContent.length === 0
109
+ );
107
110
 
108
111
  // Return clean code
109
112
  return placeholder.innerHTML
@@ -353,16 +356,14 @@ EdithEditor.prototype.onPasteEvent = function (e) {
353
356
  };
354
357
 
355
358
  EdithEditor.prototype.destroy = function () {
356
- // Check the current mode
357
- if (this.mode === EditorModes.Visual) {
358
- // Remove the visual editor
359
- this.editors.visual.remove();
360
- } else {
361
- // Remove the code editor
359
+ if (this.mode === EditorModes.Code) {
362
360
  this.codeMirror.destroy();
363
361
  this.codeMirror = null;
364
- this.editors.code.remove();
365
362
  }
363
+
364
+ // Remove editors from the DOM
365
+ this.editors.wrapper.remove();
366
+ this.editors = {};
366
367
  };
367
368
 
368
369
  export { EdithEditor };