add app-shell, master-detail, pane-transition
This commit is contained in:
@@ -0,0 +1,209 @@
|
||||
/**
|
||||
* 主从分栏展开/收起时长。
|
||||
*
|
||||
* MasterDetail 的主栏宽度和这里列表页导航栏的宽度必须同步,
|
||||
* 所以时长只在这一处定义,两边都引用它。
|
||||
*/
|
||||
:root {
|
||||
--tui-split-duration: 320ms;
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面骨架样式。
|
||||
*
|
||||
* 需与 PageTransition 配合使用:页面根节点加 .tui-page-layout,
|
||||
* 内容区加 .tui-page-content。
|
||||
* - 有顶部导航栏时,根节点追加 .tui-navbar;
|
||||
* - 需要为底部导航栏留出空白时,再追加 .tui-tabbar。
|
||||
*
|
||||
* .tui-page-content 使用绝对定位铺满上级容器,因此上级必须是
|
||||
* PageTransition 渲染出的定位容器,不要在普通 div 中直接使用。
|
||||
*/
|
||||
|
||||
/**
|
||||
* 主从分栏里详情栏的空白态容器:占满右栏并垂直居中,顶部让出导航栏。
|
||||
*
|
||||
* 用在 <master-detail> 的 #detail 插槽里,省去每个页面自己写 height 和内边距。
|
||||
* 已经给 master-detail 传了 top-inset 的话,用 --tui-detail-empty-inset 把默认值改掉,避免叠加。
|
||||
*/
|
||||
.tui-detail-empty {
|
||||
height: 100%;
|
||||
display: flex;
|
||||
box-sizing: border-box;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding-top: var(--tui-detail-empty-inset, calc(env(safe-area-inset-top, 0px) + 48px));
|
||||
// 必须有不透明背景:切换视图时它和旧内容同时在场,
|
||||
// 透明的话提示文字会浮在还在滑出的页面上,看起来像重影。
|
||||
background: var(--tui-detail-empty-background, var(--td-bg-color-page));
|
||||
}
|
||||
|
||||
|
||||
.tui-page-layout {
|
||||
min-height: 100vh;
|
||||
box-sizing: border-box;
|
||||
color: var(--td-text-color-primary);
|
||||
background: var(--td-bg-color-page);
|
||||
|
||||
.tui-page-content {
|
||||
flex: 1;
|
||||
inset: 0;
|
||||
width: 100%;
|
||||
display: flex;
|
||||
overflow: hidden;
|
||||
position: absolute;
|
||||
min-height: 0;
|
||||
box-sizing: border-box;
|
||||
padding-top: calc(env(safe-area-inset-top, 0px) + 48px + 1rem);
|
||||
padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 1rem);
|
||||
flex-direction: column;
|
||||
|
||||
&.scroll {
|
||||
overflow: auto;
|
||||
-webkit-overflow-scrolling: touch;
|
||||
}
|
||||
|
||||
&.column {
|
||||
gap: 1rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
&.scroll > * {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
&.tui-navbar {
|
||||
--tui-nav-height: calc(env(safe-area-inset-top, 0px) + 48px);
|
||||
|
||||
.t-navbar__content {
|
||||
color: var(--td-text-color-primary);
|
||||
overflow: visible;
|
||||
box-shadow: 0 3px 8px rgba(0, 0, 0, .1);
|
||||
background: var(--tui-navbar-background, color-mix(in srgb, var(--td-bg-color-container) 85%, transparent));
|
||||
backdrop-filter: blur(10px);
|
||||
-webkit-backdrop-filter: blur(10px);
|
||||
}
|
||||
|
||||
&.tui-tabbar {
|
||||
|
||||
.tui-page-content.scroll,
|
||||
.tui-page-content.column {
|
||||
padding-bottom: 6rem;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 左侧导航栏出现后,TDesign 导航栏的 fixed 内容需要让出左侧宽度,
|
||||
* 否则会整条横跨到导航栏上方。
|
||||
*/
|
||||
.tui-app-shell.is-rail {
|
||||
|
||||
.t-navbar--fixed .t-navbar__content {
|
||||
left: var(--tui-fixed-left, 0px);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转场期间页面带 transform 且整体已经右移,fixed 元素改为相对页面定位,
|
||||
* 不能再叠加一次导航栏宽度,否则顶部导航左侧会空出一格。
|
||||
*/
|
||||
.page-transition-page[class*="push-"] .t-navbar--fixed .t-navbar__content {
|
||||
left: 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 贴着内容区定位的 fixed 元素(底部操作栏、悬浮按钮等)。
|
||||
*
|
||||
* 加上这个类后会避开左侧导航栏,并与内容区保持 --tui-fixed-gap 的边距。
|
||||
*/
|
||||
.tui-fixed-inset {
|
||||
left: calc(var(--tui-fixed-left, 0px) + var(--tui-fixed-gap, 1rem)) !important;
|
||||
right: var(--tui-fixed-gap, 1rem) !important;
|
||||
}
|
||||
|
||||
/**
|
||||
* 双栏导航:分栏时左右各一条头部,而不是一条横跨全宽的导航栏。
|
||||
*
|
||||
* 思路是把列表页的导航栏收缩到主栏宽度,详情页的导航栏退化成栏内头部。
|
||||
* 两者都在 y=0 起算、高度一致,看起来就是一条被分开的工具栏。
|
||||
*
|
||||
* 全部依赖 :has(),老浏览器里整块失效,退化为单导航栏,不影响功能。
|
||||
* 注意不能用 transform 限制 fixed:那会连详情页里的弹层一起锁进栏位。
|
||||
*/
|
||||
// 主从分栏的曲线,和 MasterDetail 共用;时长统一走 --tui-split-duration
|
||||
@split-easing: var(--tui-bezier, cubic-bezier(.19, 1, .22, 1));
|
||||
|
||||
/**
|
||||
* 主从分栏的详情栏里,入场动画要短一些。
|
||||
*
|
||||
* 整页进入用 400ms 是合适的,但在右栏展开的同一时间播 400ms,
|
||||
* 内容会比列表缩窄晚 300ms 才完全显现,看起来像"详情跟不上"。
|
||||
*/
|
||||
.tui-master-detail .detail .entrance-fade-up-enter-active {
|
||||
transition-duration: 200ms;
|
||||
}
|
||||
|
||||
@supports selector(:has(*)) {
|
||||
|
||||
// 分栏时内容区整体上移,两栏各自从顶部开始
|
||||
.tui-page-layout:has(> .tui-page-content > .tui-master-detail.split) > .tui-page-content {
|
||||
padding-top: 0;
|
||||
}
|
||||
|
||||
// 列表页导航栏收缩到主栏宽度,右侧让给详情栏的头部。
|
||||
// 时长/曲线与 master-detail 里的主栏过渡保持一致,两者必须同步到位。
|
||||
.tui-page-layout:has(> .tui-page-content > .tui-master-detail.split) > .t-navbar--fixed .t-navbar__content {
|
||||
right: auto;
|
||||
width: var(--tui-master-width, 380px);
|
||||
transition: width var(--tui-split-duration, 320ms) @split-easing;
|
||||
}
|
||||
|
||||
// 详情未选中、主栏占满时,导航栏也要满宽。
|
||||
// fixed 的 100% 是视口宽度,还得扣掉左侧 rail,才和内容区对齐。
|
||||
.tui-page-layout:has(> .tui-page-content > .tui-master-detail.split.is-collapsed) > .t-navbar--fixed .t-navbar__content {
|
||||
width: calc(100% - var(--tui-fixed-left, 0px));
|
||||
}
|
||||
|
||||
// 详情栏放的是整页组件时:栏位本身不再滚动,改由页面内容区滚动
|
||||
.tui-master-detail .detail:has(> .tui-page-layout) {
|
||||
display: flex;
|
||||
overflow: hidden;
|
||||
padding-top: 0;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.tui-master-detail .detail > .tui-page-layout {
|
||||
height: 100%;
|
||||
display: flex;
|
||||
min-height: 0;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
// 页面导航栏浮在内容之上,而不是占位:这样滚动内容才能穿进它的毛玻璃层
|
||||
.tui-master-detail .detail > .tui-page-layout > .t-navbar--fixed {
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
z-index: 20;
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
// 栏内定位是相对栏位算的,必须清掉 rail 留下的左偏移,否则整条导航右移 96px
|
||||
.tui-master-detail .detail > .tui-page-layout > .t-navbar--fixed .t-navbar__content {
|
||||
left: 0;
|
||||
right: 0;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
// 内容区铺满栏位,顶部内边距沿用页面骨架的导航高度,滚上去就被导航盖住
|
||||
.tui-master-detail .detail > .tui-page-layout > .tui-page-content {
|
||||
flex: 1;
|
||||
position: static;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import view from "./index.vue";
|
||||
import { WebToolkit } from "timi-web";
|
||||
|
||||
export const AppShell = WebToolkit.withInstall(view);
|
||||
export default AppShell;
|
||||
|
||||
export type {
|
||||
AppShellItem,
|
||||
AppShellLayoutOptions,
|
||||
AppShellMode,
|
||||
AppShellProps,
|
||||
AppShellValue
|
||||
} from "./type";
|
||||
|
||||
export { createLayoutContext, provideLayout, useLayout } from "../../composables/use-layout";
|
||||
export type { LayoutContext } from "../../composables/use-layout";
|
||||
@@ -0,0 +1,309 @@
|
||||
<template>
|
||||
<div class="tui-app-shell" :class="{ 'is-rail': showRail }" :style="shellStyle">
|
||||
<nav v-if="showRail" class="rail" aria-label="主导航">
|
||||
<div v-if="logoText || $slots.logo" class="logo">
|
||||
<slot name="logo">
|
||||
<span v-text="logoText" />
|
||||
</slot>
|
||||
</div>
|
||||
<t-button
|
||||
v-for="item in itemList"
|
||||
:key="item.value"
|
||||
class="rail-item"
|
||||
:class="{ on: item.value === modelValue }"
|
||||
variant="text"
|
||||
:disabled="item.disabled"
|
||||
:aria-current="item.value === modelValue ? 'page' : undefined"
|
||||
@click="onSelect(item)"
|
||||
>
|
||||
<t-badge v-if="item.badge && item.icon" :count="item.badge" :offset="[2, -2]">
|
||||
<t-icon :name="item.icon" size="1.375rem" />
|
||||
</t-badge>
|
||||
<t-icon v-else-if="item.icon" :name="item.icon" size="1.375rem" />
|
||||
<span v-text="item.text" />
|
||||
</t-button>
|
||||
<div class="rail-extra">
|
||||
<slot name="rail-extra" />
|
||||
</div>
|
||||
</nav>
|
||||
<div class="body">
|
||||
<div class="content">
|
||||
<slot />
|
||||
</div>
|
||||
<t-tab-bar
|
||||
v-if="showTabBar"
|
||||
class="tabbar"
|
||||
:value="modelValue"
|
||||
:fixed="false"
|
||||
:z-index="1"
|
||||
:bordered="false"
|
||||
:split="false"
|
||||
:safe-area-inset-bottom="false"
|
||||
:class="{ 'is-float': tabBarFloat, 'is-hidden': tabBarFloat && hideNav }"
|
||||
:shape="tabBarFloat ? 'round' : 'normal'"
|
||||
:theme="tabBarFloat ? 'tag' : 'normal'"
|
||||
@change="onTabChange"
|
||||
>
|
||||
<t-tab-bar-item
|
||||
v-for="item in itemList"
|
||||
:key="item.value"
|
||||
:class="{ 'is-disabled': item.disabled }"
|
||||
:value="item.value"
|
||||
:badge-props="item.badge ? { count: item.badge } : undefined"
|
||||
>
|
||||
<span v-text="item.text" />
|
||||
<template #icon>
|
||||
<t-icon :name="item.icon" />
|
||||
</template>
|
||||
</t-tab-bar-item>
|
||||
</t-tab-bar>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, type StyleValue } from "vue";
|
||||
import { createLayoutContext, provideLayout } from "../../composables/use-layout";
|
||||
import type { AppShellItem, AppShellProps, AppShellValue } from "./type";
|
||||
|
||||
defineOptions({
|
||||
name: "AppShell"
|
||||
});
|
||||
|
||||
const props = withDefaults(defineProps<AppShellProps>(), {
|
||||
tablet: true,
|
||||
tabletMin: 768,
|
||||
wideMin: 1024,
|
||||
railOnPortrait: false,
|
||||
items: () => [],
|
||||
modelValue: "",
|
||||
logoText: "",
|
||||
hideNav: false,
|
||||
hideRail: false,
|
||||
tabBarFloat: false,
|
||||
railWidth: 96,
|
||||
tabBarHeight: 52
|
||||
});
|
||||
|
||||
const emit = defineEmits<{
|
||||
"update:modelValue": [value: AppShellValue];
|
||||
"change": [value: AppShellValue];
|
||||
}>();
|
||||
|
||||
defineSlots<{
|
||||
default?: () => unknown;
|
||||
logo?: () => unknown;
|
||||
"rail-extra"?: () => unknown;
|
||||
}>();
|
||||
|
||||
// ---------- 布局上下文 ----------
|
||||
|
||||
const layout = createLayoutContext({
|
||||
tablet: props.tablet,
|
||||
tabletMin: props.tabletMin,
|
||||
wideMin: props.wideMin,
|
||||
railOnPortrait: props.railOnPortrait,
|
||||
mode: props.mode
|
||||
});
|
||||
|
||||
provideLayout(layout);
|
||||
|
||||
const { railVisible } = layout;
|
||||
|
||||
const itemList = computed(() => props.items ?? []);
|
||||
|
||||
/** 左侧导航栏常驻,仅受 hide-rail 控制。 */
|
||||
const showRail = computed(() => railVisible.value && itemList.value.length > 0 && !props.hideRail);
|
||||
|
||||
/** 悬浮模式下保留节点交给过渡,贴底模式下直接移除。 */
|
||||
const showTabBar = computed(() => !railVisible.value
|
||||
&& itemList.value.length > 0
|
||||
&& (props.tabBarFloat || !props.hideNav));
|
||||
|
||||
// ---------- 尺寸变量 ----------
|
||||
|
||||
function toSize(value: number | string | undefined, fallback: string): string {
|
||||
if (value === undefined || value === "") {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
return typeof value === "number" ? `${value}px` : value;
|
||||
}
|
||||
|
||||
const railWidthValue = computed(() => toSize(props.railWidth, "96px"));
|
||||
const tabBarHeightValue = computed(() => toSize(props.tabBarHeight, "52px"));
|
||||
|
||||
const shellStyle = computed<StyleValue>(() => ({
|
||||
"--tui-rail-width": railWidthValue.value,
|
||||
"--tui-tabbar-height": tabBarHeightValue.value,
|
||||
"--tui-fixed-left": showRail.value ? railWidthValue.value : "0px",
|
||||
"--tui-transition-inset-left": showRail.value ? railWidthValue.value : "0px"
|
||||
}));
|
||||
|
||||
// ---------- 导航 ----------
|
||||
|
||||
async function requestChange(next: AppShellValue): Promise<void> {
|
||||
if (next === props.modelValue) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (props.beforeChange) {
|
||||
const allowed = await props.beforeChange(next);
|
||||
if (allowed === false) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
emit("update:modelValue", next);
|
||||
emit("change", next);
|
||||
}
|
||||
|
||||
function onSelect(item: AppShellItem): void {
|
||||
if (item.disabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
void requestChange(item.value);
|
||||
}
|
||||
|
||||
function onTabChange(value: AppShellValue): void {
|
||||
const target = itemList.value.find(item => item.value === value);
|
||||
if (!target || target.disabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
void requestChange(value);
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.tui-app-shell {
|
||||
height: 100vh;
|
||||
height: 100dvh;
|
||||
display: flex;
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
color: var(--td-text-color-primary);
|
||||
background: var(--td-bg-color-page);
|
||||
|
||||
.body {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
min-width: 0;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.content {
|
||||
flex: 1;
|
||||
position: relative;
|
||||
min-height: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.rail {
|
||||
width: var(--tui-rail-width);
|
||||
flex: none;
|
||||
z-index: 10;
|
||||
display: flex;
|
||||
position: relative;
|
||||
overflow-y: auto;
|
||||
flex-direction: column;
|
||||
padding-left: env(safe-area-inset-left, 0);
|
||||
border-right: 1px solid var(--td-component-border);
|
||||
background: var(--tui-rail-background, var(--td-bg-color-container));
|
||||
|
||||
.logo {
|
||||
height: 4.5rem;
|
||||
display: grid;
|
||||
font-weight: 600;
|
||||
place-items: center;
|
||||
font-size: var(--tui-rail-logo-font-size, 1.25rem);
|
||||
}
|
||||
|
||||
.rail-item {
|
||||
--td-button-border-radius: var(--tui-rail-item-radius, .75rem);
|
||||
|
||||
width: calc(100% - 1.25rem);
|
||||
height: auto;
|
||||
display: flex;
|
||||
margin: .125rem .625rem;
|
||||
padding: var(--tui-rail-item-padding, .75rem 0);
|
||||
color: var(--td-text-color-secondary);
|
||||
font-size: var(--tui-rail-item-font-size, .75rem);
|
||||
line-height: 1.4;
|
||||
flex-direction: column;
|
||||
|
||||
:deep(.t-button__content) {
|
||||
gap: .25rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
&.on {
|
||||
color: var(--tui-rail-active-color, var(--td-brand-color));
|
||||
font-weight: 600;
|
||||
background: var(--tui-rail-active-background, var(--td-brand-color-light));
|
||||
}
|
||||
}
|
||||
|
||||
.rail-extra {
|
||||
margin-top: auto;
|
||||
padding-bottom: calc(.75rem + env(safe-area-inset-bottom, 0));
|
||||
}
|
||||
}
|
||||
|
||||
.tabbar {
|
||||
--td-tab-bar-height: var(--tui-tabbar-height);
|
||||
--td-tab-bar-bg-color: var(--tui-tabbar-background, var(--td-bg-color-container));
|
||||
|
||||
flex: none;
|
||||
border-top: 1px solid var(--td-component-border);
|
||||
padding-bottom: env(safe-area-inset-bottom, 0);
|
||||
background: var(--tui-tabbar-background, var(--td-bg-color-container));
|
||||
|
||||
:deep(.t-tab-bar-item) {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
:deep(.is-disabled) {
|
||||
opacity: .4;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
&.is-float {
|
||||
--td-tab-bar-active-bg: var(--tui-tabbar-active-background, var(--td-brand-color-light));
|
||||
--td-tab-bar-bg-color: transparent;
|
||||
|
||||
left: var(--tui-tabbar-float-inset, 1rem);
|
||||
right: var(--tui-tabbar-float-inset, 1rem);
|
||||
bottom: var(--tui-tabbar-float-inset, 1rem);
|
||||
z-index: 2;
|
||||
margin: 0;
|
||||
opacity: 1;
|
||||
position: fixed;
|
||||
border-top: 0;
|
||||
padding-bottom: 0;
|
||||
border-radius: var(--tui-tabbar-float-radius, 99rem);
|
||||
box-shadow: var(--td-shadow-3);
|
||||
background: var(--tui-tabbar-float-background, color-mix(in srgb, var(--td-bg-color-container) 60%, transparent));
|
||||
backdrop-filter: blur(var(--tui-tabbar-float-blur, 10px));
|
||||
-webkit-backdrop-filter: blur(var(--tui-tabbar-float-blur, 10px));
|
||||
transform: translateY(0);
|
||||
pointer-events: auto;
|
||||
transition: opacity var(--tui-tabbar-float-duration, 520ms), transform var(--tui-tabbar-float-duration, 520ms) var(--tui-bezier, cubic-bezier(.19, 1, .22, 1));
|
||||
|
||||
&.is-hidden {
|
||||
opacity: 0;
|
||||
pointer-events: none;
|
||||
transform: translateY(calc(100% + var(--tui-tabbar-float-inset, 1rem)));
|
||||
}
|
||||
|
||||
:deep(.t-tab-bar-item) {
|
||||
margin: 4px 0;
|
||||
padding: 0 4px;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,42 @@
|
||||
import type { AppShellLayoutOptions } from "../../composables/use-layout";
|
||||
|
||||
export type { AppShellLayoutOptions, AppShellMode } from "../../composables/use-layout";
|
||||
|
||||
/** 导航值,支持字符串或数字。 */
|
||||
export type AppShellValue = string | number;
|
||||
|
||||
/** 一级导航项。 */
|
||||
export interface AppShellItem {
|
||||
/** 唯一值,通常传路由 path。 */
|
||||
value: AppShellValue;
|
||||
/** 显示文字。 */
|
||||
text: string;
|
||||
/** TDesign 图标名;不传时只显示文字。 */
|
||||
icon?: string;
|
||||
/** 是否禁用。 */
|
||||
disabled?: boolean;
|
||||
/** 角标内容,通常是未读数;传 0 或空字符串时不显示。 */
|
||||
badge?: string | number;
|
||||
}
|
||||
|
||||
/** AppShell 组件属性。 */
|
||||
export interface AppShellProps extends AppShellLayoutOptions {
|
||||
/** 一级导航项;不传时只渲染内容区。 */
|
||||
items?: AppShellItem[];
|
||||
/** 当前激活的导航值。 */
|
||||
modelValue?: AppShellValue;
|
||||
/** 左侧导航栏顶部的标识文字,通常放一个字符。 */
|
||||
logoText?: string;
|
||||
/** 是否隐藏底部标签栏;默认 false。左侧导航栏不受影响,需要时配合 hide-rail 使用。 */
|
||||
hideNav?: boolean;
|
||||
/** 是否隐藏左侧导航栏;默认 false,用于全屏看图、沉浸阅读等场景。 */
|
||||
hideRail?: boolean;
|
||||
/** 底部标签栏是否悬浮显示;默认 false,贴底铺满。 */
|
||||
tabBarFloat?: boolean;
|
||||
/** 左侧导航栏宽度,数字以 px 为单位,默认 96。 */
|
||||
railWidth?: number | string;
|
||||
/** 底部标签栏高度,数字以 px 为单位,默认 52。 */
|
||||
tabBarHeight?: number | string;
|
||||
/** 切换前拦截,返回 false 时取消本次切换。 */
|
||||
beforeChange?: (value: AppShellValue) => boolean | Promise<boolean>;
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
/** 导出所有组件 */
|
||||
import RootLayout from "./root-layout";
|
||||
import PageTransition from "./page-transition";
|
||||
import AppShell from "./app-shell";
|
||||
import MasterDetail from "./master-detail";
|
||||
import MasterDetailPage from "./master-detail-page";
|
||||
import PaneTransition from "./pane-transition";
|
||||
import DurationPicker from "./duration-picker";
|
||||
import CodeEditor from "./code-editor";
|
||||
import TiptapEditor from "./tiptap-editor";
|
||||
@@ -23,6 +27,10 @@ import {
|
||||
export default [
|
||||
RootLayout,
|
||||
PageTransition,
|
||||
AppShell,
|
||||
MasterDetail,
|
||||
MasterDetailPage,
|
||||
PaneTransition,
|
||||
DurationPicker,
|
||||
CodeEditor,
|
||||
TiptapEditor,
|
||||
@@ -44,6 +52,10 @@ export default [
|
||||
export {
|
||||
RootLayout,
|
||||
PageTransition,
|
||||
AppShell,
|
||||
MasterDetail,
|
||||
MasterDetailPage,
|
||||
PaneTransition,
|
||||
DurationPicker,
|
||||
CodeEditor,
|
||||
TiptapEditor,
|
||||
@@ -62,6 +74,22 @@ export {
|
||||
AiChatThinking
|
||||
};
|
||||
|
||||
export { createLayoutContext, provideLayout, useLayout } from "./app-shell";
|
||||
export { useExpandToMaster, useMasterDetail, useMasterDetailPane, usePane } from "../composables/use-master-detail";
|
||||
export type { ExpandToMasterOptions, PaneContext, PaneDirection, UseMasterDetailOptions, UseMasterDetailPaneOptions, UseMasterDetailPaneReturn, UseMasterDetailReturn } from "../composables/use-master-detail";
|
||||
|
||||
export type {
|
||||
AppShellItem,
|
||||
AppShellLayoutOptions,
|
||||
AppShellMode,
|
||||
AppShellProps,
|
||||
AppShellValue,
|
||||
LayoutContext
|
||||
} from "./app-shell";
|
||||
|
||||
export type { MasterDetailProps } from "./master-detail";
|
||||
export type { MasterDetailPageProps } from "./master-detail-page";
|
||||
|
||||
export {
|
||||
AiChatHttpError,
|
||||
AiChatStreamError,
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import view from "./index.vue";
|
||||
import { WebToolkit } from "timi-web";
|
||||
|
||||
export const MasterDetailPage = WebToolkit.withInstall(view);
|
||||
export default MasterDetailPage;
|
||||
|
||||
export type { MasterDetailPageProps } from "./type";
|
||||
@@ -0,0 +1,79 @@
|
||||
<template>
|
||||
<main class="tui-page-content tui-master-detail-page" :class="split ? 'is-split' : 'scroll column'">
|
||||
<master-detail
|
||||
:split="split"
|
||||
:master-width="masterWidth"
|
||||
:top-inset="topInset"
|
||||
:detail-empty="!selected"
|
||||
>
|
||||
<template #master>
|
||||
<div class="tui-master-wrap">
|
||||
<slot name="master" />
|
||||
</div>
|
||||
</template>
|
||||
<template #detail>
|
||||
<!--
|
||||
外层 v-if 而不是让 PaneTransition 处理"从空到有":
|
||||
选中时右栏是从 0 宽度长出来的,内容应该一开始就贴着主栏,
|
||||
跟着宽度被"拉开"。交给 PaneTransition 会从 translateX(100%)
|
||||
滑进来,看起来像详情从右边追上来。
|
||||
-->
|
||||
<pane-transition v-if="selected" :direction="direction">
|
||||
<slot name="detail" />
|
||||
</pane-transition>
|
||||
</template>
|
||||
</master-detail>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed } from "vue";
|
||||
import MasterDetail from "../master-detail/index.vue";
|
||||
import PaneTransition from "../pane-transition/index.vue";
|
||||
import { useLayout } from "../../composables/use-layout";
|
||||
import type { MasterDetailPageProps } from "./type";
|
||||
|
||||
defineOptions({
|
||||
name: "MasterDetailPage"
|
||||
});
|
||||
|
||||
const props = withDefaults(defineProps<MasterDetailPageProps>(), {
|
||||
split: "auto",
|
||||
selected: false,
|
||||
direction: "forward",
|
||||
masterWidth: 380,
|
||||
topInset: ""
|
||||
});
|
||||
|
||||
defineSlots<{
|
||||
master?: () => unknown;
|
||||
detail?: () => unknown;
|
||||
}>();
|
||||
|
||||
const layout = useLayout({ tablet: true });
|
||||
|
||||
const split = computed(() => (props.split === "auto" ? layout.isWide.value : props.split));
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.tui-master-detail-page {
|
||||
|
||||
/**
|
||||
* 分栏时内容区不再自己滚动:高度由两栏分配,滚动交给主栏里的列表容器。
|
||||
* 顶部的导航间距也因此交给列表容器自己加(见 page-layout.less 的 .list 约定)。
|
||||
*/
|
||||
&.is-split {
|
||||
overflow: hidden;
|
||||
padding-top: 0;
|
||||
padding-bottom: 0;
|
||||
}
|
||||
|
||||
// 让内部的下拉刷新/列表接管滚动,主栏只负责宽度
|
||||
.tui-master-wrap {
|
||||
height: 100%;
|
||||
display: flex;
|
||||
min-height: 0;
|
||||
flex-direction: column;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { PaneDirection } from "../../composables/use-master-detail";
|
||||
|
||||
/** MasterDetailPage 的 props。 */
|
||||
export interface MasterDetailPageProps {
|
||||
/**
|
||||
* 是否分栏。
|
||||
*
|
||||
* 传 `"auto"` 时跟随 AppShell 的横屏状态;业务自己判断时传布尔值。
|
||||
*/
|
||||
split?: boolean | "auto";
|
||||
/** 右栏是否有内容。为 false 时主栏占满整宽,右栏不渲染。 */
|
||||
selected?: boolean;
|
||||
/** 右栏切换方向,直接来自 `useMasterDetailPane` 的 `paneDirection`。 */
|
||||
direction?: PaneDirection;
|
||||
/** 主栏宽度,默认 380。 */
|
||||
masterWidth?: number | string;
|
||||
/** 主栏顶部让出的高度,通常不需要传。 */
|
||||
topInset?: string;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import view from "./index.vue";
|
||||
import { WebToolkit } from "timi-web";
|
||||
|
||||
export const MasterDetail = WebToolkit.withInstall(view);
|
||||
export default MasterDetail;
|
||||
|
||||
export type { MasterDetailProps } from "./type";
|
||||
@@ -0,0 +1,153 @@
|
||||
<template>
|
||||
<div
|
||||
class="tui-master-detail"
|
||||
:class="{ split: isSplit, 'is-collapsed': isSplit && detailEmpty }"
|
||||
:style="rootStyle"
|
||||
>
|
||||
<section class="master">
|
||||
<slot name="master" />
|
||||
</section>
|
||||
<section v-if="isSplit" class="detail">
|
||||
<slot name="detail">
|
||||
<div v-if="emptyText && !detailEmpty" class="empty">
|
||||
<center-tips :content="emptyText" />
|
||||
</div>
|
||||
</slot>
|
||||
</section>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, type StyleValue } from "vue";
|
||||
import { CenterTips } from "timi-web";
|
||||
import { useLayout } from "../../composables/use-layout";
|
||||
import type { MasterDetailProps } from "./type";
|
||||
|
||||
defineOptions({
|
||||
name: "MasterDetail"
|
||||
});
|
||||
|
||||
const props = withDefaults(defineProps<MasterDetailProps>(), {
|
||||
masterWidth: 380,
|
||||
split: "auto",
|
||||
emptyText: "",
|
||||
topInset: "",
|
||||
detailEmpty: false
|
||||
});
|
||||
|
||||
defineSlots<{
|
||||
master?: () => unknown;
|
||||
detail?: () => unknown;
|
||||
}>();
|
||||
|
||||
// ---------- 分栏判定 ----------
|
||||
|
||||
const layout = useLayout({ tablet: true });
|
||||
|
||||
/** split 为 auto 时跟随 AppShell 的横屏状态,显式传入布尔值时以业务为准。 */
|
||||
const isSplit = computed(() => (props.split === "auto" ? layout.isWide.value : props.split));
|
||||
|
||||
// ---------- 尺寸 ----------
|
||||
|
||||
const rootStyle = computed<StyleValue>(() => ({
|
||||
"--tui-master-width": typeof props.masterWidth === "number"
|
||||
? `${props.masterWidth}px`
|
||||
: props.masterWidth,
|
||||
...(props.topInset ? { "--tui-master-top-inset": props.topInset } : {})
|
||||
}));
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
@easing: var(--tui-bezier, cubic-bezier(.19, 1, .22, 1));
|
||||
|
||||
.tui-master-detail {
|
||||
flex: 1;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
min-height: 0;
|
||||
|
||||
.master {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
position: relative;
|
||||
overflow-y: auto;
|
||||
padding-top: var(--tui-master-top-inset, 0px);
|
||||
-webkit-overflow-scrolling: touch;
|
||||
}
|
||||
|
||||
.empty {
|
||||
height: 100%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
&.split {
|
||||
|
||||
.master {
|
||||
flex: none;
|
||||
width: var(--tui-master-width);
|
||||
box-sizing: border-box;
|
||||
// 宽度过渡期间列表会被压窄,内容可能横向溢出,裁掉避免压到详情栏上
|
||||
overflow-x: hidden;
|
||||
border-right: 1px solid var(--td-component-border, rgba(0, 0, 0, .08));
|
||||
// 时长和曲线都来自 page-layout.less 的变量,列表页导航栏用同一组,
|
||||
// 两边必须同拍,否则列表跳、导航滑。
|
||||
transition: width var(--tui-split-duration, 320ms) @easing, border-color .2s linear;
|
||||
}
|
||||
|
||||
.detail {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
position: relative;
|
||||
overflow-x: hidden;
|
||||
overflow-y: auto;
|
||||
padding-top: var(--tui-master-top-inset, 0px);
|
||||
background: var(--tui-master-detail-background, var(--td-bg-color-page));
|
||||
-webkit-overflow-scrolling: touch;
|
||||
// 展开方向不加 opacity 过渡:
|
||||
// 宽度从 0 长出来本身就是"揭示",再叠一层淡入会让内容看着迟迟不出现。
|
||||
// 收起方向的淡出定义在 .is-collapsed 里,只在收起时生效。
|
||||
|
||||
/**
|
||||
* 详情栏里放的整页组件自动适配栏位:贴合高度、建立定位上下文。
|
||||
* 导航栏不隐藏,而是由 page-layout.less 退化成栏内头部,
|
||||
* 这样编辑等操作仍然可用。
|
||||
*/
|
||||
:deep(.tui-page-layout) {
|
||||
height: 100%;
|
||||
position: relative;
|
||||
min-height: 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 未选中详情:主栏占满,右栏收起。
|
||||
*
|
||||
* 详情栏不设 display: none——那样宽度变化就没有过渡了。
|
||||
* 保持 flex 分配、把宽度压到 0,配合 overflow-x: hidden 裁掉内容,
|
||||
* 主栏宽度一过渡,右栏自然被"挤"出来。
|
||||
*/
|
||||
&.is-collapsed {
|
||||
|
||||
.master {
|
||||
width: 100%;
|
||||
border-right-color: transparent;
|
||||
}
|
||||
|
||||
.detail {
|
||||
// 收起方向:内容比宽度先淡出。
|
||||
// 否则右栏被压到只剩几十像素时,文字会挤成竖排、按钮会卡在角上,
|
||||
// 看起来像残影而不是"被推走"。
|
||||
//
|
||||
// transition 只写在这里:展开时 class 被移除,属性回到默认的瞬间切换,
|
||||
// 内容第一帧就是可见的。
|
||||
opacity: 0;
|
||||
overflow-y: hidden;
|
||||
pointer-events: none;
|
||||
transition: opacity .12s ease-out;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,25 @@
|
||||
/** MasterDetail 组件属性。 */
|
||||
export interface MasterDetailProps {
|
||||
/** 主栏宽度,数字以 px 为单位,默认 380。 */
|
||||
masterWidth?: number | string;
|
||||
/** 是否分栏;auto 时跟随 AppShell 的横屏状态,默认 auto。 */
|
||||
split?: boolean | "auto";
|
||||
/** 分栏且详情为空时显示的文案。 */
|
||||
emptyText?: string;
|
||||
/**
|
||||
* 两栏顶部的内边距,通常填导航栏高度。
|
||||
*
|
||||
* 加在两栏自己的滚动容器上,内容滚动时才能穿到 fixed 导航栏的毛玻璃层下面;
|
||||
* 加在外层容器上只会把内容整体下移,滚不动。
|
||||
*/
|
||||
topInset?: string;
|
||||
/**
|
||||
* 详情栏当前是否为空,默认 false。
|
||||
*
|
||||
* 传 true 时主栏占满宽度、详情栏收起,选中后主栏收缩到 master-width、右栏展开。
|
||||
* 适合"先看列表、选中再展开"的平板场景:空白右栏不占位,列表也不用挤在 380 的窄条里。
|
||||
*
|
||||
* 只有卡片式列表适合打开——多列宽表格从满宽压到 master-width 会挤成一团。
|
||||
*/
|
||||
detailEmpty?: boolean;
|
||||
}
|
||||
@@ -1,8 +1,5 @@
|
||||
<template>
|
||||
<div
|
||||
class="page-transition"
|
||||
:class="{ 'page-transition-reduced-motion': props.respectReducedMotion }"
|
||||
>
|
||||
<div class="page-transition">
|
||||
<RouterView v-slot="{ Component, route }">
|
||||
<Transition
|
||||
:name="transitionName"
|
||||
@@ -81,10 +78,6 @@ const props = defineProps({
|
||||
},
|
||||
transitionMode: String as PropType<PageTransitionProps["transitionMode"]>,
|
||||
appear: Boolean,
|
||||
respectReducedMotion: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
keepAlive: {
|
||||
type: [Boolean, Function] as PropType<PageTransitionProps["keepAlive"]>,
|
||||
default: false
|
||||
@@ -227,7 +220,7 @@ onUnmounted(() => {
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
@easing: cubic-bezier(.19, 1, .22, 1);
|
||||
@easing: var(--tui-bezier, cubic-bezier(.19, 1, .22, 1));
|
||||
@duration: 520ms;
|
||||
@brightness: .98;
|
||||
@shadow-intensity: .15;
|
||||
@@ -255,8 +248,10 @@ onUnmounted(() => {
|
||||
:deep(.push-left-leave-active),
|
||||
:deep(.push-right-enter-active),
|
||||
:deep(.push-right-leave-active) {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
// 转场期间页面脱离文档流铺满视口,用 inset 让出应用外壳的导航区域
|
||||
inset: var(--tui-transition-inset-top, 0) 0 0 var(--tui-transition-inset-left, 0);
|
||||
width: auto;
|
||||
height: auto;
|
||||
position: fixed;
|
||||
transition: transform @duration @easing, opacity .2s linear;
|
||||
backface-visibility: hidden;
|
||||
|
||||
@@ -34,8 +34,6 @@ export interface PageTransitionProps {
|
||||
transitionMode?: "out-in" | "in-out";
|
||||
/** 是否在首次渲染时播放过渡。 */
|
||||
appear?: boolean;
|
||||
/** 是否尊重系统的减少动态效果设置,默认开启。 */
|
||||
respectReducedMotion?: boolean;
|
||||
/** 是否缓存页面,也可以按路由决定。默认不缓存。 */
|
||||
keepAlive?: PageTransitionKeepAliveMatcher;
|
||||
/** keep-alive 最多保留的页面数量。 */
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import view from "./index.vue";
|
||||
import { WebToolkit } from "timi-web";
|
||||
|
||||
export const PaneTransition = WebToolkit.withInstall(view);
|
||||
export default PaneTransition;
|
||||
@@ -0,0 +1,92 @@
|
||||
<template>
|
||||
<transition :name="transitionName" :css="direction !== 'none'">
|
||||
<slot />
|
||||
</transition>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed } from "vue";
|
||||
import type { PaneDirection } from "../../composables/use-master-detail";
|
||||
|
||||
defineOptions({
|
||||
name: "PaneTransition"
|
||||
});
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
/**
|
||||
* 切换方向。
|
||||
*
|
||||
* - forward:新内容从右侧推入,旧内容左移让位(进入下一层,如详情 → 编辑)
|
||||
* - back:新内容从左侧覆盖回来,旧内容滑出右侧(返回上一层)
|
||||
* - none:同级内容替换(换一个客户),直接切换不做动画
|
||||
*/
|
||||
direction?: PaneDirection;
|
||||
}>(), {
|
||||
direction: "forward"
|
||||
});
|
||||
|
||||
defineSlots<{
|
||||
default?: () => unknown;
|
||||
}>();
|
||||
|
||||
const transitionName = computed(() => `tui-pane-${props.direction}`);
|
||||
</script>
|
||||
|
||||
<style lang="less">
|
||||
/**
|
||||
* 栏内推入/推出效果,与 PageTransition 的 push-left / push-right 同源,
|
||||
* 区别是容器从视口换成了栏位:定位用 absolute 而非 fixed,时长略短。
|
||||
*
|
||||
* 这里不能用 scoped:过渡类是由 Vue 加到插槽内容上的,scoped 命中不了。
|
||||
*/
|
||||
@duration: 420ms;
|
||||
@easing: var(--tui-bezier, cubic-bezier(.19, 1, .22, 1));
|
||||
@offset: 25%;
|
||||
@shadow: -4px 0 16px rgba(0, 0, 0, .15);
|
||||
|
||||
// forward:新内容从右侧推入,旧内容左移
|
||||
.tui-pane-forward-enter-active,
|
||||
.tui-pane-forward-leave-active {
|
||||
position: absolute !important;
|
||||
inset: 0;
|
||||
transition: transform @duration @easing, opacity .2s linear;
|
||||
backface-visibility: hidden;
|
||||
}
|
||||
|
||||
.tui-pane-forward-enter-from {
|
||||
box-shadow: @shadow;
|
||||
transform: translate3d(100%, 0, 0);
|
||||
}
|
||||
|
||||
.tui-pane-forward-leave-to {
|
||||
filter: brightness(.98);
|
||||
transform: translate3d(-@offset, 0, 0);
|
||||
}
|
||||
|
||||
// back:新内容从左侧覆盖回来,旧内容滑出右侧
|
||||
.tui-pane-back-enter-active,
|
||||
.tui-pane-back-leave-active {
|
||||
position: absolute !important;
|
||||
inset: 0;
|
||||
transition: transform @duration @easing;
|
||||
backface-visibility: hidden;
|
||||
}
|
||||
|
||||
.tui-pane-back-enter-active {
|
||||
z-index: 0;
|
||||
}
|
||||
|
||||
.tui-pane-back-enter-from {
|
||||
transform: translate3d(-@offset, 0, 0);
|
||||
}
|
||||
|
||||
.tui-pane-back-leave-active {
|
||||
z-index: 1;
|
||||
transition: transform @duration @easing, box-shadow .3s;
|
||||
}
|
||||
|
||||
.tui-pane-back-leave-to {
|
||||
box-shadow: @shadow;
|
||||
transform: translate3d(100%, 0, 0);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,117 @@
|
||||
import {
|
||||
computed,
|
||||
inject,
|
||||
onUnmounted,
|
||||
provide,
|
||||
readonly,
|
||||
ref,
|
||||
type ComputedRef,
|
||||
type InjectionKey,
|
||||
type Ref
|
||||
} from "vue";
|
||||
|
||||
/** 布局模式:手机、平板竖屏、平板横屏。 */
|
||||
export type AppShellMode = "phone" | "tablet" | "wide";
|
||||
|
||||
/** 布局断点与形态选项,AppShell 与 useLayout 共用。 */
|
||||
export interface AppShellLayoutOptions {
|
||||
/** 是否启用平板布局;默认 true。 */
|
||||
tablet?: boolean;
|
||||
/** 进入平板布局的最小宽度,单位 px,默认 768。 */
|
||||
tabletMin?: number;
|
||||
/** 切换为左侧导航栏的最小宽度,单位 px,默认 1024。 */
|
||||
wideMin?: number;
|
||||
/** 竖屏平板是否也使用左侧导航栏;默认 false,保留底部导航。 */
|
||||
railOnPortrait?: boolean;
|
||||
/** 强制布局模式,传入后忽略断点;用于文档演示或嵌入式场景。 */
|
||||
mode?: AppShellMode;
|
||||
}
|
||||
|
||||
/** 布局上下文,由 AppShell 创建并提供给子组件。 */
|
||||
export interface LayoutContext {
|
||||
/** 当前布局模式。 */
|
||||
mode: Readonly<Ref<AppShellMode>>;
|
||||
/** 是否处于平板布局(竖屏或横屏)。 */
|
||||
isTablet: ComputedRef<boolean>;
|
||||
/** 是否处于横屏平板布局。 */
|
||||
isWide: ComputedRef<boolean>;
|
||||
/** 是否显示左侧导航栏。 */
|
||||
railVisible: ComputedRef<boolean>;
|
||||
}
|
||||
|
||||
const LAYOUT_KEY: InjectionKey<LayoutContext> = Symbol("tui-layout");
|
||||
|
||||
/** 创建媒体查询响应式状态;非浏览器环境或旧浏览器下恒为 false。 */
|
||||
function createMediaQuery(query: string): Ref<boolean> {
|
||||
const matched = ref(false);
|
||||
if (typeof window === "undefined" || typeof window.matchMedia !== "function") {
|
||||
return matched;
|
||||
}
|
||||
|
||||
const mediaQuery = window.matchMedia(query);
|
||||
matched.value = mediaQuery.matches;
|
||||
|
||||
const onChange = (event: MediaQueryListEvent): void => {
|
||||
matched.value = event.matches;
|
||||
};
|
||||
|
||||
mediaQuery.addEventListener("change", onChange);
|
||||
onUnmounted(() => mediaQuery.removeEventListener("change", onChange));
|
||||
|
||||
return matched;
|
||||
}
|
||||
|
||||
/** 创建布局上下文,供 provide 或独立使用。 */
|
||||
export function createLayoutContext(options: AppShellLayoutOptions = {}): LayoutContext {
|
||||
const tabletEnabled = options.tablet ?? true;
|
||||
const wideMatched = createMediaQuery(`(min-width: ${options.wideMin ?? 1024}px)`);
|
||||
const tabletMatched = createMediaQuery(`(min-width: ${options.tabletMin ?? 768}px)`);
|
||||
|
||||
const mode = computed<AppShellMode>(() => {
|
||||
if (options.mode) {
|
||||
return options.mode;
|
||||
}
|
||||
|
||||
if (!tabletEnabled) {
|
||||
return "phone";
|
||||
}
|
||||
|
||||
if (wideMatched.value) {
|
||||
return "wide";
|
||||
}
|
||||
|
||||
if (tabletMatched.value) {
|
||||
return "tablet";
|
||||
}
|
||||
|
||||
return "phone";
|
||||
});
|
||||
|
||||
return {
|
||||
mode: readonly(mode) as Readonly<Ref<AppShellMode>>,
|
||||
isTablet: computed(() => mode.value !== "phone"),
|
||||
isWide: computed(() => mode.value === "wide"),
|
||||
railVisible: computed(() => mode.value === "wide"
|
||||
|| (mode.value === "tablet" && !!options.railOnPortrait))
|
||||
};
|
||||
}
|
||||
|
||||
/** 把布局上下文提供给后代组件。 */
|
||||
export function provideLayout(context: LayoutContext): void {
|
||||
provide(LAYOUT_KEY, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取布局上下文。
|
||||
*
|
||||
* 优先使用 AppShell 提供的状态;脱离 AppShell 使用时按视口自行计算,
|
||||
* 默认开启平板布局,传入 options 可覆盖断点。
|
||||
*/
|
||||
export function useLayout(options?: AppShellLayoutOptions): LayoutContext {
|
||||
const injected = inject(LAYOUT_KEY, undefined);
|
||||
if (injected) {
|
||||
return injected;
|
||||
}
|
||||
|
||||
return createLayoutContext(options);
|
||||
}
|
||||
@@ -0,0 +1,308 @@
|
||||
import { inject, provide, ref, watch, type InjectionKey } from "vue";
|
||||
import { useRoute, useRouter } from "vue-router";
|
||||
import { useLayout } from "./use-layout";
|
||||
|
||||
/**
|
||||
* 右栏切换方向。
|
||||
*
|
||||
* - forward:进入下一层(详情 → 编辑)
|
||||
* - back:返回上一层(编辑 → 详情)
|
||||
* - none:同级内容替换(换一个客户),不做滑动
|
||||
*/
|
||||
export type PaneDirection = "forward" | "back" | "none";
|
||||
|
||||
/** 右栏内容与列表页之间的约定。 */
|
||||
export interface PaneContext {
|
||||
/**
|
||||
* 关闭当前栏内容,回到上一层。
|
||||
*
|
||||
* @param saved 是否有数据变更,上层可据此决定要不要刷新
|
||||
*/
|
||||
close: (saved?: boolean) => void;
|
||||
}
|
||||
|
||||
/** useMasterDetail 的配置。 */
|
||||
export interface UseMasterDetailOptions {
|
||||
/** 由选中 id 生成详情页路由,窄屏时用于跳转。 */
|
||||
route?: (id: string) => string;
|
||||
/** 初始选中项。 */
|
||||
initial?: string;
|
||||
/**
|
||||
* 视口从宽变窄时,把内联的选中项转成整页路由。
|
||||
*
|
||||
* 否则用户旋转平板或拖动窗口后,详情栏不再渲染,正在看的内容会凭空消失。
|
||||
*
|
||||
* 传 `true` 用 `route(id)` 生成目标;右栏里还有编辑这类子视图时,
|
||||
* 传函数由业务决定"当前该落到哪个页面",否则正在编辑的内容会被跳成详情。
|
||||
*/
|
||||
followViewport?: boolean | ((id: string) => string);
|
||||
/**
|
||||
* 右栏内容调用了 `usePane().close()`。
|
||||
*
|
||||
* 详情页、编辑页这类内联组件靠它把"返回/取消/保存完成"交回列表页处理,
|
||||
* 不需要自己判断该跳哪条路由。
|
||||
*/
|
||||
onClose?: (saved?: boolean) => void;
|
||||
}
|
||||
|
||||
/** useMasterDetail 的返回值。 */
|
||||
export interface UseMasterDetailReturn {
|
||||
/** 当前选中的 id,空字符串表示未选中。 */
|
||||
selectedId: ReturnType<typeof ref<string>>;
|
||||
/** 是否处于分栏形态,可直接用于 `master-detail` 的显式控制。 */
|
||||
isWide: ReturnType<typeof useLayout>["isWide"];
|
||||
/** 选中某一项:宽屏切换右栏内容,窄屏跳详情路由。 */
|
||||
open: (id: string) => void;
|
||||
/** 清空选中。 */
|
||||
clear: () => void;
|
||||
}
|
||||
|
||||
const PANE_KEY: InjectionKey<PaneContext> = Symbol("tui-pane");
|
||||
|
||||
/**
|
||||
* 读取右栏上下文。
|
||||
*
|
||||
* 返回 `null` 说明当前不在 `MasterDetail` 的详情栏里(例如整页路由打开),
|
||||
* 此时"返回"应该走路由,而不是通知列表页。
|
||||
*/
|
||||
export function usePane(): PaneContext | null {
|
||||
return inject(PANE_KEY, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 主从布局的选中态管理。
|
||||
*
|
||||
* 把"宽屏换右栏、窄屏跳路由"的分支收在一处,列表页只需要 `@click="open(item.id)"`。
|
||||
*/
|
||||
export function useMasterDetail(options: UseMasterDetailOptions = {}): UseMasterDetailReturn {
|
||||
const router = useRouter();
|
||||
const layout = useLayout();
|
||||
|
||||
const selectedId = ref(options.initial ?? "");
|
||||
const isWide = layout.isWide;
|
||||
|
||||
// 右栏里的页面通过 usePane().close() 把控制权交回来
|
||||
provide(PANE_KEY, {
|
||||
close: (saved = false) => options.onClose?.(saved)
|
||||
});
|
||||
|
||||
function open(id: string): void {
|
||||
if (!id) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isWide.value) {
|
||||
selectedId.value = id;
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.route) {
|
||||
void router.push(options.route(id));
|
||||
return;
|
||||
}
|
||||
|
||||
selectedId.value = id;
|
||||
}
|
||||
|
||||
function clear(): void {
|
||||
selectedId.value = "";
|
||||
}
|
||||
|
||||
if (options.followViewport) {
|
||||
const toRoute = typeof options.followViewport === "function"
|
||||
? options.followViewport
|
||||
: options.route;
|
||||
|
||||
if (toRoute) {
|
||||
watch(isWide, (wide, wasWide) => {
|
||||
if (wide === wasWide || wide || !selectedId.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
void router.push(toRoute(selectedId.value));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { selectedId, isWide, open, clear };
|
||||
}
|
||||
|
||||
|
||||
/** useMasterDetailPane 的配置。 */
|
||||
export interface UseMasterDetailPaneOptions {
|
||||
/** 列表页路由,转屏回到分栏时的落点。 */
|
||||
path: string;
|
||||
/** 详情整页路由。 */
|
||||
detail: (id: string) => string;
|
||||
/** 编辑整页路由;不传表示这个模块没有内联编辑视图。 */
|
||||
edit?: (id: string) => string;
|
||||
/** 初始选中项;不传则读 query。 */
|
||||
initial?: string;
|
||||
/** 初始是否处于编辑视图;不传则读 query。 */
|
||||
initialEditing?: boolean;
|
||||
/** 选中项写回 query 的字段名,默认 detail。 */
|
||||
queryKey?: string;
|
||||
/** 编辑态写回 query 的字段名,默认 view。 */
|
||||
viewQueryKey?: string;
|
||||
/** 是否把选中项和编辑态同步到 query,默认 true。 */
|
||||
syncQuery?: boolean;
|
||||
}
|
||||
|
||||
/** useMasterDetailPane 的返回值。 */
|
||||
export interface UseMasterDetailPaneReturn {
|
||||
/** 当前选中的 id。 */
|
||||
selectedId: ReturnType<typeof ref<string>>;
|
||||
/** 右栏是否处于编辑视图。 */
|
||||
editing: ReturnType<typeof ref<boolean>>;
|
||||
/** 详情重建计数,保存后自增可让详情重新加载。 */
|
||||
detailVersion: ReturnType<typeof ref<number>>;
|
||||
/** 右栏切换方向,直接传给 PaneTransition。 */
|
||||
paneDirection: ReturnType<typeof ref<PaneDirection>>;
|
||||
/** 是否处于分栏形态。 */
|
||||
isWide: ReturnType<typeof useLayout>["isWide"];
|
||||
/** 选中某一项:宽屏换右栏,窄屏跳详情页。 */
|
||||
openDetail: (id: string) => void;
|
||||
/** 右栏切到编辑;没有配置 edit 路由时无效。 */
|
||||
openEdit: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 主从分栏的完整状态机:选中项、编辑态、切换方向、关闭逻辑、转屏、query 同步。
|
||||
*
|
||||
* 列表页只需要 `openDetail(item.id)` 和 `openEdit()`,模板里把 `editing` /
|
||||
* `selectedId` / `paneDirection` 接到 `PaneTransition` 上即可,不用自己写分支。
|
||||
*
|
||||
* 编辑视图是可选的:不传 `edit` 时 `openEdit` 什么都不做,`editing` 恒为 false。
|
||||
*/
|
||||
export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMasterDetailPaneReturn {
|
||||
const router = useRouter();
|
||||
const route = useRoute();
|
||||
|
||||
const queryKey = options.queryKey ?? "detail";
|
||||
const viewKey = options.viewQueryKey ?? "view";
|
||||
|
||||
const editing = ref(options.initialEditing ?? String(route.query[viewKey] || "") === "edit");
|
||||
const detailVersion = ref(0);
|
||||
const paneDirection = ref<PaneDirection>("forward");
|
||||
|
||||
const hasEdit = typeof options.edit === "function";
|
||||
|
||||
const { selectedId, isWide, open } = useMasterDetail({
|
||||
route: options.detail,
|
||||
initial: options.initial ?? String(route.query[queryKey] || ""),
|
||||
// 宽变窄时按右栏当前视图决定落到哪个整页:正在编辑就进编辑页
|
||||
followViewport: id => (editing.value && options.edit
|
||||
? options.edit(id)
|
||||
: options.detail(id)),
|
||||
onClose: saved => {
|
||||
// 取消/保存编辑:回到详情,保存过就让详情重新加载
|
||||
if (editing.value) {
|
||||
paneDirection.value = "back";
|
||||
editing.value = false;
|
||||
if (saved) {
|
||||
detailVersion.value++;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 关闭详情:右滑出,回到空栏
|
||||
paneDirection.value = "back";
|
||||
selectedId.value = "";
|
||||
}
|
||||
});
|
||||
|
||||
function openDetail(id: string): void {
|
||||
// 右栏已经有内容时,换一项是同级内容替换,不做滑入;
|
||||
// 从空到有由外层 v-if 处理(首次挂载不播 enter),方向不影响结果
|
||||
paneDirection.value = selectedId.value ? "none" : "forward";
|
||||
editing.value = false;
|
||||
open(id);
|
||||
}
|
||||
|
||||
function openEdit(): void {
|
||||
if (!hasEdit) {
|
||||
return;
|
||||
}
|
||||
|
||||
paneDirection.value = "forward";
|
||||
editing.value = true;
|
||||
}
|
||||
|
||||
// 选中项和编辑态一起写回 query:刷新、分享链接、转屏都靠它恢复。
|
||||
// 合并成一次 replace,避免两个 watcher 互相覆盖。
|
||||
if (options.syncQuery !== false) {
|
||||
watch([selectedId, editing], ([id, isEditing]) => {
|
||||
const nextDetail = id || "";
|
||||
const nextView = isEditing && id ? "edit" : "";
|
||||
|
||||
if (String(route.query[queryKey] || "") === nextDetail
|
||||
&& String(route.query[viewKey] || "") === nextView) {
|
||||
return;
|
||||
}
|
||||
|
||||
void router.replace({
|
||||
path: options.path,
|
||||
query: {
|
||||
...route.query,
|
||||
[queryKey]: nextDetail || undefined,
|
||||
[viewKey]: nextView || undefined
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return { selectedId, editing, detailVersion, paneDirection, isWide, openDetail, openEdit };
|
||||
}
|
||||
|
||||
/** useExpandToMaster 的配置。 */
|
||||
export interface ExpandToMasterOptions {
|
||||
/** 列表页路由,通常就是 MasterDetail 所在的页面。 */
|
||||
path: string;
|
||||
/** 当前详情 id;返回空值时不动。 */
|
||||
id: () => string | undefined;
|
||||
/** 回填选中项用的 query 字段名,默认 detail。 */
|
||||
queryKey?: string;
|
||||
/**
|
||||
* 附加的 query 字段,用来把"右栏当前是哪个子视图"带回列表页。
|
||||
*
|
||||
* 例如编辑页传 `{ view: "edit" }`,列表页读出来就能直接把右栏还原成编辑态。
|
||||
* 只写这里给出的字段,不会带上当前页面的 query——编辑页的 redirect 之类不该带过去。
|
||||
*/
|
||||
query?: Record<string, string>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 整页详情在视口变宽时合并回主从分栏。
|
||||
*
|
||||
* 竖屏点进详情是整页路由,转成横屏后如果继续停在整页,用户就看不到列表了——
|
||||
* 而横屏本来就放得下两栏。这里在变宽时 replace 回列表页,并把选中项写进 query,
|
||||
* 列表页用 `initial` 读出来即可恢复右栏。
|
||||
*
|
||||
* 只在"整页"场景生效:内联在右栏时(`usePane()` 非空)不做任何事。
|
||||
* 编辑页不要调用——变宽会重建表单,用户填了一半的内容会丢。
|
||||
*/
|
||||
export function useExpandToMaster(options: ExpandToMasterOptions): void {
|
||||
const layout = useLayout();
|
||||
const router = useRouter();
|
||||
const pane = usePane();
|
||||
|
||||
watch(layout.isWide, wide => {
|
||||
if (!wide || pane) {
|
||||
return;
|
||||
}
|
||||
|
||||
const id = options.id();
|
||||
if (!id) {
|
||||
return;
|
||||
}
|
||||
|
||||
void router.replace({
|
||||
path: options.path,
|
||||
query: {
|
||||
[options.queryKey ?? "detail"]: id,
|
||||
...(options.query ?? {})
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import "./assets/style/tdesign-custom.less";
|
||||
import "./assets/style/entrance-transition.less";
|
||||
import "./assets/style/page-layout.less";
|
||||
import "./components/tiptap-content.less";
|
||||
import "timi-web/style.css";
|
||||
import "../../timi-web/src/components/prism-code-block.less";
|
||||
|
||||
Reference in New Issue
Block a user