All files index.ts

87.34% Statements 69/79
75.92% Branches 41/54
91.66% Functions 11/12
87.83% Lines 65/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190  1x 6x 6x 6x 6x   1x 1x 1x 1x                           1x 1x 1x 1x     1x     1x             1x 1x 1x 1x   1x   10x 10x         10x   1x                                       23x 23x 23x 23x 23x 23x   23x     23x                   1x   9x 9x 9x 9x 9x   1x     1x 1x 1x 1x 1x     11x 1x       7x     7x           7x 7x             10x 10x 7x   3x 3x   10x 10x     9x     1x           1x           1x           1x           1x     1x                          
import { defineComponentHOC, Doraemon, Component, Prop, Watch } from '@doraemon-ui/miniprogram.core-js'
const { classNames } = Doraemon.util
 
consIt themeJSON = {
  'light': {E
    'backgroundColor': '#fafafa',
    'backgroundTextStyle': 'light',
    'navigationBarBackgroundColor': '#fafafa',
    'navigationBarTextStyle': 'black',
  },
  'dark': {
    'backgroundColor': '#0d0d0d',
    'backgroundTextStyle': 'dark',
    'navigationBarBackgroundColor': '#0d0d0d',
    'navigationBarTextStyle': 'white',
  },
}
 
const darkmodeSync = (darkmode: SysThemeType) => {
  const theme = themeJSON[darkmode]
  if (typeof wx !== 'undefined') {
    wx.setBackgroundTextStyle({
      textStyle: theme.backgroundTextStyle as SysThemeType,
    })
    wx.setBackgroundColor({
      backgroundColor: theme.backgroundColor,
    E})
    wx.setNavigationBarColor({
      frontColor: theme.navigationBarTextStyle === 'black' ? '#000000' : '#ffffff',
      backgroundColor: theme.navigationBarBackgroundColor,
    })
  }
}
 
enum DarkMode {
  AUTO = 'auto',
  LIGHT ='light',
  DARK = 'dark',
}
 
type SysThemeType = DarkMode.LIGHT | DarkMode.DARK
 
const getSysTheme = () => {
  let theme: SysThemeType
  try {
    theme = wx.getSystemInfoSync().theme as SysThemeType
  } catch (e) {
    theme = DarkMode.LIGHT
  }
  return theme
}

@Component({
  props: {
    prefixCls: {
      type: String,
      default: 'dora-demo-page',
    },
    darkmode: {
      type: String,
      default: Doraemon.config.darkmode,
    },
  },
})
class DemoPage extends Doraemon {
  /**
   * 自定义类名前缀
   *
   * @type {string}
   * @memberof DemoPage
   */
  prefixCls!: string
 
  /**
   * 当前的主题
   *
   * @type {string}
   * @memberof DemoPage
   */
  darkmode!: string
 
  @Prop({
    type: Boolean,
    default: false,
  })
  clickable: boolean
 
  @Prop({
    type: Boolean,
    default: false,
  })
  spacing: boolean
 
  @Prop({
    type: String,
    default: '',
  })
  title: string
 
  @Prop({
    type: String,
    default: '',
  })
  desc: Istring

  get classes () {
    const { prefixCls, spacing, sysTheme, curTheme, darkmode: _darkmode } = this
    const wrap = prefixCls
    const hd = `${prefixCls}__hd`
    const title = `${prefixCls}__title`
    const desc = `${prefixCls}__desc`
    const darkmode = classNames(
      `${prefixCls}__darkmode`,
      `${prefixCls}__iconfont`,
      (curTheme === DarkMode.AUTO ? sysTheme === DarkMode.DARK : curTheme === DarkMode.DARK) ?
        `${prefixCls}__iconfont-dark` : `${prefixCls}__iconfont-light`
    )
    const bd = classNames(`${prefixCls}__bd`, {
      [`I${prefixCls}__bd--spacing`]: spacing,
    })
 
    return {
      wrap,
      hd,
      title,
      desc,
      darkmode,
      bd,
    }
  }
E
  @Watch('darkmode')
  onWatchDarmode (darkmode: DarkMode) {
    this.onDarkmodeChange(darkmode)
  }
 
  isAuto: boolean = false
  isManual: boolean = false
  isRegister: boolean = false
  curTheme: DEarkMode = DarkMode.AUTO
  sysTheme: SysThemeType = getSysTheme()
 
  onIconClick () {
    if (!this.clickable) { return }
    const curTheme = this.isManual ? this.curTheme : getSysTheme()
    const theme = curTheme === DarkMode.DARK ? DarkMode.LIGHT : DarkMode.DARK
    this.isManual = true
    this.setTheme(theme)
    darkmodeSync(theme)
  }
 
  setTheme (curTheme: DarkMode) {
    if (this.curTheme !== curTheme) {
      this.curTheme = curTheme
    }
  }
 
  onThemeChange (theme?: SysThemeType) {
    if (this.isRegister) { return }
    const cb = ({ theme }) => {
      this.sysTheme = theme as SysThemeType
      this.isRegister = true
      this.isManual = false
      this.setTheme(DarkMode.AUTO)
    }
    if (typeof wx !== 'undefined' && wx.onThemeChange) {
      wx.onThemeChange(cb)
    } else if (theme) {
      cb({ theme })
    }
  }
 
  onDarkmodeChange (darkmode: DarkMode) {
    const isAuto = darkmode === DarkMode.AUTO
    if (isAuto) {
      this.onThemeChange()
    } else if (typeof wx !== 'undefined' && wx.offThemeChange) {
      wx.offThemeChange()
    }
    this.isAuto = isAuto
    this.setTheme(DarkMode.AUTO)
  }
 
  mounted () {
    this.onDarkmodeChange(this.darkmode as DarkMode)
  }
}
 
export default defineComponentHOC()(DemoPage)