add app-shell, master-detail, pane-transition
This commit is contained in:
@@ -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 ?? {})
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user