From d3b33bbec9e46c5bd8debb6d2408daaffde75efb Mon Sep 17 00:00:00 2001 From: qiang Date: Thu, 27 May 2021 22:01:33 +0800 Subject: [PATCH] feat(ssr): setNavigationBarTitle --- packages/uni-app/dist/uni-app.cjs.js | 2 +- packages/uni-app/dist/uni-app.es.js | 2 +- packages/uni-h5/dist/uni-h5.cjs.js | 70 ++++++++++++++++--- packages/uni-h5/dist/uni-h5.es.js | 21 +++--- .../uni-h5/src/helpers/useDocumentTitle.ts | 17 +++-- packages/uni-h5/src/service/api/index.ts | 1 + .../src/service/api/ui/navigationBar.ts | 5 ++ 7 files changed, 92 insertions(+), 26 deletions(-) diff --git a/packages/uni-app/dist/uni-app.cjs.js b/packages/uni-app/dist/uni-app.cjs.js index ccde10325..a834ee3a7 100644 --- a/packages/uni-app/dist/uni-app.cjs.js +++ b/packages/uni-app/dist/uni-app.cjs.js @@ -117,7 +117,7 @@ const onError = /*#__PURE__*/ createHook(ON_ERROR); const onThemeChange = /*#__PURE__*/ createHook(ON_THEME_CHANGE); const onPageNotFound = /*#__PURE__*/ createHook(ON_PAGE_NOT_FOUND); const onUnhandledRejection = /*#__PURE__*/ createHook(ON_UNHANDLE_REJECTION); - +// export const onLoad = /*#__PURE__*/ createHook(ON_LOAD) const onReady = /*#__PURE__*/ createHook(ON_READY); const onUnload = /*#__PURE__*/ createHook(ON_UNLOAD); const onResize = /*#__PURE__*/ createHook(ON_RESIZE); diff --git a/packages/uni-app/dist/uni-app.es.js b/packages/uni-app/dist/uni-app.es.js index 32fba22ad..00ab56a62 100644 --- a/packages/uni-app/dist/uni-app.es.js +++ b/packages/uni-app/dist/uni-app.es.js @@ -82,7 +82,7 @@ const onError = /*#__PURE__*/ createHook(ON_ERROR); const onThemeChange = /*#__PURE__*/ createHook(ON_THEME_CHANGE); const onPageNotFound = /*#__PURE__*/ createHook(ON_PAGE_NOT_FOUND); const onUnhandledRejection = /*#__PURE__*/ createHook(ON_UNHANDLE_REJECTION); - +// export const onLoad = /*#__PURE__*/ createHook(ON_LOAD) const onReady = /*#__PURE__*/ createHook(ON_READY); const onUnload = /*#__PURE__*/ createHook(ON_UNLOAD); const onResize = /*#__PURE__*/ createHook(ON_RESIZE); diff --git a/packages/uni-h5/dist/uni-h5.cjs.js b/packages/uni-h5/dist/uni-h5.cjs.js index 3eddd34e8..86dc18b67 100644 --- a/packages/uni-h5/dist/uni-h5.cjs.js +++ b/packages/uni-h5/dist/uni-h5.cjs.js @@ -7341,6 +7341,16 @@ const RequestOptions = { } } }; +const API_SET_NAVIGATION_BAR_COLOR = "setNavigationBarColor"; +const API_SET_NAVIGATION_BAR_TITLE = "setNavigationBarTitle"; +const SetNavigationBarTitleProtocol = { + title: { + type: String, + required: true + } +}; +const API_SHOW_NAVIGATION_BAR_LOADING = "showNavigationBarLoading"; +const API_HIDE_NAVIGATION_BAR_LOADING = "hideNavigationBarLoading"; const envMethod = /* @__PURE__ */ (() => "env")(); function normalizeWindowBottom(windowBottom) { return `calc(${windowBottom}px + ${envMethod}(safe-area-inset-bottom))`; @@ -10288,11 +10298,61 @@ const getSystemInfoSync = /* @__PURE__ */ defineSyncApi("getSystemInfoSync", () }; } }); +function updateDocumentTitle(title) { + { + const ctx = vue.useSSRContext(); + ctx[uniShared.UNI_SSR_TITLE] = title; + } +} +function useDocumentTitle(pageMeta) { + function update() { + updateDocumentTitle(pageMeta.navigationBar.titleText); + } + vue.watchEffect(update); +} +function setNavigationBar(pageMeta, type, args, resolve, reject) { + if (!pageMeta) { + return reject("page not found"); + } + const {navigationBar} = pageMeta; + switch (type) { + case API_SET_NAVIGATION_BAR_COLOR: + const {frontColor, backgroundColor, animation: animation2} = args; + const {duration, timingFunc} = animation2; + if (frontColor) { + navigationBar.titleColor = frontColor === "#000000" ? "#000" : "#fff"; + } + if (backgroundColor) { + navigationBar.backgroundColor = backgroundColor; + } + navigationBar.duration = duration + "ms"; + navigationBar.timingFunc = timingFunc; + break; + case API_SHOW_NAVIGATION_BAR_LOADING: + navigationBar.loading = true; + break; + case API_HIDE_NAVIGATION_BAR_LOADING: + navigationBar.loading = false; + break; + case API_SET_NAVIGATION_BAR_TITLE: + const {title} = args; + navigationBar.titleText = title; + { + updateDocumentTitle(args.title); + } + break; + } + resolve(); +} +const setNavigationBarTitle = /* @__PURE__ */ defineAsyncApi(API_SET_NAVIGATION_BAR_TITLE, (args, {resolve, reject}) => { + setNavigationBar(getCurrentPageMeta(), API_SET_NAVIGATION_BAR_TITLE, args, resolve, reject); +}, SetNavigationBarTitleProtocol); require("localstorage-polyfill"); global.XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; var api = /* @__PURE__ */ Object.freeze({ __proto__: null, [Symbol.toStringTag]: "Module", + setNavigationBarTitle, request, setStorageSync, setStorage, @@ -10848,15 +10908,6 @@ function createRightWindowTsx(rightWindow, layoutState, windowState) { }, windowState), null, 16)])], 12, ["data-show"]), [[vue.vShow, layoutState.showRightWindow || layoutState.apiShowRightWindow]]); } } -function useDocumentTitle(pageMeta) { - const ctx = vue.useSSRContext(); - function update() { - { - ctx[uniShared.UNI_SSR_TITLE] = pageMeta.navigationBar.titleText; - } - } - vue.watchEffect(update); -} function hexToRgba(hex) { let r; let g2; @@ -11421,6 +11472,7 @@ exports.plugin = index$9; exports.removeStorage = removeStorage; exports.removeStorageSync = removeStorageSync; exports.request = request; +exports.setNavigationBarTitle = setNavigationBarTitle; exports.setStorage = setStorage; exports.setStorageSync = setStorageSync; exports.setupApp = setupApp; diff --git a/packages/uni-h5/dist/uni-h5.es.js b/packages/uni-h5/dist/uni-h5.es.js index c13136a93..4a5af42d3 100644 --- a/packages/uni-h5/dist/uni-h5.es.js +++ b/packages/uni-h5/dist/uni-h5.es.js @@ -17714,6 +17714,18 @@ const loadFontFace = /* @__PURE__ */ defineAsyncApi(API_LOAD_FONT_FACE, ({family reject(`loadFontFace:fail ${err}`); }); }, LoadFontFaceProtocol); +function updateDocumentTitle(title) { + { + document.title = title; + } +} +function useDocumentTitle(pageMeta) { + function update() { + updateDocumentTitle(pageMeta.navigationBar.titleText); + } + watchEffect(update); + onActivated(update); +} function setNavigationBar(pageMeta, type, args, resolve, reject) { if (!pageMeta) { return reject("page not found"); @@ -19958,15 +19970,6 @@ const UniServiceJSBridge$1 = /* @__PURE__ */ extend(ServiceJSBridge, { UniViewJSBridge.subscribeHandler(pageId + "." + event, args, pageId); } }); -function useDocumentTitle(pageMeta) { - function update() { - { - document.title = pageMeta.navigationBar.titleText; - } - } - watchEffect(update); - onActivated(update); -} function hexToRgba(hex) { let r; let g2; diff --git a/packages/uni-h5/src/helpers/useDocumentTitle.ts b/packages/uni-h5/src/helpers/useDocumentTitle.ts index bd93ae3de..31c80e5fe 100644 --- a/packages/uni-h5/src/helpers/useDocumentTitle.ts +++ b/packages/uni-h5/src/helpers/useDocumentTitle.ts @@ -1,13 +1,18 @@ import { watchEffect, onActivated, useSSRContext } from 'vue' import { UNI_SSR_TITLE } from '@dcloudio/uni-shared' + +export function updateDocumentTitle(title: string) { + if (__NODE_JS__) { + const ctx = useSSRContext() + ctx![UNI_SSR_TITLE] = title + } else { + document.title = title + } +} + export function useDocumentTitle(pageMeta: UniApp.PageRouteMeta) { - const ctx = (__NODE_JS__ && useSSRContext()) as Record function update() { - if (__NODE_JS__) { - ctx![UNI_SSR_TITLE] = pageMeta.navigationBar.titleText - } else { - document.title = pageMeta.navigationBar.titleText! - } + updateDocumentTitle(pageMeta.navigationBar.titleText!) } watchEffect(update) onActivated(update) diff --git a/packages/uni-h5/src/service/api/index.ts b/packages/uni-h5/src/service/api/index.ts index ece642f28..33ae2ba71 100644 --- a/packages/uni-h5/src/service/api/index.ts +++ b/packages/uni-h5/src/service/api/index.ts @@ -7,6 +7,7 @@ global.XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest export * from './network/request' export * from './storage/storage' export * from './device/getSystemInfoSync' +export { setNavigationBarTitle } from './ui/navigationBar' //#else export * from './base/canIUse' diff --git a/packages/uni-h5/src/service/api/ui/navigationBar.ts b/packages/uni-h5/src/service/api/ui/navigationBar.ts index 28e06c841..c602265c1 100644 --- a/packages/uni-h5/src/service/api/ui/navigationBar.ts +++ b/packages/uni-h5/src/service/api/ui/navigationBar.ts @@ -13,6 +13,7 @@ import { SetNavigationBarTitleProtocol, } from '@dcloudio/uni-api' import { getCurrentPageMeta } from '@dcloudio/uni-core' +import { updateDocumentTitle } from '../../../helpers/useDocumentTitle' function setNavigationBar( pageMeta: UniApp.PageRouteMeta | undefined, @@ -53,6 +54,10 @@ function setNavigationBar( case API_SET_NAVIGATION_BAR_TITLE: const { title } = args navigationBar.titleText = title + if (__NODE_JS__) { + // watch 无效,主动更新 title + updateDocumentTitle(args.title) + } // TODO isCurrentPage逻辑主要是navigationBar组件使用 // if (isCurrentPage(page)) { // // 仅当前页面 -- GitLab