@json-layout/core
Advanced tools
Comparing version 0.8.1 to 0.9.0
{ | ||
"name": "@json-layout/core", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"description": "Compilation and state management utilities for JSON Layout.", | ||
@@ -24,2 +24,8 @@ "type": "module", | ||
} | ||
}, | ||
"./utils/doc-options": { | ||
"import": { | ||
"default": "./src/utils/doc-options.js", | ||
"types": "./types/utils/doc-options.d.ts" | ||
} | ||
} | ||
@@ -65,3 +71,3 @@ }, | ||
"immer": "^10.0.3", | ||
"magicast": "^0.3.0", | ||
"magicast": "^0.3.3", | ||
"markdown-it": "^13.0.2", | ||
@@ -68,0 +74,0 @@ "mitt": "^3.0.1", |
@@ -41,2 +41,3 @@ // compileStatic is meant to produce a serializable result | ||
const ajvOpts = { allErrors: true, strict: false } | ||
if (partialOptions.ajvOptions) Object.assign(ajvOpts, partialOptions.ajvOptions) | ||
if (partialOptions.code) ajvOpts.code = { source: true, esm: true, lines: true } | ||
@@ -52,3 +53,3 @@ const newAjv = new Ajv(ajvOpts) | ||
if (!markdown) { | ||
const markdownIt = new MarkdownIt(partialOptions.markdownIt ?? {}) | ||
const markdownIt = new MarkdownIt(partialOptions.markdownItOptions ?? {}) | ||
markdown = markdownIt.render.bind(markdownIt) | ||
@@ -119,4 +120,4 @@ } | ||
const expressionsParams = expression.pure | ||
? ['data', 'options', 'context', 'display'] | ||
: ['data', 'options', 'context', 'display', 'parentData', 'rootData'] | ||
? ['data', 'options', 'context', 'display', 'layout'] | ||
: ['data', 'options', 'context', 'display', 'layout', 'parentData', 'rootData'] | ||
/* if (expression.type === 'expr-eval') { | ||
@@ -123,0 +124,0 @@ expressions.push(exprEvalParser.parse(expression.expr).toJSFunction(expressionsParams.join(','))) |
@@ -6,2 +6,3 @@ // import Debug from 'debug' | ||
import { parse, print } from 'recast' | ||
import clone from '../utils/clone.js' | ||
@@ -57,3 +58,3 @@ /** | ||
skeletonTree: compiledLayout.skeletonTree, | ||
normalizedLayouts: compiledLayout.normalizedLayouts, | ||
normalizedLayouts: clone(compiledLayout.normalizedLayouts), | ||
validates: {}, | ||
@@ -60,0 +61,0 @@ validationErrors: compiledLayout.validationErrors, |
@@ -72,8 +72,13 @@ // import Debug from 'debug' | ||
if ('const' in schema) compObject.constData = { type: 'js-eval', expr: JSON.stringify(schema.const), pure: true } | ||
if (compObject.constData) pushExpression(expressions, compObject.constData) | ||
if (schema.const !== undefined && compObject.constData === undefined) compObject.constData = schema.const | ||
if (compObject.constData !== undefined && !compObject.getConstData) compObject.getConstData = { type: 'js-eval', expr: 'layout.constData', pure: true } | ||
if (compObject.getConstData) pushExpression(expressions, compObject.getConstData) | ||
if (defaultData && !compObject.defaultData) compObject.defaultData = { type: 'js-eval', expr: JSON.stringify(defaultData), pure: true } | ||
if (compObject.defaultData) pushExpression(expressions, compObject.defaultData) | ||
if (defaultData !== undefined && compObject.defaultData === undefined) compObject.defaultData = defaultData | ||
if (compObject.defaultData !== undefined && !compObject.getDefaultData) compObject.getDefaultData = { type: 'js-eval', expr: 'layout.defaultData', pure: true } | ||
if (compObject.getDefaultData) pushExpression(expressions, compObject.getDefaultData) | ||
if (compObject.options !== undefined && !compObject.getOptions) compObject.getOptions = { type: 'js-eval', expr: 'layout.options', pure: true } | ||
if (compObject.getOptions) pushExpression(expressions, compObject.getOptions) | ||
if (compObject.transformData) pushExpression(expressions, compObject.transformData) | ||
@@ -80,0 +85,0 @@ |
import type ajvModule from 'ajv/dist/2019.js' | ||
import type MarkdownIt from 'markdown-it' | ||
import { type NormalizedLayout, type StateNodeOptionsBase } from '@json-layout/vocabulary' | ||
import { type CompObject, type NormalizedLayout, type StateNodeOptionsBase } from '@json-layout/vocabulary' | ||
import { type ValidateFunction, type SchemaObject, type ErrorObject } from 'ajv/dist/2019.js' | ||
@@ -8,9 +8,18 @@ import { type Display } from '../state/utils/display.js' | ||
export type CompiledExpression = (data: any, options: StateNodeOptionsBase, context: object, display: Display, rootData?: unknown, parentData?: unknown) => any | ||
export type CompiledExpression = ( | ||
data: any, | ||
options: StateNodeOptionsBase, | ||
context: object, | ||
display: Display, | ||
layout: CompObject, | ||
rootData?: unknown, | ||
parentData?: unknown | ||
) => any | ||
export interface CompileOptions { | ||
ajv: ajvModule.default | ||
ajvOptions?: ajvModule.Options | ||
code: boolean | ||
markdown: (text: string) => string | ||
markdownIt?: MarkdownIt.Options | ||
markdownItOptions?: MarkdownIt.Options | ||
locale: string | ||
@@ -17,0 +26,0 @@ messages: LocaleMessages |
@@ -69,2 +69,3 @@ // eslint-disable-next-line import/no-named-default | ||
summary: false, | ||
density: 'default', | ||
titleDepth: 2, | ||
@@ -340,3 +341,3 @@ validateOn: 'input', | ||
const parentData = parentNode ? parentNode.data : null | ||
return evalExpression(this.compiledLayout.expressions, expression, data, node.options, new Display(node.width), parentData, this._data) | ||
return evalExpression(this.compiledLayout.expressions, expression, data, node.options, new Display(node.width), node.layout, parentData, this._data) | ||
} | ||
@@ -343,0 +344,0 @@ |
@@ -30,4 +30,4 @@ import { isSwitchStruct, childIsCompObject, isCompositeLayout, isFocusableLayout } from '@json-layout/vocabulary' | ||
// use Immer for efficient updating with immutability and no-op detection | ||
/** @type {(draft: import('./types.js').StateNode, key: string | number, fullKey: string, parentFullKey: string | null, dataPath: string, parentDataPath: string | null, skeleton: import('../index.js').SkeletonNode, layout: import('@json-layout/vocabulary').CompObject, width: number, cols: number, data: unknown, error: string | undefined, validated: boolean, options: import('./types.js').StateNodeOptions, autofocus: boolean, children: import('../index.js').StateNode[] | undefined) => import('../index.js').StateNode} */ | ||
const produceStateNode = produce((draft, key, fullKey, parentFullKey, dataPath, parentDataPath, skeleton, layout, width, cols, data, error, validated, options, autofocus, children) => { | ||
/** @type {(draft: import('./types.js').StateNode, key: string | number, fullKey: string, parentFullKey: string | null, dataPath: string, parentDataPath: string | null, skeleton: import('../index.js').SkeletonNode, layout: import('@json-layout/vocabulary').CompObject, width: number, cols: number, data: unknown, error: string | undefined, validated: boolean, options: import('./types.js').StateNodeOptions, autofocus: boolean, props: import('@json-layout/vocabulary').StateNodePropsLib, children: import('../index.js').StateNode[] | undefined) => import('../index.js').StateNode} */ | ||
const produceStateNode = produce((draft, key, fullKey, parentFullKey, dataPath, parentDataPath, skeleton, layout, width, cols, data, error, validated, options, autofocus, props, children) => { | ||
draft.messages = layout.messages ? produceStateNodeMessages(draft.messages || {}, layout.messages, options) : options.messages | ||
@@ -167,2 +167,3 @@ | ||
* @param {import('./utils/display.js').Display} display | ||
* @param {import('@json-layout/vocabulary').CompObject} layout | ||
* @param {unknown} rootData | ||
@@ -172,6 +173,6 @@ * @param {unknown} parentData | ||
*/ | ||
export function evalExpression (expressions, expression, data, options, display, rootData, parentData) { | ||
export function evalExpression (expressions, expression, data, options, display, layout, rootData, parentData) { | ||
if (expression.ref === undefined) throw new Error('expression was not compiled : ' + JSON.stringify(expression)) | ||
const compiledExpression = expressions[expression.ref] | ||
return expression.pure ? compiledExpression(data, options, options.context, display) : compiledExpression(data, options, options.context, display, rootData, parentData) | ||
return expression.pure ? compiledExpression(data, options, options.context, display, layout) : compiledExpression(data, options, options.context, display, layout, rootData, parentData) | ||
} | ||
@@ -192,3 +193,3 @@ | ||
for (const compObject of normalizedLayout.switch) { | ||
if (!compObject.if || !!evalExpression(compiledLayout.expressions, compObject.if, data, options, display, parentData, rootData)) { | ||
if (!compObject.if || !!evalExpression(compiledLayout.expressions, compObject.if, data, options, display, compObject, parentData, rootData)) { | ||
return compObject | ||
@@ -199,3 +200,3 @@ } | ||
if (normalizedLayout.if) { | ||
if (evalExpression(compiledLayout.expressions, normalizedLayout.if, data, options, display, parentData, rootData)) { | ||
if (evalExpression(compiledLayout.expressions, normalizedLayout.if, data, options, display, normalizedLayout, parentData, rootData)) { | ||
return normalizedLayout | ||
@@ -259,7 +260,7 @@ } | ||
const options = layout.options | ||
const options = layout.getOptions | ||
? produceNodeOptions( | ||
reusedNode?.options ?? /** @type {import('./types.js').StateNodeOptions} */({}), | ||
parentOptions, | ||
layout.options | ||
evalExpression(compiledLayout.expressions, layout.getOptions, data, parentOptions, display, layout, context.rootData, parentData) | ||
) | ||
@@ -400,10 +401,10 @@ : parentOptions | ||
if (layout.constData) { | ||
if (layout.getConstData) { | ||
if (!context.rehydrate) { | ||
nodeData = evalExpression(compiledLayout.expressions, layout.constData, nodeData, options, display, context.rootData, parentData) | ||
nodeData = evalExpression(compiledLayout.expressions, layout.getConstData, nodeData, options, display, layout, context.rootData, parentData) | ||
} | ||
} else { | ||
if (layout.defaultData && useDefaultData(nodeData, layout, options)) { | ||
if (layout.getDefaultData && useDefaultData(nodeData, layout, options)) { | ||
if (!context.rehydrate) { | ||
nodeData = evalExpression(compiledLayout.expressions, layout.defaultData, nodeData, options, display, context.rootData, parentData) | ||
nodeData = evalExpression(compiledLayout.expressions, layout.getDefaultData, nodeData, options, display, layout, context.rootData, parentData) | ||
} | ||
@@ -426,2 +427,7 @@ } else { | ||
let props | ||
if (layout.getProps) { | ||
props = evalExpression(compiledLayout.expressions, layout.getProps, nodeData, options, display, layout, context.rootData, parentData) | ||
} | ||
const autofocus = isFocusableLayout(layout) && !options.readOnly && !options.summary && context.autofocusTarget === fullKey | ||
@@ -444,2 +450,3 @@ const node = produceStateNode( | ||
autofocus, | ||
props, | ||
children && shallowProduceArray(reusedNode?.children, children) | ||
@@ -446,0 +453,0 @@ ) |
@@ -28,3 +28,4 @@ import { type ErrorObject } from 'ajv/dist/2019.js' | ||
type FileInput, | ||
type Child | ||
type Child, | ||
type StateNodePropsLib | ||
} from '@json-layout/vocabulary' | ||
@@ -52,2 +53,3 @@ import { type SkeletonTree, type SkeletonNode, type StatefulLayout, type CompiledLayout } from '../index.js' | ||
autofocusChild?: string | number | ||
props?: StateNodePropsLib | ||
children?: StateNode[] | ||
@@ -54,0 +56,0 @@ } |
import type ajvModule from 'ajv/dist/2019.js'; | ||
import type MarkdownIt from 'markdown-it'; | ||
import { type NormalizedLayout, type StateNodeOptionsBase } from '@json-layout/vocabulary'; | ||
import { type CompObject, type NormalizedLayout, type StateNodeOptionsBase } from '@json-layout/vocabulary'; | ||
import { type ValidateFunction, type SchemaObject } from 'ajv/dist/2019.js'; | ||
import { type Display } from '../state/utils/display.js'; | ||
import { type LocaleMessages } from '../i18n/types.js'; | ||
export type CompiledExpression = (data: any, options: StateNodeOptionsBase, context: object, display: Display, rootData?: unknown, parentData?: unknown) => any; | ||
export type CompiledExpression = (data: any, options: StateNodeOptionsBase, context: object, display: Display, layout: CompObject, rootData?: unknown, parentData?: unknown) => any; | ||
export interface CompileOptions { | ||
ajv: ajvModule.default; | ||
ajvOptions?: ajvModule.Options; | ||
code: boolean; | ||
markdown: (text: string) => string; | ||
markdownIt?: MarkdownIt.Options; | ||
markdownItOptions?: MarkdownIt.Options; | ||
locale: string; | ||
@@ -14,0 +15,0 @@ messages: LocaleMessages; |
@@ -7,2 +7,3 @@ /** | ||
* @param {import('./utils/display.js').Display} display | ||
* @param {import('@json-layout/vocabulary').CompObject} layout | ||
* @param {unknown} rootData | ||
@@ -12,3 +13,3 @@ * @param {unknown} parentData | ||
*/ | ||
export function evalExpression(expressions: import('../index.js').CompiledExpression[], expression: import('@json-layout/vocabulary').Expression, data: any, options: import('./types.js').StateNodeOptions, display: import('./utils/display.js').Display, rootData: unknown, parentData: unknown): any; | ||
export function evalExpression(expressions: import('../index.js').CompiledExpression[], expression: import('@json-layout/vocabulary').Expression, data: any, options: import('./types.js').StateNodeOptions, display: import('./utils/display.js').Display, layout: import('@json-layout/vocabulary').CompObject, rootData: unknown, parentData: unknown): any; | ||
/** | ||
@@ -15,0 +16,0 @@ * |
import { type ErrorObject } from 'ajv/dist/2019.js'; | ||
import { type CompObject, type Cols, type StateNodeOptionsBase, type TextField, type Textarea, type NumberField, type Slider, type Checkbox, type Switch, type DatePicker, type DateTimePicker, type TimePicker, type ColorPicker, type Section, type OneOfSelect, type Select, type Autocomplete, type Tabs, type VerticalTabs, type ExpansionPanels, type Stepper, type List, type Combobox, type Markdown, type FileInput, type Child } from '@json-layout/vocabulary'; | ||
import { type CompObject, type Cols, type StateNodeOptionsBase, type TextField, type Textarea, type NumberField, type Slider, type Checkbox, type Switch, type DatePicker, type DateTimePicker, type TimePicker, type ColorPicker, type Section, type OneOfSelect, type Select, type Autocomplete, type Tabs, type VerticalTabs, type ExpansionPanels, type Stepper, type List, type Combobox, type Markdown, type FileInput, type Child, type StateNodePropsLib } from '@json-layout/vocabulary'; | ||
import { type SkeletonTree, type SkeletonNode, type StatefulLayout, type CompiledLayout } from '../index.js'; | ||
@@ -23,2 +23,3 @@ import { type LocaleMessages } from '../i18n/types.js'; | ||
autofocusChild?: string | number; | ||
props?: StateNodePropsLib; | ||
children?: StateNode[]; | ||
@@ -25,0 +26,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
124637
62
2797
Updatedmagicast@^0.3.3