@lesjoursfr/edith 0.1.0 → 0.1.4

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": "0.1.0",
3
+ "version": "0.1.4",
4
4
  "description": "Simple WYSIWYG editor.",
5
5
  "license": "MIT",
6
6
  "repository": "lesjoursfr/edith",
@@ -12,7 +12,7 @@
12
12
  "access": "public"
13
13
  },
14
14
  "engines": {
15
- "node": ">=v18.3.0"
15
+ "node": ">=16.15.1 || >=18.4.0"
16
16
  },
17
17
  "browserslist": [
18
18
  "Chrome >= 60",
@@ -45,40 +45,40 @@
45
45
  "editor"
46
46
  ],
47
47
  "devDependencies": {
48
- "@babel/core": "^7.18.5",
49
- "@babel/preset-env": "^7.18.2",
50
- "@codemirror/lang-html": "^6.0.0",
51
- "@fortawesome/fontawesome-free": "^6.1.1",
48
+ "@babel/core": "^7.18.10",
49
+ "@babel/preset-env": "^7.18.10",
50
+ "@codemirror/lang-html": "^6.1.0",
51
+ "@fortawesome/fontawesome-free": "^6.1.2",
52
52
  "@popperjs/core": "^2.11.5",
53
- "ava": "^4.3.0",
53
+ "ava": "^4.3.1",
54
54
  "babel-loader": "^8.2.5",
55
- "codemirror": "^6.0.0",
55
+ "codemirror": "^6.0.1",
56
56
  "css-loader": "^6.7.1",
57
- "eslint": "^8.18.0",
57
+ "eslint": "^8.21.0",
58
58
  "eslint-config-prettier": "^8.5.0",
59
59
  "eslint-config-standard": "^17.0.0",
60
60
  "eslint-plugin-import": "^2.26.0",
61
- "eslint-plugin-n": "^15.2.3",
61
+ "eslint-plugin-n": "^15.2.4",
62
62
  "eslint-plugin-promise": "^6.0.0",
63
63
  "jsdom": "^20.0.0",
64
64
  "mini-css-extract-plugin": "^2.6.1",
65
65
  "postcss": "^8.4.14",
66
66
  "prettier": "^2.7.1",
67
- "sass": "^1.52.3",
68
- "sass-loader": "^13.0.0",
67
+ "sass": "^1.54.0",
68
+ "sass-loader": "^13.0.2",
69
69
  "style-loader": "^3.3.1",
70
70
  "stylelint": "^14.9.1",
71
71
  "stylelint-config-prettier": "^9.0.3",
72
72
  "stylelint-config-sass-guidelines": "^9.0.1",
73
- "webpack": "^5.73.0",
73
+ "webpack": "^5.74.0",
74
74
  "webpack-cli": "^4.10.0",
75
- "webpack-dev-server": "^4.9.2"
75
+ "webpack-dev-server": "^4.9.3"
76
76
  },
77
77
  "peerDependencies": {
78
- "@codemirror/lang-html": "^6.0.0",
79
- "@fortawesome/fontawesome-free": "^6.1.1",
78
+ "@codemirror/lang-html": "^6.1.0",
79
+ "@fortawesome/fontawesome-free": "^6.1.2",
80
80
  "@popperjs/core": "^2.11.5",
81
- "codemirror": "^6.0.0"
81
+ "codemirror": "^6.0.1"
82
82
  },
83
- "packageManager": "yarn@3.2.1"
83
+ "packageManager": "yarn@3.2.2"
84
84
  }
package/readme.md CHANGED
@@ -1,5 +1,5 @@
1
1
  [![npm version](https://badge.fury.io/js/@lesjoursfr%2Fedith.svg)](https://badge.fury.io/js/@lesjoursfr%2Fedith)
2
- [![Build Status](https://travis-ci.org/lesjoursfr/edith.svg?branch=master)](https://travis-ci.org/lesjoursfr/edith)
2
+ [![QC Checks](https://github.com/lesjoursfr/edith/actions/workflows/quality-control.yml/badge.svg)](https://github.com/lesjoursfr/edith/actions/workflows/quality-control.yml)
3
3
 
4
4
  # @lesjoursfr/edith
5
5
 
package/src/core/range.js CHANGED
@@ -62,3 +62,13 @@ export function selectNodeContents(target) {
62
62
  sel.removeAllRanges();
63
63
  sel.addRange(range);
64
64
  }
65
+
66
+ /**
67
+ * Check if the current selection is inside the given node.
68
+ * @param {Node} node the targeted node
69
+ * @returns {boolean} true if the selection is inside
70
+ */
71
+ export function isSelectionInsideNode(node) {
72
+ const { range } = getSelection();
73
+ return node.contains(range.startContainer) && node.contains(range.endContainer);
74
+ }
package/src/ui/button.js CHANGED
@@ -46,7 +46,10 @@ function EdithButton(ctx, options) {
46
46
  }
47
47
 
48
48
  EdithButton.prototype.click = function (event) {
49
+ // Prevent default
49
50
  event.preventDefault();
51
+
52
+ // Check if the onclick attribute is a internal function ID or a custom function
50
53
  if (typeof this.onclick === "string") {
51
54
  onButtonClick(this.ctx, this.onclick, event);
52
55
  } else {
package/src/ui/editor.js CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  import { Events } from "../core/event.js";
12
12
  import { History } from "../core/history.js";
13
13
  import { EditorModes } from "../core/mode.js";
14
- import { getSelection, restoreSelection } from "../core/range.js";
14
+ import { getSelection, restoreSelection, isSelectionInsideNode } from "../core/range.js";
15
15
  import { throttle } from "../core/throttle.js";
16
16
  import { EdithModal, createInputModalField, createCheckboxModalField } from "./modal.js";
17
17
 
@@ -58,6 +58,14 @@ EdithEditor.prototype.render = function () {
58
58
  return editorWrapper;
59
59
  };
60
60
 
61
+ EdithEditor.prototype.getVisualEditorElement = function () {
62
+ return this.editors.visual;
63
+ };
64
+
65
+ EdithEditor.prototype.getCodeEditorElement = function () {
66
+ return this.editors.code;
67
+ };
68
+
61
69
  EdithEditor.prototype.setContent = function (content) {
62
70
  // Replace   by the string we use as a visual return
63
71
  content = content.replace(/&nbsp;/g, '<span class="edith-nbsp" contenteditable="false">¶</span>');
@@ -107,13 +115,17 @@ EdithEditor.prototype.restoreSnapshot = function () {
107
115
  };
108
116
 
109
117
  EdithEditor.prototype.wrapInsideTag = function (tag) {
110
- wrapInsideTag(tag);
111
- this.takeSnapshot();
118
+ if (isSelectionInsideNode(this.editors.visual)) {
119
+ wrapInsideTag(tag);
120
+ this.takeSnapshot();
121
+ }
112
122
  };
113
123
 
114
124
  EdithEditor.prototype.replaceByHtml = function (html) {
115
- replaceSelectionByHtml(html);
116
- this.takeSnapshot();
125
+ if (isSelectionInsideNode(this.editors.visual)) {
126
+ replaceSelectionByHtml(html);
127
+ this.takeSnapshot();
128
+ }
117
129
  };
118
130
 
119
131
  EdithEditor.prototype.clearStyle = function () {
@@ -291,7 +303,7 @@ EdithEditor.prototype.onPasteEvent = function (e) {
291
303
  const lines = e.clipboardData.getData("text/plain").split(/[\r\n]+/g);
292
304
 
293
305
  // Add the content as text nodes with a <br> node between each line
294
- for (let i = 0; i < lines.length - 1; i++) {
306
+ for (let i = 0; i < lines.length; i++) {
295
307
  if (frag.length !== 0) {
296
308
  frag.append(document.createElement("br"));
297
309
  }