glass-easel-miniprogram-adapter 0.1.0 → 0.2.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/dist/glass_easel_miniprogram_adapter.js +1 -1
- package/dist/glass_easel_miniprogram_adapter.js.map +1 -1
- package/dist/types/src/backend.d.ts +8 -0
- package/dist/types/src/backend.d.ts.map +1 -1
- package/dist/types/src/behavior.d.ts +1 -1
- package/dist/types/src/behavior.d.ts.map +1 -1
- package/dist/types/src/component.d.ts +12 -2
- package/dist/types/src/component.d.ts.map +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/intersection.d.ts +24 -0
- package/dist/types/src/intersection.d.ts.map +1 -0
- package/dist/types/src/media_query.d.ts +13 -0
- package/dist/types/src/media_query.d.ts.map +1 -0
- package/dist/types/src/selector_query.d.ts.map +1 -1
- package/dist/types/src/space.d.ts +3 -3
- package/dist/types/src/space.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +4 -4
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/utils.d.ts.map +1 -1
- package/dist/types/tests/base/env.d.ts.map +1 -1
- package/jest.config.js +1 -3
- package/package.json +2 -2
- package/src/backend.ts +20 -10
- package/src/behavior.ts +7 -29
- package/src/component.ts +56 -38
- package/src/index.ts +1 -3
- package/src/intersection.ts +119 -0
- package/src/media_query.ts +28 -0
- package/src/selector_query.ts +176 -174
- package/src/space.ts +154 -238
- package/src/types.ts +33 -33
- package/src/utils.ts +4 -3
- package/tests/base/env.ts +1 -2
- package/tests/data_update.test.ts +43 -37
- package/tests/env.test.ts +37 -37
- package/tests/selector.test.ts +295 -189
- package/tests/space.test.ts +216 -91
- package/tests/trait_behavior.test.ts +26 -23
- package/webpack.config.js +14 -13
package/src/selector_query.ts
CHANGED
|
@@ -2,13 +2,13 @@ import * as glassEasel from 'glass-easel'
|
|
|
2
2
|
import type { GeneralComponent } from './component'
|
|
3
3
|
|
|
4
4
|
export type SelectorQueryFields = {
|
|
5
|
-
id?: boolean
|
|
6
|
-
dataset?: boolean
|
|
7
|
-
mark?: boolean
|
|
8
|
-
rect?: boolean
|
|
9
|
-
size?: boolean
|
|
10
|
-
scrollOffset?: boolean
|
|
11
|
-
properties?: string[]
|
|
5
|
+
id?: boolean
|
|
6
|
+
dataset?: boolean
|
|
7
|
+
mark?: boolean
|
|
8
|
+
rect?: boolean
|
|
9
|
+
size?: boolean
|
|
10
|
+
scrollOffset?: boolean
|
|
11
|
+
properties?: string[]
|
|
12
12
|
// TODO support computedStyle
|
|
13
13
|
}
|
|
14
14
|
|
|
@@ -31,29 +31,26 @@ export type SelectorQueryResult<T extends SelectorQueryFields> = {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
export type BoundingClientRect = {
|
|
34
|
-
id: string
|
|
35
|
-
dataset: { [k: string]: any }
|
|
36
|
-
left: number
|
|
37
|
-
top: number
|
|
38
|
-
right: number
|
|
39
|
-
bottom: number
|
|
40
|
-
width: number
|
|
41
|
-
height: number
|
|
34
|
+
id: string
|
|
35
|
+
dataset: { [k: string]: any }
|
|
36
|
+
left: number
|
|
37
|
+
top: number
|
|
38
|
+
right: number
|
|
39
|
+
bottom: number
|
|
40
|
+
width: number
|
|
41
|
+
height: number
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
export type ScrollOffset = {
|
|
45
|
-
id: string
|
|
46
|
-
dataset: { [k: string]: any }
|
|
47
|
-
scrollLeft: number
|
|
48
|
-
scrollTop: number
|
|
49
|
-
scrollWidth: number
|
|
50
|
-
scrollHeight: number
|
|
45
|
+
id: string
|
|
46
|
+
dataset: { [k: string]: any }
|
|
47
|
+
scrollLeft: number
|
|
48
|
+
scrollTop: number
|
|
49
|
+
scrollWidth: number
|
|
50
|
+
scrollHeight: number
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
const joinAsync = <T>(
|
|
54
|
-
inits: ((cb: (ret: T) => void) => void)[],
|
|
55
|
-
cb: (rets: T[]) => void,
|
|
56
|
-
) => {
|
|
53
|
+
const joinAsync = <T>(inits: ((cb: (ret: T) => void) => void)[], cb: (rets: T[]) => void) => {
|
|
57
54
|
let initDone = false
|
|
58
55
|
let jobCount = inits.length
|
|
59
56
|
const rets = new Array<T>(jobCount)
|
|
@@ -94,16 +91,18 @@ class NodesRef<S extends boolean> {
|
|
|
94
91
|
|
|
95
92
|
fields<T extends SelectorQueryFields>(
|
|
96
93
|
fields: T,
|
|
97
|
-
cb: (res: S extends true ? SelectorQueryResult<T> : SelectorQueryResult<T>[]) => void
|
|
98
|
-
|
|
94
|
+
cb: (res: S extends true ? SelectorQueryResult<T> : SelectorQueryResult<T>[]) => void = () => {
|
|
95
|
+
/* empty */
|
|
96
|
+
},
|
|
99
97
|
) {
|
|
100
98
|
this._$sq._$push(this._$sel, this._$comp, this._$single, fields, cb)
|
|
101
99
|
return this._$sq
|
|
102
100
|
}
|
|
103
101
|
|
|
104
102
|
boundingClientRect(
|
|
105
|
-
cb: (res: S extends true ? BoundingClientRect : BoundingClientRect[]) => void
|
|
106
|
-
|
|
103
|
+
cb: (res: S extends true ? BoundingClientRect : BoundingClientRect[]) => void = () => {
|
|
104
|
+
/* empty */
|
|
105
|
+
},
|
|
107
106
|
) {
|
|
108
107
|
this._$sq._$push(
|
|
109
108
|
this._$sel,
|
|
@@ -121,8 +120,9 @@ class NodesRef<S extends boolean> {
|
|
|
121
120
|
}
|
|
122
121
|
|
|
123
122
|
scrollOffset(
|
|
124
|
-
cb: (res: S extends true ? ScrollOffset : ScrollOffset[]) => void
|
|
125
|
-
|
|
123
|
+
cb: (res: S extends true ? ScrollOffset : ScrollOffset[]) => void = () => {
|
|
124
|
+
/* empty */
|
|
125
|
+
},
|
|
126
126
|
) {
|
|
127
127
|
this._$sq._$push(
|
|
128
128
|
this._$sel,
|
|
@@ -144,11 +144,11 @@ export class SelectorQuery {
|
|
|
144
144
|
private _$comp: GeneralComponent
|
|
145
145
|
/** @internal */
|
|
146
146
|
_$queue: {
|
|
147
|
-
component: GeneralComponent
|
|
148
|
-
selector: string | null
|
|
149
|
-
single: boolean
|
|
150
|
-
fields: SelectorQueryFields
|
|
151
|
-
cb: (res: any) => void
|
|
147
|
+
component: GeneralComponent
|
|
148
|
+
selector: string | null
|
|
149
|
+
single: boolean
|
|
150
|
+
fields: SelectorQueryFields
|
|
151
|
+
cb: (res: any) => void
|
|
152
152
|
}[]
|
|
153
153
|
|
|
154
154
|
/** @internal */
|
|
@@ -204,153 +204,155 @@ export class SelectorQuery {
|
|
|
204
204
|
*
|
|
205
205
|
* The `cb` is called after all queries done.
|
|
206
206
|
*/
|
|
207
|
-
exec(
|
|
207
|
+
exec(
|
|
208
|
+
cb: (resList: any[]) => void = () => {
|
|
209
|
+
/* empty */
|
|
210
|
+
},
|
|
211
|
+
) {
|
|
208
212
|
const q = this._$queue
|
|
209
|
-
joinAsync<any>(
|
|
210
|
-
|
|
211
|
-
component,
|
|
212
|
-
selector
|
|
213
|
-
|
|
214
|
-
fields,
|
|
215
|
-
cb,
|
|
216
|
-
} = req
|
|
217
|
-
if (selector === null) {
|
|
218
|
-
// for the viewport selection, return available fields
|
|
219
|
-
const res = {} as { [k: string]: any }
|
|
220
|
-
joinAsync<undefined>([
|
|
221
|
-
(done) => {
|
|
222
|
-
const ctx = component._$.getBackendContext()
|
|
223
|
-
const width = ctx.getWindowWidth()
|
|
224
|
-
const height = ctx.getWindowHeight()
|
|
225
|
-
if (fields.id) res.id = ''
|
|
226
|
-
if (fields.dataset) res.dataset = {}
|
|
227
|
-
if (fields.mark) res.mark = {}
|
|
228
|
-
if (fields.rect) {
|
|
229
|
-
res.left = 0
|
|
230
|
-
res.top = 0
|
|
231
|
-
res.right = width
|
|
232
|
-
res.bottom = height
|
|
233
|
-
}
|
|
234
|
-
if (fields.size) {
|
|
235
|
-
res.width = width
|
|
236
|
-
res.height = height
|
|
237
|
-
}
|
|
238
|
-
if (fields.scrollOffset) {
|
|
239
|
-
let rootComp = component._$.general()
|
|
240
|
-
while (rootComp.ownerShadowRoot) {
|
|
241
|
-
rootComp = rootComp.ownerShadowRoot.getHostNode()
|
|
242
|
-
}
|
|
243
|
-
rootComp.getBoundingClientRect(({
|
|
244
|
-
left,
|
|
245
|
-
top,
|
|
246
|
-
width,
|
|
247
|
-
height,
|
|
248
|
-
}) => {
|
|
249
|
-
res.scrollLeft = left
|
|
250
|
-
res.scrollTop = top
|
|
251
|
-
res.scrollWidth = width
|
|
252
|
-
res.scrollHeight = height
|
|
253
|
-
done(undefined)
|
|
254
|
-
})
|
|
255
|
-
} else {
|
|
256
|
-
done(undefined)
|
|
257
|
-
}
|
|
258
|
-
},
|
|
259
|
-
], () => {
|
|
260
|
-
cb(res)
|
|
261
|
-
done(res)
|
|
262
|
-
})
|
|
263
|
-
} else {
|
|
264
|
-
// for common node selection, return specified fields
|
|
265
|
-
const constructSingleRes = (
|
|
266
|
-
elem: glassEasel.Element,
|
|
267
|
-
cb: (res: any) => void,
|
|
268
|
-
) => {
|
|
213
|
+
joinAsync<any>(
|
|
214
|
+
q.map((req) => (done) => {
|
|
215
|
+
const { component, selector, single, fields, cb } = req
|
|
216
|
+
if (selector === null) {
|
|
217
|
+
// for the viewport selection, return available fields
|
|
269
218
|
const res = {} as { [k: string]: any }
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
219
|
+
joinAsync<undefined>(
|
|
220
|
+
[
|
|
221
|
+
(done) => {
|
|
222
|
+
const ctx = component._$.getBackendContext()
|
|
223
|
+
const width = ctx.getWindowWidth()
|
|
224
|
+
const height = ctx.getWindowHeight()
|
|
225
|
+
if (fields.id) res.id = ''
|
|
226
|
+
if (fields.dataset) res.dataset = {}
|
|
227
|
+
if (fields.mark) res.mark = {}
|
|
228
|
+
if (fields.rect) {
|
|
229
|
+
res.left = 0
|
|
230
|
+
res.top = 0
|
|
231
|
+
res.right = width
|
|
232
|
+
res.bottom = height
|
|
233
|
+
}
|
|
234
|
+
if (fields.size) {
|
|
235
|
+
res.width = width
|
|
236
|
+
res.height = height
|
|
237
|
+
}
|
|
238
|
+
if (fields.scrollOffset) {
|
|
239
|
+
let rootComp = component._$.general()
|
|
240
|
+
while (rootComp.ownerShadowRoot) {
|
|
241
|
+
rootComp = rootComp.ownerShadowRoot.getHostNode()
|
|
282
242
|
}
|
|
283
|
-
|
|
284
|
-
res.
|
|
285
|
-
res.
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
}
|
|
292
|
-
},
|
|
293
|
-
(done) => {
|
|
294
|
-
if (fields.scrollOffset) {
|
|
295
|
-
elem.getScrollOffset((rect) => {
|
|
296
|
-
res.scrollLeft = rect.scrollLeft
|
|
297
|
-
res.scrollTop = rect.scrollTop
|
|
298
|
-
res.scrollWidth = rect.scrollWidth
|
|
299
|
-
res.scrollHeight = rect.scrollHeight
|
|
243
|
+
rootComp.getBoundingClientRect(({ left, top, width, height }) => {
|
|
244
|
+
res.scrollLeft = left
|
|
245
|
+
res.scrollTop = top
|
|
246
|
+
res.scrollWidth = width
|
|
247
|
+
res.scrollHeight = height
|
|
248
|
+
done(undefined)
|
|
249
|
+
})
|
|
250
|
+
} else {
|
|
300
251
|
done(undefined)
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
],
|
|
255
|
+
() => {
|
|
256
|
+
cb(res)
|
|
257
|
+
done(res)
|
|
305
258
|
},
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
259
|
+
)
|
|
260
|
+
} else {
|
|
261
|
+
// for common node selection, return specified fields
|
|
262
|
+
const constructSingleRes = (elem: glassEasel.Element, cb: (res: any) => void) => {
|
|
263
|
+
const res = {} as { [k: string]: any }
|
|
264
|
+
if (fields.id) res.id = elem.id
|
|
265
|
+
if (fields.dataset) res.dataset = elem.dataset
|
|
266
|
+
if (fields.mark) res.mark = elem.collectMarks()
|
|
267
|
+
joinAsync<undefined>(
|
|
268
|
+
[
|
|
269
|
+
(done) => {
|
|
270
|
+
if (fields.rect || fields.size) {
|
|
271
|
+
elem.getBoundingClientRect((rect) => {
|
|
272
|
+
if (fields.rect) {
|
|
273
|
+
res.left = rect.left
|
|
274
|
+
res.top = rect.top
|
|
275
|
+
res.right = rect.left + rect.width
|
|
276
|
+
res.bottom = rect.top + rect.height
|
|
277
|
+
}
|
|
278
|
+
if (fields.size) {
|
|
279
|
+
res.width = rect.width
|
|
280
|
+
res.height = rect.height
|
|
281
|
+
}
|
|
282
|
+
done(undefined)
|
|
283
|
+
})
|
|
284
|
+
} else {
|
|
285
|
+
done(undefined)
|
|
320
286
|
}
|
|
321
|
-
}
|
|
322
|
-
|
|
287
|
+
},
|
|
288
|
+
(done) => {
|
|
289
|
+
if (fields.scrollOffset) {
|
|
290
|
+
elem.getScrollOffset((rect) => {
|
|
291
|
+
res.scrollLeft = rect.scrollLeft
|
|
292
|
+
res.scrollTop = rect.scrollTop
|
|
293
|
+
res.scrollWidth = rect.scrollWidth
|
|
294
|
+
res.scrollHeight = rect.scrollHeight
|
|
295
|
+
done(undefined)
|
|
296
|
+
})
|
|
297
|
+
} else {
|
|
298
|
+
done(undefined)
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
],
|
|
302
|
+
() => {
|
|
303
|
+
if (fields.properties) {
|
|
304
|
+
if (elem instanceof glassEasel.Component) {
|
|
305
|
+
fields.properties.forEach((propName) => {
|
|
306
|
+
if (glassEasel.Component.hasProperty(elem, propName)) {
|
|
307
|
+
const ds = elem.getComponentOptions().propertyPassingDeepCopy
|
|
308
|
+
const v = (elem.data as { [x: string]: unknown })[propName]
|
|
309
|
+
if (ds === glassEasel.DeepCopyKind.None) {
|
|
310
|
+
res[propName] = v
|
|
311
|
+
} else if (ds === glassEasel.DeepCopyKind.Simple) {
|
|
312
|
+
res[propName] = glassEasel.dataUtils.simpleDeepCopy(v)
|
|
313
|
+
} else if (ds === glassEasel.DeepCopyKind.SimpleWithRecursion) {
|
|
314
|
+
res[propName] = glassEasel.dataUtils.deepCopy(v, true)
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
})
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
cb(res)
|
|
321
|
+
},
|
|
322
|
+
)
|
|
323
|
+
}
|
|
324
|
+
if (single) {
|
|
325
|
+
// select single node
|
|
326
|
+
const node = component._$.getShadowRoot()!.querySelector(selector)
|
|
327
|
+
if (node) {
|
|
328
|
+
constructSingleRes(node, (res) => {
|
|
329
|
+
cb(res)
|
|
330
|
+
done(res)
|
|
331
|
+
})
|
|
332
|
+
} else {
|
|
333
|
+
setTimeout(() => {
|
|
334
|
+
cb(null)
|
|
335
|
+
done(null)
|
|
336
|
+
}, 0)
|
|
323
337
|
}
|
|
324
|
-
cb(res)
|
|
325
|
-
})
|
|
326
|
-
}
|
|
327
|
-
if (single) {
|
|
328
|
-
// select single node
|
|
329
|
-
const node = component._$.getShadowRoot()!.querySelector(selector)
|
|
330
|
-
if (node) {
|
|
331
|
-
constructSingleRes(node, (res) => {
|
|
332
|
-
cb(res)
|
|
333
|
-
done(res)
|
|
334
|
-
})
|
|
335
338
|
} else {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
339
|
+
// select multiple nodes
|
|
340
|
+
const nodes = component._$.getShadowRoot()!.querySelectorAll(selector)
|
|
341
|
+
joinAsync<any>(
|
|
342
|
+
nodes.map((node) => (done) => {
|
|
343
|
+
constructSingleRes(node, (res) => {
|
|
344
|
+
done(res)
|
|
345
|
+
})
|
|
346
|
+
}),
|
|
347
|
+
(resList) => {
|
|
348
|
+
cb(resList)
|
|
349
|
+
done(resList)
|
|
350
|
+
},
|
|
351
|
+
)
|
|
340
352
|
}
|
|
341
|
-
} else {
|
|
342
|
-
// select multiple nodes
|
|
343
|
-
const nodes = component._$.getShadowRoot()!.querySelectorAll(selector)
|
|
344
|
-
joinAsync<any>(nodes.map((node) => ((done) => {
|
|
345
|
-
constructSingleRes(node, (res) => {
|
|
346
|
-
done(res)
|
|
347
|
-
})
|
|
348
|
-
})), (resList) => {
|
|
349
|
-
cb(resList)
|
|
350
|
-
done(resList)
|
|
351
|
-
})
|
|
352
353
|
}
|
|
353
|
-
}
|
|
354
|
-
|
|
354
|
+
}),
|
|
355
|
+
cb,
|
|
356
|
+
)
|
|
355
357
|
}
|
|
356
358
|
}
|