dom.ts 1.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { ComponentPublicInstance } from 'vue'
fxy060608's avatar
fxy060608 已提交
2
import { getRealRoute } from '@dcloudio/uni-core'
fxy060608's avatar
fxy060608 已提交
3

fxy060608's avatar
fxy060608 已提交
4 5 6 7
export function findElem(vm: ComponentPublicInstance) {
  return vm.$el
}

fxy060608's avatar
fxy060608 已提交
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
const SCHEME_RE = /^([a-z-]+:)?\/\//i
const DATA_RE = /^data:.*,.*/

function addBase(filePath: string) {
  const base = __uniConfig.router.base
  if (!base) {
    return filePath
  }
  if (base !== '/') {
    // 部分地址已经带了base(如被webpack处理过的资源自动带了publicPath)
    if (('/' + filePath).indexOf(base) === 0) {
      return '/' + filePath
    }
  }
  return base + filePath
}

export function getRealPath(filePath: string) {
  // 相对路径模式对静态资源路径特殊处理
  if (__uniConfig.router.base === './') {
    filePath = filePath.replace(/^\.\/static\//, '/static/')
  }
  if (filePath.indexOf('/') === 0) {
    if (filePath.indexOf('//') === 0) {
      filePath = 'https:' + filePath
    } else {
      return addBase(filePath.substr(1))
    }
  }
  // 网络资源或base64
  if (
    SCHEME_RE.test(filePath) ||
    DATA_RE.test(filePath) ||
    filePath.indexOf('blob:') === 0
  ) {
    return filePath
  }

  const pages = getCurrentPages()
  if (pages.length) {
    return addBase(
      getRealRoute(pages[pages.length - 1].$page.route, filePath).substr(1)
    )
  }

  return filePath
}