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.
Files changed (39) hide show
  1. package/dist/glass_easel_miniprogram_adapter.js +1 -1
  2. package/dist/glass_easel_miniprogram_adapter.js.map +1 -1
  3. package/dist/types/src/backend.d.ts +8 -0
  4. package/dist/types/src/backend.d.ts.map +1 -1
  5. package/dist/types/src/behavior.d.ts +1 -1
  6. package/dist/types/src/behavior.d.ts.map +1 -1
  7. package/dist/types/src/component.d.ts +12 -2
  8. package/dist/types/src/component.d.ts.map +1 -1
  9. package/dist/types/src/index.d.ts.map +1 -1
  10. package/dist/types/src/intersection.d.ts +24 -0
  11. package/dist/types/src/intersection.d.ts.map +1 -0
  12. package/dist/types/src/media_query.d.ts +13 -0
  13. package/dist/types/src/media_query.d.ts.map +1 -0
  14. package/dist/types/src/selector_query.d.ts.map +1 -1
  15. package/dist/types/src/space.d.ts +3 -3
  16. package/dist/types/src/space.d.ts.map +1 -1
  17. package/dist/types/src/types.d.ts +4 -4
  18. package/dist/types/src/types.d.ts.map +1 -1
  19. package/dist/types/src/utils.d.ts.map +1 -1
  20. package/dist/types/tests/base/env.d.ts.map +1 -1
  21. package/jest.config.js +1 -3
  22. package/package.json +2 -2
  23. package/src/backend.ts +20 -10
  24. package/src/behavior.ts +7 -29
  25. package/src/component.ts +56 -38
  26. package/src/index.ts +1 -3
  27. package/src/intersection.ts +119 -0
  28. package/src/media_query.ts +28 -0
  29. package/src/selector_query.ts +176 -174
  30. package/src/space.ts +154 -238
  31. package/src/types.ts +33 -33
  32. package/src/utils.ts +4 -3
  33. package/tests/base/env.ts +1 -2
  34. package/tests/data_update.test.ts +43 -37
  35. package/tests/env.test.ts +37 -37
  36. package/tests/selector.test.ts +295 -189
  37. package/tests/space.test.ts +216 -91
  38. package/tests/trait_behavior.test.ts +26 -23
  39. package/webpack.config.js +14 -13
@@ -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
- = () => { /* empty */ },
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
- = () => { /* empty */ },
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
- = () => { /* empty */ },
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(cb: (resList: any[]) => void = () => { /* empty */ }) {
207
+ exec(
208
+ cb: (resList: any[]) => void = () => {
209
+ /* empty */
210
+ },
211
+ ) {
208
212
  const q = this._$queue
209
- joinAsync<any>(q.map((req) => ((done) => {
210
- const {
211
- component,
212
- selector,
213
- single,
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
- if (fields.id) res.id = elem.id
271
- if (fields.dataset) res.dataset = elem.dataset
272
- if (fields.mark) res.mark = elem.collectMarks()
273
- joinAsync<undefined>([
274
- (done) => {
275
- if (fields.rect || fields.size) {
276
- elem.getBoundingClientRect((rect) => {
277
- if (fields.rect) {
278
- res.left = rect.left
279
- res.top = rect.top
280
- res.right = rect.left + rect.width
281
- res.bottom = rect.top + rect.height
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
- if (fields.size) {
284
- res.width = rect.width
285
- res.height = rect.height
286
- }
287
- done(undefined)
288
- })
289
- } else {
290
- done(undefined)
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
- } else {
303
- done(undefined)
304
- }
252
+ }
253
+ },
254
+ ],
255
+ () => {
256
+ cb(res)
257
+ done(res)
305
258
  },
306
- ], () => {
307
- if (fields.properties) {
308
- if (elem instanceof glassEasel.Component) {
309
- fields.properties.forEach((propName) => {
310
- if (glassEasel.Component.hasProperty(elem, propName)) {
311
- const ds = elem.getComponentOptions().propertyPassingDeepCopy
312
- const v = (elem.data as { [x: string]: unknown })[propName]
313
- if (ds === glassEasel.DeepCopyKind.None) {
314
- res[propName] = v
315
- } else if (ds === glassEasel.DeepCopyKind.Simple) {
316
- res[propName] = glassEasel.dataUtils.simpleDeepCopy(v)
317
- } else if (ds === glassEasel.DeepCopyKind.SimpleWithRecursion) {
318
- res[propName] = glassEasel.dataUtils.deepCopy(v, true)
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
- setTimeout(() => {
337
- cb(null)
338
- done(null)
339
- }, 0)
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
- })), cb)
354
+ }),
355
+ cb,
356
+ )
355
357
  }
356
358
  }