@symbo.ls/atoms 3.1.2 → 3.2.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.
Files changed (2) hide show
  1. package/Collection.js +228 -0
  2. package/package.json +6 -6
package/Collection.js ADDED
@@ -0,0 +1,228 @@
1
+ 'use strict'
2
+
3
+ import { isState, getChildStateInKey } from '@domql/state'
4
+ import {
5
+ isString,
6
+ isNumber,
7
+ isNot,
8
+ isArray,
9
+ isObject,
10
+ isObjectLike,
11
+ exec,
12
+ deepClone,
13
+ addAdditionalExtend
14
+ } from '@domql/utils'
15
+
16
+ export const Collection = {
17
+ define: {
18
+ $collection: async (param, el, state) => {
19
+ const { __ref: ref } = el
20
+ const {
21
+ children: childrenProps,
22
+ childrenAs,
23
+ childExtends
24
+ } = el.props || {}
25
+ const children = childrenProps && (await exec(childrenProps, el, state))
26
+
27
+ const childrenAsDefault = childrenAs || 'props'
28
+
29
+ if (children) {
30
+ if (isObject(children)) {
31
+ if (children.$$typeof) return el.call('renderReact', children, el)
32
+ param = deepClone(children)
33
+ param = Object.keys(param).map(v => {
34
+ const val = param[v]
35
+ return addAdditionalExtend(v, val)
36
+ })
37
+ } else if (isArray(children)) {
38
+ param = deepClone(children)
39
+ if (childrenAsDefault && childrenAsDefault !== 'element') {
40
+ param = param.map(v => ({
41
+ ...(childExtends && { extends: childExtends }),
42
+ [childrenAsDefault]: isObjectLike(v)
43
+ ? v
44
+ : childrenAsDefault === 'state'
45
+ ? { value: v }
46
+ : { text: v }
47
+ }))
48
+ }
49
+ } else if (isString(children) || isNumber(children)) {
50
+ el.removeContent()
51
+ el.content = { text: param }
52
+ return
53
+ }
54
+ }
55
+
56
+ if (!param) return
57
+
58
+ const filterReact = param.filter(v => !v.$$typeof)
59
+ if (filterReact.length !== param.length) {
60
+ const extractedReactComponents = param.filter(v => v.$$typeof)
61
+ el.call('renderReact', extractedReactComponents, el)
62
+ }
63
+ param = filterReact
64
+
65
+ if (isString(param)) {
66
+ if (param === 'state') param = state.parse()
67
+ else param = getChildStateInKey(param, state)
68
+ }
69
+ if (isState(param)) param = param.parse()
70
+ if (isNot(param)('array', 'object')) return
71
+
72
+ param = deepClone(param)
73
+
74
+ if (ref.__collectionCache) {
75
+ const equals =
76
+ JSON.stringify(param) === JSON.stringify(ref.__collectionCache)
77
+ if (equals) {
78
+ ref.__noCollectionDifference = true
79
+ return
80
+ } else {
81
+ ref.__collectionCache = deepClone(param)
82
+ delete ref.__noCollectionDifference
83
+ }
84
+ } else {
85
+ ref.__collectionCache = deepClone(param)
86
+ }
87
+
88
+ const obj = {
89
+ tag: 'fragment',
90
+ ignoreChildProps: true,
91
+ childProps: el.props && el.props.childProps
92
+ }
93
+
94
+ for (const key in param) {
95
+ const value = param[key]
96
+ if (value) obj[key] = isObjectLike(value) ? value : { value }
97
+ }
98
+
99
+ el.removeContent()
100
+ el.content = obj
101
+
102
+ // return deepClone(param)
103
+ },
104
+
105
+ $setCollection: async (param, el, state) => {
106
+ if (!param) return
107
+
108
+ if (isString(param)) {
109
+ if (param === 'state') param = state.parse()
110
+ else param = getChildStateInKey(param, state)
111
+ }
112
+
113
+ const data = (
114
+ isArray(param) ? param : isObject(param) ? Object.values(param) : []
115
+ ).map(item => (!isObjectLike(item) ? { value: item } : item))
116
+
117
+ if (data.length) {
118
+ const t = setTimeout(() => {
119
+ el.set(
120
+ { tag: 'fragment', ...data },
121
+ { preventDefineUpdate: '$setCollection' }
122
+ )
123
+ clearTimeout(t)
124
+ })
125
+ }
126
+
127
+ return data
128
+ },
129
+
130
+ $stateCollection: async (param, el, state, ctx) => {
131
+ const { children, childrenAs } = el.props || {}
132
+ if (!param || children || childrenAs) return
133
+
134
+ if (isString(param)) {
135
+ if (param === 'state') param = state.parse()
136
+ else param = getChildStateInKey(param, state)
137
+ }
138
+ if (isState(param)) param = param.parse()
139
+ if (isNot(param)('array', 'object')) return
140
+
141
+ const { __ref: ref } = el
142
+ param = deepClone(param)
143
+
144
+ if (ref.__stateCollectionCache) {
145
+ const equals =
146
+ JSON.stringify(param) === JSON.stringify(ref.__stateCollectionCache)
147
+ if (equals) {
148
+ ref.__noCollectionDifference = true
149
+ return
150
+ } else {
151
+ ref.__stateCollectionCache = deepClone(param)
152
+ delete ref.__noCollectionDifference
153
+ }
154
+ } else {
155
+ ref.__stateCollectionCache = deepClone(param)
156
+ }
157
+
158
+ const obj = {
159
+ tag: 'fragment',
160
+ ignoreChildProps: true,
161
+ childProps: el.props && el.props.childProps
162
+ }
163
+
164
+ for (const key in param) {
165
+ const value = param[key]
166
+ if (value) obj[key] = { state: isObjectLike(value) ? value : { value } }
167
+ }
168
+
169
+ el.removeContent()
170
+ el.content = obj
171
+
172
+ // return deepClone(param)
173
+ },
174
+
175
+ $propsCollection: async (param, el, state) => {
176
+ const { children, childrenAs } = el.props || {}
177
+ if (!param || children || childrenAs) return
178
+
179
+ if (isString(param)) {
180
+ if (param === 'state') param = state.parse()
181
+ else param = getChildStateInKey(param, state)
182
+ }
183
+ if (isState(param)) param = param.parse()
184
+ if (isNot(param)('array', 'object')) return
185
+
186
+ const { __ref: ref } = el
187
+ param = deepClone(param)
188
+
189
+ if (ref.__propsCollectionCache) {
190
+ const equals =
191
+ JSON.stringify(param) === JSON.stringify(ref.__propsCollectionCache) // eslint-disable-line
192
+ if (equals) {
193
+ ref.__noCollectionDifference = true
194
+ return
195
+ } else {
196
+ ref.__propsCollectionCache = deepClone(param)
197
+ delete ref.__noCollectionDifference
198
+ }
199
+ } else {
200
+ ref.__propsCollectionCache = deepClone(param)
201
+ }
202
+
203
+ const obj = {
204
+ tag: 'fragment',
205
+ ignoreChildProps: true,
206
+ childProps: el.props && el.props.childProps
207
+ }
208
+
209
+ for (const key in param) {
210
+ const value = param[key]
211
+ if (value) obj[key] = { props: isObjectLike(value) ? value : { value } }
212
+ }
213
+
214
+ el.removeContent()
215
+ el.content = obj
216
+
217
+ // const set = () => {
218
+ // el.set(obj, { preventDefineUpdate: '$propsCollection' })
219
+ // }
220
+
221
+ // if (el.props && el.props.lazyLoad) {
222
+ // window.requestAnimationFrame(set)
223
+ // } else set()
224
+
225
+ // return deepClone(param)
226
+ }
227
+ }
228
+ }
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@symbo.ls/atoms",
3
- "version": "3.1.2",
3
+ "version": "3.2.3",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
- "gitHead": "429b36616aa04c8587a26ce3c129815115e35897",
6
+ "gitHead": "9fc1b79b41cdc725ca6b24aec64920a599634681",
7
7
  "dependencies": {
8
- "@domql/state": "^3.1.2",
9
- "@domql/utils": "^3.1.2",
10
- "@symbo.ls/emotion": "^3.1.2",
11
- "@symbo.ls/scratch": "^3.1.2"
8
+ "@domql/state": "^3.2.3",
9
+ "@domql/utils": "^3.2.3",
10
+ "@symbo.ls/emotion": "^3.2.3",
11
+ "@symbo.ls/scratch": "^3.2.3"
12
12
  },
13
13
  "source": "src/index.js",
14
14
  "devDependencies": {