@lesjoursfr/edith 2.1.2 → 2.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/build/edith.css +1 -0
- package/build/edith.js +1 -0
- package/dist/core/edit.d.ts +44 -0
- package/dist/core/edit.js +327 -0
- package/dist/core/events.d.ts +4 -0
- package/dist/core/events.js +5 -0
- package/dist/core/history.d.ts +14 -0
- package/dist/core/history.js +24 -0
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.js +5 -0
- package/dist/core/mode.d.ts +4 -0
- package/dist/core/mode.js +5 -0
- package/dist/core/range.d.ts +45 -0
- package/dist/core/range.js +86 -0
- package/dist/css/edith.scss +283 -0
- package/dist/edith-options.d.ts +17 -0
- package/dist/edith-options.js +56 -0
- package/dist/edith.d.ts +30 -0
- package/dist/edith.js +77 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/ui/button.d.ts +25 -0
- package/dist/ui/button.js +166 -0
- package/dist/ui/editor.d.ts +37 -0
- package/dist/ui/editor.js +323 -0
- package/dist/ui/index.d.ts +3 -0
- package/dist/ui/index.js +3 -0
- package/dist/ui/modal.d.ts +32 -0
- package/dist/ui/modal.js +145 -0
- package/package.json +12 -9
- package/src/core/edit.ts +94 -2
- package/src/core/events.ts +0 -144
- package/src/core/index.ts +0 -2
- package/src/core/range.ts +1 -1
- package/src/edith.ts +2 -1
- package/src/ui/button.ts +2 -1
- package/src/ui/editor.ts +12 -9
- package/src/ui/modal.ts +1 -1
- package/src/core/dom.ts +0 -584
- package/src/core/throttle.ts +0 -172
package/src/core/events.ts
CHANGED
|
@@ -1,148 +1,4 @@
|
|
|
1
|
-
const eventsNamespace = "edithEvents";
|
|
2
|
-
let eventsGuid = 0;
|
|
3
|
-
|
|
4
|
-
type EdithEvent = { type: string; ns: Array<string> | null; handler: EventListenerOrEventListenerObject };
|
|
5
|
-
|
|
6
|
-
type EdithEvents = {
|
|
7
|
-
[key: string]: EdithEvent;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
declare global {
|
|
11
|
-
interface Window {
|
|
12
|
-
edithEvents: EdithEvents;
|
|
13
|
-
}
|
|
14
|
-
interface Document {
|
|
15
|
-
edithEvents: EdithEvents;
|
|
16
|
-
}
|
|
17
|
-
interface HTMLElement {
|
|
18
|
-
edithEvents: EdithEvents;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
1
|
export enum Events {
|
|
23
2
|
modeChanged = "edith-mode-changed",
|
|
24
3
|
initialized = "edith-initialized",
|
|
25
4
|
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Parse an event type to separate the type & the namespace
|
|
29
|
-
* @param {string} string
|
|
30
|
-
*/
|
|
31
|
-
function parseEventType(string: string): Omit<EdithEvent, "handler"> {
|
|
32
|
-
const [type, ...nsArray] = string.split(".");
|
|
33
|
-
return {
|
|
34
|
-
type,
|
|
35
|
-
ns: nsArray ?? null,
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Set an event listener on the node.
|
|
41
|
-
* @param {Window|Document|HTMLElement} node
|
|
42
|
-
* @param {string} events
|
|
43
|
-
* @param {Function} handler
|
|
44
|
-
*/
|
|
45
|
-
function addEventListener(
|
|
46
|
-
node: Window | Document | HTMLElement,
|
|
47
|
-
events: string,
|
|
48
|
-
handler: EventListenerOrEventListenerObject
|
|
49
|
-
): void {
|
|
50
|
-
if (node[eventsNamespace] === undefined) {
|
|
51
|
-
node[eventsNamespace] = {};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
for (const event of events.split(" ")) {
|
|
55
|
-
const { type, ns } = parseEventType(event);
|
|
56
|
-
const handlerGuid = (++eventsGuid).toString(10);
|
|
57
|
-
node.addEventListener(type, handler);
|
|
58
|
-
node[eventsNamespace][handlerGuid] = { type, ns, handler };
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Remove event listeners from the node.
|
|
64
|
-
* @param {Window|Document|HTMLElement} node
|
|
65
|
-
* @param {string} events
|
|
66
|
-
* @param {Function|undefined} handler
|
|
67
|
-
*/
|
|
68
|
-
function removeEventListener(
|
|
69
|
-
node: Window | Document | HTMLElement,
|
|
70
|
-
events: string,
|
|
71
|
-
handler?: EventListenerOrEventListenerObject
|
|
72
|
-
): void {
|
|
73
|
-
if (node[eventsNamespace] === undefined) {
|
|
74
|
-
node[eventsNamespace] = {};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
for (const event of events.split(" ")) {
|
|
78
|
-
const { type, ns } = parseEventType(event);
|
|
79
|
-
|
|
80
|
-
for (const [guid, handlerObj] of Object.entries(node[eventsNamespace])) {
|
|
81
|
-
if (handlerObj.type !== type && type !== "*") {
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (
|
|
86
|
-
(ns === null || handlerObj.ns?.includes(ns[0])) &&
|
|
87
|
-
(handler === undefined || (typeof handler === "function" && handler === handlerObj.handler))
|
|
88
|
-
) {
|
|
89
|
-
delete node[eventsNamespace][guid];
|
|
90
|
-
node.removeEventListener(handlerObj.type, handlerObj.handler);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Set an event listener on every node.
|
|
98
|
-
* @param {Window|Document|HTMLElement|NodeList} nodes
|
|
99
|
-
* @param {string} events
|
|
100
|
-
* @param {Function} handler
|
|
101
|
-
*/
|
|
102
|
-
export function on(
|
|
103
|
-
nodes: Window | Document | HTMLElement | NodeListOf<HTMLElement>,
|
|
104
|
-
events: string,
|
|
105
|
-
handler: EventListenerOrEventListenerObject
|
|
106
|
-
): void {
|
|
107
|
-
if (nodes instanceof NodeList) {
|
|
108
|
-
for (const node of nodes) {
|
|
109
|
-
addEventListener(node, events, handler);
|
|
110
|
-
}
|
|
111
|
-
} else {
|
|
112
|
-
addEventListener(nodes, events, handler);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Remove event listeners from the node.
|
|
118
|
-
* @param {Window|Document|HTMLElement|NodeList} node
|
|
119
|
-
* @param {string} events
|
|
120
|
-
* @param {Function|undefined} handler
|
|
121
|
-
*/
|
|
122
|
-
export function off(
|
|
123
|
-
nodes: Window | Document | HTMLElement | NodeListOf<HTMLElement>,
|
|
124
|
-
events: string,
|
|
125
|
-
handler?: EventListenerOrEventListenerObject
|
|
126
|
-
): void {
|
|
127
|
-
if (nodes instanceof NodeList) {
|
|
128
|
-
for (const node of nodes) {
|
|
129
|
-
removeEventListener(node, events, handler);
|
|
130
|
-
}
|
|
131
|
-
} else {
|
|
132
|
-
removeEventListener(nodes, events, handler);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Trigger the EdithEvent on the node.
|
|
138
|
-
* @param {Window|Document|HTMLElement} node
|
|
139
|
-
* @param {string} event
|
|
140
|
-
* @param {Object|undefined} payload
|
|
141
|
-
*/
|
|
142
|
-
export function trigger(
|
|
143
|
-
node: Window | Document | HTMLElement,
|
|
144
|
-
event: string,
|
|
145
|
-
payload?: { [key: string]: unknown }
|
|
146
|
-
): void {
|
|
147
|
-
node.dispatchEvent(new CustomEvent(event, { detail: payload }));
|
|
148
|
-
}
|
package/src/core/index.ts
CHANGED
package/src/core/range.ts
CHANGED
package/src/edith.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { addClass, createNodeWith, off, on, removeClass, trigger } from "@lesjoursfr/browser-tools";
|
|
2
|
+
import { Events } from "./core/index.js";
|
|
2
3
|
import { DefaultOptions, EdithButtonsOption, EdithOptions, EdithToolbarOption } from "./edith-options.js";
|
|
3
4
|
import { EdithButton, EdithButtons, EdithEditor } from "./ui/index.js";
|
|
4
5
|
|
package/src/ui/button.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { createNodeWith } from "@lesjoursfr/browser-tools";
|
|
1
2
|
import { createPopper } from "@popperjs/core";
|
|
2
|
-
import { EditorModes, Events
|
|
3
|
+
import { EditorModes, Events } from "../core/index.js";
|
|
3
4
|
import { EdithButtonsOption } from "../edith-options.js";
|
|
4
5
|
import { Edith } from "../edith.js";
|
|
5
6
|
|
package/src/ui/editor.ts
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
import { html } from "@codemirror/lang-html";
|
|
2
|
+
import {
|
|
3
|
+
createNodeWith,
|
|
4
|
+
hasClass,
|
|
5
|
+
hasTagName,
|
|
6
|
+
isHTMLElement,
|
|
7
|
+
isSelfClosing,
|
|
8
|
+
isTextNode,
|
|
9
|
+
removeNodesRecursively,
|
|
10
|
+
throttle,
|
|
11
|
+
unwrapNode,
|
|
12
|
+
} from "@lesjoursfr/browser-tools";
|
|
2
13
|
import { EditorView, basicSetup } from "codemirror";
|
|
3
14
|
import {
|
|
4
15
|
EditorModes,
|
|
@@ -6,18 +17,10 @@ import {
|
|
|
6
17
|
History,
|
|
7
18
|
cleanPastedHtml,
|
|
8
19
|
clearSelectionStyle,
|
|
9
|
-
createNodeWith,
|
|
10
20
|
getSelection,
|
|
11
|
-
hasClass,
|
|
12
|
-
hasTagName,
|
|
13
|
-
isHTMLElement,
|
|
14
21
|
isSelectionInsideNode,
|
|
15
|
-
isSelfClosing,
|
|
16
|
-
removeNodesRecursively,
|
|
17
22
|
replaceSelectionByHtml,
|
|
18
23
|
restoreSelection,
|
|
19
|
-
throttle,
|
|
20
|
-
unwrapNode,
|
|
21
24
|
wrapInsideLink,
|
|
22
25
|
wrapInsideTag,
|
|
23
26
|
} from "../core/index.js";
|
|
@@ -362,7 +365,7 @@ export class EdithEditor {
|
|
|
362
365
|
}
|
|
363
366
|
} else {
|
|
364
367
|
// Detect style blocs in parents
|
|
365
|
-
let dest = sel.anchorNode as HTMLElement;
|
|
368
|
+
let dest = (isTextNode(sel.anchorNode!) ? sel.anchorNode!.parentNode : sel.anchorNode) as HTMLElement;
|
|
366
369
|
const style = { B: false, I: false, U: false, S: false, Q: false };
|
|
367
370
|
while (dest !== null && !hasClass(dest, "edith-visual")) {
|
|
368
371
|
// Check if it's a style tag
|
package/src/ui/modal.ts
CHANGED