@lesjoursfr/edith 1.0.1 → 1.1.0

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.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Simple WYSIWYG editor.",
5
5
  "license": "MIT",
6
6
  "repository": "lesjoursfr/edith",
@@ -45,29 +45,29 @@
45
45
  "editor"
46
46
  ],
47
47
  "devDependencies": {
48
- "@babel/core": "^7.20.5",
48
+ "@babel/core": "^7.20.12",
49
49
  "@babel/preset-env": "^7.20.2",
50
50
  "@codemirror/lang-html": "^6.4.0",
51
51
  "@fortawesome/fontawesome-free": "^6.2.1",
52
52
  "@popperjs/core": "^2.11.6",
53
53
  "ava": "^5.1.0",
54
- "babel-loader": "^9.1.0",
54
+ "babel-loader": "^9.1.2",
55
55
  "codemirror": "^6.0.1",
56
56
  "css-loader": "^6.7.3",
57
- "eslint": "^8.30.0",
58
- "eslint-config-prettier": "^8.5.0",
57
+ "eslint": "^8.31.0",
58
+ "eslint-config-prettier": "^8.6.0",
59
59
  "eslint-config-standard": "^17.0.0",
60
60
  "eslint-plugin-import": "^2.26.0",
61
- "eslint-plugin-n": "^15.6.0",
61
+ "eslint-plugin-n": "^15.6.1",
62
62
  "eslint-plugin-promise": "^6.1.1",
63
- "jsdom": "^20.0.3",
63
+ "jsdom": "^21.0.0",
64
64
  "mini-css-extract-plugin": "^2.7.2",
65
- "postcss": "^8.4.20",
66
- "prettier": "^2.8.1",
67
- "sass": "^1.57.0",
65
+ "postcss": "^8.4.21",
66
+ "prettier": "^2.8.2",
67
+ "sass": "^1.57.1",
68
68
  "sass-loader": "^13.2.0",
69
69
  "style-loader": "^3.3.1",
70
- "stylelint": "^14.16.0",
70
+ "stylelint": "^14.16.1",
71
71
  "stylelint-config-prettier": "^9.0.4",
72
72
  "stylelint-config-sass-guidelines": "^9.0.1",
73
73
  "webpack": "^5.75.0",
@@ -80,5 +80,5 @@
80
80
  "@popperjs/core": "^2.11.6",
81
81
  "codemirror": "^6.0.1"
82
82
  },
83
- "packageManager": "yarn@3.3.0"
83
+ "packageManager": "yarn@3.3.1"
84
84
  }
package/src/core/dom.js CHANGED
@@ -105,6 +105,25 @@ export function removeNodes(node, callbackFn) {
105
105
  }
106
106
  }
107
107
 
108
+ /**
109
+ * Remove recursively all node's child nodes that pass the test implemented by the provided function.
110
+ * @param {Node} node the node to process
111
+ * @param {Function} callbackFn the predicate
112
+ */
113
+ export function removeNodesRecursively(node, callbackFn) {
114
+ // Remove the node if it meets the condition
115
+ if (callbackFn(node)) {
116
+ node.remove();
117
+ return;
118
+ }
119
+
120
+ // Loop through the node’s children
121
+ for (const el of [...node.childNodes]) {
122
+ // Execute the same function if it’s an element node
123
+ removeNodesRecursively(el, callbackFn);
124
+ }
125
+ }
126
+
108
127
  /**
109
128
  * Remove all node's child nodes that are empty text nodes.
110
129
  * @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
+ removeNodes,
9
10
  removeEmptyTextNodes,
10
11
  removeCommentNodes,
11
12
  } from "./dom.js";
@@ -167,6 +168,11 @@ export function wrapInsideTag(tag, options = {}) {
167
168
  const node = document.createElement(tag);
168
169
  node.appendChild(range.extractContents());
169
170
  range.insertNode(node);
171
+
172
+ // Remove empty tags
173
+ removeNodes(parent, (el) => el.textContent.length === 0);
174
+
175
+ // Return the node
170
176
  selectNodeContents(node);
171
177
  return node;
172
178
  }
package/src/core/event.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export const Events = Object.freeze({
2
2
  modeChanged: "edith-mode-changed",
3
+ initialized: "edith-initialized",
3
4
  });
package/src/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { EdithEditor } from "./ui/editor.js";
2
2
  import { EdithButton, EdithButtons } from "./ui/button.js";
3
+ import { Events } from "./core/event.js";
3
4
 
4
5
  /*
5
6
  * Represents an editor
@@ -43,6 +44,9 @@ function Edith(element, options) {
43
44
  this.modals = document.createElement("div");
44
45
  this.modals.setAttribute("class", "edith-modals");
45
46
  this.element.append(this.modals);
47
+
48
+ // Trigger the initialized event once its initialized
49
+ this.trigger(Events.initialized);
46
50
  }
47
51
 
48
52
  Edith.prototype.on = function (type, listener, options) {
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 } from "../core/dom.js";
3
+ import { hasClass, hasTagName, createNodeWith, removeNodesRecursively } from "../core/dom.js";
4
4
  import {
5
5
  wrapInsideTag,
6
6
  replaceSelectionByHtml,
@@ -101,8 +101,12 @@ EdithEditor.prototype.getContent = function () {
101
101
  return "";
102
102
  }
103
103
 
104
+ // Remove empty tags
105
+ const placeholder = createNodeWith("div", { innerHTML: code });
106
+ removeNodesRecursively(placeholder, (el) => el.nodeType === Node.ELEMENT_NODE && el.textContent.length === 0);
107
+
104
108
  // Return clean code
105
- return code
109
+ return placeholder.innerHTML
106
110
  .replace(/\u200B/gi, "")
107
111
  .replace(/<\/p>\s*<p>/gi, "<br>")
108
112
  .replace(/(<p>|<\/p>)/gi, "")