A web-based tool to view, edit, format, and validate JSON with multiple editing modes including tree, code, text, and preview
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Mode switching functionality enabling dynamic transitions between different editor interfaces and behaviors, plus mode registration system for custom editor plugins.
Dynamically change the editor mode, switching between different editing interfaces.
/**
* Change the mode of the editor
* @param mode - Target mode (tree, view, form, code, text, preview)
*/
setMode(mode: "tree" | "view" | "form" | "code" | "text" | "preview"): void;Usage Example:
// Switch to code mode
editor.setMode("code");
// Switch to tree mode
editor.setMode("tree");
// Switch to text mode
editor.setMode("text");Retrieve the currently active editor mode.
/**
* Get the current mode of the editor
* @returns Current active mode
*/
getMode(): "tree" | "view" | "form" | "code" | "text" | "preview";Usage Example:
const currentMode = editor.getMode();
console.log(`Current mode: ${currentMode}`); // e.g., "Current mode: tree"Register a custom mode plugin that extends the editor with new functionality.
/**
* Register a plugin mode for the JSON Editor
* @param mode - Mode definition object or array of mode definitions
*/
static registerMode(mode: ModeDefinition | ModeDefinition[]): void;Usage Example:
// Register a custom readonly mode
const readonlyMode = {
mode: "readonly",
mixin: {
create: function(container, options) {
// Custom mode implementation
this.container = container;
this.options = options;
// Create readonly interface
},
get: function() {
return this.json;
},
set: function(json) {
this.json = json;
// Update readonly display
},
getText: function() {
return JSON.stringify(this.json);
},
setText: function(jsonString) {
this.json = JSON.parse(jsonString);
// Update readonly display
}
},
data: "json"
};
JSONEditor.registerMode(readonlyMode);
// Use the custom mode
const editor = new JSONEditor(container, { mode: "readonly" });Access the global mode configuration registry.
/**
* Configuration for all registered modes
* Object mapping mode names to their definitions
*/
static modes: { [modeName: string]: ModeDefinition };Usage Example:
// Check available modes
const availableModes = Object.keys(JSONEditor.modes);
console.log("Available modes:", availableModes); // ["tree", "view", "form", "code", "text", "preview"]
// Get specific mode configuration
const treeMode = JSONEditor.modes.tree;
console.log("Tree mode data type:", treeMode.data); // "json"interface ModeDefinition {
/** Unique name for the mode */
mode: string;
/** Mixin object containing mode implementation */
mixin: ModeMixin;
/** Data type the mode operates on */
data: "text" | "json";
/** Optional load function called after mode initialization */
load?: () => void;
}
interface ModeMixin {
/** Required: Create the mode interface */
create: (container: HTMLElement, options: JSONEditorOptions) => void;
/** Get current data (return type depends on mode.data) */
get?: () => any;
/** Set data (parameter type depends on mode.data) */
set?: (data: any) => void;
/** Get data as text string */
getText?: () => string;
/** Set data from text string */
setText?: (text: string) => void;
/** Destroy the mode and clean up resources */
destroy?: () => void;
/** Additional mode-specific methods */
[methodName: string]: any;
}When switching modes, JSONEditor automatically:
onModeChange(newMode, oldMode) if configuredUsage Example:
const options = {
mode: "tree",
modes: ["tree", "code", "text"], // Enable mode switching UI
onModeChange: (newMode, oldMode) => {
console.log(`Switched from ${oldMode} to ${newMode}`);
}
};
const editor = new JSONEditor(container, options);
// Programmatic mode switching
editor.setMode("code"); // Triggers onModeChange callbackMode operations can throw errors in certain conditions:
try {
editor.setMode("nonexistent");
} catch (error) {
console.error(error.message); // "Unknown mode 'nonexistent'"
}
try {
JSONEditor.registerMode({
mode: "tree", // Already exists
mixin: {},
data: "json"
});
} catch (error) {
console.error(error.message); // "Mode 'tree' already registered"
}// Register multiple modes at once
JSONEditor.registerMode([
{
mode: "debug",
mixin: { /* debug mode implementation */ },
data: "json",
load: () => console.log("Debug mode loaded")
},
{
mode: "compact",
mixin: { /* compact mode implementation */ },
data: "text"
}
]);
// Mode with custom load function
const customMode = {
mode: "custom",
mixin: {
create: function(container, options) {
this.container = container;
this.options = options;
}
},
data: "json",
load: function() {
// Called after mode is loaded and mixin is applied
console.log("Custom mode ready");
this.setupCustomFeatures();
}
};
JSONEditor.registerMode(customMode);