page.ts 2.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { isString } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
2 3 4 5 6 7 8
import {
  createScrollListener,
  CreateScrollListenerOptions,
  disableScrollListener,
  updateCssVar,
} from '@dcloudio/uni-core'
import { formatLog } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
9
import { PageCreateData } from '../../../PageAction'
fxy060608's avatar
fxy060608 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

import { createBuiltInComponent } from './components'

import { UniElement } from './elements/UniElement'
import { UniNode } from './elements/UniNode'

const elements = new Map<number, UniNode>()

export function $(id: number) {
  return elements.get(id) as UniElement
}

export function createElement(id: number, tag: string | number) {
  let element: UniNode
  if (isString(tag)) {
    element = new UniElement(id, document.createElement(tag))
  } else {
    element = createBuiltInComponent(tag, id)
  }
  elements.set(id, element)
  return element
}
fxy060608's avatar
fxy060608 已提交
32 33

export function onPageCreated() {}
fxy060608's avatar
fxy060608 已提交
34 35

export function onPageCreate({
fxy060608's avatar
fxy060608 已提交
36
  css,
fxy060608's avatar
fxy060608 已提交
37 38 39 40 41 42 43 44 45
  route,
  disableScroll,
  onPageScroll,
  onPageReachBottom,
  onReachBottomDistance,
  statusbarHeight,
  windowTop,
  windowBottom,
}: PageCreateData) {
fxy060608's avatar
fxy060608 已提交
46 47 48 49 50 51 52
  // 初始化页面容器元素
  initPageElement()

  if (css) {
    initPageCss(route)
  }

fxy060608's avatar
fxy060608 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65
  const pageId = plus.webview.currentWebview().id!
  ;(window as any).__id__ = pageId
  document.title = `${route}[${pageId}]`

  initCssVar(statusbarHeight, windowTop, windowBottom)

  if (disableScroll) {
    document.addEventListener('touchmove', disableScrollListener)
  } else if (onPageScroll || onPageReachBottom) {
    initPageScroll(onPageScroll, onPageReachBottom, onReachBottomDistance)
  }
}

fxy060608's avatar
fxy060608 已提交
66 67 68 69 70 71 72 73 74 75 76 77
function initPageElement() {
  createElement(0, 'div').$ = document.getElementById('app')!
}

function initPageCss(route: string) {
  const element = document.createElement('link')
  element.type = 'text/css'
  element.rel = 'stylesheet'
  element.href = route + '.css'
  document.head.appendChild(element)
}

fxy060608's avatar
fxy060608 已提交
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
function initCssVar(
  statusbarHeight: number,
  windowTop: number,
  windowBottom: number
) {
  const cssVars = {
    '--window-left': '0px',
    '--window-right': '0px',
    '--window-top': windowTop + 'px',
    '--window-bottom': windowBottom + 'px',
    '--status-bar-height': statusbarHeight + 'px',
  }
  if (__DEV__) {
    console.log(formatLog('initCssVar', cssVars))
  }
  updateCssVar(cssVars)
}

function initPageScroll(
  onPageScroll: boolean,
  onPageReachBottom: boolean,
  onReachBottomDistance: number
) {
  const opts: CreateScrollListenerOptions = {}
  if (onPageScroll) {
    opts.onPageScroll = (scrollTop) => {
      UniViewJSBridge.publishHandler('onPageScroll', { scrollTop })
    }
  }
  if (onPageReachBottom) {
    opts.onReachBottomDistance = onReachBottomDistance
    opts.onReachBottom = () => UniViewJSBridge.publishHandler('onReachBottom')
  }
  // 避免监听太早,直接触发了 scroll
  requestAnimationFrame(() =>
    document.addEventListener('scroll', createScrollListener(opts))
  )
}