improve code

This commit is contained in:
Rene
2021-04-25 20:24:56 +02:00
parent 6e0add8989
commit 970f69a5ab
4 changed files with 44 additions and 18 deletions
@@ -1,4 +1,4 @@
import { XY, TRBL, CacheValues, PartialOptions, each, push, keys, hasOwnProperty, isNumber, scrollLeft, scrollTop } from 'support'; import { XY, WH, TRBL, CacheValues, PartialOptions, each, push, keys, hasOwnProperty, isNumber, scrollLeft, scrollTop } from 'support';
import { OSOptions } from 'options'; import { OSOptions } from 'options';
import { getEnvironment } from 'environment'; import { getEnvironment } from 'environment';
import { StructureSetup } from 'setups/structureSetup'; import { StructureSetup } from 'setups/structureSetup';
@@ -42,6 +42,7 @@ export type Lifecycle = (
export interface LifecycleHubInstance { export interface LifecycleHubInstance {
_update(changedOptions?: PartialOptions<OSOptions> | null, force?: boolean): void; _update(changedOptions?: PartialOptions<OSOptions> | null, force?: boolean): void;
_state(): any;
_destroy(): void; _destroy(): void;
} }
@@ -56,7 +57,9 @@ export interface LifecycleHub {
_getViewportPaddingStyle(): StyleObject; _getViewportPaddingStyle(): StyleObject;
_setViewportPaddingStyle(newPaddingStlye?: StyleObject | null): void; _setViewportPaddingStyle(newPaddingStlye?: StyleObject | null): void;
_getViewportOverflowScroll(): XY<boolean>; _getViewportOverflowScroll(): XY<boolean>;
_setViewportOverflowScroll(newViewportOverflowScroll: XY<boolean>): void; _setViewportOverflowScroll(newViewportOverflowScroll?: XY<boolean>): void;
_getViewportOverflowAmount(): WH<number>;
_setViewportOverflowAmount(newViewportOverflowAmount?: WH<number>): void;
} }
const getPropByPath = <T>(obj: any, path: string): T => const getPropByPath = <T>(obj: any, path: string): T =>
@@ -98,6 +101,10 @@ const viewportOverflowScrollFallback: XY<boolean> = {
x: false, x: false,
y: false, y: false,
}; };
const viewportOverflowAmountFallback: WH<number> = {
w: 0,
h: 0,
};
const directionIsRTLCacheValuesFallback: CacheValues<boolean> = { const directionIsRTLCacheValuesFallback: CacheValues<boolean> = {
_value: false, _value: false,
_previous: false, _previous: false,
@@ -113,6 +120,7 @@ export const createLifecycleHub = (options: OSOptions, structureSetup: Structure
let paddingInfo = paddingInfoFallback; let paddingInfo = paddingInfoFallback;
let viewportPaddingStyle = viewportPaddingStyleFallback; let viewportPaddingStyle = viewportPaddingStyleFallback;
let viewportOverflowScroll = viewportOverflowScrollFallback; let viewportOverflowScroll = viewportOverflowScrollFallback;
let viewportOverflowAmount = viewportOverflowAmountFallback;
const { _host, _viewport, _content } = structureSetup._targetObj; const { _host, _viewport, _content } = structureSetup._targetObj;
const { const {
_nativeScrollbarStyling, _nativeScrollbarStyling,
@@ -139,6 +147,10 @@ export const createLifecycleHub = (options: OSOptions, structureSetup: Structure
_setViewportOverflowScroll(newViewportOverflowScroll) { _setViewportOverflowScroll(newViewportOverflowScroll) {
viewportOverflowScroll = newViewportOverflowScroll || viewportOverflowScrollFallback; viewportOverflowScroll = newViewportOverflowScroll || viewportOverflowScrollFallback;
}, },
_getViewportOverflowAmount: () => viewportOverflowAmount,
_setViewportOverflowAmount(newViewportOverflowAmount) {
viewportOverflowAmount = newViewportOverflowAmount || viewportOverflowAmountFallback;
},
}; };
push(lifecycles, createTrinsicLifecycle(instance)); push(lifecycles, createTrinsicLifecycle(instance));
@@ -266,6 +278,9 @@ export const createLifecycleHub = (options: OSOptions, structureSetup: Structure
return { return {
_update: update, _update: update,
_state: () => ({
_overflowAmount: viewportOverflowAmount,
}),
_destroy() { _destroy() {
removeEnvironmentListener(envUpdateListener); removeEnvironmentListener(envUpdateListener);
}, },
@@ -4,7 +4,6 @@ import {
attr, attr,
WH, WH,
XY, XY,
equalXY,
style, style,
scrollSize, scrollSize,
CacheValues, CacheValues,
@@ -58,18 +57,25 @@ const overlaidScrollbarsHideOffset = 42;
* @returns * @returns
*/ */
export const createOverflowLifecycle = (lifecycleHub: LifecycleHub): Lifecycle => { export const createOverflowLifecycle = (lifecycleHub: LifecycleHub): Lifecycle => {
const { _structureSetup, _doViewportArrange, _getViewportPaddingStyle, _getPaddingInfo, _setViewportOverflowScroll } = lifecycleHub; const {
const { _host, _padding, _viewport, _viewportArrange } = _structureSetup._targetObj; _structureSetup,
_doViewportArrange,
_getViewportPaddingStyle,
_getPaddingInfo,
_setViewportOverflowScroll,
_setViewportOverflowAmount,
} = lifecycleHub;
const { _host, _viewport, _viewportArrange } = _structureSetup._targetObj;
const { _update: updateContentScrollSizeCache, _current: getCurrentContentScrollSizeCache } = createCache< const { _update: updateContentScrollSizeCache, _current: getCurrentContentScrollSizeCache } = createCache<
WH<number>, WH<number>,
ContentScrollSizeCacheContext ContentScrollSizeCacheContext
>((ctx) => fixScrollSizeRounding(ctx._viewportScrollSize, ctx._viewportOffsetSize, ctx._viewportRect), { _equal: equalWH }); >((ctx) => fixScrollSizeRounding(ctx._viewportScrollSize, ctx._viewportOffsetSize, ctx._viewportRect), { _equal: equalWH });
const { _update: updateOverflowAmountCache, _current: getCurrentOverflowAmountCache } = createCache<XY<number>, OverflowAmountCacheContext>( const { _update: updateOverflowAmountCache, _current: getCurrentOverflowAmountCache } = createCache<WH<number>, OverflowAmountCacheContext>(
(ctx) => ({ (ctx) => ({
x: Math.max(0, ctx._contentScrollSize.w - ctx._viewportSize.w), w: Math.max(0, ctx._contentScrollSize.w - ctx._viewportSize.w),
y: Math.max(0, ctx._contentScrollSize.h - ctx._viewportSize.h), h: Math.max(0, ctx._contentScrollSize.h - ctx._viewportSize.h),
}), }),
{ _equal: equalXY, _initialValue: { x: 0, y: 0 } } { _equal: equalWH, _initialValue: { w: 0, h: 0 } }
); );
/** /**
@@ -151,7 +157,7 @@ export const createOverflowLifecycle = (lifecycleHub: LifecycleHub): Lifecycle =
*/ */
const setViewportOverflowState = ( const setViewportOverflowState = (
showNativeOverlaidScrollbars: boolean, showNativeOverlaidScrollbars: boolean,
overflowAmount: XY<number>, overflowAmount: WH<number>,
overflow: OverflowOption, overflow: OverflowOption,
viewportStyleObj: StyleObject viewportStyleObj: StyleObject
): ViewportOverflowState => { ): ViewportOverflowState => {
@@ -173,8 +179,8 @@ export const createOverflowLifecycle = (lifecycleHub: LifecycleHub): Lifecycle =
_behavior: behaviorIsVisibleHidden ? 'hidden' : 'scroll', _behavior: behaviorIsVisibleHidden ? 'hidden' : 'scroll',
}; };
}; };
const { _visible: xVisible, _behavior: xVisibleBehavior } = setPartialStylePerAxis(true, overflowAmount!.x, overflow.x, viewportStyleObj); const { _visible: xVisible, _behavior: xVisibleBehavior } = setPartialStylePerAxis(true, overflowAmount!.w, overflow.x, viewportStyleObj);
const { _visible: yVisible, _behavior: yVisibleBehavior } = setPartialStylePerAxis(false, overflowAmount!.y, overflow.y, viewportStyleObj); const { _visible: yVisible, _behavior: yVisibleBehavior } = setPartialStylePerAxis(false, overflowAmount!.h, overflow.y, viewportStyleObj);
if (xVisible && !yVisible) { if (xVisible && !yVisible) {
viewportStyleObj.overflowX = xVisibleBehavior; viewportStyleObj.overflowX = xVisibleBehavior;
@@ -338,7 +344,7 @@ export const createOverflowLifecycle = (lifecycleHub: LifecycleHub): Lifecycle =
const showNativeOverlaidScrollbars = showNativeOverlaidScrollbarsOption && _nativeScrollbarIsOverlaid.x && _nativeScrollbarIsOverlaid.y; const showNativeOverlaidScrollbars = showNativeOverlaidScrollbarsOption && _nativeScrollbarIsOverlaid.x && _nativeScrollbarIsOverlaid.y;
const adjustFlexboxGlue = const adjustFlexboxGlue =
!_flexboxGlue && (_sizeChanged || _contentMutation || _hostMutation || showNativeOverlaidScrollbarsChanged || heightIntrinsicChanged); !_flexboxGlue && (_sizeChanged || _contentMutation || _hostMutation || showNativeOverlaidScrollbarsChanged || heightIntrinsicChanged);
let overflowAmuntCache: CacheValues<XY<number>> = getCurrentOverflowAmountCache(force); let overflowAmuntCache: CacheValues<WH<number>> = getCurrentOverflowAmountCache(force);
let contentScrollSizeCache: CacheValues<WH<number>> = getCurrentContentScrollSizeCache(force); let contentScrollSizeCache: CacheValues<WH<number>> = getCurrentContentScrollSizeCache(force);
let preMeasureViewportOverflowState: ViewportOverflowState | undefined; let preMeasureViewportOverflowState: ViewportOverflowState | undefined;
@@ -433,10 +439,12 @@ export const createOverflowLifecycle = (lifecycleHub: LifecycleHub): Lifecycle =
// TODO: Test without content // TODO: Test without content
// TODO: Test without padding // TODO: Test without padding
// TODO: overflow: visible on padding / host if overflow visible on both axis // TODO: overflow: visible on padding / host if overflow visible on both axis
// TODO: change lifecyclehub communication to single object & assign
style(_viewport, viewportStyle); style(_viewport, viewportStyle);
_setViewportOverflowScroll(viewportOverflowState._overflowScroll); _setViewportOverflowScroll(viewportOverflowState._overflowScroll);
_setViewportOverflowAmount(overflowAmount);
} }
}; };
}; };
@@ -14,6 +14,8 @@ export interface OverlayScrollbars {
options(newOptions?: PartialOptions<OSOptions>): OSOptions; options(newOptions?: PartialOptions<OSOptions>): OSOptions;
update(force?: boolean): void; update(force?: boolean): void;
state(): any;
} }
export const OverlayScrollbars: OverlayScrollbarsStatic = ( export const OverlayScrollbars: OverlayScrollbarsStatic = (
@@ -41,6 +43,7 @@ export const OverlayScrollbars: OverlayScrollbarsStatic = (
} }
return currentOptions; return currentOptions;
}, },
state: () => lifecycleHub._state(),
update(force?: boolean) { update(force?: boolean) {
lifecycleHub._update(null, force); lifecycleHub._update(null, force);
}, },
@@ -142,9 +142,6 @@ const checkMetrics = async () => {
}, },
}; };
//console.log('t', targetMetrics);
//console.log('c', comparisonMatrics);
should.equal(targetMetrics.offset.left, comparisonMetrics.offset.left, 'Offset left equality.'); should.equal(targetMetrics.offset.left, comparisonMetrics.offset.left, 'Offset left equality.');
should.equal(targetMetrics.offset.top, comparisonMetrics.offset.top, 'Offset top equality.'); should.equal(targetMetrics.offset.top, comparisonMetrics.offset.top, 'Offset top equality.');
@@ -154,6 +151,9 @@ const checkMetrics = async () => {
should.equal(targetMetrics.scroll.width, comparisonMetrics.scroll.width, 'Scroll width equality.'); should.equal(targetMetrics.scroll.width, comparisonMetrics.scroll.width, 'Scroll width equality.');
should.equal(targetMetrics.scroll.height, comparisonMetrics.scroll.height, 'Scroll height equality.'); should.equal(targetMetrics.scroll.height, comparisonMetrics.scroll.height, 'Scroll height equality.');
should.equal(osInstance.state()._overflowAmount.w, comparisonMetrics.scroll.width, 'Overflow amount width equality.');
should.equal(osInstance.state()._overflowAmount.h, comparisonMetrics.scroll.height, 'Overflow amount height equality.');
if (targetMetrics.scroll.width > 0) { if (targetMetrics.scroll.width > 0) {
should.equal(style(targetViewport!, 'overflowX'), 'scroll', 'Overflow-X should result in scroll.'); should.equal(style(targetViewport!, 'overflowX'), 'scroll', 'Overflow-X should result in scroll.');
} else { } else {
@@ -324,8 +324,8 @@ const overflowTest = async () => {
await checkMetrics(); await checkMetrics();
}; };
style(targetResize, { boxSizing: 'border-box' }); style(targetResize, { boxSizing: 'border-box', background: 'rgba(0, 0, 0, 0.1)' });
style(comparisonResize, { boxSizing: 'border-box' }); style(comparisonResize, { boxSizing: 'border-box', background: 'rgba(0, 0, 0, 0.1)' });
style(targetPercent, { display: 'none' }); style(targetPercent, { display: 'none' });
style(comparisonPercent, { display: 'none' }); style(comparisonPercent, { display: 'none' });
style(targetEnd, { display: 'none' }); style(targetEnd, { display: 'none' });