68 lines
3.0 KiB
TypeScript
68 lines
3.0 KiB
TypeScript
/** Small helpers for reading and editing a block tree in place. */
|
|
import type { BlkBlock, BlkParam, BlkValue } from './model.ts';
|
|
|
|
export function findBlock(block: BlkBlock, name: string): BlkBlock | undefined {
|
|
return block.blocks.find((child) => child.name === name);
|
|
}
|
|
|
|
export function findParam(block: BlkBlock, name: string): BlkParam | undefined {
|
|
return block.params.find((param) => param.name === name);
|
|
}
|
|
|
|
/** Return the first sub-block called `name`, adding an empty one if there is none. */
|
|
export function ensureBlock(block: BlkBlock, name: string): BlkBlock {
|
|
const existing = findBlock(block, name);
|
|
if (existing) return existing;
|
|
const created: BlkBlock = { name, params: [], blocks: [] };
|
|
block.blocks.push(created);
|
|
return created;
|
|
}
|
|
|
|
/** Give the first parameter called `name` this value, or append one; later duplicates are removed. */
|
|
export function setParam(block: BlkBlock, name: string, value: BlkValue): void {
|
|
const index = block.params.findIndex((param) => param.name === name);
|
|
if (index < 0) {
|
|
block.params.push({ name, value });
|
|
return;
|
|
}
|
|
block.params[index] = { name, value };
|
|
block.params = block.params.filter((param, position) => position <= index || param.name !== name);
|
|
}
|
|
|
|
/** Append a parameter called `name` without touching existing parameters of the same name (for repeated BLK keys such as `aircraftGift:t`). */
|
|
export function appendParam(block: BlkBlock, name: string, value: BlkValue): void {
|
|
block.params.push({ name, value });
|
|
}
|
|
|
|
/** Deep-clone a BLK block tree, optionally under a new block name. */
|
|
export function cloneBlock(block: BlkBlock, newName = block.name): BlkBlock {
|
|
return {
|
|
name: newName,
|
|
params: block.params.map((param) => ({
|
|
name: param.name,
|
|
value: cloneValue(param.value),
|
|
})),
|
|
blocks: block.blocks.map((child) => cloneBlock(child)),
|
|
};
|
|
}
|
|
|
|
function cloneValue(value: BlkValue): BlkValue {
|
|
switch (value.type) {
|
|
case 'point2': return { type: 'point2', value: [...value.value] as [number, number] };
|
|
case 'point3': return { type: 'point3', value: [...value.value] as [number, number, number] };
|
|
case 'point4': return { type: 'point4', value: [...value.value] as [number, number, number, number] };
|
|
case 'ipoint2': return { type: 'ipoint2', value: [...value.value] as [number, number] };
|
|
case 'ipoint3': return { type: 'ipoint3', value: [...value.value] as [number, number, number] };
|
|
case 'ipoint4': return { type: 'ipoint4', value: [...value.value] as [number, number, number, number] };
|
|
case 'matrix': return { type: 'matrix', value: [...value.value] };
|
|
default: return { ...value };
|
|
}
|
|
}
|
|
|
|
/** Visit every parameter of the tree, depth first, with the path of block names leading to it. */
|
|
export function visitParams(block: BlkBlock, visit: (param: BlkParam, path: readonly string[]) => void,
|
|
path: readonly string[] = []): void {
|
|
for (const param of block.params) visit(param, path);
|
|
for (const child of block.blocks) visitParams(child, visit, [...path, child.name]);
|
|
}
|