@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 +7 -0
- package/package.json +1 -1
- package/src/updateText.js +120 -47
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
package/src/updateText.js
CHANGED
@@ -1,82 +1,155 @@
|
|
1
|
-
import crdt from
|
2
|
-
import { getAttributes } from
|
3
|
-
import { getStringPosition } from
|
1
|
+
import crdt from "@cocreate/crdt";
|
2
|
+
import { getAttributes } from "@cocreate/utils";
|
3
|
+
import { getStringPosition } from "@cocreate/selection";
|
4
4
|
|
5
|
-
export function insertAdjacentElement({
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
13
|
-
|
22
|
+
elementValue = domTextEditor.htmlString.substring(
|
23
|
+
remove.start,
|
24
|
+
remove.end
|
25
|
+
);
|
26
|
+
}
|
14
27
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
54
|
+
updateDomText({ domTextEditor, target });
|
28
55
|
}
|
29
56
|
|
30
57
|
export function setInnerText({ domTextEditor, target, value, start, end }) {
|
31
|
-
|
58
|
+
updateDomText({ domTextEditor, target, value, pos: { start, end } });
|
32
59
|
}
|
33
60
|
|
34
61
|
export function setClass({ domTextEditor, target, value }) {
|
35
|
-
|
62
|
+
updateDomText({ domTextEditor, target, attribute: "class", value });
|
36
63
|
}
|
37
64
|
export function removeClass({ domTextEditor, target, value }) {
|
38
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
95
|
+
updateDomText({ domTextEditor, target, attribute: name, value });
|
51
96
|
}
|
52
97
|
|
53
98
|
export function removeAttribute({ domTextEditor, target, name }) {
|
54
|
-
|
99
|
+
updateDomText({ domTextEditor, target, attribute: name, remove: "true" });
|
55
100
|
}
|
56
101
|
|
57
102
|
export function replaceInnerText({ domTextEditor, target, value }) {
|
58
|
-
|
103
|
+
updateDomText({ domTextEditor, target, value });
|
59
104
|
}
|
60
105
|
|
61
|
-
export function updateDomText({
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
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
|
}
|