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

fxy060608's avatar
fxy060608 已提交
19
const ICON_PATHS = {
fxy060608's avatar
fxy060608 已提交
20
  none: '',
fxy060608's avatar
fxy060608 已提交
21 22 23 24 25 26 27
  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',
28 29
  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',
Q
qiang 已提交
30
  close: ICON_PATH_CLOSE,
fxy060608's avatar
fxy060608 已提交
31 32
}

33
export default /*#__PURE__*/ defineSystemComponent({
fxy060608's avatar
fxy060608 已提交
34 35
  name: 'PageHead',
  setup() {
fxy060608's avatar
fxy060608 已提交
36
    const headRef = ref(null)
fxy060608's avatar
fxy060608 已提交
37 38
    const pageMeta = usePageMeta()
    const navigationBar = pageMeta.navigationBar
fxy060608's avatar
fxy060608 已提交
39
    // UniServiceJSBridge.emit('onNavigationBarChange', navigationBar.titleText)
fxy060608's avatar
fxy060608 已提交
40
    const { clazz, style } = usePageHead(navigationBar)
41

fxy060608's avatar
fxy060608 已提交
42
    const buttons = (__UNI_FEATURE_NAVIGATIONBAR_BUTTONS__ &&
fxy060608's avatar
fxy060608 已提交
43
      usePageHeadButtons(pageMeta)) as PageHeadButtons
44

fxy060608's avatar
fxy060608 已提交
45
    const searchInput = (__UNI_FEATURE_NAVIGATIONBAR_SEARCHINPUT__ &&
fxy060608's avatar
fxy060608 已提交
46 47
      navigationBar.searchInput &&
      usePageHeadSearchInput(pageMeta)) as PageHeadSearchInput
48

fxy060608's avatar
fxy060608 已提交
49
    __UNI_FEATURE_NAVIGATIONBAR_TRANSPARENT__ &&
50 51 52
      navigationBar.type === 'transparent' &&
      usePageHeadTransparent(headRef, pageMeta)

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

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

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

fxy060608's avatar
fxy060608 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153
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 已提交
154
  type,
fxy060608's avatar
fxy060608 已提交
155
  loading,
fxy060608's avatar
fxy060608 已提交
156
  titleSize,
fxy060608's avatar
fxy060608 已提交
157 158 159 160 161 162
  titleText,
  titleImage,
}: UniApp.PageNavigationBar) {
  return (
    <div class="uni-page-head-bd">
      <div
fxy060608's avatar
fxy060608 已提交
163
        style={{ fontSize: titleSize, opacity: type === 'transparent' ? 0 : 1 }}
fxy060608's avatar
fxy060608 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        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 已提交
180 181 182 183 184 185 186 187 188 189
  {
    text,
    focus,
    composing,
    onBlur,
    onFocus,
    onInput,
    onKeyup,
    onClick,
  }: PageHeadSearchInput
fxy060608's avatar
fxy060608 已提交
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 215 216 217 218
) {
  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 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
      {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 已提交
241 242 243 244
    </div>
  )
}

fxy060608's avatar
fxy060608 已提交
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
    }).catch(() => {})
fxy060608's avatar
fxy060608 已提交
254 255 256
  }
}

fxy060608's avatar
fxy060608 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
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 已提交
272 273 274 275 276
    const backgroundColor =
      __UNI_FEATURE_NAVIGATIONBAR_TRANSPARENT__ &&
      navigationBar.type === 'transparent'
        ? usePageHeadTransparentBackgroundColor(navigationBar.backgroundColor!)
        : navigationBar.backgroundColor
fxy060608's avatar
fxy060608 已提交
277
    return {
fxy060608's avatar
fxy060608 已提交
278 279
      backgroundColor,
      color: navigationBar.titleColor,
fxy060608's avatar
fxy060608 已提交
280 281 282 283 284 285 286 287 288
      transitionDuration: navigationBar.duration,
      transitionTimingFunction: navigationBar.timingFunc,
    }
  })
  return {
    clazz,
    style,
  }
}
fxy060608's avatar
fxy060608 已提交
289

fxy060608's avatar
fxy060608 已提交
290 291
type PageHeadButton = ReturnType<typeof usePageHeadButton>
type PageHeadButtons = ReturnType<typeof usePageHeadButtons>
fxy060608's avatar
fxy060608 已提交
292

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

function usePageHeadButton(
fxy060608's avatar
fxy060608 已提交
329 330
  pageId: number,
  index: number,
fxy060608's avatar
fxy060608 已提交
331 332 333
  btn: UniApp.PageNavigationBarButton,
  isTransparent: boolean
) {
fxy060608's avatar
fxy060608 已提交
334 335 336 337 338 339 340 341
  const iconStyle: UniApp.StyleObj = {
    color: btn.color,
    fontSize: btn.fontSize,
    fontWeight: btn.fontWeight,
  }
  if (btn.fontFamily) {
    iconStyle.fontFamily = btn.fontFamily
  }
fxy060608's avatar
fxy060608 已提交
342 343 344 345 346 347 348 349 350 351 352 353
  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 已提交
354 355
      btn.fontSrc && btn.fontFamily ? btn.text.replace('\\u', '&#x') : btn.text,
    btnIconPath: ICON_PATHS[btn.type],
fxy060608's avatar
fxy060608 已提交
356
    badgeText: btn.badgeText,
fxy060608's avatar
fxy060608 已提交
357
    iconStyle,
fxy060608's avatar
fxy060608 已提交
358 359 360
    onClick() {
      invokeHook(pageId, 'onNavigationBarButtonTap', extend({ index }, btn))
    },
fxy060608's avatar
fxy060608 已提交
361 362 363
  }
}

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
  }
}