feat: master-detail third pane
- MasterDetail / MasterDetailPage: add `sub` prop and `#sub` slot. The three columns share one container and are placed by left/right, so the detail pane moves between columns without remounting and keeps its scroll position. - useMasterDetailPane: add subView / openSub / closeSub, keep the sub view in query and browser history alongside detail and view. - page-layout: sub pane reuses the in-pane navbar rules; the list navbar slides out on the third level instead of being hidden. - docs: demos for the third pane, the layered navbars and PaneTransition; restore transition durations in demo previews under prefers-reduced-motion, where VitePress zeroes them. Fix the missing `split` ref in the master-detail demo and the incorrect note about the back arrow.
This commit is contained in:
@@ -154,7 +154,8 @@
|
||||
* 整页进入用 400ms 是合适的,但在右栏展开的同一时间播 400ms,
|
||||
* 内容会比列表缩窄晚 300ms 才完全显现,看起来像"详情跟不上"。
|
||||
*/
|
||||
.tui-master-detail .detail .entrance-fade-up-enter-active {
|
||||
.tui-master-detail .detail .entrance-fade-up-enter-active,
|
||||
.tui-master-detail .sub .entrance-fade-up-enter-active {
|
||||
transition-duration: 200ms;
|
||||
}
|
||||
|
||||
@@ -170,7 +171,22 @@
|
||||
.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;
|
||||
transition:
|
||||
width var(--tui-split-duration, 320ms) @split-easing,
|
||||
margin-left var(--tui-split-duration, 320ms) @split-easing,
|
||||
opacity .12s ease-out;
|
||||
}
|
||||
|
||||
/**
|
||||
* 第三层:列表整列滑出,列表页的导航栏跟着让位。
|
||||
*
|
||||
* 用 margin-left 而不是 transform——导航栏里可能有弹层,transform 会把它们锁进来。
|
||||
* 宽度不变、只是移出可视区,回退时按原路滑回,不用重新测量。
|
||||
*/
|
||||
.tui-page-layout:has(> .tui-page-content > .tui-master-detail.split.has-sub) > .t-navbar--fixed .t-navbar__content {
|
||||
margin-left: calc(-1 * var(--tui-master-width, 380px));
|
||||
opacity: 0;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
// 详情未选中、主栏占满时,导航栏也要满宽。
|
||||
@@ -179,15 +195,24 @@
|
||||
width: calc(100% - var(--tui-fixed-left, 0px));
|
||||
}
|
||||
|
||||
// 详情栏放的是整页组件时:栏位本身不再滚动,改由页面内容区滚动
|
||||
.tui-master-detail .detail:has(> .tui-page-layout) {
|
||||
// push 转场时 fixed 改以页面容器为定位基准,页面宽度已避开 rail,不能再次扣除 rail。
|
||||
.tui-app-shell.is-rail .page-transition-page[class*="push-"] .tui-page-layout:has(> .tui-page-content > .tui-master-detail.split.is-collapsed) > .t-navbar--fixed .t-navbar__content {
|
||||
left: 0;
|
||||
right: 0;
|
||||
width: auto;
|
||||
}
|
||||
|
||||
// 详情栏、子页栏放的是整页组件时:栏位本身不再滚动,改由页面内容区滚动
|
||||
.tui-master-detail .detail:has(> .tui-page-layout),
|
||||
.tui-master-detail .sub:has(> .tui-page-layout) {
|
||||
display: flex;
|
||||
overflow: hidden;
|
||||
padding-top: 0;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.tui-master-detail .detail > .tui-page-layout {
|
||||
.tui-master-detail .detail > .tui-page-layout,
|
||||
.tui-master-detail .sub > .tui-page-layout {
|
||||
height: 100%;
|
||||
display: flex;
|
||||
min-height: 0;
|
||||
@@ -195,7 +220,8 @@
|
||||
}
|
||||
|
||||
// 页面导航栏浮在内容之上,而不是占位:这样滚动内容才能穿进它的毛玻璃层
|
||||
.tui-master-detail .detail > .tui-page-layout > .t-navbar--fixed {
|
||||
.tui-master-detail .detail > .tui-page-layout > .t-navbar--fixed,
|
||||
.tui-master-detail .sub > .tui-page-layout > .t-navbar--fixed {
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
@@ -204,14 +230,16 @@
|
||||
}
|
||||
|
||||
// 栏内定位是相对栏位算的,必须清掉 rail 留下的左偏移,否则整条导航右移 96px
|
||||
.tui-master-detail .detail > .tui-page-layout > .t-navbar--fixed .t-navbar__content {
|
||||
.tui-master-detail .detail > .tui-page-layout > .t-navbar--fixed .t-navbar__content,
|
||||
.tui-master-detail .sub > .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 {
|
||||
.tui-master-detail .detail > .tui-page-layout > .tui-page-content,
|
||||
.tui-master-detail .sub > .tui-page-layout > .tui-page-content {
|
||||
flex: 1;
|
||||
position: static;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
:master-width="masterWidth"
|
||||
:top-inset="topInset"
|
||||
:detail-empty="!selected"
|
||||
:sub="sub"
|
||||
>
|
||||
<template #master>
|
||||
<div class="tui-master-wrap">
|
||||
@@ -22,6 +23,16 @@
|
||||
<slot name="detail" />
|
||||
</pane-transition>
|
||||
</template>
|
||||
<template #sub>
|
||||
<!--
|
||||
常驻而不是 v-if:退出第三层时子页要能播 leave 滑出右侧。
|
||||
进入时子页从右侧推入,和右栏展开同时进行;
|
||||
子页互切靠 slot 内容自己的 :key 触发。
|
||||
-->
|
||||
<pane-transition :direction="direction">
|
||||
<slot name="sub" />
|
||||
</pane-transition>
|
||||
</template>
|
||||
</master-detail>
|
||||
</main>
|
||||
</template>
|
||||
@@ -42,12 +53,14 @@ const props = withDefaults(defineProps<MasterDetailPageProps>(), {
|
||||
selected: false,
|
||||
direction: "forward",
|
||||
masterWidth: 380,
|
||||
topInset: ""
|
||||
topInset: "",
|
||||
sub: false
|
||||
});
|
||||
|
||||
defineSlots<{
|
||||
master?: () => unknown;
|
||||
detail?: () => unknown;
|
||||
sub?: () => unknown;
|
||||
}>();
|
||||
|
||||
const layout = useLayout({ tablet: true });
|
||||
|
||||
@@ -16,4 +16,12 @@ export interface MasterDetailPageProps {
|
||||
masterWidth?: number | string;
|
||||
/** 主栏顶部让出的高度,通常不需要传。 */
|
||||
topInset?: string;
|
||||
/**
|
||||
* 是否进入第三层:主栏换成详情内容,右栏显示 `#sub` 槽。
|
||||
*
|
||||
* 三层依次是:列表 → 详情 → 子页。进入时列表整列滑出左侧、详情退到主栏,
|
||||
* 返回时反向还原成「列表 + 详情」。只有分栏形态下有效,
|
||||
* 窄屏请让子页走整页路由(`usePane().openSub()` 会返回 false)。
|
||||
*/
|
||||
sub?: boolean;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
<template>
|
||||
<div
|
||||
class="tui-master-detail"
|
||||
:class="{ split: isSplit, 'is-collapsed': isSplit && detailEmpty }"
|
||||
:class="{
|
||||
split: isSplit,
|
||||
'is-collapsed': isSplit && detailEmpty,
|
||||
'has-sub': isSplit && showSub
|
||||
}"
|
||||
:style="rootStyle"
|
||||
>
|
||||
<section class="master">
|
||||
@@ -14,6 +18,13 @@
|
||||
</div>
|
||||
</slot>
|
||||
</section>
|
||||
<!--
|
||||
子页层常驻:宽度收到 0 藏在右侧边界外。
|
||||
用 v-if 新建的话,进入时没有起始值可供过渡,子页会直接"蹦"出来。
|
||||
-->
|
||||
<section v-if="isSplit" class="sub">
|
||||
<slot name="sub" />
|
||||
</section>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -32,12 +43,14 @@ const props = withDefaults(defineProps<MasterDetailProps>(), {
|
||||
split: "auto",
|
||||
emptyText: "",
|
||||
topInset: "",
|
||||
detailEmpty: false
|
||||
detailEmpty: false,
|
||||
sub: false
|
||||
});
|
||||
|
||||
defineSlots<{
|
||||
master?: () => unknown;
|
||||
detail?: () => unknown;
|
||||
sub?: () => unknown;
|
||||
}>();
|
||||
|
||||
// ---------- 分栏判定 ----------
|
||||
@@ -47,6 +60,9 @@ const layout = useLayout({ tablet: true });
|
||||
/** split 为 auto 时跟随 AppShell 的横屏状态,显式传入布尔值时以业务为准。 */
|
||||
const isSplit = computed(() => (props.split === "auto" ? layout.isWide.value : props.split));
|
||||
|
||||
/** 第三层只在有详情时成立:空栏上叠不了子页。 */
|
||||
const showSub = computed(() => props.sub && !props.detailEmpty);
|
||||
|
||||
// ---------- 尺寸 ----------
|
||||
|
||||
const rootStyle = computed<StyleValue>(() => ({
|
||||
@@ -59,6 +75,7 @@ const rootStyle = computed<StyleValue>(() => ({
|
||||
|
||||
<style scoped lang="less">
|
||||
@easing: var(--tui-bezier, cubic-bezier(.19, 1, .22, 1));
|
||||
@duration: var(--tui-split-duration, 320ms);
|
||||
|
||||
.tui-master-detail {
|
||||
flex: 1;
|
||||
@@ -82,38 +99,58 @@ const rootStyle = computed<StyleValue>(() => ({
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分栏:三层常驻在同一个容器里,用 left/right 分配位置。
|
||||
*
|
||||
* 用绝对定位而不是 flex 宽度,是因为详情要在两层之间换位置:
|
||||
* 选中时它占右栏,进入子页时它退到主栏、右栏让给子页。
|
||||
* 换父容器会让整棵子树重建(重新请求、丢滚动位置),改 left/right 只是挪动同一个元素。
|
||||
*
|
||||
* 刻意不用 transform 平移:那会把栏内页面的 fixed 弹层锁进栏位。
|
||||
*/
|
||||
&.split {
|
||||
display: block;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
|
||||
.master {
|
||||
flex: none;
|
||||
width: var(--tui-master-width);
|
||||
.master,
|
||||
.detail,
|
||||
.sub {
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
position: absolute;
|
||||
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 的变量,列表页导航栏用同一组,
|
||||
// 两边必须同拍,否则列表跳、导航滑。
|
||||
transition: left @duration @easing, right @duration @easing, opacity .12s ease-out;
|
||||
}
|
||||
|
||||
// 未选中:主栏占满,另外两层收在右侧边界外
|
||||
.master {
|
||||
left: 0;
|
||||
right: 0;
|
||||
}
|
||||
|
||||
.detail,
|
||||
.sub {
|
||||
left: 100%;
|
||||
right: 0;
|
||||
opacity: 0;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
/**
|
||||
* 详情栏里放的整页组件自动适配栏位:贴合高度、建立定位上下文。
|
||||
* 导航栏不隐藏,而是由 page-layout.less 退化成栏内头部,
|
||||
* 这样编辑等操作仍然可用。
|
||||
*/
|
||||
.detail,
|
||||
.sub {
|
||||
|
||||
/**
|
||||
* 详情栏里放的整页组件自动适配栏位:贴合高度、建立定位上下文。
|
||||
* 导航栏不隐藏,而是由 page-layout.less 退化成栏内头部,
|
||||
* 这样编辑等操作仍然可用。
|
||||
*/
|
||||
:deep(.tui-page-layout) {
|
||||
height: 100%;
|
||||
position: relative;
|
||||
@@ -121,31 +158,68 @@ const rootStyle = computed<StyleValue>(() => ({
|
||||
}
|
||||
}
|
||||
|
||||
.detail {
|
||||
background: var(--tui-master-detail-background, var(--td-bg-color-page));
|
||||
}
|
||||
|
||||
// 选中:主栏收缩到 master-width,详情从右侧展开
|
||||
&:not(.is-collapsed) {
|
||||
|
||||
.master {
|
||||
right: calc(100% - var(--tui-master-width));
|
||||
border-right: 1px solid var(--td-component-border, rgba(0, 0, 0, .08));
|
||||
}
|
||||
|
||||
.detail,
|
||||
.sub {
|
||||
opacity: 1;
|
||||
pointer-events: auto;
|
||||
}
|
||||
|
||||
.detail {
|
||||
left: var(--tui-master-width);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 未选中详情:主栏占满,右栏收起。
|
||||
*
|
||||
* 详情栏不设 display: none——那样宽度变化就没有过渡了。
|
||||
* 保持 flex 分配、把宽度压到 0,配合 overflow-x: hidden 裁掉内容,
|
||||
* 主栏宽度一过渡,右栏自然被"挤"出来。
|
||||
* 收起方向:内容比宽度先淡出。
|
||||
* 否则右栏被压到只剩几十像素时,文字会挤成竖排、按钮会卡在角上,
|
||||
* 看起来像残影而不是"被推走"。
|
||||
*/
|
||||
&.is-collapsed {
|
||||
|
||||
.master {
|
||||
width: 100%;
|
||||
border-right-color: transparent;
|
||||
}
|
||||
|
||||
.detail {
|
||||
// 收起方向:内容比宽度先淡出。
|
||||
// 否则右栏被压到只剩几十像素时,文字会挤成竖排、按钮会卡在角上,
|
||||
// 看起来像残影而不是"被推走"。
|
||||
//
|
||||
// transition 只写在这里:展开时 class 被移除,属性回到默认的瞬间切换,
|
||||
// 内容第一帧就是可见的。
|
||||
opacity: 0;
|
||||
.detail,
|
||||
.sub {
|
||||
overflow-y: hidden;
|
||||
pointer-events: none;
|
||||
transition: opacity .12s ease-out;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 第三层:列表整列滑出左侧,详情退到主栏,子页接管右栏。
|
||||
*
|
||||
* 三列宽度都是流式表达式(100% 与 px 的 calc),浏览器能连续插值,
|
||||
* 看起来就是"整条轨道往左挪一格"。列表宽度始终是 master-width,
|
||||
* 只是位置变了,所以里面的滚动位置和展开状态都还在。
|
||||
*/
|
||||
&.has-sub {
|
||||
|
||||
.master {
|
||||
left: calc(-1 * var(--tui-master-width));
|
||||
right: 100%;
|
||||
}
|
||||
|
||||
.detail {
|
||||
left: 0;
|
||||
right: calc(100% - var(--tui-master-width));
|
||||
}
|
||||
|
||||
.sub {
|
||||
left: var(--tui-master-width);
|
||||
right: 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,4 +22,13 @@ export interface MasterDetailProps {
|
||||
* 只有卡片式列表适合打开——多列宽表格从满宽压到 master-width 会挤成一团。
|
||||
*/
|
||||
detailEmpty?: boolean;
|
||||
/**
|
||||
* 是否进入第三层,默认 false。
|
||||
*
|
||||
* 三层依次是:列表 → 详情 → 子页。此时列表整列滑出左侧,详情退到主栏位置,
|
||||
* 右栏交给 `sub` 槽,形成「详情 + 子页」的两栏;返回时回到「列表 + 详情」。
|
||||
*
|
||||
* 只有分栏形态下有效,窄屏请让子页走整页路由。
|
||||
*/
|
||||
sub?: boolean;
|
||||
}
|
||||
|
||||
@@ -16,9 +16,20 @@ export interface PaneContext {
|
||||
/**
|
||||
* 关闭当前栏内容,回到上一层。
|
||||
*
|
||||
* 分层时关最上面一层:有子页先关子页,其次关编辑,最后关详情。
|
||||
*
|
||||
* @param saved 是否有数据变更,上层可据此决定要不要刷新
|
||||
*/
|
||||
close: (saved?: boolean) => void;
|
||||
/**
|
||||
* 在当前栏之上再压一层子页面(分栏内的第三层)。
|
||||
*
|
||||
* 只有分栏形态下接得住。返回 false 表示没接住——窄屏、没有选中项,
|
||||
* 或者宿主页面没有声明子页槽,调用方应当改走整页路由。
|
||||
*
|
||||
* @param view 子页标识,由列表页解析,例如 `recycle-rule:698f`
|
||||
*/
|
||||
openSub: (view: string) => boolean;
|
||||
}
|
||||
|
||||
/** useMasterDetail 的配置。 */
|
||||
@@ -43,6 +54,12 @@ export interface UseMasterDetailOptions {
|
||||
* 不需要自己判断该跳哪条路由。
|
||||
*/
|
||||
onClose?: (saved?: boolean) => void;
|
||||
/**
|
||||
* 右栏内容请求压入一个子页面。
|
||||
*
|
||||
* 返回 false 表示宿主管不了这个子页,调用方应改走整页跳转。
|
||||
*/
|
||||
onOpenSub?: (view: string) => boolean;
|
||||
}
|
||||
|
||||
/** useMasterDetail 的返回值。 */
|
||||
@@ -83,7 +100,8 @@ export function useMasterDetail(options: UseMasterDetailOptions = {}): UseMaster
|
||||
|
||||
// 右栏里的页面通过 usePane().close() 把控制权交回来
|
||||
provide(PANE_KEY, {
|
||||
close: (saved = false) => options.onClose?.(saved)
|
||||
close: (saved = false) => options.onClose?.(saved),
|
||||
openSub: view => options.onOpenSub?.(view) ?? false
|
||||
});
|
||||
|
||||
function open(id: string): void {
|
||||
@@ -164,13 +182,15 @@ export interface UseMasterDetailPaneOptions {
|
||||
queryKey?: string;
|
||||
/** 编辑态写回 query 的字段名,默认 view。 */
|
||||
viewQueryKey?: string;
|
||||
/** 子页标识写回 query 的字段名,默认 sub。 */
|
||||
subQueryKey?: string;
|
||||
/** 是否把选中项和编辑态同步到 query,默认 true。 */
|
||||
syncQuery?: boolean;
|
||||
/**
|
||||
* 是否让栏内层级进入浏览器历史,默认 true。
|
||||
*
|
||||
* 开启后「选中一项」和「进入编辑」各推一条 history,手势返回会逐级回退
|
||||
* (编辑 → 详情 → 空栏 → 离开页面),前进手势也能还原右栏。
|
||||
* 开启后「选中一项」「进入编辑」「打开子页」各推一条 history,手势返回会逐级回退
|
||||
* (子页 → 详情 → 空栏 → 离开页面),前进手势也能还原右栏。
|
||||
* 关掉则退回旧行为:层级只改 query,手势返回直接离开列表页。
|
||||
*/
|
||||
historyLevels?: boolean;
|
||||
@@ -182,6 +202,12 @@ export interface UseMasterDetailPaneReturn {
|
||||
selectedId: ReturnType<typeof ref<string>>;
|
||||
/** 右栏是否处于编辑视图。 */
|
||||
editing: ReturnType<typeof ref<boolean>>;
|
||||
/**
|
||||
* 当前栏内子页标识,空字符串表示没有子页。
|
||||
*
|
||||
* 非空时页面处于第三层:列表滑出,详情在主栏,右栏渲染 `#sub` 槽。
|
||||
*/
|
||||
subView: ReturnType<typeof ref<string>>;
|
||||
/** 详情重建计数,保存后自增可让详情重新加载。 */
|
||||
detailVersion: ReturnType<typeof ref<number>>;
|
||||
/** 右栏切换方向,直接传给 PaneTransition。 */
|
||||
@@ -192,18 +218,30 @@ export interface UseMasterDetailPaneReturn {
|
||||
openDetail: (id: string) => void;
|
||||
/** 右栏切到编辑;没有配置 edit 路由时无效。 */
|
||||
openEdit: () => void;
|
||||
/**
|
||||
* 在右栏压入一层子页面;返回 false 表示当前不是分栏,调用方应改走整页路由。
|
||||
*
|
||||
* 调用方通常是详情页:`if (!pane.openSub("recycle-rule:" + id)) router.push(...)`。
|
||||
*/
|
||||
openSub: (view: string) => boolean;
|
||||
/** 关闭子页面,回到详情层。 */
|
||||
closeSub: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 主从分栏的完整状态机:选中项、编辑态、切换方向、关闭逻辑、转屏、query 同步。
|
||||
* 主从分栏的完整状态机:选中项、编辑态、子页、切换方向、关闭逻辑、转屏、query 同步。
|
||||
*
|
||||
* 列表页只需要 `openDetail(item.id)` 和 `openEdit()`,模板里把 `editing` /
|
||||
* `selectedId` / `paneDirection` 接到 `PaneTransition` 上即可,不用自己写分支。
|
||||
* 列表页只需要 `openDetail(item.id)`、`openEdit()` 和 `subView`,
|
||||
* 模板里把 `editing` / `selectedId` / `subView` / `paneDirection` 接到
|
||||
* `MasterDetailPage` 上即可,不用自己写分支。
|
||||
*
|
||||
* 编辑视图是可选的:不传 `edit` 时 `openEdit` 什么都不做,`editing` 恒为 false。
|
||||
*
|
||||
* 栏内层级默认会进浏览器历史(见 `historyLevels`):选中一项和进入编辑各推一条,
|
||||
* 所以浏览器返回手势、物理返回键会先在栏内逐级回退(编辑 → 详情 → 空栏),
|
||||
* 子页是第三层:详情页通过 `usePane().openSub(view)` 压入,列表页用 `#sub` 槽渲染。
|
||||
* 此时布局从「列表 + 详情」变成「详情 + 子页」,返回再还原。
|
||||
*
|
||||
* 栏内层级默认会进浏览器历史(见 `historyLevels`):选中一项、进入编辑、打开子页各推一条,
|
||||
* 所以浏览器返回手势、物理返回键会先在栏内逐级回退(子页/编辑 → 详情 → 空栏),
|
||||
* 空栏时再返回才离开列表页。返回手势触发的是 query 变化,
|
||||
* `PageTransition` 按 path 和 depth 判定,不会把它当成翻页。
|
||||
*/
|
||||
@@ -213,8 +251,10 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
|
||||
const queryKey = options.queryKey ?? "detail";
|
||||
const viewKey = options.viewQueryKey ?? "view";
|
||||
const subKey = options.subQueryKey ?? "sub";
|
||||
|
||||
const editing = ref(options.initialEditing ?? String(route.query[viewKey] || "") === "edit");
|
||||
const subView = ref(String(route.query[subKey] || ""));
|
||||
const detailVersion = ref(0);
|
||||
const paneDirection = ref<PaneDirection>("forward");
|
||||
|
||||
@@ -228,9 +268,13 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
/** 从编辑态直接换一项时,先退回详情层,再由反向同步换成这个 id。 */
|
||||
const pendingSwapId = ref("");
|
||||
|
||||
/** 栏内层级:有选中项记 1 级,再进编辑记 2 级,正好对应三级返回。 */
|
||||
function levelOf(id: string | undefined, isEditing: boolean): number {
|
||||
return (id ? 1 : 0) + (isEditing && id ? 1 : 0);
|
||||
/**
|
||||
* 栏内层级:有选中项记 1 级,子页或编辑再各加 1 级。
|
||||
*
|
||||
* 子页和编辑都从详情发起,互斥,所以最多两级叠加。
|
||||
*/
|
||||
function levelOf(id: string | undefined, isEditing: boolean, view: string): number {
|
||||
return id ? 1 + (view || isEditing ? 1 : 0) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -248,13 +292,27 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
return true;
|
||||
}
|
||||
|
||||
function requestSub(view: string): boolean {
|
||||
// 窄屏没有右栏可叠;没有选中项也没有"上一层"可以退
|
||||
if (!view || !isWide.value || !selectedId.value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
pendingNav.value = "push";
|
||||
paneDirection.value = "forward";
|
||||
subView.value = view;
|
||||
return true;
|
||||
}
|
||||
|
||||
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)),
|
||||
onOpenSub: requestSub,
|
||||
onClose: saved => {
|
||||
if (popPane(saved)) {
|
||||
return;
|
||||
@@ -264,6 +322,12 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
pendingNav.value = "replace";
|
||||
paneDirection.value = "back";
|
||||
|
||||
// 有子页先关子页,回到详情
|
||||
if (subView.value) {
|
||||
subView.value = "";
|
||||
return;
|
||||
}
|
||||
|
||||
// 取消/保存编辑:回到详情,保存过就让详情重新加载
|
||||
if (editing.value) {
|
||||
editing.value = false;
|
||||
@@ -294,6 +358,8 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
// 从空到有才算开新层级;换一项不堆历史,否则连点 N 项要按 N 次返回
|
||||
pendingNav.value = selectedId.value ? "replace" : "push";
|
||||
editing.value = false;
|
||||
// 换订单时子页属于上一单,跟着一起收掉
|
||||
subView.value = "";
|
||||
open(id);
|
||||
}
|
||||
|
||||
@@ -305,23 +371,43 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
// 详情 → 编辑是加深一层,手势返回应该先退回详情
|
||||
pendingNav.value = "push";
|
||||
paneDirection.value = "forward";
|
||||
subView.value = "";
|
||||
editing.value = true;
|
||||
}
|
||||
|
||||
// 选中项和编辑态一起写回 query:刷新、分享链接、转屏都靠它恢复。
|
||||
// 合并成一次导航,避免两个 watcher 互相覆盖。
|
||||
function closeSub(): void {
|
||||
if (!subView.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 子页滑出右侧,栏宽同时收缩
|
||||
paneDirection.value = "back";
|
||||
|
||||
if (popPane()) {
|
||||
return;
|
||||
}
|
||||
|
||||
pendingNav.value = "replace";
|
||||
subView.value = "";
|
||||
}
|
||||
|
||||
// 选中项、编辑态、子页一起写回 query:刷新、分享链接、转屏都靠它恢复。
|
||||
// 合并成一次导航,避免多个 watcher 互相覆盖。
|
||||
if (options.syncQuery !== false) {
|
||||
watch([selectedId, editing], ([id, isEditing]) => {
|
||||
watch([selectedId, editing, subView], ([id, isEditing, view]) => {
|
||||
// 窄屏走的是整页详情路由,列表页不该再改 query 把它顶掉
|
||||
if (!isWide.value || route.path !== options.path) {
|
||||
return;
|
||||
}
|
||||
|
||||
const nextDetail = id || "";
|
||||
const nextView = isEditing && id ? "edit" : "";
|
||||
// 子页比编辑深一层,两者互斥:子页在时不再记编辑态
|
||||
const nextSub = id ? view : "";
|
||||
const nextView = isEditing && id && !nextSub ? "edit" : "";
|
||||
|
||||
if (String(route.query[queryKey] || "") === nextDetail
|
||||
&& String(route.query[viewKey] || "") === nextView) {
|
||||
&& String(route.query[viewKey] || "") === nextView
|
||||
&& String(route.query[subKey] || "") === nextSub) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -331,13 +417,14 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
// paneLevel 跟着 history 条目走:pop 时浏览器自动还原它,前进手势也能对上。
|
||||
// replace 只继承不抬升,取 min 是为了:直接带 ?detail=x 链接进来时这一条
|
||||
// 底下没有列表层,标成栏内层级的话再返回就会真的退出站点。
|
||||
const nextLevel = levelOf(id, isEditing);
|
||||
const nextLevel = levelOf(id, isEditing, nextSub);
|
||||
void router[nav]({
|
||||
path: options.path,
|
||||
query: {
|
||||
...route.query,
|
||||
[queryKey]: nextDetail || undefined,
|
||||
[viewKey]: nextView || undefined
|
||||
[viewKey]: nextView || undefined,
|
||||
[subKey]: nextSub || undefined
|
||||
},
|
||||
state: { paneLevel: nav === "push" ? nextLevel : Math.min(readPaneLevel(), nextLevel) }
|
||||
});
|
||||
@@ -351,15 +438,16 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
() => [
|
||||
route.path,
|
||||
String(route.query[queryKey] || ""),
|
||||
String(route.query[viewKey] || "")
|
||||
String(route.query[viewKey] || ""),
|
||||
String(route.query[subKey] || "")
|
||||
] as const,
|
||||
([path, nextId, nextView]) => {
|
||||
([path, nextId, nextView, nextSub]) => {
|
||||
if (!isWide.value || path !== options.path) {
|
||||
return;
|
||||
}
|
||||
|
||||
const nextEditing = nextView === "edit" && !!nextId;
|
||||
if (nextId === selectedId.value && nextEditing === editing.value) {
|
||||
const nextEditing = nextView === "edit" && !!nextId && !nextSub;
|
||||
if (nextId === selectedId.value && nextEditing === editing.value && nextSub === subView.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -370,12 +458,13 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
paneDirection.value = "back";
|
||||
pendingNav.value = "replace";
|
||||
editing.value = false;
|
||||
subView.value = "";
|
||||
selectedId.value = swapId;
|
||||
return;
|
||||
}
|
||||
|
||||
const nextLevel = levelOf(nextId, nextEditing);
|
||||
const previousLevel = levelOf(selectedId.value, editing.value);
|
||||
const nextLevel = levelOf(nextId, nextEditing, nextSub);
|
||||
const previousLevel = levelOf(selectedId.value, editing.value, subView.value);
|
||||
paneDirection.value = nextLevel < previousLevel
|
||||
? "back"
|
||||
: nextLevel > previousLevel
|
||||
@@ -384,6 +473,7 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
|
||||
selectedId.value = nextId;
|
||||
editing.value = nextEditing;
|
||||
subView.value = nextSub;
|
||||
|
||||
// 从保存过的编辑退回详情,让详情重新加载
|
||||
if (nextLevel < previousLevel && pendingSaved.value) {
|
||||
@@ -394,7 +484,18 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
|
||||
);
|
||||
}
|
||||
|
||||
return { selectedId, editing, detailVersion, paneDirection, isWide, openDetail, openEdit };
|
||||
return {
|
||||
selectedId,
|
||||
editing,
|
||||
subView,
|
||||
detailVersion,
|
||||
paneDirection,
|
||||
isWide,
|
||||
openDetail,
|
||||
openEdit,
|
||||
openSub: requestSub,
|
||||
closeSub
|
||||
};
|
||||
}
|
||||
|
||||
/** useExpandToMaster 的配置。 */
|
||||
|
||||
Reference in New Issue
Block a user