@companix/uikit 0.1.12 → 0.1.13
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/dist/WriteBar/Editor.d.ts +5 -0
- package/dist/WriteBar/utils.d.ts +0 -2
- package/dist/bundle.es41.js +27 -19
- package/dist/bundle.es72.js +29 -30
- package/package.json +1 -1
package/dist/WriteBar/utils.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
export declare const isTextNode: (node: Node) => node is Text;
|
|
2
|
-
export declare const isElementNode: (node: Node) => node is Element;
|
|
3
1
|
export declare const getNodeTextContent: (node: Node) => string;
|
|
4
2
|
export declare const getEditorSelection: (root: HTMLElement) => {
|
|
5
3
|
range: Range;
|
package/dist/bundle.es41.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { escape as u, unescape as f, isElementNode as g } from "@companix/utils-browser";
|
|
2
|
+
import { onElementKeyDownFactory as v } from "./bundle.es71.js";
|
|
3
|
+
import { getEditorSelection as l, normalizeEditorText as a, getNodeTextContent as h, scrollSelectionIntoView as d, getTextSelectionOffsets as E } from "./bundle.es72.js";
|
|
4
|
+
class k {
|
|
4
5
|
element;
|
|
5
6
|
prevText = "";
|
|
6
7
|
prevTextSelection = { from: 0, to: 0 };
|
|
@@ -21,7 +22,7 @@ class C {
|
|
|
21
22
|
};
|
|
22
23
|
}
|
|
23
24
|
onKeyCombo(e) {
|
|
24
|
-
const t =
|
|
25
|
+
const t = v(e);
|
|
25
26
|
return this.element.addEventListener("keydown", t), this.handlers.keyCombo = e, () => {
|
|
26
27
|
this.handlers.keyCombo = void 0, this.element.removeEventListener("keydown", t);
|
|
27
28
|
};
|
|
@@ -42,13 +43,18 @@ class C {
|
|
|
42
43
|
};
|
|
43
44
|
}
|
|
44
45
|
setSelection(e) {
|
|
45
|
-
const t =
|
|
46
|
+
const t = l(this.element);
|
|
46
47
|
if (!("selection" in t)) return;
|
|
47
48
|
const n = document.createRange(), s = this.resolveDomPosition(e.from), o = this.resolveDomPosition(e.to);
|
|
48
49
|
n.setStart(s.container, s.offset), n.setEnd(o.container, o.offset), t.selection.removeAllRanges(), t.selection.addRange(n);
|
|
49
50
|
}
|
|
51
|
+
// вставить переданный текст в текущую выделенную область/позицию курсора внутри contenteditable
|
|
50
52
|
insertAtSelection(e, t) {
|
|
51
|
-
t && this.setSelection(t)
|
|
53
|
+
t && this.setSelection(t);
|
|
54
|
+
const n = C(u(e));
|
|
55
|
+
for (const s of n)
|
|
56
|
+
s.kind === "Text" && document.execCommand("insertText", !1, f(s.text));
|
|
57
|
+
this.checkContentChanged();
|
|
52
58
|
}
|
|
53
59
|
removeFormat() {
|
|
54
60
|
document.execCommand("removeFormat");
|
|
@@ -57,7 +63,7 @@ class C {
|
|
|
57
63
|
const s = !this.isFocused() && !n;
|
|
58
64
|
this.element.innerText = "";
|
|
59
65
|
const o = document.activeElement, i = this.element.inputMode;
|
|
60
|
-
s && (this.element.inputMode = "none"), this.element.focus(), document.execCommand("removeFormat"), t ? document.execCommand("inserthtml", !1, e) : this.insertAtSelection(
|
|
66
|
+
s && (this.element.inputMode = "none"), this.element.focus(), document.execCommand("removeFormat"), t ? document.execCommand("inserthtml", !1, e) : this.insertAtSelection(a(e)), s && (this.element.blur(), this.element.inputMode = i, o?.focus?.()), this.checkContentChanged();
|
|
61
67
|
}
|
|
62
68
|
getText() {
|
|
63
69
|
let e = "";
|
|
@@ -67,7 +73,7 @@ class C {
|
|
|
67
73
|
);
|
|
68
74
|
for (; t.nextNode(); )
|
|
69
75
|
e += h(t.currentNode);
|
|
70
|
-
return
|
|
76
|
+
return a(e);
|
|
71
77
|
}
|
|
72
78
|
getHtml() {
|
|
73
79
|
return this.element.innerHTML;
|
|
@@ -76,18 +82,18 @@ class C {
|
|
|
76
82
|
const e = this.element.innerHTML, t = /<img class="Emoji .+?" src=".+?" alt=".+?">/, n = /alt=".+?"/, s = (o) => {
|
|
77
83
|
const i = o.match(t);
|
|
78
84
|
if (!i?.[0]) return o;
|
|
79
|
-
const
|
|
80
|
-
if (!
|
|
81
|
-
const
|
|
82
|
-
return s(o.replace(i[0],
|
|
85
|
+
const c = i[0].match(n);
|
|
86
|
+
if (!c?.[0]) return o;
|
|
87
|
+
const m = c[0].replace("alt=", "").replaceAll('"', "");
|
|
88
|
+
return s(o.replace(i[0], m));
|
|
83
89
|
};
|
|
84
90
|
return s(e);
|
|
85
91
|
}
|
|
86
92
|
focus(e = !1) {
|
|
87
93
|
if (this.isFocused()) return;
|
|
88
94
|
this.element.focus();
|
|
89
|
-
const t =
|
|
90
|
-
"selection" in t && (t.selection.selectAllChildren(this.element), e ? t.selection.collapseToStart() : t.selection.collapseToEnd(),
|
|
95
|
+
const t = l(this.element);
|
|
96
|
+
"selection" in t && (t.selection.selectAllChildren(this.element), e ? t.selection.collapseToStart() : t.selection.collapseToEnd(), d());
|
|
91
97
|
}
|
|
92
98
|
isFocused() {
|
|
93
99
|
return document.activeElement === this.element;
|
|
@@ -101,7 +107,7 @@ class C {
|
|
|
101
107
|
if (!t) return;
|
|
102
108
|
const n = t.getData("text");
|
|
103
109
|
if (n) {
|
|
104
|
-
this.insertAtSelection(n),
|
|
110
|
+
this.insertAtSelection(n), d();
|
|
105
111
|
return;
|
|
106
112
|
}
|
|
107
113
|
};
|
|
@@ -117,9 +123,9 @@ class C {
|
|
|
117
123
|
n && (t.selectNode(e.target), t.collapse(e.offsetX <= 8), n.removeAllRanges(), n.addRange(t));
|
|
118
124
|
};
|
|
119
125
|
checkContentChanged = () => {
|
|
120
|
-
const e =
|
|
126
|
+
const e = a(this.getText()), t = l(this.element);
|
|
121
127
|
if (!("range" in t)) return;
|
|
122
|
-
const n =
|
|
128
|
+
const n = E(this.element, t.range);
|
|
123
129
|
if (e === this.prevText && n.from === this.prevTextSelection.from && n.to === this.prevTextSelection.to)
|
|
124
130
|
return;
|
|
125
131
|
const s = e === `
|
|
@@ -139,7 +145,7 @@ class C {
|
|
|
139
145
|
for (; t.nextNode(); ) {
|
|
140
146
|
const s = t.currentNode, o = h(s).length;
|
|
141
147
|
if (o >= n)
|
|
142
|
-
return
|
|
148
|
+
return g(s) && s.nodeName === "BR" && (n -= 1), {
|
|
143
149
|
container: s,
|
|
144
150
|
offset: Math.max(n, 0)
|
|
145
151
|
};
|
|
@@ -151,6 +157,8 @@ class C {
|
|
|
151
157
|
};
|
|
152
158
|
}
|
|
153
159
|
}
|
|
160
|
+
const C = (r) => r ? [{ kind: "Text", text: r }] : [];
|
|
154
161
|
export {
|
|
155
|
-
|
|
162
|
+
k as Editor,
|
|
163
|
+
C as toTextChunksLite
|
|
156
164
|
};
|
package/dist/bundle.es72.js
CHANGED
|
@@ -1,44 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { isTextNode as s, isElementNode as i } from "@companix/utils-browser";
|
|
2
|
+
const a = (t) => s(t) ? t.nodeValue || "" : i(t) && t.nodeName === "IMG" ? t.getAttribute("alt") || "" : i(t) && t.nodeName === "DIV" || i(t) && t.nodeName === "BR" && !(t.parentElement && i(t.parentElement) && t.parentElement.nodeName === "DIV" && t.parentElement.getAttribute("contenteditable") !== "true") ? `
|
|
3
|
+
` : "", m = (t) => {
|
|
4
|
+
const e = document.getSelection();
|
|
5
|
+
if (!e || e.rangeCount === 0)
|
|
5
6
|
return {};
|
|
6
|
-
const n =
|
|
7
|
-
return n &&
|
|
8
|
-
},
|
|
9
|
-
let r = n, o =
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
for (;
|
|
13
|
-
if (
|
|
7
|
+
const n = e.getRangeAt(0);
|
|
8
|
+
return n && t.contains(n.commonAncestorContainer) ? { range: n, selection: e } : {};
|
|
9
|
+
}, l = (t, e, n) => {
|
|
10
|
+
let r = n, o = e;
|
|
11
|
+
t === e && (r = 0, o = t.childNodes[n]);
|
|
12
|
+
const c = document.createTreeWalker(t, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT);
|
|
13
|
+
for (; c.nextNode(); ) {
|
|
14
|
+
if (c.currentNode === o)
|
|
14
15
|
return r;
|
|
15
|
-
r += a(
|
|
16
|
+
r += a(c.currentNode).length;
|
|
16
17
|
}
|
|
17
18
|
return r;
|
|
18
|
-
},
|
|
19
|
-
const n =
|
|
19
|
+
}, f = (t, e) => {
|
|
20
|
+
const n = l(t, e.startContainer, e.startOffset);
|
|
20
21
|
return {
|
|
21
22
|
from: n,
|
|
22
|
-
to:
|
|
23
|
+
to: e.collapsed ? n : l(t, e.endContainer, e.endOffset)
|
|
23
24
|
};
|
|
24
|
-
},
|
|
25
|
-
const
|
|
26
|
-
if (!
|
|
27
|
-
const
|
|
28
|
-
if (
|
|
29
|
-
const n =
|
|
25
|
+
}, g = () => {
|
|
26
|
+
const t = window.getSelection();
|
|
27
|
+
if (!t || !t.rangeCount) return;
|
|
28
|
+
const e = t.getRangeAt(0);
|
|
29
|
+
if (e.commonAncestorContainer === document) return;
|
|
30
|
+
const n = e.commonAncestorContainer.parentElement;
|
|
30
31
|
if (!n) return;
|
|
31
|
-
const r = n.getBoundingClientRect(), o =
|
|
32
|
+
const r = n.getBoundingClientRect(), o = e.getBoundingClientRect().bottom - r.bottom;
|
|
32
33
|
o > 0 && n.scrollBy({ top: o });
|
|
33
|
-
}, d = (
|
|
34
|
+
}, d = (t) => t.replace(/ /gi, " ").replace(/<br\s*\/?>/gi, `
|
|
34
35
|
`);
|
|
35
36
|
export {
|
|
36
|
-
|
|
37
|
+
m as getEditorSelection,
|
|
37
38
|
a as getNodeTextContent,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
c as isElementNode,
|
|
41
|
-
l as isTextNode,
|
|
39
|
+
l as getPlainTextOffset,
|
|
40
|
+
f as getTextSelectionOffsets,
|
|
42
41
|
d as normalizeEditorText,
|
|
43
|
-
|
|
42
|
+
g as scrollSelectionIntoView
|
|
44
43
|
};
|