Skip to content

Commit ac4a345

Browse files
committed
feat: update resize observer api
1 parent bd0a623 commit ac4a345

File tree

5 files changed

+87
-7
lines changed

5 files changed

+87
-7
lines changed

glass-easel-miniprogram-adapter/src/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ export class ComponentCaller<
364364
}
365365

366366
/** Create a resize observer */
367-
createResizeObserver(options: { observeAll?: boolean }): ResizeObserver {
367+
createResizeObserver(options?: { observeAll?: boolean }): ResizeObserver {
368368
return new ResizeObserver(this, !!options?.observeAll)
369369
}
370370

glass-easel-miniprogram-adapter/tests/selector.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,46 @@ describe('intersection observer', () => {
700700
})
701701
})
702702

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+
703743
describe('media query observer', () => {
704744
test('create media query observer', () => {
705745
const env = new MiniProgramEnv()

glass-easel/src/backend/current_window_backend_context.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
type MediaQueryStatus,
1414
type Observer,
1515
} from './shared'
16-
import type * as shared from './shared'
16+
import * as shared from './shared'
1717

1818
const DELEGATE_EVENTS = [
1919
'touchstart',
@@ -453,6 +453,47 @@ export class CurrentWindowBackendContext implements Context {
453453
}
454454
}
455455

456+
createResizeObserver(
457+
targetElement: Element,
458+
mode: shared.ResizeObserverMode,
459+
listener: (res: shared.ResizeStatus) => void,
460+
): Observer {
461+
const observer = new ResizeObserver((entries) => {
462+
entries.forEach((entry) => {
463+
const elem = entry.target
464+
const isVertialWritingMode = window
465+
.getComputedStyle(elem)
466+
.writingMode.startsWith('vertical-')
467+
if (entry.contentBoxSize.length !== 1 || entry.borderBoxSize.length !== 1) {
468+
// eslint-disable-next-line no-console
469+
console.error('currently multiple boxes are not supported in resize observer')
470+
return
471+
}
472+
const contentBox = entry.contentBoxSize[0]!
473+
const borderBox = entry.borderBoxSize[0]!
474+
listener({
475+
boundingContentBoxWidth: isVertialWritingMode
476+
? contentBox.blockSize
477+
: contentBox.inlineSize,
478+
boundingContentBoxHeight: isVertialWritingMode
479+
? contentBox.inlineSize
480+
: contentBox.blockSize,
481+
boundingBorderBoxWidth: isVertialWritingMode ? borderBox.blockSize : borderBox.inlineSize,
482+
boundingBorderBoxHeight: isVertialWritingMode
483+
? borderBox.inlineSize
484+
: borderBox.blockSize,
485+
})
486+
})
487+
})
488+
const box = mode === shared.ResizeObserverMode.BorderBox ? 'border-box' : 'content-box'
489+
observer.observe(targetElement as unknown as HTMLElement, { box })
490+
return {
491+
disconnect() {
492+
observer.disconnect()
493+
},
494+
}
495+
}
496+
456497
createMediaQueryObserver(
457498
status: MediaQueryStatus,
458499
listener: (res: { matches: boolean }) => void,

glass-easel/src/backend/shared.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ export const enum ResizeObserverMode {
8585
}
8686

8787
export type ResizeStatus = {
88-
borderWidth: number
89-
borderHeight: number
90-
contentWidth: number
91-
contentHeight: number
88+
boundingContentBoxWidth: number
89+
boundingContentBoxHeight: number
90+
boundingBorderBoxWidth: number
91+
boundingBorderBoxHeight: number
9292
}
9393

9494
export type MediaQueryStatus = {

glass-easel/src/element.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3263,7 +3263,6 @@ export class Element implements NodeCast {
32633263
* Create a resize observer
32643264
*
32653265
* It is possible to choose to observe either the content box or the border box.
3266-
* The listener always triggers once immediately after this call.
32673266
*/
32683267
createResizeObserver(
32693268
mode: ResizeObserverMode,

0 commit comments

Comments
 (0)