@lesjoursfr/edith 0.1.0 → 0.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 +2 -2
- package/readme.md +1 -1
- package/src/core/range.js +10 -0
- package/src/ui/button.js +3 -0
- package/src/ui/editor.js +18 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lesjoursfr/edith",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
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": ">=
|
|
15
|
+
"node": ">=16.15.1 || >=18.4.0"
|
|
16
16
|
},
|
|
17
17
|
"browserslist": [
|
|
18
18
|
"Chrome >= 60",
|
package/readme.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
[](https://badge.fury.io/js/@lesjoursfr%2Fedith)
|
|
2
|
-
[](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(/ /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
|
-
|
|
111
|
-
|
|
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
|
-
|
|
116
|
-
|
|
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
|
|
306
|
+
for (let i = 0; i < lines.length; i++) {
|
|
295
307
|
if (frag.length !== 0) {
|
|
296
308
|
frag.append(document.createElement("br"));
|
|
297
309
|
}
|