提交 d3b33bbe 编写于 作者: Q qiang

feat(ssr): setNavigationBarTitle

上级 75d1c7e9
......@@ -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);
......
......@@ -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);
......
......@@ -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;
......
......@@ -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;
......
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<string, any>
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)
......
......@@ -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'
......
......@@ -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)) {
// // 仅当前页面
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册