|
| 1 | +import type { Draft07, Draft07DefinitionProperty, Draft07DefinitionPropertyAnyOf, Draft07DefinitionPropertyAllOf, Draft07DefinitionPropertyOneOf } from '@nuxt/content' |
| 2 | +import type { FormTree, FormItem } from '../types' |
| 3 | +import { upperFirst } from 'scule' |
| 4 | + |
| 5 | +export const buildFormTreeFromSchema = (treeKey: string, schema: Draft07): FormTree => { |
| 6 | + if (!schema || !schema.definitions || !schema.definitions[treeKey]) { |
| 7 | + return {} |
| 8 | + } |
| 9 | + |
| 10 | + const buildFormTreeItem = (def: Draft07DefinitionProperty, id: string = `#frontmatter/${treeKey}`): FormItem | null => { |
| 11 | + const paths = id.split('/') |
| 12 | + const itemKey = paths.pop()! |
| 13 | + const level = paths.length - 1 // deduce #frontmatter |
| 14 | + |
| 15 | + const editor = def.$content?.editor |
| 16 | + if (editor?.hidden) { |
| 17 | + return null |
| 18 | + } |
| 19 | + |
| 20 | + // Handle two level deep for objects keys |
| 21 | + if (level <= 3) { |
| 22 | + // Handle `Of` fields |
| 23 | + if ( |
| 24 | + (def as Draft07DefinitionPropertyAllOf).allOf |
| 25 | + || (def as Draft07DefinitionPropertyAnyOf).anyOf |
| 26 | + || (def as Draft07DefinitionPropertyOneOf).oneOf |
| 27 | + ) { |
| 28 | + let item: FormItem | null |
| 29 | + const defs = (def as Draft07DefinitionPropertyAllOf).allOf |
| 30 | + || (def as Draft07DefinitionPropertyAnyOf).anyOf |
| 31 | + || (def as Draft07DefinitionPropertyOneOf).oneOf |
| 32 | + |
| 33 | + const objectDef = defs.find(item => item.type === 'object') |
| 34 | + const stringDef = defs.find(item => item.type === 'string') |
| 35 | + const booleanDef = defs.find(item => item.type === 'boolean') |
| 36 | + |
| 37 | + // Choose object type in priority |
| 38 | + if (objectDef) { |
| 39 | + item = buildFormTreeItem(objectDef, id) |
| 40 | + } |
| 41 | + |
| 42 | + // Then string type |
| 43 | + else if (stringDef) { |
| 44 | + item = buildFormTreeItem(stringDef, id) |
| 45 | + } |
| 46 | + |
| 47 | + // Else select first one |
| 48 | + else { |
| 49 | + item = buildFormTreeItem(defs[0], id) |
| 50 | + } |
| 51 | + |
| 52 | + // Handle multiple types with boolean |
| 53 | + if (item?.type !== 'boolean' && booleanDef) { |
| 54 | + item!.toggleable = true |
| 55 | + } |
| 56 | + |
| 57 | + return item |
| 58 | + } |
| 59 | + |
| 60 | + // Handle custom object form |
| 61 | + if (def.type === 'object' && def.properties) { |
| 62 | + const children = Object.keys(def.properties).reduce((acc, key) => { |
| 63 | + // Hide content internal keys |
| 64 | + const hiddenKeys = ['id', 'contentId', 'weight', 'stem', 'extension', 'path', 'meta', 'body'] |
| 65 | + if (hiddenKeys.includes(key) || def.properties![key]!.$content?.editor?.hidden) { |
| 66 | + return acc |
| 67 | + } |
| 68 | + |
| 69 | + const item = { |
| 70 | + ...acc, |
| 71 | + [key]: buildFormTreeItem(def.properties![key], `${id}/${key}`), |
| 72 | + } as FormItem |
| 73 | + |
| 74 | + return item |
| 75 | + }, {}) |
| 76 | + |
| 77 | + const item: FormItem = { |
| 78 | + id, |
| 79 | + title: upperFirst(itemKey), |
| 80 | + type: editor?.input ?? def.type, |
| 81 | + children, |
| 82 | + } |
| 83 | + |
| 84 | + if (def.enum && Array.isArray(def.enum) && def.enum.length > 0) { |
| 85 | + item.options = def.enum as string[] |
| 86 | + } |
| 87 | + |
| 88 | + return item |
| 89 | + } |
| 90 | + |
| 91 | + if (def.type === 'array' && def.items) { |
| 92 | + return { |
| 93 | + id, |
| 94 | + title: upperFirst(itemKey), |
| 95 | + type: 'array', |
| 96 | + items: buildFormTreeItem(def.items, `#${itemKey}/items`)!, |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Handle primitive types |
| 101 | + const editorType = editor?.input |
| 102 | + const type = def.type === 'string' && def.format?.includes('date') ? 'date' : editorType ?? def.type as never |
| 103 | + |
| 104 | + const item: FormItem = { |
| 105 | + id, |
| 106 | + title: upperFirst(itemKey), |
| 107 | + type: editorType ?? type, |
| 108 | + } |
| 109 | + |
| 110 | + if (def.enum && Array.isArray(def.enum) && def.enum.length > 0) { |
| 111 | + item.options = def.enum as string[] |
| 112 | + } |
| 113 | + |
| 114 | + return item |
| 115 | + } |
| 116 | + |
| 117 | + // Else edit directly as the return type |
| 118 | + const editorType = editor?.input |
| 119 | + const type = def.type === 'string' && def.format?.includes('date') ? 'date' : editorType ?? def.type |
| 120 | + |
| 121 | + const item: FormItem = { |
| 122 | + id, |
| 123 | + title: upperFirst(itemKey), |
| 124 | + type: editorType ?? type as never, |
| 125 | + } |
| 126 | + |
| 127 | + if (type === 'array' && def.items) { |
| 128 | + item.items = buildFormTreeItem(def.items, `#${itemKey}/items`)! |
| 129 | + } |
| 130 | + |
| 131 | + if (def.enum && Array.isArray(def.enum) && def.enum.length > 0) { |
| 132 | + item.options = def.enum as string[] |
| 133 | + } |
| 134 | + |
| 135 | + return item |
| 136 | + } |
| 137 | + |
| 138 | + return { |
| 139 | + [treeKey]: buildFormTreeItem(schema.definitions[treeKey]), |
| 140 | + } |
| 141 | +} |
0 commit comments