forked from juiceo/react-native-easy-markdown
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Utils.js
64 lines (58 loc) · 1.97 KB
/
Utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const Utils = {
isTextOnly(nodes) {
try {
if (nodes.length) {
for (let i = 0; i < nodes.length; i++) {
if (nodes[i] &&
typeof nodes[i].hasOwnProperty === 'function' &&
nodes[i].hasOwnProperty('type') &&
typeof nodes[i].type.hasOwnProperty === 'function' &&
(
nodes[i].type.hasOwnProperty('displayName') ||
nodes[i].type.hasOwnProperty('name')
)
) {
if (nodes[i].type.displayName !== 'Text' && nodes[i].type.name !== 'Text') {
return false;
}
} else {
return false;
}
}
}
} catch(e) {
return false;
}
// It's a miracle, I guess we're text only
return true;
},
concatStyles: function concatStyles(extras, newStyle) {
let newExtras;
if (extras) {
newExtras = JSON.parse(JSON.stringify(extras));
if (extras.style) {
newExtras.style.push(newStyle);
} else {
newExtras.style = [newStyle];
}
} else {
newExtras = {
style: [newStyle]
};
}
return newExtras;
},
logDebug: function logDebug(nodeTree, level = 0) {
for (let i = 0; i < nodeTree.length; i++) {
const node = nodeTree[i];
if (node) {
let prefix = Array(level).join('-');
console.log(prefix + '> ' + node.key + ', NODE TYPE: ' + node.type.displayName);
if (Array.isArray(node.props.children)) {
this.logDebug(node.props.children, level + 1);
}
}
}
}
}
module.exports = Utils;