Blame view

node_modules/we-cropper/src/prepare.js 1.23 KB
ce4c83ff   wxy   初始提交
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
  import {
    getDevice
  } from './utils/helper'
  
  const {
    windowWidth
  } = getDevice()
  
  export default function prepare () {
    const self = this
  
    // v1.4.0 版本中将不再自动绑定we-cropper实例
    self.attachPage = () => {
      const pages = getCurrentPages()
      // 获取到当前page上下文
      const pageContext = pages[pages.length - 1]
      // 把this依附在Page上下文的wecropper属性上,便于在page钩子函数中访问
      Object.defineProperty(pageContext, 'wecropper', {
        get () {
          console.warn(
            'Instance will not be automatically bound to the page after v1.4.0\n\n' +
            'Please use a custom instance name instead\n\n' +
            'Example: \n' +
            'this.mycropper = new WeCropper(options)\n\n' +
            '// ...\n' +
            'this.mycropper.getCropperImage()'
          )
          return self
        }
      })
    }
  
    self.createCtx = () => {
      const {
        id,
        targetId
      } = self
  
      if (id) {
        self.ctx = self.ctx || wx.createCanvasContext(id)
        self.targetCtx = self.targetCtx || wx.createCanvasContext(targetId)
      } else {
        console.error(`constructor: create canvas context failed, 'id' must be valuable`)
      }
    }
  
    self.deviceRadio = windowWidth / 750
  }