feat(18.1-01): task 4 flat reorder in the admin SPA
- ReorderView reads flat from the reorder data; ReorderBranch takes a flat prop and shows only move up and move down - ArrowRight and ArrowLeft do nothing, an inside drop lands after the row (tree.ts flatZone, flatMove) and flatten sends parent_id null - vitest flat cases; docs describe the flat page; rebuilt dist
This commit is contained in:
@@ -58,12 +58,15 @@ export function buildTree(nodes: readonly AdminReorderNode[]): TreeNode[] {
|
||||
return roots
|
||||
}
|
||||
|
||||
/** The tree as sent: pre-order, sort_order counting from 1, parent null at the top. */
|
||||
export function flatten(tree: readonly TreeNode[]): FlatNode[] {
|
||||
/**
|
||||
* The tree as sent: pre-order, sort_order counting from 1, parent null at the
|
||||
* top. A flat reorder (D-17) sends parent null on every node.
|
||||
*/
|
||||
export function flatten(tree: readonly TreeNode[], flat = false): FlatNode[] {
|
||||
const out: FlatNode[] = []
|
||||
const walk = (list: readonly TreeNode[], parent: number | null) => {
|
||||
for (const node of list) {
|
||||
out.push({ id: node.id, parent_id: parent, sort_order: out.length + 1 })
|
||||
out.push({ id: node.id, parent_id: flat ? null : parent, sort_order: out.length + 1 })
|
||||
walk(node.children, node.id)
|
||||
}
|
||||
}
|
||||
@@ -150,6 +153,16 @@ export function applyMove(tree: TreeNode[], id: number, move: Move): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
/** The drop zone a flat reorder (D-17) uses: it never nests, so inside lands after the row. */
|
||||
export function flatZone(zone: Zone, flat: boolean): Zone {
|
||||
return flat && zone === 'inside' ? 'after' : zone
|
||||
}
|
||||
|
||||
/** The moves a flat reorder offers: siblings only, never indent or outdent. */
|
||||
export function flatMove(move: Move, flat: boolean): boolean {
|
||||
return !flat || move === 'up' || move === 'down'
|
||||
}
|
||||
|
||||
/** Whether node `dragged` may land on `target` in zone: never in its own subtree. */
|
||||
export function canDrop(tree: TreeNode[], dragged: number, target: number): boolean {
|
||||
const at = locate(tree, dragged)
|
||||
|
||||
Reference in New Issue
Block a user