@cocreate/text 1.27.1 → 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 +24 -0
- package/package.json +1 -1
- package/prettier.config.js +16 -0
- package/src/index.js +5 -3
- package/src/updateDom.js +10 -22
- package/src/updateText.js +121 -45
package/CHANGELOG.md
CHANGED
@@ -1,3 +1,27 @@
|
|
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
|
+
|
8
|
+
# [1.28.0](https://github.com/CoCreate-app/CoCreate-text/compare/v1.27.1...v1.28.0) (2024-11-04)
|
9
|
+
|
10
|
+
|
11
|
+
### Bug Fixes
|
12
|
+
|
13
|
+
* formating ([df994e2](https://github.com/CoCreate-app/CoCreate-text/commit/df994e2495420d626f1429a8a00f0cd8287d1cbd))
|
14
|
+
* if condition remove ([ac7bf6a](https://github.com/CoCreate-app/CoCreate-text/commit/ac7bf6a3cca3c2e876900f5d86d372880295dc73))
|
15
|
+
* if no selection return ([a271cb3](https://github.com/CoCreate-app/CoCreate-text/commit/a271cb3c73e74d0b104939a2d6a662dd11da59f8))
|
16
|
+
* observer taget has been renamed to selector ([da1e9e9](https://github.com/CoCreate-app/CoCreate-text/commit/da1e9e9eeaddcbafcfb589278ff8fadd10e4dd52))
|
17
|
+
* pretier.config.js and file formating ([0ae4aed](https://github.com/CoCreate-app/CoCreate-text/commit/0ae4aed54f9358cd39b4de513684d9949f7e8844))
|
18
|
+
* send position on mousedown and arrow keys ([1e63d00](https://github.com/CoCreate-app/CoCreate-text/commit/1e63d00fedef6de42594c592a6171b5c061ebd06))
|
19
|
+
|
20
|
+
|
21
|
+
### Features
|
22
|
+
|
23
|
+
* add prettier.config.js and format files ([9a8b71d](https://github.com/CoCreate-app/CoCreate-text/commit/9a8b71d52a4fa98a95f723238fbe7cd32e254adc))
|
24
|
+
|
1
25
|
## [1.27.1](https://github.com/CoCreate-app/CoCreate-text/compare/v1.27.0...v1.27.1) (2024-06-19)
|
2
26
|
|
3
27
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
@@ -173,7 +173,7 @@ function _mousedown(event) {
|
|
173
173
|
if (array && object && key && !target.hasAttribute('contenteditable'))
|
174
174
|
target.setAttribute('contenteditable', 'true');
|
175
175
|
}
|
176
|
-
|
176
|
+
sendPosition(domTextEditor)
|
177
177
|
}
|
178
178
|
|
179
179
|
function _blur(event) {
|
@@ -245,6 +245,8 @@ function _keydown(event) {
|
|
245
245
|
} else if (event.keyCode == 89) {
|
246
246
|
updateText({ element, range, undoRedo: 'redo' });
|
247
247
|
}
|
248
|
+
} else {
|
249
|
+
sendPosition(element)
|
248
250
|
}
|
249
251
|
}
|
250
252
|
|
@@ -430,7 +432,7 @@ export function _dispatchInputEvent(element, content, start, end, prev_start, pr
|
|
430
432
|
observer.init({
|
431
433
|
name: 'CoCreateTextAddedNodes',
|
432
434
|
observe: ['addedNodes'],
|
433
|
-
|
435
|
+
selector: selectors,
|
434
436
|
callback(mutation) {
|
435
437
|
let isCrdt = mutation.target.getAttribute('crdt');
|
436
438
|
if (isCrdt) return;
|
@@ -442,7 +444,7 @@ observer.init({
|
|
442
444
|
name: 'CoCreateTextAttribtes',
|
443
445
|
observe: ['attributes'],
|
444
446
|
attributeName: [...getAttributeNames(['array', 'object', 'key']), 'contenteditable'],
|
445
|
-
|
447
|
+
selector: selectors,
|
446
448
|
callback(mutation) {
|
447
449
|
let _id = mutation.target.getAttribute('object')
|
448
450
|
if (!_id) {
|
package/src/updateDom.js
CHANGED
@@ -40,13 +40,11 @@ export function updateDom({ domTextEditor, value, start, end, html }) {
|
|
40
40
|
newEl.innerHTML = domTextEditor.htmlString;
|
41
41
|
domEl = domTextEditor;
|
42
42
|
type = 'innerHTML';
|
43
|
-
}
|
44
|
-
else if (element.tagName == 'HTML') {
|
43
|
+
} else if (element.tagName == 'HTML') {
|
45
44
|
// console.log('element = html')
|
46
45
|
domEl = domTextEditor;
|
47
46
|
type = 'innerHTML';
|
48
|
-
}
|
49
|
-
else if (path) {
|
47
|
+
} else if (path) {
|
50
48
|
// console.log("else path", path)
|
51
49
|
domEl = domTextEditor.querySelector(path);
|
52
50
|
// if (!domEl || !oldEl){
|
@@ -103,44 +101,34 @@ export function updateDom({ domTextEditor, value, start, end, html }) {
|
|
103
101
|
// console.log('newEl', newEl)
|
104
102
|
if (start != end && type == 'innerHTML') {
|
105
103
|
domTextEditor.htmlString = html;
|
106
|
-
if (domEl.tagName != 'HTML') {
|
107
|
-
|
108
|
-
|
109
|
-
// console.log('parent', domEl.parentElement)
|
110
|
-
} else {
|
111
|
-
domEl.replaceChildren(...newEl.childNodes);
|
112
|
-
// console.log('domEl', domEl)
|
113
|
-
}
|
114
|
-
}
|
115
|
-
else {
|
104
|
+
if (domEl.tagName != 'HTML' && newEl.parentElement) {
|
105
|
+
domEl.parentElement.replaceChildren(...newEl.parentElement.childNodes);
|
106
|
+
} else {
|
116
107
|
domEl.replaceChildren(...newEl.childNodes);
|
117
108
|
// console.log('Html tag', domEl)
|
118
109
|
}
|
110
|
+
|
119
111
|
if (curCaret && curCaret.range) {
|
120
112
|
curCaret.range.startContainer = domEl;
|
121
113
|
curCaret.range.endContainer = domEl;
|
122
114
|
}
|
123
|
-
}
|
124
|
-
else if (type == 'isStartTag') {
|
115
|
+
} else if (type == 'isStartTag') {
|
125
116
|
oldEl = domTextEditor.oldHtml.querySelector(path);
|
126
117
|
if (!oldEl && domEl.tagName == 'HTML')
|
127
118
|
oldEl = domTextEditor.oldHtml
|
128
119
|
assignAttributes(newEl, oldEl, domEl);
|
129
120
|
// console.log('isStartTag', domEl, newEl)
|
130
121
|
|
131
|
-
}
|
132
|
-
else if (type == 'insertAdjacent') {
|
122
|
+
} else if (type == 'insertAdjacent') {
|
133
123
|
domEl.insertAdjacentHTML(position, value);
|
134
124
|
// console.log('insertAdjacent', domEl, value)
|
135
|
-
}
|
136
|
-
else if (type == 'textNode') {
|
125
|
+
} else if (type == 'textNode') {
|
137
126
|
if (start != end)
|
138
127
|
domTextEditor.htmlString = html;
|
139
128
|
domEl.innerHTML = newEl.innerHTML;
|
140
129
|
// console.log('textnode', domEl.innerHTML, newEl.innerHTML)
|
141
130
|
|
142
|
-
}
|
143
|
-
else if (type == 'innerHTML') {
|
131
|
+
} else if (type == 'innerHTML') {
|
144
132
|
domEl.replaceChildren(...newEl.childNodes);
|
145
133
|
// console.log('innerHtml', domEl, newEl)
|
146
134
|
}
|
package/src/updateText.js
CHANGED
@@ -1,79 +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
|
-
|
11
|
-
|
12
|
-
|
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");
|
13
21
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
elementValue = domTextEditor.htmlString.substring(
|
23
|
+
remove.start,
|
24
|
+
remove.end
|
25
|
+
);
|
26
|
+
}
|
27
|
+
|
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
|
+
}
|
23
51
|
}
|
24
52
|
|
25
53
|
export function removeElement({ domTextEditor, target }) {
|
26
|
-
|
54
|
+
updateDomText({ domTextEditor, target });
|
27
55
|
}
|
28
56
|
|
29
57
|
export function setInnerText({ domTextEditor, target, value, start, end }) {
|
30
|
-
|
58
|
+
updateDomText({ domTextEditor, target, value, pos: { start, end } });
|
31
59
|
}
|
32
60
|
|
33
61
|
export function setClass({ domTextEditor, target, value }) {
|
34
|
-
|
62
|
+
updateDomText({ domTextEditor, target, attribute: "class", value });
|
35
63
|
}
|
36
64
|
export function removeClass({ domTextEditor, target, value }) {
|
37
|
-
|
65
|
+
updateDomText({
|
66
|
+
domTextEditor,
|
67
|
+
target,
|
68
|
+
attribute: "class",
|
69
|
+
value,
|
70
|
+
remove: true
|
71
|
+
});
|
38
72
|
}
|
39
73
|
|
40
74
|
export function setStyle({ domTextEditor, target, property, value }) {
|
41
|
-
|
75
|
+
updateDomText({
|
76
|
+
domTextEditor,
|
77
|
+
target,
|
78
|
+
attribute: "style",
|
79
|
+
property,
|
80
|
+
value
|
81
|
+
});
|
42
82
|
}
|
43
83
|
|
44
84
|
export function removeStyle({ domTextEditor, target, property }) {
|
45
|
-
|
85
|
+
updateDomText({
|
86
|
+
domTextEditor,
|
87
|
+
target,
|
88
|
+
attribute: "style",
|
89
|
+
property,
|
90
|
+
remove: true
|
91
|
+
});
|
46
92
|
}
|
47
93
|
|
48
94
|
export function setAttribute({ domTextEditor, target, name, value }) {
|
49
|
-
|
95
|
+
updateDomText({ domTextEditor, target, attribute: name, value });
|
50
96
|
}
|
51
97
|
|
52
98
|
export function removeAttribute({ domTextEditor, target, name }) {
|
53
|
-
|
99
|
+
updateDomText({ domTextEditor, target, attribute: name, remove: "true" });
|
54
100
|
}
|
55
101
|
|
56
102
|
export function replaceInnerText({ domTextEditor, target, value }) {
|
57
|
-
|
103
|
+
updateDomText({ domTextEditor, target, value });
|
58
104
|
}
|
59
105
|
|
60
|
-
export function updateDomText({
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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 });
|
72
140
|
}
|
73
141
|
|
74
142
|
function _updateText({ domTextEditor, value, start, end }) {
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
+
});
|
79
155
|
}
|