UNPKG

@anilkumarthakur/vue3-json-viewer

Version:

A beautiful, customizable JSON viewer component for Vue 3 with TypeScript support, dark/light mode, expand/collapse controls, and syntax highlighting.

60 lines 3.04 kB
import { InjectionKey, Ref } from 'vue'; import { JsonValue, ToggleEventPayload, CopyEventPayload } from '../../types'; /** * Builds a unique, JSONPath-style path for a child node. * * Identifier-safe object keys use dot notation (`$.address.city`); any other * key (containing `.`, `[`, quotes, spaces, etc.) is bracket-quoted * (`$["a.b"]`) so it can never collide with a genuinely nested path. Array * items always use bracket-index notation (`$.items[0]`). */ export declare function buildChildPath(basePath: string, key: string, isArrayItem: boolean): string; /** The reactive expand/collapse store shared across the tree. */ export interface JsonViewerContext { /** Whether the node at `path` is currently expanded. */ isExpanded: (path: string) => boolean; /** Explicitly set the expanded state for a node and notify listeners. */ setExpanded: (path: string, key: string, expanded: boolean) => void; /** Flip the expanded state for a node and notify listeners. */ toggle: (path: string, key: string) => void; /** Expand every node (clears per-node overrides, baseline → expanded). */ expandAll: () => void; /** Collapse every node (clears per-node overrides, baseline → collapsed). */ collapseAll: () => void; /** Reset all state to a given baseline (used when the `expanded` prop changes). */ reset: (expanded: boolean) => void; /** Notify listeners that a node's value was copied. */ notifyCopy: (path: string, key: string, value: JsonValue) => void; } /** Injection key for the shared {@link JsonViewerContext}. */ export declare const JSON_VIEWER_CONTEXT: InjectionKey<JsonViewerContext>; /** Options for {@link createJsonViewerContext}. */ export interface CreateContextOptions { /** * The baseline expanded state applied to any node without an explicit * override. Passing a ref lets the root keep it in sync with the `expanded` * prop. */ defaultExpanded: Ref<boolean>; /** Called whenever a node is toggled/set. */ onToggle?: (payload: ToggleEventPayload) => void; /** Called whenever a node's value is copied. */ onCopy?: (payload: CopyEventPayload) => void; } /** * Creates the reactive expand/collapse store. * * State model: a per-node override map layered over a single baseline value. * A node with no override falls back to the baseline, so "expand all" / * "collapse all" is just clearing overrides and flipping the baseline — no need * to enumerate every node in the tree. */ export declare function createJsonViewerContext(options: CreateContextOptions): JsonViewerContext; /** Provides the context to descendants. Must be called during `setup`. */ export declare function provideJsonViewerContext(ctx: JsonViewerContext): void; /** * Injects the shared context, or `null` when a component is used outside of a * provider (e.g. an internal component rendered in isolation). */ export declare function useJsonViewerContext(): JsonViewerContext | null; //# sourceMappingURL=context.d.ts.map