@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.
@@ -31,4 +31,9 @@ export declare class Editor {
31
31
  private checkContentChanged;
32
32
  private resolveDomPosition;
33
33
  }
34
+ interface TextChunk {
35
+ kind: 'Text';
36
+ text: string;
37
+ }
38
+ export declare const toTextChunksLite: (input: string) => TextChunk[];
34
39
  export {};
@@ -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;
@@ -1,6 +1,7 @@
1
- import { onElementKeyDownFactory as m } from "./bundle.es71.js";
2
- import { getEditorSelection as r, normalizeEditorText as l, getNodeTextContent as h, scrollSelectionIntoView as c, getTextSelectionOffsets as u, isElementNode as f } from "./bundle.es72.js";
3
- class C {
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 = m(e);
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 = r(this.element);
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), this.checkContentChanged();
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(l(e)), s && (this.element.blur(), this.element.inputMode = i, o?.focus?.()), this.checkContentChanged();
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 l(e);
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 a = i[0].match(n);
80
- if (!a?.[0]) return o;
81
- const d = a[0].replace("alt=", "").replaceAll('"', "");
82
- return s(o.replace(i[0], d));
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 = r(this.element);
90
- "selection" in t && (t.selection.selectAllChildren(this.element), e ? t.selection.collapseToStart() : t.selection.collapseToEnd(), c());
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), c();
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 = l(this.getText()), t = r(this.element);
126
+ const e = a(this.getText()), t = l(this.element);
121
127
  if (!("range" in t)) return;
122
- const n = u(this.element, t.range);
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 f(s) && s.nodeName === "BR" && (n -= 1), {
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
- C as Editor
162
+ k as Editor,
163
+ C as toTextChunksLite
156
164
  };
@@ -1,44 +1,43 @@
1
- const l = (e) => e.nodeType === Node.TEXT_NODE, c = (e) => e.nodeType === Node.ELEMENT_NODE, a = (e) => l(e) ? e.nodeValue || "" : c(e) && e.nodeName === "IMG" ? e.getAttribute("alt") || "" : c(e) && e.nodeName === "DIV" || c(e) && e.nodeName === "BR" && !(e.parentElement && c(e.parentElement) && e.parentElement.nodeName === "DIV" && e.parentElement.getAttribute("contenteditable") !== "true") ? `
2
- ` : "", u = (e) => {
3
- const t = document.getSelection();
4
- if (!t || t.rangeCount === 0)
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 = t.getRangeAt(0);
7
- return n && e.contains(n.commonAncestorContainer) ? { range: n, selection: t } : {};
8
- }, s = (e, t, n) => {
9
- let r = n, o = t;
10
- e === t && (r = 0, o = e.childNodes[n]);
11
- const i = document.createTreeWalker(e, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT);
12
- for (; i.nextNode(); ) {
13
- if (i.currentNode === o)
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(i.currentNode).length;
16
+ r += a(c.currentNode).length;
16
17
  }
17
18
  return r;
18
- }, m = (e, t) => {
19
- const n = s(e, t.startContainer, t.startOffset);
19
+ }, f = (t, e) => {
20
+ const n = l(t, e.startContainer, e.startOffset);
20
21
  return {
21
22
  from: n,
22
- to: t.collapsed ? n : s(e, t.endContainer, t.endOffset)
23
+ to: e.collapsed ? n : l(t, e.endContainer, e.endOffset)
23
24
  };
24
- }, f = () => {
25
- const e = window.getSelection();
26
- if (!e || !e.rangeCount) return;
27
- const t = e.getRangeAt(0);
28
- if (t.commonAncestorContainer === document) return;
29
- const n = t.commonAncestorContainer.parentElement;
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 = t.getBoundingClientRect().bottom - r.bottom;
32
+ const r = n.getBoundingClientRect(), o = e.getBoundingClientRect().bottom - r.bottom;
32
33
  o > 0 && n.scrollBy({ top: o });
33
- }, d = (e) => e.replace(/&nbsp;/gi, " ").replace(/<br\s*\/?>/gi, `
34
+ }, d = (t) => t.replace(/&nbsp;/gi, " ").replace(/<br\s*\/?>/gi, `
34
35
  `);
35
36
  export {
36
- u as getEditorSelection,
37
+ m as getEditorSelection,
37
38
  a as getNodeTextContent,
38
- s as getPlainTextOffset,
39
- m as getTextSelectionOffsets,
40
- c as isElementNode,
41
- l as isTextNode,
39
+ l as getPlainTextOffset,
40
+ f as getTextSelectionOffsets,
42
41
  d as normalizeEditorText,
43
- f as scrollSelectionIntoView
42
+ g as scrollSelectionIntoView
44
43
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@companix/uikit",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "main": "./dist/bundle.es.js",
5
5
  "module": "./dist/bundle.es.js",
6
6
  "types": "./dist/index.d.ts",