All files index.ts

93.5% Statements 72/77
80.43% Branches 37/46
100% Functions 12/12
94.44% Lines 68/72

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  1x 6x 6x 6x 6x   1x 1x 1x 1x                           1x 1x 1x     1x     1x           1x 1x 1x 1x   1x   10x 10x         10x   1x                                       22x 22x 22x 22x 22x 22x   22x     22x                   1x   9x 9x 9x 9x 9x   1x     1x 1x 1x 1x 1x     9x 1x       8x     8x 1x 1x 1x 1x   8x     8x 1x       10x 10x 7x     3x   7x 7x     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': '#ffffff',
    'backgroundTextStyle': 'light',
    'navigationBarBackgroundColor': '#ffffff',
    'navigationBarTextStyle': 'black',
  },
  'dark': {
    'backgroundColor': '#000000',
    'backgroundTextStyle': 'dark',
    'navigationBarBackgroundColor': '#000000',
    'navigationBarTextStyle': 'white',
  },
}
 
const darkmodeSync = (darkmode: SysThemeType) => {
  const theme = themeJSON[darkmode]
  wx.setBackgroundTextStyle({
    textStyle: theme.backgroundTextStyle as SysThemeType,
  })
  wx.setBackgroundColor({
    backgroundColor: theme.backgroundColor,
  })
  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,
      bdI,
    }
  }
 
  @Watch('darkmode')
  onWatchDarmode (darkmode: DarkMode) {
    this.onDarkmodeChange(darkmode)
  }
 
  isAuto: boolean = false
  isManual: boolean = false
  isRegister: boolean = false
  curTheme: DarkMode = 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 (wx.onThemeChange) {
      wx.onThemeChange(cb)
    } else if (theme) {
      cb({ theme })
    }
  }
 
  onDarkmodeChange (darkmode: DarkMode) {
    const isAuto = darkmode === DarkMode.AUTO
    if (isAuto) {
      this.onThemeChange()
    } else {
      wx.offThemeChange()
    }
    this.isAuto = isAuto
    this.setTheme(DarkMode.AUTO)
  }
 
  mounted () {
    this.onDarkmodeChange(this.darkmode as DarkMode)
  }
}
 
export default defineComponentHOC()(DemoPage)