@cocreate/text 1.28.0 → 1.28.1

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [1.28.1](https://github.com/CoCreate-app/CoCreate-text/compare/v1.28.0...v1.28.1) (2024-12-14)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * try catchblock ([ca984f1](https://github.com/CoCreate-app/CoCreate-text/commit/ca984f1276cfed24ee34c21e874905a39b4bfac7))
7
+
1
8
  # [1.28.0](https://github.com/CoCreate-app/CoCreate-text/compare/v1.27.1...v1.28.0) (2024-11-04)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/text",
3
- "version": "1.28.0",
3
+ "version": "1.28.1",
4
4
  "description": "A simple text component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "text",
package/src/updateText.js CHANGED
@@ -1,82 +1,155 @@
1
- import crdt from '@cocreate/crdt';
2
- import { getAttributes } from '@cocreate/utils';
3
- import { getStringPosition } from '@cocreate/selection';
1
+ import crdt from "@cocreate/crdt";
2
+ import { getAttributes } from "@cocreate/utils";
3
+ import { getStringPosition } from "@cocreate/selection";
4
4
 
5
- export function insertAdjacentElement({ domTextEditor, target, position, element, elementValue }) {
6
- let remove;
7
- if (element && !elementValue) {
8
- remove = getStringPosition({ string: domTextEditor.htmlString, target: element });
9
- if (!remove || !remove.start && !remove.end)
10
- throw new Error('insertAdjacentElement: element not found');
5
+ export function insertAdjacentElement({
6
+ domTextEditor,
7
+ target,
8
+ position,
9
+ element,
10
+ elementValue
11
+ }) {
12
+ try {
13
+ let remove;
14
+ if (element && !elementValue) {
15
+ remove = getStringPosition({
16
+ string: domTextEditor.htmlString,
17
+ target: element
18
+ });
19
+ if (!remove || (!remove.start && !remove.end))
20
+ throw new Error("insertAdjacentElement: element not found");
11
21
 
12
- elementValue = domTextEditor.htmlString.substring(remove.start, remove.end);
13
- }
22
+ elementValue = domTextEditor.htmlString.substring(
23
+ remove.start,
24
+ remove.end
25
+ );
26
+ }
14
27
 
15
- let { start } = getStringPosition({ string: domTextEditor.htmlString, target, position, value: elementValue });
16
- if (remove)
17
- _updateText({ domTextEditor, start: remove.start, end: remove.end });
18
- if (remove && remove.start < start) {
19
- let length = remove.end - remove.start;
20
- _updateText({ domTextEditor, value: elementValue, start: start - length });
21
- }
22
- else
23
- _updateText({ domTextEditor, value: elementValue, start });
28
+ let { start } = getStringPosition({
29
+ string: domTextEditor.htmlString,
30
+ target,
31
+ position,
32
+ value: elementValue
33
+ });
34
+ if (remove)
35
+ _updateText({
36
+ domTextEditor,
37
+ start: remove.start,
38
+ end: remove.end
39
+ });
40
+ if (remove && remove.start < start) {
41
+ let length = remove.end - remove.start;
42
+ _updateText({
43
+ domTextEditor,
44
+ value: elementValue,
45
+ start: start - length
46
+ });
47
+ } else _updateText({ domTextEditor, value: elementValue, start });
48
+ } catch (error) {
49
+ console.error(error);
50
+ }
24
51
  }
25
52
 
26
53
  export function removeElement({ domTextEditor, target }) {
27
- updateDomText({ domTextEditor, target });
54
+ updateDomText({ domTextEditor, target });
28
55
  }
29
56
 
30
57
  export function setInnerText({ domTextEditor, target, value, start, end }) {
31
- updateDomText({ domTextEditor, target, value, pos: { start, end } });
58
+ updateDomText({ domTextEditor, target, value, pos: { start, end } });
32
59
  }
33
60
 
34
61
  export function setClass({ domTextEditor, target, value }) {
35
- updateDomText({ domTextEditor, target, attribute: 'class', value });
62
+ updateDomText({ domTextEditor, target, attribute: "class", value });
36
63
  }
37
64
  export function removeClass({ domTextEditor, target, value }) {
38
- updateDomText({ domTextEditor, target, attribute: 'class', value, remove: true });
65
+ updateDomText({
66
+ domTextEditor,
67
+ target,
68
+ attribute: "class",
69
+ value,
70
+ remove: true
71
+ });
39
72
  }
40
73
 
41
74
  export function setStyle({ domTextEditor, target, property, value }) {
42
- updateDomText({ domTextEditor, target, attribute: 'style', property, value });
75
+ updateDomText({
76
+ domTextEditor,
77
+ target,
78
+ attribute: "style",
79
+ property,
80
+ value
81
+ });
43
82
  }
44
83
 
45
84
  export function removeStyle({ domTextEditor, target, property }) {
46
- updateDomText({ domTextEditor, target, attribute: 'style', property, remove: true });
85
+ updateDomText({
86
+ domTextEditor,
87
+ target,
88
+ attribute: "style",
89
+ property,
90
+ remove: true
91
+ });
47
92
  }
48
93
 
49
94
  export function setAttribute({ domTextEditor, target, name, value }) {
50
- updateDomText({ domTextEditor, target, attribute: name, value });
95
+ updateDomText({ domTextEditor, target, attribute: name, value });
51
96
  }
52
97
 
53
98
  export function removeAttribute({ domTextEditor, target, name }) {
54
- updateDomText({ domTextEditor, target, attribute: name, remove: 'true' });
99
+ updateDomText({ domTextEditor, target, attribute: name, remove: "true" });
55
100
  }
56
101
 
57
102
  export function replaceInnerText({ domTextEditor, target, value }) {
58
- updateDomText({ domTextEditor, target, value });
103
+ updateDomText({ domTextEditor, target, value });
59
104
  }
60
105
 
61
- export function updateDomText({ domTextEditor, target, position, element, elementValue, attribute, value, property, pos, remove }) {
62
- let selection = getStringPosition({ string: domTextEditor.htmlString, target, attribute, property, value, remove });
63
- if (!selection) return
64
- let { start, end, newValue } = selection;
65
- if (pos) {
66
- start += pos.start;
67
- end += pos.end;
68
- }
69
- if (start != end)
70
- _updateText({ domTextEditor, start, end });
71
- if (attribute && remove != 'true' || attribute && value)
72
- _updateText({ domTextEditor, value: ` ${attribute}="${newValue}"`, start });
73
- else if (value)
74
- _updateText({ domTextEditor, value, start });
106
+ export function updateDomText({
107
+ domTextEditor,
108
+ target,
109
+ position,
110
+ element,
111
+ elementValue,
112
+ attribute,
113
+ value,
114
+ property,
115
+ pos,
116
+ remove
117
+ }) {
118
+ let selection = getStringPosition({
119
+ string: domTextEditor.htmlString,
120
+ target,
121
+ attribute,
122
+ property,
123
+ value,
124
+ remove
125
+ });
126
+ if (!selection) return;
127
+ let { start, end, newValue } = selection;
128
+ if (pos) {
129
+ start += pos.start;
130
+ end += pos.end;
131
+ }
132
+ if (start != end) _updateText({ domTextEditor, start, end });
133
+ if ((attribute && remove != "true") || (attribute && value))
134
+ _updateText({
135
+ domTextEditor,
136
+ value: ` ${attribute}="${newValue}"`,
137
+ start
138
+ });
139
+ else if (value) _updateText({ domTextEditor, value, start });
75
140
  }
76
141
 
77
142
  function _updateText({ domTextEditor, value, start, end }) {
78
- if (domTextEditor.tagName == 'HTML')
79
- domTextEditor = domTextEditor.ownerDocument.defaultView.frameElement;
80
- const { array, object, key, isCrud } = getAttributes(domTextEditor);
81
- crdt.updateText({ array, object, key, value, start, length: end - start, crud: isCrud });
143
+ if (domTextEditor.tagName == "HTML")
144
+ domTextEditor = domTextEditor.ownerDocument.defaultView.frameElement;
145
+ const { array, object, key, isCrud } = getAttributes(domTextEditor);
146
+ crdt.updateText({
147
+ array,
148
+ object,
149
+ key,
150
+ value,
151
+ start,
152
+ length: end - start,
153
+ crud: isCrud
154
+ });
82
155
  }