pageHead.tsx 14.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { computed, defineComponent, ref } from 'vue'
fxy060608's avatar
fxy060608 已提交
2
import { isArray } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
3
import { Input } from '@dcloudio/uni-components'
fxy060608's avatar
fxy060608 已提交
4
import { getRealPath } from '@dcloudio/uni-platform'
fxy060608's avatar
fxy060608 已提交
5 6 7 8 9
import {
  ICON_PATH_SEARCH,
  createSvgIconVNode,
  invokeHook,
} from '@dcloudio/uni-core'
fxy060608's avatar
fxy060608 已提交
10
import { usePageMeta } from '../../setup/provide'
fxy060608's avatar
fxy060608 已提交
11 12 13 14
import {
  usePageHeadTransparent,
  usePageHeadTransparentBackgroundColor,
} from './transparent'
fxy060608's avatar
fxy060608 已提交
15

fxy060608's avatar
fxy060608 已提交
16 17 18 19 20 21
import { updateStyle } from '../../../helpers/dom'

const ICON_PATH_BACK =
  'M21.781 7.844l-9.063 8.594 9.063 8.594q0.25 0.25 0.25 0.609t-0.25 0.578q-0.25 0.25-0.578 0.25t-0.578-0.25l-9.625-9.125q-0.156-0.125-0.203-0.297t-0.047-0.359q0-0.156 0.047-0.328t0.203-0.297l9.625-9.125q0.25-0.25 0.578-0.25t0.578 0.25q0.25 0.219 0.25 0.578t-0.25 0.578z'

const ICON_PATHS = {
fxy060608's avatar
fxy060608 已提交
22
  none: '',
fxy060608's avatar
fxy060608 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35
  forward:
    'M11 7.844q-0.25-0.219-0.25-0.578t0.25-0.578q0.219-0.25 0.563-0.25t0.563 0.25l9.656 9.125q0.125 0.125 0.188 0.297t0.063 0.328q0 0.188-0.063 0.359t-0.188 0.297l-9.656 9.125q-0.219 0.25-0.563 0.25t-0.563-0.25q-0.25-0.219-0.25-0.578t0.25-0.609l9.063-8.594-9.063-8.594z',
  back: ICON_PATH_BACK,
  share:
    'M26.563 24.844q0 0.125-0.109 0.234t-0.234 0.109h-17.938q-0.125 0-0.219-0.109t-0.094-0.234v-13.25q0-0.156 0.094-0.25t0.219-0.094h5.5v-1.531h-6q-0.531 0-0.906 0.391t-0.375 0.922v14.375q0 0.531 0.375 0.922t0.906 0.391h18.969q0.531 0 0.891-0.391t0.359-0.953v-5.156h-1.438v4.625zM29.813 10.969l-5.125-5.375-1.031 1.094 3.438 3.594-3.719 0.031q-2.313 0.188-4.344 1.125t-3.578 2.422-2.5 3.453-1.109 4.188l-0.031 0.25h1.469v-0.219q0.156-1.875 1-3.594t2.25-3.063 3.234-2.125 3.828-0.906l0.188-0.031 3.313-0.031-3.438 3.625 1.031 1.063 5.125-5.375-0.031-0.063 0.031-0.063z',
  favorite:
    'M27.594 13.375q-0.063-0.188-0.219-0.313t-0.344-0.156l-7.094-0.969-3.219-6.406q-0.094-0.188-0.25-0.281t-0.375-0.094q-0.188 0-0.344 0.094t-0.25 0.281l-3.125 6.438-7.094 1.094q-0.188 0.031-0.344 0.156t-0.219 0.313q-0.031 0.188 0.016 0.375t0.172 0.313l5.156 4.969-1.156 7.063q-0.031 0.188 0.047 0.375t0.234 0.313q0.094 0.063 0.188 0.094t0.219 0.031q0.063 0 0.141-0.031t0.172-0.063l6.313-3.375 6.375 3.313q0.063 0.031 0.141 0.047t0.172 0.016q0.188 0 0.344-0.094t0.25-0.281q0.063-0.094 0.078-0.234t-0.016-0.234q0-0.031 0-0.063l-1.25-6.938 5.094-5.031q0.156-0.156 0.203-0.344t-0.016-0.375zM11.469 19.063q0.031-0.188-0.016-0.344t-0.172-0.281l-4.406-4.25 6.063-0.906q0.156-0.031 0.297-0.125t0.203-0.25l2.688-5.531 2.75 5.5q0.063 0.156 0.203 0.25t0.297 0.125l6.094 0.844-4.375 4.281q-0.125 0.125-0.172 0.297t-0.016 0.328l1.063 6.031-5.438-2.813q-0.156-0.094-0.328-0.078t-0.297 0.078l-5.438 2.875 1-6.031z',
  home:
    'M23.719 16.5q-0.313 0-0.531 0.219t-0.219 0.5v7.063q0 0.219-0.172 0.391t-0.391 0.172h-12.344q-0.25 0-0.422-0.172t-0.172-0.391v-7.063q0-0.281-0.219-0.5t-0.531-0.219q-0.281 0-0.516 0.219t-0.234 0.5v7.063q0.031 0.844 0.625 1.453t1.438 0.609h12.375q0.844 0 1.453-0.609t0.609-1.453v-7.063q0-0.125-0.063-0.266t-0.156-0.234q-0.094-0.125-0.234-0.172t-0.297-0.047zM26.5 14.875l-8.813-8.813q-0.313-0.313-0.688-0.453t-0.781-0.141-0.781 0.141-0.656 0.422l-8.813 8.844q-0.188 0.219-0.188 0.516t0.219 0.484q0.094 0.125 0.234 0.172t0.297 0.047q0.125 0 0.25-0.047t0.25-0.141l8.781-8.781q0.156-0.156 0.406-0.156t0.406 0.156l8.813 8.781q0.219 0.188 0.516 0.188t0.516-0.219q0.188-0.188 0.203-0.484t-0.172-0.516z',
  menu:
    'M8.938 18.313q0.875 0 1.484-0.609t0.609-1.453-0.609-1.453-1.484-0.609q-0.844 0-1.453 0.609t-0.609 1.453 0.609 1.453 1.453 0.609zM16.188 18.313q0.875 0 1.484-0.609t0.609-1.453-0.609-1.453-1.484-0.609q-0.844 0-1.453 0.609t-0.609 1.453 0.609 1.453 1.453 0.609zM23.469 18.313q0.844 0 1.453-0.609t0.609-1.453-0.609-1.453-1.453-0.609q-0.875 0-1.484 0.609t-0.609 1.453 0.609 1.453 1.484 0.609z',
  close:
    'M17.25 16.156l7.375-7.313q0.281-0.281 0.281-0.641t-0.281-0.641q-0.25-0.25-0.625-0.25t-0.625 0.25l-7.375 7.344-7.313-7.344q-0.25-0.25-0.625-0.25t-0.625 0.25q-0.281 0.25-0.281 0.625t0.281 0.625l7.313 7.344-7.375 7.344q-0.281 0.25-0.281 0.625t0.281 0.625q0.125 0.125 0.281 0.188t0.344 0.063q0.156 0 0.328-0.063t0.297-0.188l7.375-7.344 7.375 7.406q0.125 0.156 0.297 0.219t0.328 0.063q0.188 0 0.344-0.078t0.281-0.203q0.281-0.25 0.281-0.609t-0.281-0.641l-7.375-7.406z',
fxy060608's avatar
fxy060608 已提交
36 37 38
}

export default /*#__PURE__*/ defineComponent({
fxy060608's avatar
fxy060608 已提交
39 40
  name: 'PageHead',
  setup() {
fxy060608's avatar
fxy060608 已提交
41
    const headRef = ref(null)
fxy060608's avatar
fxy060608 已提交
42 43
    const pageMeta = usePageMeta()
    const navigationBar = pageMeta.navigationBar
fxy060608's avatar
fxy060608 已提交
44
    // UniServiceJSBridge.emit('onNavigationBarChange', navigationBar.titleText)
fxy060608's avatar
fxy060608 已提交
45
    const { clazz, style } = usePageHead(navigationBar)
46

fxy060608's avatar
fxy060608 已提交
47
    const buttons = (__UNI_FEATURE_NAVIGATIONBAR_BUTTONS__ &&
fxy060608's avatar
fxy060608 已提交
48
      usePageHeadButtons(navigationBar)) as PageHeadButtons
49

fxy060608's avatar
fxy060608 已提交
50
    const searchInput = (__UNI_FEATURE_NAVIGATIONBAR_SEARCHINPUT__ &&
fxy060608's avatar
fxy060608 已提交
51 52
      navigationBar.searchInput &&
      usePageHeadSearchInput(pageMeta)) as PageHeadSearchInput
53

fxy060608's avatar
fxy060608 已提交
54
    __UNI_FEATURE_NAVIGATIONBAR_TRANSPARENT__ &&
55 56 57
      navigationBar.type === 'transparent' &&
      usePageHeadTransparent(headRef, pageMeta)

fxy060608's avatar
fxy060608 已提交
58 59
    return () => {
      // 单页面无需back按钮
fxy060608's avatar
fxy060608 已提交
60
      const backButtonTsx = __UNI_FEATURE_PAGES__
fxy060608's avatar
fxy060608 已提交
61
        ? createBackButtonTsx(pageMeta)
fxy060608's avatar
fxy060608 已提交
62
        : null
fxy060608's avatar
fxy060608 已提交
63 64
      const leftButtonsTsx = __UNI_FEATURE_NAVIGATIONBAR_BUTTONS__
        ? createButtonsTsx(buttons.left)
fxy060608's avatar
fxy060608 已提交
65
        : []
fxy060608's avatar
fxy060608 已提交
66 67
      const rightButtonsTsx = __UNI_FEATURE_NAVIGATIONBAR_BUTTONS__
        ? createButtonsTsx(buttons.right)
fxy060608's avatar
fxy060608 已提交
68
        : []
fxy060608's avatar
fxy060608 已提交
69 70 71 72 73 74 75 76 77
      const type = navigationBar.type || 'default'
      const placeholderTsx = type !== 'transparent' && type !== 'float' && (
        <div
          class={{
            'uni-placeholder': true,
            'uni-placeholder-titlePenetrate': navigationBar.titlePenetrate,
          }}
        ></div>
      )
fxy060608's avatar
fxy060608 已提交
78
      return (
fxy060608's avatar
fxy060608 已提交
79
        <uni-page-head uni-page-head-type={type}>
fxy060608's avatar
fxy060608 已提交
80
          <div ref={headRef} class={clazz.value} style={style.value}>
fxy060608's avatar
fxy060608 已提交
81
            <div class="uni-page-head-hd">
fxy060608's avatar
fxy060608 已提交
82 83
              {backButtonTsx}
              {...leftButtonsTsx}
fxy060608's avatar
fxy060608 已提交
84
            </div>
fxy060608's avatar
fxy060608 已提交
85 86
            {createPageHeadBdTsx(navigationBar, searchInput)}
            <div class="uni-page-head-ft">{...rightButtonsTsx}</div>
fxy060608's avatar
fxy060608 已提交
87
          </div>
fxy060608's avatar
fxy060608 已提交
88
          {placeholderTsx}
fxy060608's avatar
fxy060608 已提交
89 90 91
        </uni-page-head>
      )
    }
fxy060608's avatar
fxy060608 已提交
92 93 94
  },
})

fxy060608's avatar
fxy060608 已提交
95 96 97
function createBackButtonTsx(pageMeta: UniApp.PageRouteMeta) {
  const { navigationBar, isQuit } = pageMeta
  if (navigationBar.backButton && !isQuit) {
fxy060608's avatar
fxy060608 已提交
98
    return (
fxy060608's avatar
fxy060608 已提交
99
      <div class="uni-page-head-btn" onClick={onPageHeadBackButton}>
fxy060608's avatar
fxy060608 已提交
100 101 102 103 104 105 106
        {createSvgIconVNode(
          ICON_PATH_BACK,
          navigationBar.type === 'transparent'
            ? '#fff'
            : navigationBar.titleColor!,
          27
        )}
fxy060608's avatar
fxy060608 已提交
107 108 109 110 111
      </div>
    )
  }
}

fxy060608's avatar
fxy060608 已提交
112
function createButtonsTsx(btns: PageHeadButton[]) {
fxy060608's avatar
fxy060608 已提交
113
  return btns.map(
fxy060608's avatar
fxy060608 已提交
114 115 116 117
    (
      { btnClass, btnStyle, btnText, btnIconPath, badgeText, iconStyle },
      index
    ) => {
fxy060608's avatar
fxy060608 已提交
118 119 120 121 122 123 124
      return (
        <div
          key={index}
          class={btnClass}
          style={btnStyle}
          badge-text={badgeText}
        >
fxy060608's avatar
fxy060608 已提交
125 126 127 128 129
          {btnIconPath ? (
            createSvgIconVNode(btnIconPath, iconStyle.color, iconStyle.fontSize)
          ) : (
            <i class="uni-btn-icon" style={iconStyle} v-html={btnText} />
          )}
fxy060608's avatar
fxy060608 已提交
130
        </div>
fxy060608's avatar
fxy060608 已提交
131 132 133
      )
    }
  )
fxy060608's avatar
fxy060608 已提交
134 135
}

fxy060608's avatar
fxy060608 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149
function createPageHeadBdTsx(
  navigationBar: UniApp.PageNavigationBar,
  searchInput: PageHeadSearchInput
) {
  if (
    !__UNI_FEATURE_NAVIGATIONBAR_SEARCHINPUT__ ||
    !navigationBar.searchInput
  ) {
    return createPageHeadTitleTextTsx(navigationBar)
  }
  return createPageHeadSearchInputTsx(navigationBar, searchInput)
}

function createPageHeadTitleTextTsx({
fxy060608's avatar
fxy060608 已提交
150
  type,
fxy060608's avatar
fxy060608 已提交
151
  loading,
fxy060608's avatar
fxy060608 已提交
152
  titleSize,
fxy060608's avatar
fxy060608 已提交
153 154 155 156 157 158
  titleText,
  titleImage,
}: UniApp.PageNavigationBar) {
  return (
    <div class="uni-page-head-bd">
      <div
fxy060608's avatar
fxy060608 已提交
159
        style={{ fontSize: titleSize, opacity: type === 'transparent' ? 0 : 1 }}
fxy060608's avatar
fxy060608 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
        class="uni-page-head__title"
      >
        {loading ? (
          <i class="uni-loading" />
        ) : titleImage ? (
          <img src={titleImage} class="uni-page-head__title_image" />
        ) : (
          titleText
        )}
      </div>
    </div>
  )
}

function createPageHeadSearchInputTsx(
  navigationBar: UniApp.PageNavigationBar,
fxy060608's avatar
fxy060608 已提交
176 177 178 179 180 181 182 183 184 185
  {
    text,
    focus,
    composing,
    onBlur,
    onFocus,
    onInput,
    onKeyup,
    onClick,
  }: PageHeadSearchInput
fxy060608's avatar
fxy060608 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
) {
  const {
    color,
    align,
    autoFocus,
    disabled,
    borderRadius,
    backgroundColor,
    placeholder,
    placeholderColor,
  } = navigationBar.searchInput!
  const searchStyle = {
    borderRadius,
    backgroundColor,
  }
  const placeholderClass = [
    'uni-page-head-search-placeholder',
    `uni-page-head-search-placeholder-${
      focus.value || text.value ? 'left' : align
    }`,
  ]
  return (
    <div class="uni-page-head-search" style={searchStyle}>
      <div style={{ color: placeholderColor }} class={placeholderClass}>
        <div class="uni-page-head-search-icon">
          {createSvgIconVNode(ICON_PATH_SEARCH, placeholderColor, 20)}
        </div>
        {text.value || composing.value ? '' : placeholder}
      </div>
fxy060608's avatar
fxy060608 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
      {disabled ? (
        <Input
          disabled={true}
          style={{ color }}
          placeholder-style={{ color: placeholderColor }}
          class="uni-page-head-search-input"
          confirm-type="search"
          onClick={onClick}
        />
      ) : (
        <Input
          focus={autoFocus}
          style={{ color }}
          placeholder-style={{ color: placeholderColor }}
          class="uni-page-head-search-input"
          confirm-type="search"
          onFocus={onFocus}
          onBlur={onBlur}
          onInput={onInput}
          onKeyup={onKeyup}
        />
      )}
fxy060608's avatar
fxy060608 已提交
237 238 239 240
    </div>
  )
}

fxy060608's avatar
fxy060608 已提交
241 242 243 244 245 246 247 248 249 250 251 252
function onPageHeadBackButton() {
  if (getCurrentPages().length === 1) {
    uni.reLaunch({
      url: '/',
    })
  } else {
    ;(uni.navigateBack as Function)({
      from: 'backbutton',
    })
  }
}

fxy060608's avatar
fxy060608 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
function usePageHead(navigationBar: UniApp.PageNavigationBar) {
  const clazz = computed(() => {
    const { type, titlePenetrate, shadowColorType } = navigationBar
    const clazz: Record<string, boolean> = {
      'uni-page-head': true,
      'uni-page-head-transparent': type === 'transparent',
      'uni-page-head-titlePenetrate': titlePenetrate === 'YES',
      'uni-page-head-shadow': !!shadowColorType,
    }
    if (shadowColorType) {
      clazz[`uni-page-head-shadow-${shadowColorType}`] = true
    }
    return clazz
  })
  const style = computed(() => {
fxy060608's avatar
fxy060608 已提交
268 269 270 271 272
    const backgroundColor =
      __UNI_FEATURE_NAVIGATIONBAR_TRANSPARENT__ &&
      navigationBar.type === 'transparent'
        ? usePageHeadTransparentBackgroundColor(navigationBar.backgroundColor!)
        : navigationBar.backgroundColor
fxy060608's avatar
fxy060608 已提交
273
    return {
fxy060608's avatar
fxy060608 已提交
274 275
      backgroundColor,
      color: navigationBar.titleColor,
fxy060608's avatar
fxy060608 已提交
276 277 278 279 280 281 282 283 284
      transitionDuration: navigationBar.duration,
      transitionTimingFunction: navigationBar.timingFunc,
    }
  })
  return {
    clazz,
    style,
  }
}
fxy060608's avatar
fxy060608 已提交
285 286 287 288 289

interface PageHeadButton {
  btnClass: UniApp.ClassObj
  btnStyle: UniApp.StyleObj
  btnText: string
fxy060608's avatar
fxy060608 已提交
290
  btnIconPath?: string
fxy060608's avatar
fxy060608 已提交
291 292 293 294 295 296 297 298 299
  badgeText?: string
  iconStyle: UniApp.StyleObj
}

interface PageHeadButtons {
  left: PageHeadButton[]
  right: PageHeadButton[]
}

fxy060608's avatar
fxy060608 已提交
300
function usePageHeadButtons(navigationBar: UniApp.PageNavigationBar) {
fxy060608's avatar
fxy060608 已提交
301 302 303
  const left: PageHeadButton[] = []
  const right: PageHeadButton[] = []
  const { buttons } = navigationBar
fxy060608's avatar
fxy060608 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
  if (isArray(buttons)) {
    const { type } = navigationBar
    const isTransparent = type === 'transparent'
    const fonts = Object.create(null)
    buttons.forEach((btn) => {
      if (btn.fontSrc && !btn.fontFamily) {
        const fontSrc = getRealPath(btn.fontSrc)
        let fontFamily = fonts[fontSrc]
        if (!fontFamily) {
          fontFamily = `font${Date.now()}`
          fonts[fontSrc] = fontFamily
          updateStyle(
            'uni-btn-' + fontFamily,
            `@font-face{font-family: "${fontFamily}";src: url("${fontSrc}") format("truetype")}`
          )
        }
        btn.fontFamily = fontFamily
      }
      const pageHeadBtn = usePageHeadButton(btn, isTransparent)
      if (btn.float === 'left') {
        left.push(pageHeadBtn)
      } else {
        right.push(pageHeadBtn)
      }
    })
fxy060608's avatar
fxy060608 已提交
329
  }
fxy060608's avatar
fxy060608 已提交
330
  return { left, right }
fxy060608's avatar
fxy060608 已提交
331 332 333 334 335 336
}

function usePageHeadButton(
  btn: UniApp.PageNavigationBarButton,
  isTransparent: boolean
) {
fxy060608's avatar
fxy060608 已提交
337 338 339 340 341 342 343 344
  const iconStyle: UniApp.StyleObj = {
    color: btn.color,
    fontSize: btn.fontSize,
    fontWeight: btn.fontWeight,
  }
  if (btn.fontFamily) {
    iconStyle.fontFamily = btn.fontFamily
  }
fxy060608's avatar
fxy060608 已提交
345 346 347 348 349 350 351 352 353 354 355 356
  return {
    btnClass: {
      // 类似这样的大量重复的字符串,会在gzip时压缩大小,无需在代码层考虑优化相同字符串
      'uni-page-head-btn': true,
      'uni-page-head-btn-red-dot': !!(btn.redDot || btn.badgeText),
      'uni-page-head-btn-select': !!btn.select,
    },
    btnStyle: {
      backgroundColor: isTransparent ? btn.background : 'transparent',
      width: btn.width,
    },
    btnText:
fxy060608's avatar
fxy060608 已提交
357 358
      btn.fontSrc && btn.fontFamily ? btn.text.replace('\\u', '&#x') : btn.text,
    btnIconPath: ICON_PATHS[btn.type],
fxy060608's avatar
fxy060608 已提交
359
    badgeText: btn.badgeText,
fxy060608's avatar
fxy060608 已提交
360 361 362 363
    iconStyle,
  }
}

fxy060608's avatar
fxy060608 已提交
364
type PageHeadSearchInput = ReturnType<typeof usePageHeadSearchInput>
fxy060608's avatar
fxy060608 已提交
365

fxy060608's avatar
fxy060608 已提交
366 367 368 369
function usePageHeadSearchInput({
  id,
  navigationBar: { searchInput },
}: UniApp.PageRouteMeta) {
fxy060608's avatar
fxy060608 已提交
370 371 372
  const focus = ref(false)
  const text = ref('')
  const composing = ref(false)
fxy060608's avatar
fxy060608 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385
  const { disabled } = searchInput!
  if (disabled) {
    const onClick = () => {
      invokeHook(id, 'onNavigationBarSearchInputClicked')
    }
    return {
      focus,
      text,
      composing,
      onClick,
    }
  }
  const onFocus = () => {
fxy060608's avatar
fxy060608 已提交
386
    focus.value = true
fxy060608's avatar
fxy060608 已提交
387
    invokeHook(id, 'onNavigationBarSearchInputFocusChanged', { focus: true })
fxy060608's avatar
fxy060608 已提交
388
  }
fxy060608's avatar
fxy060608 已提交
389
  const onBlur = () => {
fxy060608's avatar
fxy060608 已提交
390
    focus.value = false
fxy060608's avatar
fxy060608 已提交
391
    invokeHook(id, 'onNavigationBarSearchInputFocusChanged', { focus: false })
fxy060608's avatar
fxy060608 已提交
392
  }
fxy060608's avatar
fxy060608 已提交
393
  const onInput = (evt: { detail: { value: string } }) => {
fxy060608's avatar
fxy060608 已提交
394
    text.value = evt.detail.value
fxy060608's avatar
fxy060608 已提交
395 396 397 398 399 400 401 402
    invokeHook(id, 'onNavigationBarSearchInputChanged', { text: text.value })
  }
  const onKeyup = (evt: KeyboardEvent) => {
    if (evt.key === 'Enter' || evt.keyCode === 13) {
      invokeHook(id, 'onNavigationBarSearchInputConfirmed', {
        text: text.value,
      })
    }
fxy060608's avatar
fxy060608 已提交
403 404 405 406 407 408 409 410
  }
  return {
    focus,
    text,
    composing,
    onFocus,
    onBlur,
    onInput,
fxy060608's avatar
fxy060608 已提交
411
    onKeyup,
fxy060608's avatar
fxy060608 已提交
412 413
  }
}