Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use compatible systemInfo util API #2975

Open
wants to merge 5 commits into
base: feat_v3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/packages/avatarcropper/avatarcropper.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Taro, { useReady, createSelectorQuery } from '@tarojs/taro'
import classNames from 'classnames'
import { Canvas, CommonEventFunction, View } from '@tarojs/components'
import { getWindowInfo } from '@/utils/get-system-info'
import { Button } from '@/packages/button/button.taro'
import { useConfig } from '@/packages/configprovider/index.taro'

Expand Down Expand Up @@ -118,7 +119,7 @@
const [moving, setMoving] = useState(false)
const [zooming, setZooming] = useState(false)

const systemInfo: Taro.getSystemInfoSync.Result = Taro.getSystemInfoSync()
const systemInfo = getWindowInfo()
Alex-huxiyang marked this conversation as resolved.
Show resolved Hide resolved
// 支付宝基础库2.7.0以上支持,需要开启支付宝小程序canvas2d
const showAlipayCanvas2D = useMemo(() => {
return (
Expand Down Expand Up @@ -173,7 +174,7 @@
...canvasAll,
cropperCanvasContext: Taro.createCanvasContext(canvasAll.canvasId),
})
}, [])

Check warning on line 177 in src/packages/avatarcropper/avatarcropper.taro.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'canvasAll'. Either include it or remove the dependency array. You can also do a functional update 'setCanvasAll(c => ...)' if you only need 'canvasAll' in the 'setCanvasAll' call

const touch = useTouch()

Expand All @@ -185,7 +186,7 @@
height,
borderRadius: shape === 'round' ? '50%' : '',
}
}, [pixelRatio, state.cropperWidth])

Check warning on line 189 in src/packages/avatarcropper/avatarcropper.taro.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useMemo has a missing dependency: 'shape'. Either include it or remove the dependency array

// 是否是横向
const isAngle = useMemo(() => {
Expand Down Expand Up @@ -267,7 +268,7 @@
ctx.scale(scale, scale)
ctx.drawImage(src as HTMLImageElement, x, y, width, height)
},
[drawImage, state]

Check warning on line 271 in src/packages/avatarcropper/avatarcropper.taro.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useCallback has missing dependencies: 'pixelRatio' and 'space'. Either include them or remove the dependency array
)

// web绘制
Expand All @@ -284,7 +285,7 @@
canvas.height = state.displayHeight
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
canvas2dDraw(ctx)
}, [canvas2dDraw])

Check warning on line 288 in src/packages/avatarcropper/avatarcropper.taro.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useCallback has missing dependencies: 'canvasAll.canvasId', 'state.displayHeight', and 'state.displayWidth'. Either include them or remove the dependency array

const alipayDraw = useCallback(() => {
const ctx = canvasAll.cropperCanvas.getContext(
Expand Down Expand Up @@ -341,7 +342,7 @@
ctx.scale(scale, scale)
ctx.drawImage(src as string, x, y, width, height)
ctx.draw()
}, [drawImage, state.scale, state.angle, state.moveX, state.moveY])

Check warning on line 345 in src/packages/avatarcropper/avatarcropper.taro.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useCallback has missing dependencies: 'alipayDraw', 'canvasAll', 'showAlipayCanvas2D', 'space', 'state', and 'webDraw'. Either include them or remove the dependency array

useEffect(() => {
if (Math.abs(state.moveX) > maxMoveX) {
Expand Down
4 changes: 2 additions & 2 deletions src/packages/drag/demos/taro/demo4.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react'
import { Drag, Button } from '@nutui/nutui-react-taro'
import { getSystemInfoSync } from '@tarojs/taro'
import { getWindowInfo } from '@/utils/get-system-info'

const Demo4 = () => {
const { screenWidth, windowHeight, screenHeight } = getSystemInfoSync()
const { screenWidth, windowHeight, screenHeight } = getWindowInfo()

console.log(windowHeight, screenHeight)

Expand Down
5 changes: 3 additions & 2 deletions src/packages/drag/drag.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { FunctionComponent, useState, useEffect, useRef } from 'react'
import { getSystemInfoSync, createSelectorQuery } from '@tarojs/taro'
import { createSelectorQuery } from '@tarojs/taro'
import { getWindowInfo } from '@/utils/get-system-info'
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
import { getRectByTaro } from '@/utils/get-rect-by-taro'
import { DragState } from './drag'
Expand Down Expand Up @@ -64,7 +65,7 @@ export const Drag: FunctionComponent<
const el = myDrag.current
if (el) {
const { top, left, bottom, right } = boundary
const { screenWidth, windowHeight } = getSystemInfoSync()
const { screenWidth, windowHeight } = getWindowInfo()

const { width, height } = await getRectByTaro(dragRef.current)
dragRef.current?.getBoundingClientRect()
Expand Down
11 changes: 3 additions & 8 deletions src/packages/lottie/mp.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import React, { useImperativeHandle, useRef } from 'react'
import {
createSelectorQuery,
getEnv,
getSystemInfoSync,
useReady,
useUnload,
} from '@tarojs/taro'
import { createSelectorQuery, getEnv, useReady, useUnload } from '@tarojs/taro'
import lottie from 'lottie-miniprogram'
import { getWindowInfo } from '@/utils/get-system-info'
import useUuid from '@/utils/use-uuid'
import { LottieProps } from './types'

Expand All @@ -31,7 +26,7 @@ export const Lottie = React.forwardRef((props: LottieProps, ref: any) => {
}
}
useImperativeHandle(ref, () => animation.current || {})
const dpr = useRef(getSystemInfoSync().pixelRatio)
const dpr = useRef(getWindowInfo().pixelRatio)
useReady(() => {
createSelectorQuery()
.select(`#${id}`)
Expand Down
9 changes: 5 additions & 4 deletions src/packages/menuitem/menuitem.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import React, {
useRef,
} from 'react'
import classNames from 'classnames'
import { getSystemInfoSync, usePageScroll } from '@tarojs/taro'
import { usePageScroll } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { CSSTransition } from 'react-transition-group'
import { Check } from '@nutui/icons-react-taro'
import { getWindowInfo } from '@/utils/get-system-info'
import { Overlay } from '@/packages/overlay/overlay.taro'
import { getRectByTaro } from '@/utils/get-rect-by-taro'
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
Expand Down Expand Up @@ -106,7 +107,7 @@ export const MenuItem = forwardRef((props: Partial<MenuItemProps>, ref) => {
getParentOffset()
}, [showPopup, getParentOffset])

const windowHeight = useMemo(() => getSystemInfoSync().windowHeight, [])
const windowHeight = useMemo(() => getWindowInfo().windowHeight, [])
const updateItemOffset = useCallback(() => {
if (!parent.lockScroll) return
const p = parent.menuRef.current
Expand Down Expand Up @@ -163,7 +164,7 @@ export const MenuItem = forwardRef((props: Partial<MenuItemProps>, ref) => {
height: 'initial',
}
: {
bottom: `${getSystemInfoSync().windowHeight - position.top}px`,
bottom: `${getWindowInfo().windowHeight - position.top}px`,
top: '0',
height: 'initial',
}
Expand All @@ -177,7 +178,7 @@ export const MenuItem = forwardRef((props: Partial<MenuItemProps>, ref) => {
}
}
return {
height: `${getSystemInfoSync().windowHeight - position.top}px`,
height: `${getWindowInfo().windowHeight - position.top}px`,
top: 'auto',
...isShow(),
}
Expand Down
4 changes: 3 additions & 1 deletion src/packages/pulltorefresh/pulltorefresh.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { sleep } from '@/utils/sleep'
import { BasicComponent, ComponentDefaults, Timeout } from '@/utils/typings'
import { PullToRefreshType } from './types'
import pxTransform from '@/utils/px-transform'
import { getDeviceInfo } from '@/utils/get-system-info'

export type PullStatus = 'pulling' | 'canRelease' | 'refreshing' | 'complete'

Expand Down Expand Up @@ -159,7 +160,8 @@ export const PullToRefresh: FunctionComponent<Partial<PullToRefreshProps>> = (
}
// 安卓微信小程序onTouchMove回调次数少导致下拉卡顿,增加动效会更顺畅
const isAndroidWeApp =
Taro.getSystemInfoSync().platform === 'android' && Taro.getEnv() === 'WEAPP'
getDeviceInfo().platform === 'android' && Taro.getEnv() === 'WEAPP'

const springStyles = {
height: pxTransform(height),
...(!pullingRef.current || isAndroidWeApp
Expand Down
10 changes: 3 additions & 7 deletions src/packages/sticky/sticky.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ import React, {
useState,
} from 'react'
import classNames from 'classnames'
import {
getEnv,
getSystemInfoSync,
PageScrollObject,
usePageScroll,
} from '@tarojs/taro'
import { getEnv, PageScrollObject, usePageScroll } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { getWindowInfo } from '@/utils/get-system-info'
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
import useWatch from '@/utils/use-watch'
import { getRectByTaro } from '@/utils/get-rect-by-taro'
Expand Down Expand Up @@ -129,7 +125,7 @@ export const Sticky: FunctionComponent<Partial<StickyProps>> = (props) => {
setFixed(threshold > curRootRect.top)
}
} else {
const windowHeight = getSystemInfoSync().windowHeight
const windowHeight = getWindowInfo().windowHeight
setFixed(windowHeight - threshold < curRootRect.bottom)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/packages/virtuallist/virtuallist.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import React, {
useState,
} from 'react'
import { ScrollView, View } from '@tarojs/components'
import { getSystemInfoSync } from '@tarojs/taro'
import classNames from 'classnames'
import { getWindowInfo } from '@/utils/get-system-info'
import { Data, PositionType } from './types'
import { initPositinoCache, updateItemSize } from './utils'
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
Expand Down Expand Up @@ -56,7 +56,7 @@ export const VirtualList: FunctionComponent<Partial<VirtualListProps>> = (
}

const clientHeight = useMemo(
() => getSystemInfoSync().windowHeight - 5 || 667,
() => getWindowInfo().windowHeight - 5 || 667,
[]
)

Expand Down
34 changes: 34 additions & 0 deletions src/utils/get-system-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Taro from '@tarojs/taro'

interface IDeviceInfo
extends Omit<Taro.getDeviceInfo.Result, 'deviceAbi' | 'CPUType'> {}

/**
* 获取设备基础信息,兼容新旧 API
* @returns {IDeviceInfo} 设备基础信息
*/
export function getDeviceInfo(): IDeviceInfo {
Alex-huxiyang marked this conversation as resolved.
Show resolved Hide resolved
return Taro.canIUse('getDeviceInfo')
? Taro.getDeviceInfo()
: Taro.getSystemInfoSync()
}

/**
* 获取窗口信息,兼容新旧 API
* @returns {Taro.getWindowInfo.Result} 窗口信息
*/
export function getWindowInfo(): Taro.getWindowInfo.Result {
return Taro.canIUse('getWindowInfo')
? Taro.getWindowInfo()
: Taro.getSystemInfoSync()
}

/**
* 获取应用基础信息,兼容新旧 API
* @returns {Taro.getAppBaseInfo.Result} 应用基础信息
*/
export function getAppBaseInfo(): Taro.getAppBaseInfo.Result {
return Taro.canIUse('getAppBaseInfo')
? Taro.getAppBaseInfo()
: Taro.getSystemInfoSync()
}
Loading