Skip to content

Conversation

@obrobrio2000
Copy link

@obrobrio2000 obrobrio2000 commented Oct 15, 2025

📝 Description

This PR implements immediate JSON synchronization in the JSON Editor tab while maintaining validation to ensure data integrity. Previously, JSON changes were only saved when switching away from the JSON Editor tab, causing issues with immediate saves and potential data loss during rapid edits.

✨ Changes

Modified Files

  • packages/survey-creator-core/src/components/tabs/json-editor-plugin.ts

Key Changes

  1. Added Model import from survey-core

    • Required for semantic validation of survey JSON
  2. New property: lastSyncedText

    • Tracks what has been synced to creator.JSON
    • Prevents redundant synchronization operations
  3. New method: syncToCreator()

    • Validates JSON syntax using JSON.parse()
    • Validates survey semantics using new Model(surveyDefinition).jsonErrors
    • Only syncs to creator.JSON if both validations pass
    • Sets isJSONChanged = true only after successful sync
    • Fires onModified event immediately for real-time updates
  4. Enhanced: processErrors()

    • After syntax validation, calls syncToCreator() if no errors detected
    • Enables immediate syncing after the 1-second validation timeout
  5. New method: hasUnsyncedChanges()

    • Public method to check if there are pending changes
    • Used by deactivate() to handle fast tab switches
  6. Enhanced: deactivate()

    • Checks for unsynced changes when leaving the JSON Editor tab
    • Immediately validates and syncs if changes are found
    • Prevents tab switch if JSON is invalid (returns false)
    • Prevents data loss when users switch tabs before timeout fires
  7. Enhanced: onPluginActivate()

    • Initializes lastSyncedText when tab is activated
    • Ensures accurate tracking of sync state

🔄 Workflow

New Synchronization Flow

  1. User types in JSON Editor
  2. After 1 second of inactivity, processErrors() validates syntax
  3. If valid, syncToCreator() validates semantics
  4. If both validations pass, immediately updates creator.JSON
  5. Fires onModified event for external listeners
  6. If invalid, displays errors and doesn't sync

Fast Tab Switch Handling

  1. User switches tabs before timeout fires
  2. deactivate() detects unsynced changes
  3. Immediately validates JSON
  4. If valid, syncs before leaving tab
  5. If invalid, prevents tab switch and shows errors

✅ Benefits

  • Immediate saves: External save handlers receive updates in real-time
  • Data safety: Invalid JSON never corrupts the creator state
  • No data loss: Fast tab switching is handled gracefully
  • Better UX: Users don't need to switch tabs to trigger saves
  • Proper flag management: isJSONChanged set only after successful validation
  • Backward compatible: Existing functionality preserved

🧪 Testing Recommendations

Manual Testing

  1. Immediate Save Test

    • Navigate to JSON Editor tab
    • Modify valid JSON
    • Wait 1 second
    • Attempt to save without switching tabs
    • ✅ Expected: Latest changes should be saved
  2. Invalid JSON Test

    • Navigate to JSON Editor tab
    • Enter invalid JSON (syntax error)
    • Wait 1 second
    • Try to switch tabs
    • ✅ Expected: Tab switch prevented, errors shown
  3. Rapid Edit Test

    • Navigate to JSON Editor tab
    • Make edit, switch to Designer
    • Switch back to JSON Editor
    • Make edit, switch to Designer
    • ✅ Expected: Both edits preserved
  4. Fast Tab Switch Test

    • Navigate to JSON Editor tab
    • Type valid JSON
    • Immediately switch tabs (before 1 second)
    • ✅ Expected: Changes validated and saved before leaving
  5. onModified Event Test

    • Add listener to creator.onModified
    • Edit JSON in JSON Editor
    • Wait 1 second
    • ✅ Expected: Event fires after validation succeeds

Automated Testing (if applicable)

  • Test syncToCreator() with valid JSON
  • Test syncToCreator() with invalid syntax
  • Test syncToCreator() with invalid survey semantics
  • Test hasUnsyncedChanges() returns correct state
  • Test deactivate() with unsynced changes
  • Test deactivate() with invalid JSON

🔍 Breaking Changes

None. This PR maintains backward compatibility:

  • Existing deactivate() behavior preserved
  • All public APIs remain unchanged
  • Only internal synchronization timing improved

💬 Additional Notes

This implementation follows the validation-first principle emphasized by the SurveyJS team in support tickets. JSON is validated both syntactically (JSON.parse) and semantically (Survey Model jsonErrors) before any synchronization occurs, ensuring that creator.JSON always reflects a valid survey state.

The solution addresses the core issue while maintaining all safety guarantees that prevent invalid JSON from corrupting the creator state.

Copilot AI review requested due to automatic review settings October 15, 2025 07:10
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements real-time synchronization of the JSON Editor text to fix issues where changes made in the JSON Editor tab weren't immediately available via creator.text. Previously, JSON changes only synchronized when switching away from the tab, causing data loss and preventing immediate saves.

Key changes:

  • Enhanced the text getter to retrieve current JSON directly from the editor plugin when on the JSON tab
  • Added a new syncJsonEditorText() method to force synchronization before critical operations
  • Set the isJSONChanged flag immediately when text changes in the editor

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@obrobrio2000 obrobrio2000 changed the title enhancement: Sync JSON Editor text in real-time to enable immediate saving enhancement: Enable immediate JSON synchronization with validation in JSON Editor Oct 15, 2025
@obrobrio2000 obrobrio2000 force-pushed the enhancement/json-editor-text-sync branch from 7bfc5af to da91a27 Compare October 15, 2025 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON Editor: Unable to save changes immediately without switching tabs

1 participant