Skip to content

Latest commit

 

History

History
148 lines (121 loc) · 5.41 KB

File metadata and controls

148 lines (121 loc) · 5.41 KB

Edit Node Feature Implementation Guide

Overview

I've successfully implemented an "Edit Node" feature for your JSON editor that allows users to select a node in the visualization graph and edit its contents directly. When saved, changes are reflected both in the graph visualization and in the JSON text editor on the left.

What Was Implemented

1. EditNodeModal Component (src/features/modals/EditNodeModal/index.tsx)

A new React modal component that provides a user interface for editing node data with the following features:

  • Display Current Value: Shows the current node data before editing
  • Edit Key: Allows changing the property key (if applicable)
  • Type Selector: Choose between String, Number, and Boolean types
  • Value Input: Input field or dropdown selector based on the chosen type
  • Error Handling: Displays validation errors with user-friendly messages
  • Cancel Button: Discards changes and closes the modal
  • Save Button: Validates, saves changes, and updates both the visualization and JSON

2. Modal Registration

The EditNodeModal has been automatically registered in the modal system by adding it to src/features/modals/index.ts, making it available throughout the application.

3. Edit Button in BottomBar (src/features/editor/BottomBar.tsx)

Added an "Edit" button in the bottom status bar with:

  • Visual Icon: Edit icon (pencil) for intuitive UX
  • Smart Enable/Disable: Button is disabled when no node is selected
  • Tooltip: Shows helpful text when hovering over the button
  • Accessibility: Only enabled when a node is selected

How It Works

User Flow:

  1. Select a Node: Click on any node in the graph visualization
  2. Open Edit Modal: Click the "Edit" button in the bottom bar (or use the keyboard shortcut if configured)
  3. Modify Data:
    • Change the property key (if present)
    • Select the data type (String, Number, Boolean)
    • Enter the new value
  4. Save or Cancel:
    • Click Save: Updates are applied to both graph and JSON
    • Click Cancel: Changes are discarded

Technical Flow:

User clicks Edit Button
    ↓
EditNodeModal opens with selected node data
    ↓
User modifies values
    ↓
User clicks Save
    ↓
Values are parsed and validated
    ↓
JSON object is updated at the selected path
    ↓
Updates propagate to:
  - useJson store (JSON state)
  - useFile store (text editor contents)
  - useGraph store (triggers graph rebuild)
    ↓
Modal closes
    ↓
UI reflects all changes

Code Architecture

Component Hierarchy:

BottomBar (contains Edit button)
    ↓
ModalController (renders all modals including EditNodeModal)
    ↓
EditNodeModal (handles editing UI and logic)
    ↓
useGraph (selected node state)
useJson (JSON data state)
useFile (text editor content state)

Key Functions:

parseValue(value: string, type: string): EditValue

  • Converts user input to the correct JavaScript type
  • Validates input (e.g., numeric strings, boolean values)
  • Throws errors for invalid inputs

updateJsonAtPath(jsonObj, path, newValue, newKey?): object

  • Navigates to the node location using the JSONPath
  • Updates or replaces the key if specified
  • Maintains JSON structure integrity
  • Returns the updated object

handleSave()

  • Validates all inputs
  • Parses and type-converts the new value
  • Updates the JSON object
  • Propagates changes to all stores
  • Rebuilds the graph visualization

State Management

The feature uses existing Zustand stores for state management:

  • useGraph: Manages selectedNode state
  • useJson: Manages overall JSON content
  • useFile: Manages text editor contents and tracks changes
  • useModal: Controls modal visibility

Features

Edit Button appears in the UI - Visible in the bottom status bar ✅ Edit Mode - Click edit button to enter edit mode ✅ Save button - Saves changes to node visualization and JSON ✅ Cancel button - Discards any unsaved changes ✅ Type Support - String, Number, Boolean types ✅ Validation - Input validation with error messages ✅ Smart Button State - Button disabled when no node selected ✅ Bidirectional Sync - Changes sync between graph and JSON editor

Potential Enhancements

  1. Array and Object Support: Extend to handle editing arrays and objects
  2. Keyboard Shortcut: Add a keyboard shortcut (e.g., Ctrl+E) to open edit mode
  3. Inline Editing: Add double-click support for inline editing in the graph
  4. Undo/Redo: Integrate with browser history API for undo/redo functionality
  5. Search & Replace: Add find/replace functionality for multiple edits
  6. Data Validation: Support for custom validation rules or JSON schema constraints
  7. Confirmation Dialog: Add a confirmation before overwriting values

Testing Checklist

  • Select a node in the graph
  • Verify "Edit" button becomes enabled
  • Click "Edit" to open the modal
  • Change the value and click Save
  • Verify the node updates in the graph
  • Verify the JSON text editor reflects the changes
  • Try clicking Cancel to discard changes
  • Test with different data types (string, number, boolean)
  • Try with invalid input to see error handling

Files Modified

  1. Created: src/features/modals/EditNodeModal/index.tsx
  2. Modified: src/features/modals/index.ts (added export)
  3. Modified: src/features/editor/BottomBar.tsx (added Edit button)