import { type LocalisedStrings, type TranslateFunction } from './localisation' import { CustomNodeData } from './CustomNode' import { type SelectProps } from './NativeSelect' export type JsonData = Record | Array | unknown export interface JsonEditorProps { data: T setData: (data: T) => void rootName?: string onUpdate?: UpdateFunction onChange?: OnChangeFunction onError?: OnErrorFunction showErrorMessages?: boolean showClipboardButton?: boolean theme?: ThemeInput className?: string id?: string indent?: number collapse?: boolean | number | FilterFunction collapseAnimationTime?: number // ms showCollectionCount?: boolean | 'when-collapsed' | 'when-collapsed-or-filtered' allowEdit?: boolean | FilterFunction allowDelete?: boolean | FilterFunction allowAdd?: boolean | FilterFunction allowTypeSelection?: boolean | TypeOptions | TypeFilterFunction allowDrag?: boolean | FilterFunction searchText?: string searchFilter?: 'key' | 'value' | 'all' | SearchFilterFunction searchDebounceTime?: number sortKeys?: boolean | CompareFunction showArrayIndexes?: boolean arrayIndexStart?: number showStringQuotes?: boolean showIconTooltips?: boolean defaultValue?: string | number | boolean | null | object | DefaultValueFunction newKeyOptions?: string[] | NewKeyOptionsFunction minWidth?: string | number maxWidth?: string | number baseFontSize?: string | number stringTruncateLength?: number translations?: Partial // Using "any" here, as internal props don't matter, the generic is just for // enforcing consistency between the component and the definition that uses it // eslint-disable-next-line @typescript-eslint/no-explicit-any customNodeDefinitions?: CustomNodeDefinition, Record>[] customText?: CustomTextDefinitions customButtons?: CustomButtonDefinition[] jsonParse?: (input: string, reviver?: (key: string, value: string) => unknown) => JsonData jsonStringify?: (input: JsonData, replacer?: (key: string, value: unknown) => unknown) => string TextEditor?: React.FC Select?: React.ComponentType errorDisplayTime?: number // ms keyboardControls?: KeyboardControls insertAtTop?: boolean | 'array' | 'object' collapseClickZones?: Array<'left' | 'header' | 'property'> // Additional events onEditEvent?: OnEditEventFunction onCollapse?: OnCollapseFunction onCopy?: OnCopyFunction // Imperative handle — see `JsonEditorHandle`. Attach with `useRef` and the // `editorRef` prop (a plain ref-valued prop, not the `ref` attribute, so the // component stays a generic function with full type inference). editorRef?: React.Ref } export interface StartEditOptions { /** The target node to open a value-edit session on. */ path: CollectionKey[] /** * Bypass `allowEdit` (default `false`). When `false`, the filter is * evaluated for this node at call time and `startEdit` is a no-op (returning * `'RESTRICTED'`) if editing isn't permitted; when `true`, the session opens * regardless — the intended use is to lock the tree and imperatively enable a * single node. Skips ONLY the filter, never `onUpdate`: the consumer's * `onUpdate` still runs (and may veto) at `confirm()`. */ overrideRestrictions?: boolean } /** * The result of `editorRef.startEdit`: `true` if it opens a value-edit session, * or the reason it doesn't — `'PATH_NOT_FOUND'` (target gone) or `'RESTRICTED'` * (`allowEdit` blocked it, and `overrideRestrictions` wasn't set). */ export type StartEditResult = true | 'RESTRICTED' | 'PATH_NOT_FOUND' /** * Imperative handle exposed via the `editorRef` prop. The commands drive editor * *UI* a consumer can't otherwise reach — they open a value-edit session, * confirm/cancel it, or collapse nodes. They deliberately do NOT mutate data * directly: the consumer owns `data`/`setData`, so changing a value is just * `setData(newData)` and the editor reflects it. */ export interface JsonEditorHandle { /** Collapse/expand a node (or subtree, with `includeChildren`). */ collapse: (state: CollapseState | CollapseState[]) => void /** * Open a value-edit session at a node. Respects `allowEdit` unless * `overrideRestrictions` is set; auto-reveals a target collapsed below the * mount frontier. Returns `true` if it opened, else why not — see * `StartEditResult`. */ startEdit: (options: StartEditOptions) => StartEditResult /** Commit the active session (clicks the live confirm control), then exit. */ confirm: () => void /** * Abort the active session without committing (fires the matching `cancel*` * event). */ cancel: () => void } /** * The `editorRef` handle for `JsonViewer` — collapse only; editing is not * exposed in a read-only context. */ export type JsonViewerHandle = Pick export type JsonViewerProps = Omit< JsonEditorProps, | 'setData' | 'onUpdate' | 'onChange' | 'allowEdit' | 'allowAdd' | 'allowDelete' | 'allowDrag' | 'allowTypeSelection' | 'editorRef' > & { /** Collapse-only imperative handle — see `JsonViewerHandle`. */ editorRef?: React.Ref } export const valueDataTypes = ['string', 'number', 'boolean', 'null'] as const export const collectionDataTypes = ['object', 'array'] as const export const standardDataTypes = [...valueDataTypes, ...collectionDataTypes] as const export type CollectionDataType = (typeof collectionDataTypes)[number] export type DataType = (typeof standardDataTypes)[number] | 'invalid' export type CollectionKey = string | number export type CollectionData = object | unknown[] export interface EnumDefinition { enum: string values: string[] matchPriority?: number } export type TypeOptions = Array export type ErrorString = string export type TabDirection = 'next' | 'prev' // The operation an active/held session represents. Editor ops (`edit`/`rename`/ // `add`) open an inline UI; `delete`/`move` are instant and only occupy the // active slot while a `hold()` gate is running (phase `held`). export type EditOperation = 'edit' | 'rename' | 'add' | 'delete' | 'move' export interface EditingState { path: CollectionKey[] op: EditOperation // `editing` = the inline UI is open (pre-submit); `held` = submitted and a // `hold()` gate is running (tree blocked) until `release()`/settlement. phase: 'editing' | 'held' // Set when the session was started imperatively via the `editorRef` handle. // A forced edit overrides the `allowEdit` filter — the node-skip redirect // in ValueNodeWrapper leaves it in place instead of bouncing off it. force?: boolean } export interface TextEditorProps { value: string onChange: (value: string) => void onKeyDown: (e: React.KeyboardEvent) => void } /** * FUNCTIONS */ /** * The definitive error code list — surfaced via the `onError` observer and * `UpdateResult.error`. */ export type JerErrorCode = | 'UPDATE_ERROR' // an edit was rejected, or an internal failure occurred | 'ADD_ERROR' // an add was rejected | 'DELETE_ERROR' // a delete was rejected | 'RENAME_ERROR' // a key rename was rejected | 'MOVE_ERROR' // a drag-and-drop move was rejected | 'KEY_EXISTS' // a new/renamed key collides with an existing sibling | 'INVALID_JSON' // raw JSON typed into the editor failed to parse | 'CLIPBOARD_ERROR' // a copy-to-clipboard write failed (onCopy) /** * The one canonical error shape, used everywhere an error is produced or * reported. */ export interface JerError { code: JerErrorCode message: string } /** * The one canonical update result. `void`/`undefined`/`true` commit; `false` * rejects with a generic error; `null` is a silent abort (no commit, no * error); the object form overrides what gets committed, or rejects with a * custom `error` (a bare `string` is wrapped into a `JerError`). * * The two override keys differ in scope: * - `value` — the edited node's value, applied at its path. Mirrors the * `newValue` the callback receives. Honoured only for `edit`/`add`; ignored * for `rename`/`move`/`delete` (the commit proceeds unchanged). * - `data` — the whole document, applied at the root. Mirrors `newData`. Works * on every event. * * Returning both is a mistake: `data` (the broader operation) wins and `value` * is ignored (with a dev-time warning). */ export type UpdateResult = | true | void | undefined | false | null | { value?: unknown data?: T error?: string | JerError } /** * `NodeData` carries the CURRENT identity/value; the event-specific field * carries the NEW bit; `newData` is always the resulting document. `rename` and * `move` are first-class events even though both are delete+add under the * hood — they arrive via distinct user interactions and carry distinct deltas. * For `add`, `NodeData` describes the new node's *position* (`path`/`key`); * `value` is unset until commit. */ export type UpdateFunctionProps = NodeData & { newData: T } & ( | { event: 'edit'; newValue: unknown } // value changes (incl. type change) | { event: 'add'; newValue: unknown } | { event: 'delete' } // `newData` reflects the removal // `NodeData.key`/`path` = OLD; use `newKey` + `newData`. Only object keys // are renameable, so always a string | { event: 'rename'; newKey: string } // `NodeData.path` = source; `newPath` = destination | { event: 'move'; newPath: CollectionKey[] } ) /** * Control object passed as the 2nd arg to `onUpdate`. `hold()` opts this commit * out of the default optimistic close — the editor stays open and the tree is * blocked ("gate") until the returned `release()` is called (or `onUpdate` * settles). MUST be called synchronously, before the first `await`. */ export interface UpdateControl { hold: () => () => void } /** * One `onUpdate` — branch on `event`. Fires for user- and command-driven alike. */ export type UpdateFunction = ( props: UpdateFunctionProps, control: UpdateControl ) => UpdateResult | Promise> /** Transform: returns the value itself, not an `UpdateResult`. */ export type OnChangeFunction = ( props: NodeData & { newValue: ValueData } ) => ValueData /** * Observer: fires after any error condition. */ export type OnErrorFunction = ( props: NodeData & { error: JerError; errorValue: JsonData } ) => void export type FilterFunction = (input: NodeData) => boolean export type TypeFilterFunction = (input: NodeData) => boolean | TypeOptions export type CustomTextFunction = (input: NodeData) => string | null export type DefaultValueFunction = (input: NodeData, newKey?: string) => unknown export type SearchFilterFunction = ( inputData: NodeData, searchText: string ) => boolean export type NewKeyOptionsFunction = (input: NodeData) => string[] | null | void export type CopyType = 'path' | 'value' // Observer: fires after a copy-to-clipboard. A failed copy carries a // `CLIPBOARD_ERROR`. export type OnCopyFunction = ( props: NodeData & { success: boolean stringValue: string type: CopyType error?: JerError } ) => void export type CompareFunction = ( a: [string | number, unknown], b: [string | number, unknown] ) => number export type SortFunction = (arr: T[], nodeMap: (input: T) => [string | number, unknown]) => void /** * Observer: the complete interaction-lifecycle stream. Value-edit, key-rename * and add sessions open with a `start*`, then `submit*` (the user committed; a * `hold()` gate may run), then terminate with `commit*` (applied — editor * closed) or `cancel*` (closed without applying — Esc/✗, or a `null` gate). * `delete`/`move` are instant (one event at commit). When `onUpdate` runs, the * background settlement reports `updateSuccess` / `updateError` after the * `commit*`/`delete`/`move`. `commitRename` carries `{ oldKey, newKey }` and * `updateError` the `error`; both settlement events carry the `operation`, so * interleaved background settlements can be correlated. */ export type EditEvent = NodeData & ( | { event: 'startEdit' } | { event: 'submitEdit' } | { event: 'commitEdit' } | { event: 'cancelEdit' } | { event: 'startRename' } | { event: 'submitRename' } | { event: 'commitRename'; oldKey: CollectionKey; newKey: CollectionKey } | { event: 'cancelRename' } | { event: 'startAdd' } | { event: 'submitAdd' } | { event: 'commitAdd' } | { event: 'cancelAdd' } | { event: 'delete' } | { event: 'move' } | { event: 'updateSuccess'; operation: EditOperation } | { event: 'updateError'; operation: EditOperation; error: JerError } ) export type OnEditEventFunction = (e: EditEvent) => void // The `editorRef.collapse` command input. Distinct from the `onCollapse` // observer payload, which is flat `NodeData`. export interface CollapseState { path: CollectionKey[] collapsed: boolean includeChildren: boolean } /** * Observer: fires on collapse/expand, from a user click or * `editorRef.collapse`. */ export type OnCollapseFunction = ( props: NodeData & { collapsed: boolean; includeChildren: boolean } ) => void // Builds a node's full `NodeData` from its path against the LIVE document. // Bridged via a ref from the inner `Editor` into the editing/collapse providers // (its ancestors), so observer events fired from those contexts (`onEditEvent` // start*/cancel*, `onCollapse` broadcast) can carry a flat `NodeData`. export type BuildNodeDataFromPath = (path: CollectionKey[]) => NodeData // `.current` is assigned in JsonEditor. Under the pinned @types/react 19, // `RefObject.current` is mutable and `MutableRefObject` is deprecated, so // `RefObject` is the right type here. export type BuildNodeDataFromPathRef = React.RefObject // For drag-n-drop export type Position = 'above' | 'below' export interface KeyEvent { key: string modifier?: React.ModifierKey | React.ModifierKey[] } // Any control can be set to `null` to disable it — the key then falls through // to its native browser behaviour (e.g. Tab resumes normal focus traversal). export interface KeyboardControls { confirm?: KeyEvent | string | null // value node defaults, key entry cancel?: KeyEvent | string | null // all "Cancel" operations objectConfirm?: KeyEvent | string | null objectLineBreak?: KeyEvent | string | null stringConfirm?: KeyEvent | string | null stringLineBreak?: KeyEvent | string | null // for Value nodes booleanConfirm?: KeyEvent | string | null booleanToggle?: KeyEvent | string | null numberConfirm?: KeyEvent | string | null numberUp?: KeyEvent | string | null numberDown?: KeyEvent | string | null tabForward?: KeyEvent | string | null tabBack?: KeyEvent | string | null clipboardModifier?: React.ModifierKey | React.ModifierKey[] | null collapseModifier?: React.ModifierKey | React.ModifierKey[] | null } export type KeyboardControlsFull = Omit< Required<{ [Property in keyof KeyboardControls]: KeyEvent | null }>, 'clipboardModifier' | 'collapseModifier' > & { clipboardModifier: React.ModifierKey[] collapseModifier: React.ModifierKey[] } /** * NODES */ export interface NodeData { key: CollectionKey path: CollectionKey[] level: number index: number value: JsonData size: number | null // Visible direct-child count under the current search filter: a `number` on // tracked collections while a filter is active, and `null` on render-path // `NodeData` when no filter is active or this isn't a tracked collection // (a leaf, say). `undefined` only when the `NodeData` wasn't built by the // render path — the `searchFilter` callback, which the visibility walk // invokes before counts are known, or the `editorRef`/`onCollapse`/ // `onEditEvent` bridges. Use `!= null` to gate on "has a real count". visibleSize?: number | null parentData: object | null fullData: T collapsed?: boolean } interface BaseNodeProps { data: unknown parentData: CollectionData | null nodeData: NodeData // Reads the latest whole document at call time, for event-time reads that // need the live tree rather than the `nodeData.fullData` prop, which a // memoised sibling keeps stale after a commit elsewhere. Stable identity, so // it doesn't weaken the node memo. getLatestData: () => JsonData onError?: OnErrorFunction showErrorMessages: boolean showIconTooltips: boolean showClipboardButton: boolean onCopy?: OnCopyFunction onEditEvent?: OnEditEventFunction allowEditFilter: FilterFunction allowDeleteFilter: FilterFunction allowAddFilter: FilterFunction allowDragFilter: FilterFunction // The parent collection's permissions, as seen by a child that's a drop // target: `canDragOnto` (parent editable) admits a same-collection REORDER, // and `canAddHere` (parent accepts adds) a cross-collection RELOCATE. A // relocate also needs the dragged node's own delete-permission, carried on // `dragSource`. See useDragNDrop. canDragOnto: boolean canAddHere: boolean allowTypeSelection: boolean | TypeOptions | TypeFilterFunction stringTruncateLength: number indent: number arrayIndexStart: number sort: SortFunction translate: TranslateFunction customNodeDefinitions: CustomNodeDefinition[] customNodeData: CustomNodeData customButtons: CustomButtonDefinition[] Select: React.ComponentType errorDisplayTime: number keyboardControls: KeyboardControlsFull handleKeyboard: ( e: React.KeyboardEvent, eventMap: Partial void>> ) => void editConfirmRef: React.RefObject jsonStringify: ( data: JsonData, // eslint-disable-next-line replacer?: (this: any, key: string, value: unknown) => string ) => string } export interface CollectionNodeProps extends BaseNodeProps { mainContainerRef: React.RefObject data: CollectionData collapseFilter: FilterFunction collapseAnimationTime: number showArrayIndexes: boolean showCollectionCount: boolean | 'when-collapsed' | 'when-collapsed-or-filtered' showStringQuotes: boolean defaultValue: unknown newKeyOptions?: string[] | NewKeyOptionsFunction jsonParse: ( input: string, // eslint-disable-next-line reviver?: (this: any, key: string, value: string) => unknown ) => JsonData insertAtTop: { object: boolean; array: boolean } TextEditor?: React.FC onCollapse?: OnCollapseFunction collapseClickZones: Array<'left' | 'header' | 'property'> } export type ValueData = string | number | boolean export interface ValueNodeProps extends BaseNodeProps { data: ValueData showLabel: boolean showStringQuotes: boolean onChange?: OnChangeFunction } export interface CustomKeyProps> { nodeData: NodeData // The displayed key — already a string, and for array indices already // offset for `arrayIndexStart`. Use `nodeData.key` for the raw key. name: string path: CollectionKey[] canEditKey: boolean handleEditKey: (newKey: string) => void startEditingKey: () => void handleClick?: (e: React.MouseEvent) => void styles: React.CSSProperties componentProps?: T getStyles: (element: ThemeableElement, nodeData: NodeData) => React.CSSProperties } export interface CustomComponentProps> extends Omit< BaseNodeProps, // `data` is omitted: it duplicated `value` (and `nodeData.value`). Read the // node's value via `value` — see its doc below. 'onError' | 'data' > { // The node's current value: the live edit buffer while this node is being // edited, so your component renders what the user is typing. The committed // value (what's in `data`/`setData`) is always on `nodeData.value`. value: JsonData componentProps?: T parentData: CollectionData | null // On a value node (including `renderCollectionAsValue`), writes into the // node's edit buffer; accepts any `JsonData` so those components can buffer // object values. On a collection node, commits the value, the same as // `handleEdit(value)`. // TO-DO (v3): make it buffer-only on collection nodes too, so `handleEdit` // is the one commit call everywhere. setValue: (value: JsonData) => void // Commits the edit: `handleEdit(value)` commits that value as-is, and // `handleEdit()` commits the node's edit buffer. handleEdit: (value?: JsonData) => void handleCancel: () => void onKeyDown: (e: React.KeyboardEvent) => void isEditing: boolean // True while this node's optimistic commit is in flight — the edited value // is already applied locally, but the consumer's async `onUpdate` hasn't // settled yet. Use it to show a "saving"/pending state. Always `false` when // there's no `onUpdate` (the commit settles synchronously) or for a no-op // edit. isPending: boolean setIsEditing: React.Dispatch> getStyles: (element: ThemeableElement, nodeData: NodeData) => React.CSSProperties children?: React.JSX.Element | React.JSX.Element[] | null originalNode?: React.JSX.Element originalNodeKey?: React.JSX.Element canEdit: boolean keyboardCommon: Partial void>> // No error-reporter prop: a custom component rejects invalid input by // throwing from its definition's `fromStandardType`, which the editor catches // (rejects the commit, keeps the editor open, shows the message inline, and // fires the consumer's `onError`). The consumer's flat `onError` observer is // omitted above so it isn't mistaken for a per-component reporter. } // Props received by a `wrapperComponent` — the standard node machinery plus the // `wrapperProps` configured on the definition (delivered here, not as // `componentProps`). export interface CustomWrapperProps> extends Omit< CustomComponentProps, 'componentProps' > { wrapperProps?: U } export interface CustomNodeDefinition, U = Record> { condition: FilterFunction component?: React.FC> keyComponent?: React.FC> name?: string // appears in "Type" selector componentProps?: T // shared by `component` and `keyComponent` showKey?: boolean // default true // Seeds the node's value when switching TO this type. May be a value, or a // function `(nodeData) => value` called on each switch — use the function // form for a fresh or lazy default (e.g. `() => new Date()`) rather than one // fixed when the module loads. Typed `unknown` because a custom type's value // needn't be JSON (e.g. `bigint`, `symbol`, `Date`). defaultValue?: unknown showInTypeSelector?: boolean // default false // `showOnView`/`showOnEdit` default to rendering the custom node in view mode // and the standard editor while editing — the common case. Set `showOnEdit` // for nodes that need a custom editor too (e.g. a date picker). showOnEdit?: boolean // default false showOnView?: boolean // default true // Switching TO this type in the type selector opens the node for editing — // the buffer is seeded with `defaultValue`, this definition's component // renders in edit state, a single commit happens on confirm, and Esc // cancels — instead of committing `defaultValue` instantly. Requires // `component` + `showOnEdit`. editOnTypeSwitch?: boolean // default false showEditTools?: boolean // default true // Opt-in (default false) because it makes the editor build the original // node's JSX up-front to pass as `originalNode`/`originalNodeKey` — wasted // work for custom nodes that fully replace the rendering. For a collection // node, `originalNode` is its raw-JSON editor while editing (else undefined), // and there's no `originalNodeKey`: the key renders in the collection's own // header. passOriginalNode?: boolean // default false // For collection nodes only: showCollectionWrapper?: boolean // default true wrapperComponent?: React.FC> wrapperProps?: U renderCollectionAsValue?: boolean // For JSON stringify/parse stringifyReplacer?: (value: unknown) => unknown parseReviver?: (stringified: string) => unknown // Demotes the custom value to a primitive seed when the type selector // switches this node to a standard type; core's generic coercion takes it // from there per target type. toStandardType?: (value: unknown) => ValueData // The inverse: converts a standard-typed value into this type's value. Runs // at every confirm of a custom edit (the ✓ button, Enter, Tab, // `editorRef.confirm()`) to turn the edit buffer — usually the editor's // string — into the value to commit; THROW to reject the confirm: nothing // commits, the session stays open, and the thrown message surfaces via // `onError` (inline error + observer callback). Also runs on an // `editOnTypeSwitch` switch to seed the editor from the node's current // value (a throw there isn't a reject — the seed falls back to // `defaultValue`, the same as switching with no hook). Must pass // already-correct values through unchanged — the buffer holds the raw // committed value until the editor's first keystroke. fromStandardType?: (value: unknown, nodeData: NodeData, componentProps?: T) => unknown } export type CustomTextDefinitions = Partial<{ [key in keyof LocalisedStrings]: CustomTextFunction }> // Commits a new value at the button's row through the same pipeline as an // edit: `onUpdate`, the `onEditEvent` stream, and the row's inline error and // `onError` on rejection. Not gated by `allowEdit` — the button decides, using // `canEdit` if it wants to. type CustomButtonHandleEdit = (value: JsonData) => void export interface CustomButtonElementProps { nodeData: NodeData // Whether `allowEdit` permits editing this row. canEdit: boolean handleEdit: CustomButtonHandleEdit } export interface CustomButtonDefinition { Element: React.FC onClick?: ( nodeData: NodeData, e: React.MouseEvent, context: { handleEdit: CustomButtonHandleEdit; canEdit: boolean } ) => void // Accessible name for the button. Supplying it renders the wrapper as a real //