glass-easel-miniprogram-adapter 0.16.2 → 0.17.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.d.ts +15 -0
- package/dist/glass_easel_miniprogram_adapter.es.js +1 -1
- package/dist/glass_easel_miniprogram_adapter.es.js.map +1 -1
- package/dist/glass_easel_miniprogram_adapter.js +1 -1
- package/dist/glass_easel_miniprogram_adapter.js.map +1 -1
- package/package.json +4 -4
- package/src/component.ts +6 -0
- package/src/resize.ts +56 -0
- package/tests/selector.test.ts +40 -0
package/src/component.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { type utils as typeUtils } from './types'
|
|
|
3
3
|
import { type ComponentType, type GeneralBehavior, type TraitBehavior } from './behavior'
|
|
4
4
|
import { SelectorQuery } from './selector_query'
|
|
5
5
|
import { IntersectionObserver } from './intersection'
|
|
6
|
+
import { ResizeObserver } from './resize'
|
|
6
7
|
import { MediaQueryObserver } from './media_query'
|
|
7
8
|
|
|
8
9
|
type DataList = typeUtils.DataList
|
|
@@ -362,6 +363,11 @@ export class ComponentCaller<
|
|
|
362
363
|
)
|
|
363
364
|
}
|
|
364
365
|
|
|
366
|
+
/** Create a resize observer */
|
|
367
|
+
createResizeObserver(options?: { observeAll?: boolean }): ResizeObserver {
|
|
368
|
+
return new ResizeObserver(this, !!options?.observeAll)
|
|
369
|
+
}
|
|
370
|
+
|
|
365
371
|
/** Create an media query observer */
|
|
366
372
|
createMediaQueryObserver(): MediaQueryObserver {
|
|
367
373
|
return new MediaQueryObserver(this)
|
package/src/resize.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import * as glassEasel from 'glass-easel'
|
|
2
|
+
import { type GeneralComponent } from './component'
|
|
3
|
+
|
|
4
|
+
export class ResizeObserver {
|
|
5
|
+
private _$comp: GeneralComponent
|
|
6
|
+
private _$observeAll: boolean
|
|
7
|
+
private _$mode = glassEasel.backend.ResizeObserverMode.ContentBox
|
|
8
|
+
private _$observers: glassEasel.backend.Observer[] = []
|
|
9
|
+
|
|
10
|
+
/** @internal */
|
|
11
|
+
constructor(comp: GeneralComponent, observeAll: boolean) {
|
|
12
|
+
this._$comp = comp
|
|
13
|
+
this._$observeAll = observeAll
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
contentBox(): this {
|
|
17
|
+
this._$mode = glassEasel.backend.ResizeObserverMode.ContentBox
|
|
18
|
+
return this
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
borderBox(): this {
|
|
22
|
+
this._$mode = glassEasel.backend.ResizeObserverMode.BorderBox
|
|
23
|
+
return this
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
observe(targetSelector: string, listener: (status: glassEasel.backend.ResizeStatus) => void) {
|
|
27
|
+
const shadowRoot = this._$comp._$.getShadowRoot()
|
|
28
|
+
let targets: glassEasel.Element[]
|
|
29
|
+
if (!shadowRoot) {
|
|
30
|
+
targets = []
|
|
31
|
+
} else if (this._$observeAll) {
|
|
32
|
+
targets = shadowRoot.querySelectorAll(targetSelector)
|
|
33
|
+
} else {
|
|
34
|
+
const elem = shadowRoot.querySelector(targetSelector)
|
|
35
|
+
if (elem === null) {
|
|
36
|
+
targets = []
|
|
37
|
+
} else {
|
|
38
|
+
targets = [elem]
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
targets.forEach((target) => {
|
|
42
|
+
const observer = target.createResizeObserver(this._$mode, listener)
|
|
43
|
+
if (observer === null) {
|
|
44
|
+
// TODO warn no observer attached
|
|
45
|
+
} else {
|
|
46
|
+
this._$observers.push(observer)
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
disconnect() {
|
|
52
|
+
const observers = this._$observers
|
|
53
|
+
this._$observers = []
|
|
54
|
+
observers.forEach((observer) => observer.disconnect())
|
|
55
|
+
}
|
|
56
|
+
}
|
package/tests/selector.test.ts
CHANGED
|
@@ -700,6 +700,46 @@ describe('intersection observer', () => {
|
|
|
700
700
|
})
|
|
701
701
|
})
|
|
702
702
|
|
|
703
|
+
describe('resize observer', () => {
|
|
704
|
+
test('create resize observer', () => {
|
|
705
|
+
const env = new MiniProgramEnv()
|
|
706
|
+
const codeSpace = env.createCodeSpace('', true)
|
|
707
|
+
|
|
708
|
+
codeSpace.addComponentStaticConfig('path/to/comp', {
|
|
709
|
+
usingComponents: {},
|
|
710
|
+
})
|
|
711
|
+
codeSpace.addCompiledTemplate(
|
|
712
|
+
'path/to/comp',
|
|
713
|
+
tmpl(`
|
|
714
|
+
<div id="a" />
|
|
715
|
+
<div id="a" />
|
|
716
|
+
`),
|
|
717
|
+
)
|
|
718
|
+
// eslint-disable-next-line arrow-body-style
|
|
719
|
+
codeSpace.componentEnv('path/to/comp', ({ Component }) => {
|
|
720
|
+
return Component()
|
|
721
|
+
.lifetime('attached', function () {
|
|
722
|
+
const o1 = this.createResizeObserver()
|
|
723
|
+
o1.observe('#a', () => {
|
|
724
|
+
/* empty */
|
|
725
|
+
})
|
|
726
|
+
const o2 = this.createResizeObserver({ observeAll: true })
|
|
727
|
+
o2.borderBox().observe('#a', () => {
|
|
728
|
+
/* empty */
|
|
729
|
+
})
|
|
730
|
+
o1.disconnect()
|
|
731
|
+
o2.disconnect()
|
|
732
|
+
})
|
|
733
|
+
.register()
|
|
734
|
+
})
|
|
735
|
+
|
|
736
|
+
const backendContext = new glassEasel.EmptyComposedBackendContext()
|
|
737
|
+
const ab = env.associateBackend(backendContext)
|
|
738
|
+
const root = ab.createRoot('body', codeSpace, 'path/to/comp')
|
|
739
|
+
glassEasel.Element.pretendAttached(root.getComponent())
|
|
740
|
+
})
|
|
741
|
+
})
|
|
742
|
+
|
|
703
743
|
describe('media query observer', () => {
|
|
704
744
|
test('create media query observer', () => {
|
|
705
745
|
const env = new MiniProgramEnv()
|