import { CompileOptions, Draft, JsonError, SchemaNode } from 'json-schema-library'; import { JsonSchema, Change, JsonNode, ArrayNode, PluginEvent } from './types'; export type O = Record; export type PluginInstance = { id: string; /** Called for all events, e.g. `create`, `update`, `delete`, `undo`, etc. * Each event loop is finished by a done-event, which contains a list of modified nodes * * `onEvent` may return a new root-node along with a list of changes */ onEvent: (root: JsonNode, event: PluginEvent) => void | [JsonNode, Change[]]; /** Called when Editor before Editor is going to be destroyed */ onDestroy?: () => void; } & Signature; export type Plugin[1] = any, Signature extends Record = Record, Editor extends HeadlessEditor = HeadlessEditor> = (he: Editor, options: Options) => PluginInstance | undefined; export type HeadlessEditorOptions = { /** The JSON Schema describing the form and its resulting data. JSON Schema `type` keywords are required for json-editor to work correctly. */ schema: JsonSchema; /** * list of remote JSON Schema to add for $ref resolution. Each JSON Schema requires an $d or it will throw * @throws */ remotes?: JsonSchema[]; /** The initial, partial data to be used to fill the form with values. The data has to match the JSON Schema or it will be ignored or invalidated in the we form */ data?: Data; drafts?: Draft[]; plugins?: Plugin[]; /** if data should be initially validated */ validate?: boolean; /** if all optional properties should be added when missing */ addOptionalProps?: boolean; /** if json-schema default-values should be extended (required properties, min-length of items). Defaults to false */ extendDefaults?: boolean; /** Remove data that does not match input schema. Defaults to false */ removeInvalidData?: boolean; /** Set to true to throw an Error when encountering an invalid JSON Schema */ throwOnInvalidSchema?: boolean; /** Set to true to throw an Error when encountering an unresolvable ref */ throwOnInvalidRef?: boolean; [p: string]: unknown; } & O; /** * - Manages the current node tree in state to simplify changing and updating the nodes * - Exposes a plugin api * * @todo test setSchema * @todo difference between setValue and setData (root?)? */ export declare class HeadlessEditor { readonly id: string; /** Editor root node */ root: JsonNode; /** JSON Schema API */ schemaNode: SchemaNode; /** JSON Schema API options */ schemaNodeConfig: CompileOptions; /** list of active plugins */ plugins: PluginInstance[]; /** input options for this editor instance */ options: HeadlessEditorOptions; constructor(options: HeadlessEditorOptions); get optionalProperties(): boolean; /** * Returns the current json data * @param pointer A JSON Pointer string of the data to return. Use `undefined` or `""` to get all data or specify a path like `/header` or `/todo/4` to get the data from the requested location */ getData(pointer?: string): unknown; /** * Get current data or a subset of the data specified by a JSON Pointer * * @param data to set * @returns new root node */ setData(data?: Data): JsonNode; /** * Shortcut to return default data based on json-schema only * @param schema json-schema to get default data from. Defaults to current schema * @param data optional data to merge with default data * @returns default data confirming to json-schema */ getTemplateData(schema: JsonSchema, data?: null): any; /** * @returns Input JSON Schema of the current form */ getSchema(): import("json-schema-library").JsonSchema; /** * Replace the JSON Schema and recreate the JsonNode tree of the form * @param schema JSON Schema of the new form * @returns The new root node for the given JSON Schema */ setSchema(schema: JsonSchema): JsonNode; /** * Run validation for the current form and update the list of errors * @returns List of validation errors. See [getErrors](#getErrors) for details */ validate(): JsonError[]; /** * Get current validation errors of node-tree. * An error contains the following properties * `{ type: "error", code: string, message: string, data }`. * The property `data` contains details to the error. * For further details check [json-schema-library#validate](https://github.com/sagold/json-schema-library#validate) * * @returns Current list of form validation errors of the current state. */ getErrors(): JsonError[]; /** * Set new editor state (new tree of nodes) * @returns new root node */ setState(state: JsonNode, changes: PluginEvent[]): JsonNode; /** * Get current root-node * @returns root node */ getNode(): JsonNode; /** * Get current node at json-pointer location * @returns node at requested location or json-error */ getNode(pointer: string): JsonNode | JsonError; /** * Get a plugin by pluginId */ findPlugin(pluginId: string): PluginInstance | undefined; addPlugin(plugin: T, pluginOptions?: Parameters[1]): ReturnType | undefined; /** * Runs all plugins per given change, allowing them to return a modified newState * * @returns The new root node */ runPlugins(oldState: JsonNode, newState: JsonNode, changes: PluginEvent[]): JsonNode; /** * Set or replace a value at given data location * @param pointer json-pointer in data of target value * @param value value to set at location * @returns new root node or current one if nothing changed */ setValue(pointer: string, value: unknown): JsonNode; /** * Add a value at a given location. This usually is used to add array-items * @caveat addValue **only supports adding properties** * * @param pointer A JSON Pointer string to the value to add * @returns The new root node */ addValue(pointer: string): JsonNode; /** * Delete the value at the given JSON Pointer * @param pointer A JSON Pointer string to the node which should be removed. Specify a data path like `/header` or `/todo/4` to remove the node from the specified location * @returns The new root node */ removeValue(pointer: string): JsonNode; /** * Move the referenced **array-item** to another array position * @param pointer A JSON Pointer string to the node * @param to The index to move the array item to * @returns The new root node */ moveItem(pointer: string, to: number): JsonNode; /** * Add an item defined by a JSON Schema to the end of the target array * @param node ArrayNode of item * @param itemSchema JSON Schema of the item to add * @returns The new root node */ appendItem(node: ArrayNode, itemSchema: JsonSchema): JsonNode; /** * Get all available items that can be added next to the specified array * @returns List of available JSON subschemas to insert into the array. Access the JSON Schema of each option by `SchemaNode.schema`. For more details see [json-schema-library#schemanode-methods](https://github.com/sagold/json-schema-library#schemanode-methods) */ getArrayAddOptions(node: ArrayNode): SchemaNode[]; /** * destroys editor instance including all plugins, releasing used memory. * * @caveat plugins will receive an onDestroy call once and are responsible to release memory on their own */ destroy(): void; }