export { } // import { Gestalt as Constraint, hSpace, alignCenterY, alignLeft, alignCenterX, vSpace, vAlignCenter } from './gestalt'; // import { BBoxValues, MaybeBBoxValues } from "./kiwiBBoxTransform"; // import * as Compile from './compileWithRef'; // import { objectMap, objectFilter } from "./objectMap"; // import _ from "lodash"; // /// pre-amble /// // // https://stackoverflow.com/a/49683575. merges record intersection types // type Id = T extends infer U ? { [K in keyof U]: U[K] } : never // type GlyphRelation = { fields: K[], constraints: Constraint[] }; // type Key = string | number | symbol; // type ReservedKeywords = "$canvas" | "$object" // const KONT = "KONT"; // // https://stackoverflow.com/a/50900933 // type AllowedFieldsWithType = { // [K in keyof Obj]: Obj[K] extends Type ? K : never // }; // // https://stackoverflow.com/a/50900933 // type ExtractFieldsOfType = AllowedFieldsWithType[keyof Obj] // type OmitRef = Omit> // type ExtractRefKeys = keyof T[ExtractFieldsOfType] // /// relation semantics stuff /// // export type Relation = T[] // // unwraps a Relation into a RelationInstance, and keeps RelationInstances unchanged // type RelationInstance = T extends Array ? T[number] : T // // ObjPath based on: https://twitter.com/SferaDev/status/1413761483213783045 // type StringKeys = Extract; // type NumberKeys = Extract; // type Values = O[keyof O] // type ObjPath = O extends Array // ? Values<{ [K in NumberKeys]: `[${K}]` | `[${K}]${ObjPath}` }> // : O extends Record // ? Values<{ // [K in StringKeys]: ObjPath extends "" ? `.${K}` : `.${K}${ObjPath}` // }> // : "" // // https://stackoverflow.com/a/68487744 // type addPrefix = TKey extends string // ? `${TPrefix}${TKey}` // : never; // type Ref = { $ref: true, path: addPrefix, Field> } // // K must not use reserved keywords // // https://spin.atomicobject.com/2019/03/25/disjoint-unions-typescript-conditional-types/ // export type Glyph_< // K extends object & Extract extends never ? object : never, // Reserved extends Key, // > = { // bbox?: MaybeBBoxValues, // renderFn?: (canvas: BBoxValues, index?: number) => JSX.Element, // glyphs?: Record // relations?: GlyphRelation[], // } // export type Glyph = { // [KONT]: (kont: extends never ? object : never, >(_: Glyph_) => R) => R, // } // export namespace Glyph { // export const mk = extends never ? object : never,>(exRecord_: Glyph_): Glyph => ({ // [KONT]: (kont) => kont(exRecord_) // }); // export const fromCompileGlyph = (g: Compile.GlyphNoRef): Glyph => mk({ // bbox: g.bbox, // renderFn: g.renderFn, // glyphs: objectMap(g.children, (k, v) => fromCompileGlyph(v)), // relations: g.relations?.map(({ left, right, gestalt }) => ({ fields: [left, right], constraints: gestalt })) // }) // export const inhabited: Glyph = mk({}); // } // type HostGlyphFn = (d: T) => Glyph; // // ensures T and K have disjoint keys if T is an object type // // https://spin.atomicobject.com/2019/03/25/disjoint-unions-typescript-conditional-types/ // type KConstraint = T extends object // ? (Extract extends never ? object : never) // : object // // TODOs: // // - need to test more examples // // - need to implement refs // export type GlyphFn_< // T, // K extends KConstraint // > = // T extends object ? // ( // // an arbitrary function taking in data and producing a glyph // HostGlyphFn | // // old-style making some arbitrary glyphs and putting some relations between them // // TODO: Adding the ExtractRefKeys breaks the type inference for the other unions??? // Glyph_ */> | // // you want to write some old-style glyphs, but also you want a data-driven object // // supports primitives and records // // the objectGlyph has a special name that can be used in relations // // TODO: should objectGlyph only be a HostGlyphFn or a GlyphFn more generally? // // I will generalize it for now so that everywhere you make a GlyphFn you have to call GlyphFn.mk // Id */> & { objectGlyph: GlyphFn }> | // // you might want to write some old-style glyphs and you want a data-driven record where each // // field is rendered as a separate glyph // Id & { fieldGlyphs: { [key in keyof OmitRef]: GlyphFn> } }> | // // combining both! // Id & { objectGlyph: GlyphFn } & { fieldGlyphs: { [key in keyof OmitRef]: GlyphFn> } }> // ) : ( // // same as above except no fieldGlyphs // HostGlyphFn | // Glyph_ | // Id & { objectGlyph: GlyphFn }> // ) // export type GlyphFn = { // [KONT]: (kont: >(_: GlyphFn_) => R) => R, // } // export namespace GlyphFn { // export const mk = >(exRecord_: GlyphFn_): GlyphFn => ({ // [KONT]: (kont) => kont(exRecord_) // }); // export const inhabited: GlyphFn<{}> = mk({ // fieldGlyphs: {}, // relations: [], // }); // } // const lowerGlyphRelation = (gr: GlyphRelation): Compile.Relation => ({ // left: gr.fields[0].toString(), // right: gr.fields[1].toString(), // gestalt: gr.constraints, // }); // export const lowerGlyph = (g: Glyph): Compile.Glyph => { // const kont = g[KONT]; // const glyph_ = kont((x: Glyph_) => x); // return { // bbox: glyph_.bbox, // renderFn: glyph_.renderFn, // children: glyph_.glyphs ? objectMap(glyph_.glyphs, (k, v) => lowerGlyph(v)) : {}, // relations: glyph_.relations?.map(lowerGlyphRelation), // }; // }; // // loses a bit of type safety b/c we _should_ be able to tell whether the return type is U or U[] // // based on the input, but this type doesn't tell us // // luckily this is only used internally. right??? // const mapDataRelation = (r: T, f: (d: RelationInstance) => U): U | Relation => { // if (r instanceof Array) { // return r.map(f) // } else { // // TODO: this cast seems sus, but it might not be b/c we check for array in the first branch // return f(r as RelationInstance) // } // } // // for a given relation instance, gather its ref fields so that we can lower them through the // // relation visualizations // // TODO: implementing refs properly will take a bit of thought!!! // // TODO: this function is kind of unsafe b/c it doesn't maintain the relations fields invariant and // // it introduces a $object glyph // export const glyphFnToHostGlyphFn = (gf: GlyphFn): HostGlyphFn => { // const kont = gf[KONT]; // return kont((gf: GlyphFn_): HostGlyphFn => { // if (typeof gf === "function") { // return gf; // } else { // return (data: T): Glyph => Glyph.mk({ // bbox: gf.bbox, // renderFn: gf.renderFn, // glyphs: { // // fieldGlyphs // // TODO: is it possible to get rid of this `any`? // ...("fieldGlyphs" in gf ? objectMap(gf.fieldGlyphs, (k, v: GlyphFn>) => { // const loweredGlyphs = mapDataRelation(data[k], glyphFnToHostGlyphFn(v)); // if (loweredGlyphs instanceof Array) { // return Glyph.mk({ // glyphs: loweredGlyphs.reduce((o, g, i) => ({ // ...o, [i]: g // }), {}) // }); // } else { // return loweredGlyphs; // } // }) : {}), // /* TODO: this is wrong, but removing it is wrong, too */ // // refs // ...(typeof data === "object" ? objectFilter(data, (k, v) => (typeof v === "object") && ("$ref" in v)) : {}), // // glyphs // ...gf.glyphs, // // objectGlyph // ...("objectGlyph" in gf ? { "$object": glyphFnToHostGlyphFn(gf.objectGlyph)(data) } as any : {}), // }, // relations: gf.relations as any, // }); // } // }) // } // // TODO: maybe want to use RelationInstances here, but it seems like it's subtle to do that well so // // we will defer it // export const lowerGlyphFn = (gf: GlyphFn): ((data: T) => Compile.Glyph) => { // const kont = gf[KONT]; // return kont((gf: GlyphFn_): ((data: T) => Compile.Glyph) => // (data: T) => { // if (typeof gf === "function") { // // HostGlyphFn // return lowerGlyph(gf(data)); // } else { // return { // bbox: gf.bbox, // renderFn: gf.renderFn, // children: { // // fieldGlyphs // // TODO: is it possible to get rid of this `any`? // ...("fieldGlyphs" in gf ? objectMap(gf.fieldGlyphs, (k, v: GlyphFn>) => { // const loweredGlyphs = mapDataRelation(data[k], lowerGlyphFn(v)); // if (loweredGlyphs instanceof Array) { // return { // children: loweredGlyphs.reduce((o, g, i) => ({ // ...o, [i]: g // }), {}) // }; // } else { // return loweredGlyphs; // } // }) : {}), // // refs // ...(typeof data === "object" ? objectFilter(data, (k, v) => (typeof v === "object") && ("$ref" in v)) : {}), // // glyphs // ...(gf.glyphs ? objectMap(gf.glyphs, (_k, v) => lowerGlyph(v)) : {}), // // objectGlyph // ...("objectGlyph" in gf ? { "$object": lowerGlyphFn(gf.objectGlyph)(data) } : {}), // }, // relations: gf.relations?.map(lowerGlyphRelation), // } // } // }) // }; // export type MyRef = { // $ref: true, // path: string, // } // export const mkMyRef = (path: string): MyRef => ({ // $ref: true, // path, // }) // // TODO: to get refs to work, I think all I need to do is push them into the relations as paths!! // // ok but maybe instead resolve all paths to absolute paths from top-level input // // then push those absolute paths into the relations // // then resolve those absolute paths in the compiler? // type BluefishData = // | string // | number // | boolean // | null // | BluefishData[] // | { [key: string]: BluefishData } // // | Ref // const parsePath = (path: string): (number | string)[] => path.split('/').map((s) => s === ".." ? -1 : !isNaN(parseFloat(s)) ? +s : s); // // O extends Array // // ? Values<{ [K in NumberKeys]: `[${K}]` | `[${K}]${ObjPath}` }> // // : O extends Record // // ? Values<{ // // [K in StringKeys]: ObjPath extends "" ? `.${K}` : `.${K}${ObjPath}` // // }> // // : "" // // type PathTree = any; // // type PathTree = T extends Array // // ? { [key: number]: PathTree } // // : T extends object // // ? { [key in keyof T]: PathTree } : T; // // const makePathTree = (data: BluefishData): PathTree => { // // if (Array.isArray(data)) { // // return data.reduce((o: PathTree, v, i) => ({ // // ...o, [i]: makePathTree(v) // // }), {}); // // } else if (typeof data === "object") { // // return objectMap(data, (k, v) => makePathTree(v)); // // } else { // // return data; // // } // // } // // takes a relative path and makes it absolute // // relative path must be of the form (more or less): (../)*([a-z0-9]+/)* // // basically we allow ../, but only at the beginning (for simplicity of implementation) // // and the rest is a local absolute path. neither piece is required // const resolvePath = (pathList: (number | string)[], pathFromRoot: Key[]): string[] => { // if (pathList.length === 0) { // return pathFromRoot as string[]; // } else { // const [hd, ...tl] = pathList; // if (hd === -1) { // // step up // return resolvePath(tl, pathFromRoot.slice(1)); // } else { // // step down // return resolvePath(tl, [hd, ...pathFromRoot]); // } // } // } // const makePathsAbsolute = (data: BluefishData, pathFromRoot: Key[] = []): BluefishData => { // console.log("current path from root", pathFromRoot); // if (data === null) { // return data // } else if (Array.isArray(data)) { // array/relation // return data.map((d, i) => makePathsAbsolute(d, [i, ...pathFromRoot])); // } else if (typeof data === "object" && !("$ref" in data)) { // object/record/instance // return objectMap(data, (k, v) => makePathsAbsolute(v, [k, ...pathFromRoot])) // } else if (typeof data === "object") { // ref (where the actual work is done!) // const ref = data as unknown as Ref; // console.log("making this ref absolute", ref, pathFromRoot); // const pathList = parsePath(ref.path); // // automatically bump one level up when resolving paths // const absolutePath = resolvePath(pathList, pathFromRoot.slice(1)); // console.log("absolute path", absolutePath); // return { $ref: true, path: absolutePath }; // } else { // return data; // } // } // // TODO: make refs to refs work??? // // TODO: // // TODO: makePathsAbsolute should be called in the compiler I think. // // using this, bounding boxes can be looked up easily // // like lowerGlyphFn, but makes paths absolute // export const compileGlyphFn = (gf: GlyphFn) => // (data: T): Compile.Glyph => lowerGlyphFn(gf)(makePathsAbsolute(data as unknown as BluefishData) as unknown as T) // export type MyList = { // elements: Relation, // // TODO: can refine Ref type even more to say what it refers to // neighbors: Relation<{ // curr: MyRef, // next: MyRef, // }> // }