diff --git a/demo/index-schema.js b/demo/index-schema.js index a2b1a3f9..3ffad8be 100644 --- a/demo/index-schema.js +++ b/demo/index-schema.js @@ -265,14 +265,18 @@ const schemaInstanceJSON = { returns: { type: 'never' } + }, + + { + type: 'catch', + name: 'abc', + innerType: { + type: 'string' + } } - // Todo: add Promise, function and symbol to regular demo // Todo: add these above and in schema.js, uncomment nativeEnum there // { - // type: 'catch' - // }, - // { // type: 'nativeEnum' // } @@ -287,11 +291,12 @@ const schemaInstanceJSON = { // Sparse arrays // 'effect' - // Todo: allow function/promise/symbol to be cloneable albeit not through - // structured cloneable; note that typeson has an issue for - // symbol-iterating keys // Todo: Test/Fix functionality for `toValue`, `getInput`, `setValue`, // `getValue`, `viewUI` + + // Todo: allow function/promise/symbol to be cloneable albeit not through + // structured cloneable; note that typeson has an issue for + // symbol-iterating keys; then add to regular demo and test ] }; diff --git a/src/formats/schema.js b/src/formats/schema.js index 8db27920..b1a4accc 100644 --- a/src/formats/schema.js +++ b/src/formats/schema.js @@ -352,6 +352,7 @@ const schema = { // Todo: Fix `iterate` for schemas (e.g., inject a value method in demo) /** @type {Map} */ + // @ts-expect-error Remove after Zodex update allows `catch`, etc. const zodexToStructuredCloningTypeMap = new Map([ ['boolean', 'boolean'], ['number', 'number'], @@ -382,10 +383,10 @@ const schema = { // Todo: Allow non-cloning version to return these too, but filter out // otherwise ['function', 'function'], - ['promise', 'promise'] + ['promise', 'promise'], // Todo: Wait until added to Zodex - // ['catch', 'catch'], + ['catch', 'catch'] // ['nativeEnum', 'nativeEnum'] ]); diff --git a/src/fundamentalTypes/catchType.js b/src/fundamentalTypes/catchType.js new file mode 100644 index 00000000..b2f0d4e7 --- /dev/null +++ b/src/fundamentalTypes/catchType.js @@ -0,0 +1,72 @@ +import {$e} from '../utils/templateUtils.js'; + +/** + * @type {import('../types.js').TypeObject} + */ +const catchType = { + option: ['Catch'], + stringRegex: /^catch\((.*)\)$/u, + // Todo: Fix all the following methods up to `editUI` to work with children + toValue (s) { + return {value: s.slice(6, -1)}; + }, + getInput ({root}) { + return /** @type {HTMLTextAreaElement} */ ($e(root, 'input')); + }, + setValue ({root, value}) { + this.getInput({root}).value = value; + }, + getValue ({root}) { + return this.getInput({root}).value; + }, + viewUI ({value}) { + return ['span', {dataset: {type: 'catch'}}, [value]]; + }, + editUI ({ + format, type, buildTypeChoices, specificSchemaObject, + topRoot, schemaContent, typeNamespace + }) { + const schemaName = + // @ts-expect-error Waiting for Zodex update + /** @type {import('zodex').SzCatch} */ ( + specificSchemaObject + )?.name; + return ['div', {dataset: {type: 'catch'}}, [ + ['label', [ + ['b', ['Name']], + ' ', + ['input', { + disabled: Boolean(schemaName), + value: schemaName ?? '' + }] + ]], + ['br'], + ['label', [ + ['b', ['Value']], + ' ', + ...(/** @type {import('../typeChoices.js').BuildTypeChoices} */ ( + buildTypeChoices + )({ + // resultType, + // eslint-disable-next-line object-shorthand -- TS + topRoot: /** @type {HTMLDivElement} */ (topRoot), + // eslint-disable-next-line object-shorthand -- TS + format: /** @type {import('../formats.js').AvailableFormat} */ ( + format + ), + schemaOriginal: schemaContent, + // @ts-expect-error Waiting for Zodex update + schemaContent: /** @type {import('zodex').SzCatch} */ ( + specificSchemaObject + )?.innerType ?? {type: 'any'}, + // schemaState, + state: type, + // itemIndex, + typeNamespace + }).domArray) + ]] + ]]; + } +}; + +export default catchType; diff --git a/src/fundamentalTypes/functionType.js b/src/fundamentalTypes/functionType.js index 8488f412..a454beea 100644 --- a/src/fundamentalTypes/functionType.js +++ b/src/fundamentalTypes/functionType.js @@ -27,6 +27,13 @@ const functionType = { format, type, buildTypeChoices, specificSchemaObject, topRoot, schemaContent, typeNamespace }) { + // Todo: Could make a function instance result type which builds a Function + // like `new Function('arg1', 'return !arg1');` this just builds a + // specific function schema instance, and can't have a meaningful + // `getValue`, etc.; add to demo with tests; could also add to + // use with `Promise` so that could also build a meaningful + // implementation + // We want to allow overriding its descriptions const specificSchemaObj = copyObject(specificSchemaObject); const argsTuple = /** @type {import('zodex').SzFunction} */ ( diff --git a/src/types.js b/src/types.js index fdb6addf..62faffbf 100644 --- a/src/types.js +++ b/src/types.js @@ -54,6 +54,7 @@ import noneditableType from './fundamentalTypes/noneditableType.js'; import neverType from './fundamentalTypes/neverType.js'; import promiseType from './fundamentalTypes/promiseType.js'; import functionType from './fundamentalTypes/functionType.js'; +import catchType from './fundamentalTypes/catchType.js'; /** * Utility to retrieve the property value given a legend element. @@ -413,11 +414,11 @@ export const getPropertyValueFromLegend = (legend) => { * "arrayNonindexKeys"|"error"|"errors"|"blob"|"domexception"|"domrect"| * "dompoint"|"dommatrix"|"resurrectable"|"boolean"|"nan"|"tuple"| * "record"|"void"|"enum"|"literal"|"symbol"|"never"|"promise"| - * "function" + * "function"|"catch" * } AvailableType */ // Todo: When done with function/promise/symbol, move off here -// Todo: Add when Zodex ready: "catch"|"nativeEnum" +// Todo: Add when Zodex ready: "nativeEnum" /** * @typedef {TypeObject & { @@ -501,6 +502,7 @@ class Types { never: neverType, promise: promiseType, function: functionType, + catch: catchType, buffersource: buffersourceType, dataview: {