|
| 1 | +import { EditableElement } from "./EditableElement" |
| 2 | + |
| 3 | +export type PropType = { |
| 4 | + get: (obj: any, prop: string) => any |
| 5 | + set: (obj: any, prop: string, value: any) => void |
| 6 | + control?: any |
| 7 | + load?: (obj: any, prop: string, value: any) => Promise<any> | any |
| 8 | + init?: (obj: any, prop: string, value: any) => void |
| 9 | + serialize?: (obj: any, prop: string, input: any, value: any) => any |
| 10 | +} |
| 11 | + |
| 12 | +export function createProp( |
| 13 | + type: PropType | ((props: PropInput) => any), |
| 14 | + { |
| 15 | + element = {} as EditableElement, |
| 16 | + path = [], |
| 17 | + onChange, |
| 18 | + ...settings |
| 19 | + }: PropInput & { |
| 20 | + step?: number |
| 21 | + min?: number |
| 22 | + max?: number |
| 23 | + options?: string[] | Record<any, any> |
| 24 | + lock?: boolean |
| 25 | + default?: any |
| 26 | + } |
| 27 | +) { |
| 28 | + if (typeof type === "function") { |
| 29 | + return type({ |
| 30 | + element, |
| 31 | + path, |
| 32 | + onChange, |
| 33 | + ...settings |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + const el = element.getObjectByPath(path.slice(0, path.length - 1)) |
| 38 | + |
| 39 | + let prop = path[path.length - 1] |
| 40 | + |
| 41 | + let initialValue = el |
| 42 | + ? type.get(el, prop) ?? settings.default |
| 43 | + : settings.default |
| 44 | + |
| 45 | + let handleChange = (value: any, controlPath: string, context: any) => { |
| 46 | + element.handlePropChange({ |
| 47 | + path, |
| 48 | + input: value, |
| 49 | + controlPath, |
| 50 | + context, |
| 51 | + onChange, |
| 52 | + type |
| 53 | + } as PropChange) |
| 54 | + } |
| 55 | + |
| 56 | + return type.control |
| 57 | + ? type.control({ |
| 58 | + value: initialValue, |
| 59 | + onChange: handleChange, |
| 60 | + ...settings |
| 61 | + }) |
| 62 | + : { |
| 63 | + value: initialValue, |
| 64 | + onChange: handleChange, |
| 65 | + ...settings |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +export interface PropInput { |
| 70 | + path: string[] |
| 71 | + element: EditableElement |
| 72 | + |
| 73 | + step?: number |
| 74 | + min?: number |
| 75 | + max?: number |
| 76 | + options?: string[] | Record<any, any> |
| 77 | + lock?: boolean |
| 78 | + default?: any |
| 79 | + label?: string |
| 80 | + collapsed?: boolean |
| 81 | + |
| 82 | + onChange?: (value: any, controlPath: string) => void |
| 83 | + render?(get: (prop: string) => any): boolean |
| 84 | +} |
| 85 | + |
| 86 | +export type PropChange = { |
| 87 | + controlPath: string |
| 88 | + input: any |
| 89 | + type: PropType |
| 90 | + path: string[] |
| 91 | + context: any |
| 92 | + onChange?: (value: any, controlPath: string) => void |
| 93 | + |
| 94 | + object: any |
| 95 | + prop: string |
| 96 | + closestEditable?: EditableElement |
| 97 | + value: any |
| 98 | + remainingPath: string[] |
| 99 | +} |
| 100 | + |
| 101 | +type PropTypes<K> = { |
| 102 | + [k in keyof K]: (props: PropInput) => any |
| 103 | +} |
| 104 | + |
| 105 | +export let createPropTypes = function <K extends object>( |
| 106 | + types: K |
| 107 | +): PropTypes<K> { |
| 108 | + return Object.fromEntries( |
| 109 | + Object.entries(types).map(([k, v]) => [ |
| 110 | + k, |
| 111 | + (props: PropInput) => createProp(v, props) |
| 112 | + ]) |
| 113 | + ) as any |
| 114 | +} |
0 commit comments