@json-layout/vocabulary 0.10.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-layout/vocabulary",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "description": "Main JSON Layout vocabulary as JSON schemas and Typescript types. Also contains some small utility functions to validate and normalize annotations.",
5
5
  "type": "module",
6
6
  "exports": {
package/src/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  /** @typedef {import("./types.js").SchemaFragment} SchemaFragment */
2
2
  export * from './layout-keyword/index.js'
3
3
  export * from './normalized-layout/index.js'
4
- export { normalizeLayoutFragment } from './normalize.js'
4
+ export { normalizeLayoutFragment, getSchemaFragmentType } from './normalize.js'
package/src/normalize.js CHANGED
@@ -18,7 +18,7 @@ import { validateNormalizedLayout, compositeCompNames } from './normalized-layou
18
18
  * @returns {Children}
19
19
  */
20
20
  function getDefaultChildren (schemaFragment) {
21
- const { type } = getType(schemaFragment)
21
+ const { type } = getSchemaFragmentType(schemaFragment)
22
22
  /** @type {Children} */
23
23
  const children = []
24
24
  if (type === 'object') {
@@ -87,8 +87,8 @@ function getChildren (defaultChildren, partialChildren) {
87
87
  * @returns {import('./index.js').ComponentName}
88
88
  */
89
89
  function getDefaultComp (partial, schemaFragment, arrayChild) {
90
- const { type } = getType(schemaFragment)
91
- const hasSimpleType = ['string', 'integer', 'number'].includes(type)
90
+ const { type } = getSchemaFragmentType(schemaFragment)
91
+ const hasSimpleType = type && ['string', 'integer', 'number'].includes(type)
92
92
  if (arrayChild === 'oneOf') return 'one-of-select'
93
93
  if (hasSimpleType && schemaFragment.enum) return schemaFragment.enum.length > 20 ? 'autocomplete' : 'select'
94
94
  if (hasSimpleType && schemaFragment.oneOf) return schemaFragment.oneOf.length > 20 ? 'autocomplete' : 'select'
@@ -162,8 +162,8 @@ function normalizeExpression (expression, defaultType = 'js-eval') {
162
162
  */
163
163
  function getItemsFromSchema (schemaFragment) {
164
164
  if (!schemaFragment) return null
165
- const { type } = getType(schemaFragment)
166
- const hasSimpleType = ['string', 'integer', 'number'].includes(type)
165
+ const { type } = getSchemaFragmentType(schemaFragment)
166
+ const hasSimpleType = type && ['string', 'integer', 'number'].includes(type)
167
167
  if (schemaFragment.enum && hasSimpleType) {
168
168
  return schemaFragment.enum.map((/** @type {string} */ value) => ({ key: value + '', title: value + '', value }))
169
169
  }
@@ -194,13 +194,36 @@ function getItemsFromSchema (schemaFragment) {
194
194
 
195
195
  /**
196
196
  * @param {SchemaFragment} schemaFragment
197
- * @returns {{type: string, nullable: boolean}}
197
+ * @returns {{type: string | undefined, nullable: boolean}}
198
198
  */
199
- const getType = (schemaFragment) => {
199
+ export const getSchemaFragmentType = (schemaFragment) => {
200
200
  if (Array.isArray(schemaFragment.type) && schemaFragment.type.length === 2 && schemaFragment.type.includes('null')) {
201
201
  const type = schemaFragment.type.find(t => t !== 'null')
202
202
  return { type, nullable: true }
203
203
  }
204
+
205
+ if (!schemaFragment.type && schemaFragment.properties) {
206
+ return { type: 'object', nullable: false }
207
+ }
208
+
209
+ // case where type is not defined but can be deduced from children oneOf/allOf/anyOf
210
+ /** @type {any[]} */
211
+ if (!schemaFragment.type) {
212
+ /** @type {string[]} */
213
+ const combinationTypes = []
214
+ for (const combinationKey of ['allOf', 'anyOf', 'oneOf']) {
215
+ // @ts-ignore
216
+ if (schemaFragment[combinationKey]) {
217
+ // @ts-ignore
218
+ for (const subSchema of schemaFragment[combinationKey]) {
219
+ const { type: subType } = getSchemaFragmentType(subSchema)
220
+ if (subType && !combinationTypes.includes(subType)) combinationTypes.push(subType)
221
+ }
222
+ }
223
+ }
224
+ if (combinationTypes.length === 1) return { type: combinationTypes[0], nullable: false }
225
+ }
226
+
204
227
  return { type: schemaFragment.type, nullable: false }
205
228
  }
206
229
 
@@ -218,7 +241,7 @@ function getCompObject (layoutKeyword, schemaFragment, schemaPath, markdown, opt
218
241
  const errors = []
219
242
  const key = schemaPath.slice(schemaPath.lastIndexOf('/') + 1)
220
243
 
221
- const { type, nullable } = getType(schemaFragment)
244
+ const { type, nullable } = getSchemaFragmentType(schemaFragment)
222
245
 
223
246
  if ('const' in schemaFragment) return { normalized: { comp: 'none' }, errors }
224
247
  if (!type) return { normalized: { comp: 'none' }, errors }
@@ -439,7 +462,7 @@ function lighterValidationErrors (errors) {
439
462
  return errors.map(e => e.message ?? e.keyword)
440
463
  }
441
464
 
442
- const defaultOptionsKeys = ['readOnly', 'summary', 'titleDepth', 'density', 'removeAdditional', 'validateOn', 'initialValidation', 'defaultOn', 'readOnlyPropertiesMode']
465
+ const defaultOptionsKeys = ['readOnly', 'summary', 'titleDepth', 'density', 'removeAdditional', 'validateOn', 'initialValidation', 'defaultOn', 'readOnlyPropertiesMode', 'debounceInputMs']
443
466
 
444
467
  /**
445
468
  * @param {SchemaFragment} schemaFragment
@@ -69,6 +69,10 @@ export function isSectionLayout (layout) {
69
69
 
70
70
  export const compositeCompNames = ['section', 'tabs', 'vertical-tabs', 'expansion-panels', 'stepper']
71
71
 
72
+ // these components can received keybord inputs and emit blur events
73
+ // they will be debounced and validateOn=blur is applicable
74
+ export const editableCompNames = ['text-field', 'number-field', 'textarea', 'markdown']
75
+
72
76
  /** @type {(layout: CompObject) => layout is CompositeCompObject} */
73
77
  export function isCompositeLayout (layout) {
74
78
  return compositeCompNames.includes(layout.comp)
@@ -1092,6 +1092,11 @@ export default {
1092
1092
  "show"
1093
1093
  ],
1094
1094
  "default": "show"
1095
+ },
1096
+ "debounceInputMs": {
1097
+ "type": "integer",
1098
+ "minimum": 0,
1099
+ "default": 300
1095
1100
  }
1096
1101
  }
1097
1102
  }
@@ -519,6 +519,11 @@
519
519
  "type": "string",
520
520
  "enum": ["remove", "hide", "show"],
521
521
  "default": "show"
522
+ },
523
+ "debounceInputMs": {
524
+ "type": "integer",
525
+ "minimum": 0,
526
+ "default": 300
522
527
  }
523
528
  }
524
529
  }
@@ -60,6 +60,7 @@ export type StateNodeOptionsBase = StateNodeOptionsBaseLib & {
60
60
  defaultOn?: "missing" | "empty" | "never";
61
61
  autofocus?: boolean;
62
62
  readOnlyPropertiesMode?: "remove" | "hide" | "show";
63
+ debounceInputMs?: number;
63
64
  [k: string]: unknown;
64
65
  };
65
66
  export type Cols = number;