@cocreate/text 1.17.15 → 1.18.0
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 +23 -0
- package/package.json +10 -10
- package/src/index.js +59 -20
- package/src/saveDomText.js +10 -4
- package/src/updateText.js +7 -2
package/CHANGELOG.md
CHANGED
@@ -1,3 +1,26 @@
|
|
1
|
+
# [1.18.0](https://github.com/CoCreate-app/CoCreate-text/compare/v1.17.16...v1.18.0) (2022-11-21)
|
2
|
+
|
3
|
+
|
4
|
+
### Bug Fixes
|
5
|
+
|
6
|
+
* crud.getAttr renamed to crud.getAttributes ([3f2195c](https://github.com/CoCreate-app/CoCreate-text/commit/3f2195c37d31183cf3233373ab4cbdb020ddd957))
|
7
|
+
* renamed crud.checkAttrValue to crud.checkValue ([fd58a70](https://github.com/CoCreate-app/CoCreate-text/commit/fd58a70db438677256b04aff803f435b38cf2a37))
|
8
|
+
* renamed data.data to data.document ([708ea76](https://github.com/CoCreate-app/CoCreate-text/commit/708ea76fff5c26f8cb891e8922f45515385d3ee4))
|
9
|
+
* update crud functions to receive an array of objects as the response ([f374922](https://github.com/CoCreate-app/CoCreate-text/commit/f37492263db651eb1ab86f8e6af4374141abd9cd))
|
10
|
+
|
11
|
+
|
12
|
+
### Features
|
13
|
+
|
14
|
+
* added crud.getAttributeNames to observer attributeName param, this will allow observing custom attributes ([fe08a4d](https://github.com/CoCreate-app/CoCreate-text/commit/fe08a4d64424105ab2211faa5f46e21ca03d6e98))
|
15
|
+
* newDocument param to define a new value or overwrite an existing value ([0ba44dc](https://github.com/CoCreate-app/CoCreate-text/commit/0ba44dcb128c07b118c1d542ee8528553a15d52e))
|
16
|
+
|
17
|
+
## [1.17.16](https://github.com/CoCreate-app/CoCreate-text/compare/v1.17.15...v1.17.16) (2022-10-02)
|
18
|
+
|
19
|
+
|
20
|
+
### Bug Fixes
|
21
|
+
|
22
|
+
* minor bug fixes ([20415ff](https://github.com/CoCreate-app/CoCreate-text/commit/20415ff1fddde49c9a9947ac73ce859be454c9a1))
|
23
|
+
|
1
24
|
## [1.17.15](https://github.com/CoCreate-app/CoCreate-text/compare/v1.17.14...v1.17.15) (2022-10-02)
|
2
25
|
|
3
26
|
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@cocreate/text",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.18.0",
|
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",
|
@@ -61,15 +61,15 @@
|
|
61
61
|
"webpack-log": "^3.0.1"
|
62
62
|
},
|
63
63
|
"dependencies": {
|
64
|
-
"@cocreate/actions": "^1.5.
|
65
|
-
"@cocreate/crdt": "^1.13.
|
66
|
-
"@cocreate/crud-client": "^1.12.
|
67
|
-
"@cocreate/cursors": "^1.11.
|
68
|
-
"@cocreate/docs": "^1.3.
|
64
|
+
"@cocreate/actions": "^1.5.11",
|
65
|
+
"@cocreate/crdt": "^1.13.11",
|
66
|
+
"@cocreate/crud-client": "^1.12.11",
|
67
|
+
"@cocreate/cursors": "^1.11.33",
|
68
|
+
"@cocreate/docs": "^1.3.17",
|
69
69
|
"@cocreate/hosting": "^1.4.0",
|
70
|
-
"@cocreate/observer": "^1.5.
|
71
|
-
"@cocreate/selection": "^1.4.
|
72
|
-
"@cocreate/utils": "^1.10.
|
73
|
-
"@cocreate/uuid": "^1.2.
|
70
|
+
"@cocreate/observer": "^1.5.16",
|
71
|
+
"@cocreate/selection": "^1.4.23",
|
72
|
+
"@cocreate/utils": "^1.10.11",
|
73
|
+
"@cocreate/uuid": "^1.2.15"
|
74
74
|
}
|
75
75
|
}
|
package/src/index.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
/*global CustomEvent, navigator*/
|
2
2
|
import observer from '@cocreate/observer';
|
3
|
-
import
|
3
|
+
import CRUD from '@cocreate/crud-client';
|
4
4
|
import crdt from '@cocreate/crdt';
|
5
5
|
import cursors from '@cocreate/cursors';
|
6
6
|
import uuid from '@cocreate/uuid';
|
@@ -10,6 +10,12 @@ import {getSelection, processSelection} from '@cocreate/selection';
|
|
10
10
|
import action from '@cocreate/actions';
|
11
11
|
import './saveDomText';
|
12
12
|
|
13
|
+
let crud
|
14
|
+
if(CRUD && CRUD.default)
|
15
|
+
crud = CRUD.default
|
16
|
+
else
|
17
|
+
crud = CRUD
|
18
|
+
|
13
19
|
let eventObj;
|
14
20
|
let selector = `[collection][document_id][name]`;
|
15
21
|
let selectors = `input${selector}, textarea${selector}, [contenteditable]${selector}:not([contenteditable='false'])`;
|
@@ -27,19 +33,23 @@ function initElements (elements) {
|
|
27
33
|
}
|
28
34
|
|
29
35
|
function initElement (element) {
|
30
|
-
|
31
|
-
if (document_id
|
36
|
+
let { collection, document_id, name, isRealtime, isCrdt, isCrud, isSave, isRead } = crud.getAttributes(element);
|
37
|
+
if (!collection || !document_id || !name)
|
38
|
+
return
|
39
|
+
if (document_id == 'pending') {
|
40
|
+
element.pendingDocument = true
|
41
|
+
return
|
42
|
+
}
|
32
43
|
if (['_id', 'organization_id'].includes(name))
|
33
44
|
return
|
34
45
|
if (isCrdt == "false" || isRealtime == "false" || element.type == 'number')
|
35
|
-
return
|
36
|
-
if (!crud.
|
37
|
-
return
|
38
|
-
if(name && name.startsWith('$'))
|
46
|
+
return
|
47
|
+
if (!crud.checkValue(collection) || !crud.checkValue(document_id)|| !crud.checkValue(name))
|
48
|
+
return
|
49
|
+
if (name && name.startsWith('$'))
|
39
50
|
return
|
40
51
|
|
41
52
|
if (element.tagName === "INPUT" && ["text", "tel", "url"].includes(element.type) || element.tagName === "TEXTAREA" || element.hasAttribute('contenteditable')) {
|
42
|
-
if (!collection || !document_id || !name) return;
|
43
53
|
|
44
54
|
if (!isCrdt) {
|
45
55
|
if (element.tagName == 'IFRAME'){
|
@@ -54,13 +64,34 @@ function initElement (element) {
|
|
54
64
|
}
|
55
65
|
element.setAttribute('crdt', 'true');
|
56
66
|
element.crdt = {init: true};
|
57
|
-
|
67
|
+
|
68
|
+
// ToDo: newDocument name consideration. its value is used for setting or overwriting existing value
|
69
|
+
let newDocument = ''
|
70
|
+
if (element.pendingDocument) {
|
71
|
+
|
72
|
+
let value;
|
73
|
+
if (element.hasAttribute('contenteditable'))
|
74
|
+
value = element.innerHTML;
|
75
|
+
else
|
76
|
+
value = element.value;
|
77
|
+
if (value)
|
78
|
+
newDocument = value
|
79
|
+
|
80
|
+
delete element.pendingDocument
|
81
|
+
}
|
82
|
+
|
83
|
+
crdt.getText({ collection, document_id, name, crud: isCrud, save: isSave, read: isRead, newDocument }).then(response => {
|
58
84
|
if (response === undefined)
|
59
85
|
return;
|
60
86
|
if (!response){
|
87
|
+
// if (element.pendingDocument) {
|
88
|
+
// isRead = 'true'
|
89
|
+
// delete element.pendingDocument
|
90
|
+
// }
|
91
|
+
|
61
92
|
let value;
|
62
93
|
if (element.hasAttribute('contenteditable')){
|
63
|
-
|
94
|
+
value = element.innerHTML;
|
64
95
|
}
|
65
96
|
else {
|
66
97
|
value = element.value;
|
@@ -70,7 +101,7 @@ function initElement (element) {
|
|
70
101
|
}
|
71
102
|
else {
|
72
103
|
if (element.hasAttribute('contenteditable')){
|
73
|
-
|
104
|
+
element.innerHTML = '';
|
74
105
|
}
|
75
106
|
else {
|
76
107
|
element.value = '';
|
@@ -125,7 +156,7 @@ function _mousedown (event) {
|
|
125
156
|
let contentEditable = target.closest('[collection][document_id][name]');
|
126
157
|
if (contentEditable){
|
127
158
|
target = contentEditable;
|
128
|
-
const { collection, document_id, name } = crud.
|
159
|
+
const { collection, document_id, name } = crud.getAttributes(target);
|
129
160
|
if (collection && document_id && name && !target.hasAttribute('contenteditable'))
|
130
161
|
target.setAttribute('contenteditable', 'true');
|
131
162
|
}
|
@@ -134,7 +165,7 @@ function _mousedown (event) {
|
|
134
165
|
|
135
166
|
function _blur (event) {
|
136
167
|
let element = event.currentTarget;
|
137
|
-
const { collection, document_id, name } = crud.
|
168
|
+
const { collection, document_id, name } = crud.getAttributes(element);
|
138
169
|
let start = null;
|
139
170
|
let end = null;
|
140
171
|
cursors.sendPosition({collection, document_id, name, start, end});
|
@@ -233,11 +264,13 @@ function _input (event) {
|
|
233
264
|
}
|
234
265
|
|
235
266
|
function _removeEventListeners (element) {
|
267
|
+
element.removeEventListener('mousedown', _mousedown);
|
236
268
|
element.removeEventListener('blur', _blur);
|
237
269
|
element.removeEventListener('cut', _cut);
|
238
270
|
element.removeEventListener('paste', _paste);
|
239
271
|
element.removeEventListener('keydown', _keydown);
|
240
272
|
element.removeEventListener('beforeinput', _beforeinput);
|
273
|
+
element.removeEventListener('input', _input);
|
241
274
|
}
|
242
275
|
|
243
276
|
let previousPosition = {};
|
@@ -253,7 +286,7 @@ export function sendPosition (element) {
|
|
253
286
|
}
|
254
287
|
}
|
255
288
|
if (!element) return;
|
256
|
-
const { collection, document_id, name, isCrdt } = crud.
|
289
|
+
const { collection, document_id, name, isCrdt } = crud.getAttributes(element);
|
257
290
|
if (isCrdt == 'false' || !collection || !document_id || !name) return;
|
258
291
|
let currentPosition = { collection, document_id, name, start, end };
|
259
292
|
if (JSON.stringify(currentPosition) === JSON.stringify(previousPosition))
|
@@ -272,8 +305,8 @@ function updateText ({element, value, start, end, range, undoRedo}) {
|
|
272
305
|
if (element.tagName == 'HTML' && !element.hasAttribute('collection'))
|
273
306
|
element = element.ownerDocument.defaultView.frameElement;
|
274
307
|
}
|
275
|
-
const { collection, document_id, name, isCrud, isCrdt, isSave } = crud.
|
276
|
-
if (isCrdt == "false") return;
|
308
|
+
const { collection, document_id, name, isCrud, isCrdt, isSave } = crud.getAttributes(element);
|
309
|
+
if (isCrdt == "false" || !collection || !document_id || !name) return;
|
277
310
|
|
278
311
|
if (undoRedo == 'undo')
|
279
312
|
return crdt.undoText({ collection, document_id, name, isCrud, isCrdt, isSave })
|
@@ -397,10 +430,16 @@ observer.init({
|
|
397
430
|
observer.init({
|
398
431
|
name: 'CoCreateTextAttribtes',
|
399
432
|
observe: ['attributes'],
|
400
|
-
attributeName: ['collection', 'document_id', 'name', 'contenteditable'],
|
433
|
+
attributeName: [...crud.getAttributeNames(['collection', 'document_id', 'name']), 'contenteditable'],
|
401
434
|
target: selectors,
|
402
435
|
callback (mutation) {
|
403
|
-
|
436
|
+
let _id = mutation.target.getAttribute('document_id')
|
437
|
+
if (!_id) {
|
438
|
+
_removeEventListeners(mutation.target)
|
439
|
+
mutation.target.removeAttribute('crdt')
|
440
|
+
} else {
|
441
|
+
initElement(mutation.target);
|
442
|
+
}
|
404
443
|
}
|
405
444
|
});
|
406
445
|
|
@@ -408,7 +447,7 @@ action.init({
|
|
408
447
|
name: "undo",
|
409
448
|
endEvent: "undo",
|
410
449
|
callback: (btn, data) => {
|
411
|
-
const { collection, document_id, name, isCrud, isCrdt, isSave } = crud.
|
450
|
+
const { collection, document_id, name, isCrud, isCrdt, isSave } = crud.getAttributes(btn);
|
412
451
|
crdt.undoText({ collection, document_id, name, isCrud, isCrdt, isSave })
|
413
452
|
}
|
414
453
|
});
|
@@ -417,7 +456,7 @@ action.init({
|
|
417
456
|
name: "redo",
|
418
457
|
endEvent: "redo",
|
419
458
|
callback: (btn, data) => {
|
420
|
-
const { collection, document_id, name, isCrud, isCrdt, isSave } = crud.
|
459
|
+
const { collection, document_id, name, isCrud, isCrdt, isSave } = crud.getAttributes(btn);
|
421
460
|
crdt.redoText({ collection, document_id, name, isCrud, isCrdt, isSave })
|
422
461
|
}
|
423
462
|
});
|
package/src/saveDomText.js
CHANGED
@@ -1,15 +1,21 @@
|
|
1
1
|
/*globals CustomEvent*/
|
2
2
|
import action from '@cocreate/actions';
|
3
|
-
import
|
3
|
+
import CRUD from '@cocreate/crud-client';
|
4
4
|
import crdt from '@cocreate/crdt';
|
5
5
|
|
6
|
+
let crud
|
7
|
+
if(CRUD && CRUD.default)
|
8
|
+
crud = CRUD.default
|
9
|
+
else
|
10
|
+
crud = CRUD
|
11
|
+
|
6
12
|
function save(btn){
|
7
|
-
const { collection, document_id, name, namespace, room, broadcast, broadcastSender, isUpsert} = crud.
|
13
|
+
const { collection, document_id, name, namespace, room, broadcast, broadcastSender, isUpsert} = crud.getAttributes(btn);
|
8
14
|
crdt.getText({collection, document_id, name}).then(response => {
|
9
15
|
crud.updateDocument({
|
10
16
|
collection,
|
11
|
-
|
12
|
-
|
17
|
+
document: {
|
18
|
+
_id: document_id,
|
13
19
|
[name]: response
|
14
20
|
},
|
15
21
|
upsert: isUpsert,
|
package/src/updateText.js
CHANGED
@@ -1,7 +1,12 @@
|
|
1
|
-
import
|
1
|
+
import CRUD from '@cocreate/crud-client';
|
2
2
|
import crdt from '@cocreate/crdt';
|
3
3
|
import {getStringPosition} from '@cocreate/selection';
|
4
4
|
|
5
|
+
let crud
|
6
|
+
if(CRUD && CRUD.default)
|
7
|
+
crud = CRUD.default
|
8
|
+
else
|
9
|
+
crud = CRUD
|
5
10
|
|
6
11
|
export function insertAdjacentElement({ domTextEditor, target, position, element, elementValue }) {
|
7
12
|
let remove;
|
@@ -75,6 +80,6 @@ export function updateDomText({ domTextEditor, target, position, element, elemen
|
|
75
80
|
function _updateText({ domTextEditor, value, start, end}) {
|
76
81
|
if(domTextEditor.tagName =='HTML')
|
77
82
|
domTextEditor = domTextEditor.ownerDocument.defaultView.frameElement;
|
78
|
-
const { collection, document_id, name, isCrud } = crud.
|
83
|
+
const { collection, document_id, name, isCrud } = crud.getAttributes(domTextEditor);
|
79
84
|
crdt.updateText({ collection, document_id, name, value, start, length: end - start, crud: isCrud });
|
80
85
|
}
|