FuncUtils.ets 1.74 KB
/**
 * 给函数的执行加上动画
 * @param fn:要在动画内执行的回调函数,通常fn里面会改变状态变量
 * @param duration:动画持续时长
 * @param curve:动画区线
 */
import { ImageViewerConstants } from '../constants/ImageViewerConstants';
import measure from '@ohos.measure'

export function runWithAnimation(
  fn: Function,
  duration: number = ImageViewerConstants.ANIMATE_DURATION,
  curve: Curve = Curve.Smooth): void {
  animateTo({ duration: duration, curve: curve }, () => {
    fn();
  })
}

export function getNotesContentWidth(
  newTags?: string,
  objectType?: number | string,
  objectLevel?: number | string,
  fontSize?: string | number | Resource
) {
  let textContent: string = returnTypeTitleFn(newTags, objectType, objectLevel)
  let px = 0
  const paddingRightAndLeft = vp2px(8)
  const marginRight = vp2px(6);

  if(objectType == '5'){
    textContent = returnTypeTitleFn('', objectType, objectLevel)
  } else {
    textContent = returnTypeTitleFn(newTags)
  }

  if (textContent) {
    px = measure.measureText({
      textContent: textContent,
      fontSize: fontSize || $r('app.float.font_size_11')
    })
  }

  if (px) {
    px = px + paddingRightAndLeft + marginRight
  }

  return px2vp(px)
}

/**
 * 根据接口返回字段显示不同的内容
 * @param newTags
 * @param objectType
 * @param objectLevel
 * @returns
 */
export function returnTypeTitleFn(
  newTags?: string,
  objectType?: number | string,
  objectLevel?: number | string
): string {
  if (newTags) {
    return newTags
  }
  if (objectType == 5) {
    if (objectLevel == 24) {
      return '调查'
    }
    return '专题'
  }
  if (objectType == 10) {
    return 'H5'
  }
  if (objectType == 8) {
    return '文章'
  }
  return ''
}