@cocreate/text 1.12.32 → 1.13.3
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 +33 -0
- package/package.json +1 -1
- package/src/index.js +43 -30
package/CHANGELOG.md
CHANGED
@@ -1,3 +1,36 @@
|
|
1
|
+
## [1.13.3](https://github.com/CoCreate-app/CoCreate-text/compare/v1.13.2...v1.13.3) (2021-11-29)
|
2
|
+
|
3
|
+
|
4
|
+
### Bug Fixes
|
5
|
+
|
6
|
+
* sendPosition if no element return ([fec4fbb](https://github.com/CoCreate-app/CoCreate-text/commit/fec4fbb20b574af0995f1f49ad16501a83c52b98))
|
7
|
+
|
8
|
+
## [1.13.2](https://github.com/CoCreate-app/CoCreate-text/compare/v1.13.1...v1.13.2) (2021-11-28)
|
9
|
+
|
10
|
+
|
11
|
+
### Bug Fixes
|
12
|
+
|
13
|
+
* removed console.log ([81a07d4](https://github.com/CoCreate-app/CoCreate-text/commit/81a07d4be95acd507fa4047de38847069fd53d5c))
|
14
|
+
|
15
|
+
## [1.13.1](https://github.com/CoCreate-app/CoCreate-text/compare/v1.13.0...v1.13.1) (2021-11-28)
|
16
|
+
|
17
|
+
|
18
|
+
### Bug Fixes
|
19
|
+
|
20
|
+
* replace click and keyup event with selectionchange event ([515da39](https://github.com/CoCreate-app/CoCreate-text/commit/515da39a62af4e350aa2a7857aa48b87c822a5dd))
|
21
|
+
|
22
|
+
# [1.13.0](https://github.com/CoCreate-app/CoCreate-text/compare/v1.12.32...v1.13.0) (2021-11-28)
|
23
|
+
|
24
|
+
|
25
|
+
### Bug Fixes
|
26
|
+
|
27
|
+
* remove keypress and replaced with beforeinput event to support mobile browsers ([84ae8b1](https://github.com/CoCreate-app/CoCreate-text/commit/84ae8b162657ec028e6a49b627ca104909ff11ad))
|
28
|
+
|
29
|
+
|
30
|
+
### Features
|
31
|
+
|
32
|
+
* support for undo and redo ([8a727d3](https://github.com/CoCreate-app/CoCreate-text/commit/8a727d3ba5df8bcf03cf96c07c40e889dd359aea))
|
33
|
+
|
1
34
|
## [1.12.32](https://github.com/CoCreate-app/CoCreate-text/compare/v1.12.31...v1.12.32) (2021-11-27)
|
2
35
|
|
3
36
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
@@ -16,6 +16,10 @@ function init() {
|
|
16
16
|
let elements = document.querySelectorAll(selectors);
|
17
17
|
initElements(elements);
|
18
18
|
_crdtUpdateListener();
|
19
|
+
document.addEventListener('selectionchange', (e) => {
|
20
|
+
let element = document.activeElement;
|
21
|
+
sendPosition(element)
|
22
|
+
});
|
19
23
|
}
|
20
24
|
|
21
25
|
function initElements (elements) {
|
@@ -32,10 +36,16 @@ function initElement (element) {
|
|
32
36
|
if(!collection || !document_id || !name) return;
|
33
37
|
|
34
38
|
if (!isCrdt) {
|
35
|
-
if (element.tagName == 'IFRAME')
|
39
|
+
if (element.tagName == 'IFRAME'){
|
36
40
|
_addEventListeners(element.contentDocument.documentElement);
|
37
|
-
|
38
|
-
|
41
|
+
let Document = element.contentDocument
|
42
|
+
Document.addEventListener('selectionchange', (e) => {
|
43
|
+
let element = Document.activeElement;
|
44
|
+
sendPosition(element)
|
45
|
+
}); }
|
46
|
+
else{
|
47
|
+
_addEventListeners(element);
|
48
|
+
}
|
39
49
|
}
|
40
50
|
element.setAttribute('crdt', 'true');
|
41
51
|
element.crdt = {init: true};
|
@@ -65,18 +75,12 @@ function initElement (element) {
|
|
65
75
|
}
|
66
76
|
|
67
77
|
export function _addEventListeners (element) {
|
68
|
-
element.addEventListener('click', _click);
|
69
78
|
element.addEventListener('blur', _blur);
|
70
|
-
element.addEventListener('keyup', _keyup);
|
71
79
|
element.addEventListener('cut', _cut);
|
72
80
|
element.addEventListener('paste', _paste);
|
73
81
|
element.addEventListener('keydown', _keydown);
|
74
|
-
element.addEventListener('
|
75
|
-
|
76
|
-
|
77
|
-
function _click (event) {
|
78
|
-
let element = event.currentTarget;
|
79
|
-
sendPosition(element);
|
82
|
+
element.addEventListener('beforeinput', _beforeinput);
|
83
|
+
element.addEventListener('input', _input);
|
80
84
|
}
|
81
85
|
|
82
86
|
function _blur (event) {
|
@@ -87,11 +91,6 @@ function _blur (event) {
|
|
87
91
|
cursors.sendPosition({collection, document_id, name, start, end});
|
88
92
|
}
|
89
93
|
|
90
|
-
function _keyup (event) {
|
91
|
-
let element = event.currentTarget;
|
92
|
-
sendPosition(element);
|
93
|
-
}
|
94
|
-
|
95
94
|
function _cut (event) {
|
96
95
|
let element = event.currentTarget;
|
97
96
|
const { start, end, range } = getSelection(element);
|
@@ -130,53 +129,66 @@ function _keydown (event) {
|
|
130
129
|
const { start, end, range } = getSelection(element);
|
131
130
|
if(event.key == "Backspace" || event.key == "Tab" || event.key == "Enter") {
|
132
131
|
eventObj = event;
|
133
|
-
//ToDO switch case
|
134
132
|
if(start != end) {
|
135
133
|
updateText({element, start, end, range});
|
136
134
|
}
|
135
|
+
|
137
136
|
if(event.key == "Backspace" && start == end) {
|
138
137
|
updateText({element, start: start - 1, end, range});
|
139
138
|
}
|
140
|
-
if(event.key == 'Tab') {
|
139
|
+
else if(event.key == 'Tab') {
|
141
140
|
updateText({element, value: "\t", start, range});
|
142
141
|
}
|
143
|
-
if(event.key == "Enter") {
|
142
|
+
else if(event.key == "Enter") {
|
144
143
|
updateText({element, value: "\n", start, range});
|
145
144
|
}
|
146
145
|
event.preventDefault();
|
147
146
|
}
|
147
|
+
else if (event.ctrlKey) {
|
148
|
+
if (event.keyCode == 90)
|
149
|
+
console.log('Undo');
|
150
|
+
else if (event.keyCode == 89)
|
151
|
+
console.log('Redo');
|
152
|
+
}
|
148
153
|
}
|
149
154
|
|
150
|
-
function
|
155
|
+
function _beforeinput (event) {
|
151
156
|
if(event.stopCCText) return;
|
152
157
|
let element = event.currentTarget;
|
153
158
|
let { start, end, range } = getSelection(element);
|
154
|
-
if(
|
155
|
-
|
159
|
+
if (event.data) {
|
160
|
+
if(start != end) {
|
161
|
+
updateText({element, start, end, range});
|
162
|
+
}
|
163
|
+
eventObj = event;
|
164
|
+
updateText({element, value: event.data, start, range});
|
165
|
+
event.preventDefault();
|
166
|
+
}
|
167
|
+
}
|
168
|
+
|
169
|
+
function _input (event) {
|
170
|
+
if(event.stopCCText) return;
|
171
|
+
if (event.data) {
|
172
|
+
eventObj = event;
|
156
173
|
}
|
157
|
-
eventObj = event;
|
158
|
-
updateText({element, value: event.key, start, range});
|
159
|
-
event.preventDefault();
|
160
174
|
}
|
161
175
|
|
162
176
|
function _removeEventListeners (element) {
|
163
|
-
element.removeEventListener('click', _click);
|
164
177
|
element.removeEventListener('blur', _blur);
|
165
|
-
element.removeEventListener('keyup', _keyup);
|
166
178
|
element.removeEventListener('cut', _cut);
|
167
179
|
element.removeEventListener('paste', _paste);
|
168
180
|
element.removeEventListener('keydown', _keydown);
|
169
|
-
element.removeEventListener('
|
181
|
+
element.removeEventListener('beforeinput', _beforeinput);
|
170
182
|
}
|
171
183
|
|
172
|
-
|
173
184
|
export function sendPosition (element) {
|
174
185
|
if (!element) return;
|
175
186
|
const { start, end } = getSelection(element);
|
176
187
|
if (element.tagName == 'HTML' && !element.hasAttribute('collection') || !element.hasAttribute('collection'))
|
177
188
|
element = element.ownerDocument.defaultView.frameElement;
|
189
|
+
if (!element) return;
|
178
190
|
const { collection, document_id, name, isCrdt } = crud.getAttr(element);
|
179
|
-
if (isCrdt == 'false') return;
|
191
|
+
if (isCrdt == 'false' || !collection || !document_id || !name) return;
|
180
192
|
cursors.sendPosition({ collection, document_id, name, start, end });
|
181
193
|
}
|
182
194
|
|
@@ -278,6 +290,7 @@ export function _dispatchInputEvent(element, content, start, end, prev_start, pr
|
|
278
290
|
element.dispatchEvent(event);
|
279
291
|
}
|
280
292
|
let inputEvent = new CustomEvent('input', { bubbles: true });
|
293
|
+
Object.defineProperty(inputEvent, 'stopCCText', { writable: false, value: true });
|
281
294
|
Object.defineProperty(inputEvent, 'target', { writable: false, value: element });
|
282
295
|
Object.defineProperty(inputEvent, 'detail', { writable: false, value: detail });
|
283
296
|
element.dispatchEvent(inputEvent);
|