@cocreate/utils 1.8.0 → 1.9.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 +26 -0
- package/package.json +1 -1
- package/src/encodedecode.js +111 -0
- package/src/index.js +38 -3
- package/src/index.old.js +18 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
+
## [1.9.1](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.9.0...v1.9.1) (2022-07-29)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* removed console.log ([b237022](https://github.com/CoCreate-app/CoCreate-utils/commit/b2370221c33dc1972eee8204af8a310f9e44721c))
|
|
7
|
+
|
|
8
|
+
# [1.9.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.8.1...v1.9.0) (2022-07-03)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* add queryDocumentSelector to export default ([b067ed1](https://github.com/CoCreate-app/CoCreate-utils/commit/b067ed1354488cf52014e2d417d75c5be455fc6f))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Features
|
|
17
|
+
|
|
18
|
+
* queryDocumentSelector to return one element from an iframe or iframes parent ([477f179](https://github.com/CoCreate-app/CoCreate-utils/commit/477f179ef4e00aab219d12a5a9ae3a5e1cfae992))
|
|
19
|
+
|
|
20
|
+
## [1.8.1](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.8.0...v1.8.1) (2022-07-01)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### Bug Fixes
|
|
24
|
+
|
|
25
|
+
* remove function queryFrameSelector rhas been replaced by queryDocumentSelectorAll ([3c11054](https://github.com/CoCreate-app/CoCreate-utils/commit/3c110543598ded5753c9d715f3cb650d46e93263))
|
|
26
|
+
|
|
1
27
|
# [1.8.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.7.6...v1.8.0) (2022-07-01)
|
|
2
28
|
|
|
3
29
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// function __mergeObject(target, source)
|
|
2
|
+
// {
|
|
3
|
+
// target = target || {};
|
|
4
|
+
// for (let key of Object.keys(source)) {
|
|
5
|
+
// if (source[key] instanceof Object) {
|
|
6
|
+
// Object.assign(source[key], __mergeObject(target[key], source[key]))
|
|
7
|
+
// }
|
|
8
|
+
// }
|
|
9
|
+
|
|
10
|
+
// Object.assign(target || {}, source)
|
|
11
|
+
// return target
|
|
12
|
+
// }
|
|
13
|
+
|
|
14
|
+
// function __createObject(data, path)
|
|
15
|
+
// {
|
|
16
|
+
// if (!path) return data;
|
|
17
|
+
|
|
18
|
+
// let keys = path.split('.')
|
|
19
|
+
// let newObject = data;
|
|
20
|
+
|
|
21
|
+
// for (var i = keys.length - 1; i >= 0; i--) {
|
|
22
|
+
// newObject = {[keys[i]]: newObject}
|
|
23
|
+
// }
|
|
24
|
+
// return newObject;
|
|
25
|
+
// }
|
|
26
|
+
|
|
27
|
+
// function __createArray(key, data)
|
|
28
|
+
// {
|
|
29
|
+
// try {
|
|
30
|
+
// let item = /([\w\W]+)\[(\d+)\]/gm.exec(key)
|
|
31
|
+
// if (item && item.length == 3) {
|
|
32
|
+
// let arrayKey = item[1];
|
|
33
|
+
// let index = parseInt(item[2]);
|
|
34
|
+
|
|
35
|
+
// if (!data[arrayKey] || !Array.isArray(data[arrayKey])) {
|
|
36
|
+
// data[arrayKey] = [];
|
|
37
|
+
// }
|
|
38
|
+
// data[arrayKey][index] = data[key];
|
|
39
|
+
// delete data[key];
|
|
40
|
+
// key = arrayKey;
|
|
41
|
+
// }
|
|
42
|
+
// } catch {
|
|
43
|
+
// console.log('create array error');
|
|
44
|
+
// }
|
|
45
|
+
// return key;
|
|
46
|
+
// }
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
// function isObject(item) {
|
|
51
|
+
// return (!!item) && (item.constructor === Object);
|
|
52
|
+
// }
|
|
53
|
+
// function isArray(item) {
|
|
54
|
+
// return (!!item) && (item.constructor === Array);
|
|
55
|
+
// }
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
// function decodeObject(data) {
|
|
59
|
+
// let keys = Object.keys(data)
|
|
60
|
+
// let objectData = {};
|
|
61
|
+
|
|
62
|
+
// keys.forEach((k) => {
|
|
63
|
+
// k = __createArray(k, data);
|
|
64
|
+
// if (k.split('.').length > 1) {
|
|
65
|
+
// let newData = __createObject(data[k], k);
|
|
66
|
+
// delete data[k];
|
|
67
|
+
|
|
68
|
+
// objectData = __mergeObject(objectData, newData);
|
|
69
|
+
// } else {
|
|
70
|
+
// objectData[k] = data[k];
|
|
71
|
+
// }
|
|
72
|
+
// })
|
|
73
|
+
// return objectData;
|
|
74
|
+
// }
|
|
75
|
+
|
|
76
|
+
// function encodeObject(data) {
|
|
77
|
+
// let keys = Object.keys(data);
|
|
78
|
+
// let newData = {};
|
|
79
|
+
// keys.forEach((k) => {
|
|
80
|
+
// let data_value = data[k];
|
|
81
|
+
// if (isObject(data[k])) {
|
|
82
|
+
// let new_obj = encodeObject(data[k]);
|
|
83
|
+
// let newKeys = Object.keys(new_obj);
|
|
84
|
+
// newKeys.forEach((newKey) => {
|
|
85
|
+
// let value = new_obj[newKey];
|
|
86
|
+
// if (isNaN(parseInt(newKey))) {
|
|
87
|
+
// newKey = `${k}.${newKey}`;
|
|
88
|
+
// } else {
|
|
89
|
+
// newKey = `${k}[${newKey}]`;
|
|
90
|
+
// }
|
|
91
|
+
|
|
92
|
+
// newData[newKey] = value;
|
|
93
|
+
// })
|
|
94
|
+
|
|
95
|
+
// } else if (isArray(data_value)){
|
|
96
|
+
// data_value.forEach((v, index) => {
|
|
97
|
+
// newData[`${k}[${index}]`] = v;
|
|
98
|
+
// })
|
|
99
|
+
// } else {
|
|
100
|
+
// newData[k] = data[k];
|
|
101
|
+
// }
|
|
102
|
+
// })
|
|
103
|
+
// return newData;
|
|
104
|
+
// }
|
|
105
|
+
|
|
106
|
+
module.exports = {
|
|
107
|
+
// decodeObject,
|
|
108
|
+
// encodeObject,
|
|
109
|
+
replaceArray
|
|
110
|
+
};
|
|
111
|
+
|
package/src/index.js
CHANGED
|
@@ -99,7 +99,7 @@ export function domParser(str) {
|
|
|
99
99
|
try {
|
|
100
100
|
var mainTag = str.match(/\<(?<tag>[a-z0-9]+)(.*?)?\>/).groups.tag;
|
|
101
101
|
} catch (e){
|
|
102
|
-
console.log('find position: can not find the main tag');
|
|
102
|
+
// console.log(e, 'find position: can not find the main tag');
|
|
103
103
|
}
|
|
104
104
|
let doc;
|
|
105
105
|
switch (mainTag) {
|
|
@@ -159,6 +159,41 @@ export function queryDocumentSelectorAll(selector) {
|
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
+
export function queryDocumentSelector(selector) {
|
|
163
|
+
if (selector) {
|
|
164
|
+
let selectors = [selector];
|
|
165
|
+
if(selector.indexOf(',') !== -1){
|
|
166
|
+
selectors = selector.split(',');
|
|
167
|
+
}
|
|
168
|
+
for (let selector of selectors){
|
|
169
|
+
let el;
|
|
170
|
+
if(selector.indexOf(';') !== -1) {
|
|
171
|
+
let targetDocument;
|
|
172
|
+
let [documentSelector, targetSelector] = selector.split(';');
|
|
173
|
+
if (['parent', 'parentDocument'].includes(documentSelector))
|
|
174
|
+
targetDocument = window.parent.document;
|
|
175
|
+
else {
|
|
176
|
+
let frame = document.querySelector(documentSelector);
|
|
177
|
+
if (frame)
|
|
178
|
+
targetDocument = frame.contentDocument;
|
|
179
|
+
}
|
|
180
|
+
if (targetDocument){
|
|
181
|
+
if (targetSelector)
|
|
182
|
+
el = targetDocument.querySelector(targetSelector);
|
|
183
|
+
else
|
|
184
|
+
if (targetDocument.clickedElement)
|
|
185
|
+
el = [targetDocument.clickedElement];
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
else
|
|
189
|
+
el = document.querySelector(selector);
|
|
190
|
+
if (el)
|
|
191
|
+
return el
|
|
192
|
+
}
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
162
197
|
// export function computeStyles(el, properties) {
|
|
163
198
|
// let computed = window.getComputedStyle(el);
|
|
164
199
|
// let result = {};
|
|
@@ -186,6 +221,6 @@ export default {
|
|
|
186
221
|
parseTextToHtml,
|
|
187
222
|
cssPath,
|
|
188
223
|
domParser,
|
|
189
|
-
|
|
190
|
-
|
|
224
|
+
queryDocumentSelector,
|
|
225
|
+
queryDocumentSelectorAll
|
|
191
226
|
};
|
package/src/index.old.js
CHANGED
|
@@ -425,7 +425,24 @@ async function complexSelector(comSelector, callback) {
|
|
|
425
425
|
}
|
|
426
426
|
}
|
|
427
427
|
|
|
428
|
-
|
|
428
|
+
function decodeArray(data) {
|
|
429
|
+
let keys = Object.keys(data);
|
|
430
|
+
let objectData = {};
|
|
431
|
+
|
|
432
|
+
keys.forEach((k) => {
|
|
433
|
+
let nk = k
|
|
434
|
+
if (/\[([0-9]*)\]/g.test(k)) {
|
|
435
|
+
nk = nk.replace(/\[/g, '.');
|
|
436
|
+
if (nk.endsWith(']'))
|
|
437
|
+
nk = nk.slice(0, -1)
|
|
438
|
+
nk = nk.replace(/\]./g, '.');
|
|
439
|
+
nk = nk.replace(/\]/g, '.');
|
|
440
|
+
}
|
|
441
|
+
objectData[nk] = data[k];
|
|
442
|
+
});
|
|
443
|
+
return objectData;
|
|
444
|
+
}
|
|
445
|
+
|
|
429
446
|
|
|
430
447
|
export default {
|
|
431
448
|
getElementPath,
|