|
| 1 | +import type { VNode } from "../../index.js"; |
| 2 | +import { ok } from "../engine/result.js"; |
| 3 | +import type { LayoutTree } from "../engine/types.js"; |
| 4 | +import type { Axis, Size } from "../types.js"; |
| 5 | +import type { LayoutResult } from "../validateProps.js"; |
| 6 | + |
| 7 | +type MeasureNodeFn = (vnode: VNode, maxW: number, maxH: number, axis: Axis) => LayoutResult<Size>; |
| 8 | + |
| 9 | +type LayoutNodeFn = ( |
| 10 | + vnode: VNode, |
| 11 | + x: number, |
| 12 | + y: number, |
| 13 | + maxW: number, |
| 14 | + maxH: number, |
| 15 | + axis: Axis, |
| 16 | + forcedW?: number | null, |
| 17 | + forcedH?: number | null, |
| 18 | +) => LayoutResult<LayoutTree>; |
| 19 | + |
| 20 | +type NavigationVNode = Extract< |
| 21 | + VNode, |
| 22 | + | Readonly<{ kind: "tabs"; children: readonly VNode[] }> |
| 23 | + | Readonly<{ kind: "accordion"; children: readonly VNode[] }> |
| 24 | + | Readonly<{ kind: "breadcrumb"; children: readonly VNode[] }> |
| 25 | + | Readonly<{ kind: "pagination"; children: readonly VNode[] }> |
| 26 | +>; |
| 27 | + |
| 28 | +function toSyntheticContainer(vnode: NavigationVNode): VNode { |
| 29 | + if (vnode.kind === "tabs" || vnode.kind === "accordion") { |
| 30 | + return { kind: "column", props: {}, children: vnode.children }; |
| 31 | + } |
| 32 | + return { kind: "row", props: {}, children: vnode.children }; |
| 33 | +} |
| 34 | + |
| 35 | +function syntheticAxis(vnode: NavigationVNode): Axis { |
| 36 | + return vnode.kind === "tabs" || vnode.kind === "accordion" ? "column" : "row"; |
| 37 | +} |
| 38 | + |
| 39 | +export function measureNavigationKinds( |
| 40 | + vnode: VNode, |
| 41 | + maxW: number, |
| 42 | + maxH: number, |
| 43 | + measureNode: MeasureNodeFn, |
| 44 | +): LayoutResult<Size> { |
| 45 | + switch (vnode.kind) { |
| 46 | + case "tabs": |
| 47 | + case "accordion": |
| 48 | + case "breadcrumb": |
| 49 | + case "pagination": { |
| 50 | + const navVnode = vnode as NavigationVNode; |
| 51 | + const synthetic = toSyntheticContainer(navVnode); |
| 52 | + return measureNode(synthetic, maxW, maxH, syntheticAxis(navVnode)); |
| 53 | + } |
| 54 | + default: |
| 55 | + return { |
| 56 | + ok: false, |
| 57 | + fatal: { |
| 58 | + code: "ZRUI_INVALID_PROPS", |
| 59 | + detail: "measureNavigationKinds: unexpected vnode kind", |
| 60 | + }, |
| 61 | + }; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +export function layoutNavigationKinds( |
| 66 | + vnode: VNode, |
| 67 | + x: number, |
| 68 | + y: number, |
| 69 | + rectW: number, |
| 70 | + rectH: number, |
| 71 | + layoutNode: LayoutNodeFn, |
| 72 | +): LayoutResult<LayoutTree> { |
| 73 | + switch (vnode.kind) { |
| 74 | + case "tabs": |
| 75 | + case "accordion": |
| 76 | + case "breadcrumb": |
| 77 | + case "pagination": { |
| 78 | + const navVnode = vnode as NavigationVNode; |
| 79 | + const synthetic = toSyntheticContainer(navVnode); |
| 80 | + const axis = syntheticAxis(navVnode); |
| 81 | + const res = layoutNode(synthetic, x, y, rectW, rectH, axis, rectW, rectH); |
| 82 | + if (!res.ok) return res; |
| 83 | + return ok({ |
| 84 | + vnode, |
| 85 | + rect: { x, y, w: rectW, h: rectH }, |
| 86 | + children: res.value.children, |
| 87 | + }); |
| 88 | + } |
| 89 | + default: |
| 90 | + return { |
| 91 | + ok: false, |
| 92 | + fatal: { |
| 93 | + code: "ZRUI_INVALID_PROPS", |
| 94 | + detail: "layoutNavigationKinds: unexpected vnode kind", |
| 95 | + }, |
| 96 | + }; |
| 97 | + } |
| 98 | +} |
0 commit comments