glass-easel-miniprogram-adapter 0.10.2 → 0.11.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.
@@ -9,6 +9,7 @@ export type SelectorQueryFields = {
9
9
  size?: boolean
10
10
  scrollOffset?: boolean
11
11
  properties?: string[]
12
+ context?: boolean
12
13
  // TODO support computedStyle
13
14
  }
14
15
 
@@ -50,6 +51,10 @@ export type ScrollOffset = {
50
51
  scrollHeight: number
51
52
  }
52
53
 
54
+ export type ContextResult = {
55
+ context: unknown
56
+ }
57
+
53
58
  const joinAsync = <T>(inits: ((cb: (ret: T) => void) => void)[], cb: (rets: T[]) => void) => {
54
59
  let initDone = false
55
60
  let jobCount = inits.length
@@ -137,6 +142,15 @@ class NodesRef<S extends boolean> {
137
142
  )
138
143
  return this._$sq
139
144
  }
145
+
146
+ context(
147
+ cb: (res: S extends true ? ContextResult : any[]) => void = () => {
148
+ /* empty */
149
+ },
150
+ ) {
151
+ this._$sq._$push(this._$sel, this._$comp, this._$single, { context: true }, cb)
152
+ return this._$sq
153
+ }
140
154
  }
141
155
 
142
156
  // main class
@@ -298,6 +312,16 @@ export class SelectorQuery {
298
312
  done(undefined)
299
313
  }
300
314
  },
315
+ (done) => {
316
+ if (fields.context) {
317
+ elem.getContext((ctx) => {
318
+ res.context = ctx
319
+ done(undefined)
320
+ })
321
+ } else {
322
+ done(undefined)
323
+ }
324
+ },
301
325
  ],
302
326
  () => {
303
327
  if (fields.properties) {
@@ -559,6 +559,32 @@ describe('selector query', () => {
559
559
  .register()
560
560
  })
561
561
 
562
+ const ab = env.associateBackend()
563
+ const root = ab.createRoot('body', codeSpace, 'path/to/comp')
564
+ glassEasel.Element.pretendAttached(root.getComponent())
565
+ }))
566
+
567
+ test('query context', () =>
568
+ new Promise<undefined>((resolve) => {
569
+ const env = new MiniProgramEnv()
570
+ const codeSpace = env.createCodeSpace('', true)
571
+
572
+ codeSpace.addComponentStaticConfig('path/to/comp', {})
573
+ codeSpace.addCompiledTemplate('path/to/comp', tmpl(''))
574
+ codeSpace.componentEnv('path/to/comp', ({ Component }) => {
575
+ Component()
576
+ .lifetime('attached', function () {
577
+ this.createSelectorQuery()
578
+ .selectViewport()
579
+ .context((res) => {
580
+ expect(res.context).toBeUndefined()
581
+ resolve(undefined)
582
+ })
583
+ .exec()
584
+ })
585
+ .register()
586
+ })
587
+
562
588
  const ab = env.associateBackend()
563
589
  const root = ab.createRoot('body', codeSpace, 'path/to/comp')
564
590
  glassEasel.Element.pretendAttached(root.getComponent())
@@ -877,6 +877,30 @@ describe('define', () => {
877
877
  expect(callOrder).toStrictEqual([2, 1])
878
878
  })
879
879
 
880
+ test('chaining static data', () => {
881
+ const env = new MiniProgramEnv()
882
+ const codeSpace = env.createCodeSpace('', true)
883
+
884
+ codeSpace.addComponentStaticConfig('path/to/comp', {})
885
+ codeSpace.addCompiledTemplate(
886
+ 'path/to/comp',
887
+ tmpl(`
888
+ <div>{{ text }}</div>
889
+ `),
890
+ )
891
+ codeSpace.componentEnv('path/to/comp', ({ Component }) => {
892
+ Component()
893
+ .staticData({
894
+ text: 'abc',
895
+ })
896
+ .register()
897
+ })
898
+
899
+ const ab = env.associateBackend()
900
+ const root = ab.createRoot('body', codeSpace, 'path/to/comp')
901
+ expect(domHtml(root.getComponent())).toBe('<div>abc</div>')
902
+ })
903
+
880
904
  test('chaining observer', () => {
881
905
  const env = new MiniProgramEnv()
882
906
  const codeSpace = env.createCodeSpace('', true)