Skip to content

JSON Editor: Unable to save changes immediately without switching tabs #7212

@obrobrio2000

Description

@obrobrio2000

Description

When editing survey JSON in the JSON Editor tab, changes cannot be saved immediately. Users must switch to another tab (e.g., Designer) and then back to the JSON Editor before the save operation captures the latest changes.

Steps to Reproduce

  1. Open Survey Creator and navigate to the JSON Editor tab
  2. Modify the JSON content
  3. Attempt to save immediately (without switching tabs)
  4. Expected: Latest JSON changes are saved
  5. Actual: Previous JSON state is saved, not the current edits

Additional issue: If you edit JSON, switch tabs, then make another edit, the second edit is lost on save.

Root Cause

The TabJsonEditorBasePlugin class maintains its own model.text that is separate from creator.text. Synchronization only occurs in the deactivate() method when leaving the JSON Editor tab:

public deactivate(): boolean {
  if (this.model) {
    if (!this.model.readOnly && this.model.isJSONChanged) {
      this.creator.changeText(this.model.text, false, true);  // ← ONLY SYNCS HERE
      // ...
    }
  }
  return true;
}

This design means:

  • JSON changes are stored locally in the plugin's model
  • creator.text getter doesn't reflect current JSON editor content
  • External code accessing creator.text gets stale data
  • Only tab switching triggers synchronization

Current Behavior

  • ❌ Cannot save immediately after editing JSON
  • ❌ Multiple rapid edits with tab switches lose data
  • creator.text returns stale data when on JSON tab
  • ❌ External save handlers get incorrect JSON
  • isJSONChanged flag may be set before validation completes

Expected Behavior

  • ✅ Save should work immediately after editing JSON
  • ✅ All edits should be preserved regardless of tab switches
  • creator.text should always return current JSON state
  • ✅ External handlers should access latest changes
  • ✅ Validation should occur before setting isJSONChanged flag

Environment

  • Survey Creator Version: All versions with JSON Editor
  • Browser: All browsers
  • Framework: Framework-agnostic (affects all integrations)

Impact

This affects any application that:

  • Implements custom save functionality
  • Programmatically accesses creator.text
  • Validates JSON before saving
  • Needs real-time JSON synchronization
  • Uses onModified event to track changes

Related Tickets

Proposed Solution

Implement automatic validation and synchronization after JSON editing:

  1. After the validation timeout fires (1 second after typing stops), validate JSON
  2. If JSON is syntactically and semantically valid, immediately sync to creator.JSON
  3. Set isJSONChanged = true only after successful validation
  4. Track last synced text to prevent redundant updates
  5. Handle fast tab switching by checking for unsynced changes in deactivate()

This ensures:

  • Validation happens first (maintains data integrity)
  • Valid JSON syncs immediately (enables immediate saves)
  • Invalid JSON doesn't corrupt state (maintains safety)
  • No data loss on rapid edits (improves reliability)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions