-
Couldn't load subscription status.
- Fork 436
Description
- AD Ticket https://surveyjs.answerdesk.io/ticket/details/t25244/json-editor-unable-to-save-changes-immediately-without-switching-tabs
- GH PR enhancement: Enable immediate JSON synchronization with validation in JSON Editor #7213
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
- Open Survey Creator and navigate to the JSON Editor tab
- Modify the JSON content
- Attempt to save immediately (without switching tabs)
- Expected: Latest JSON changes are saved
- 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.textgetter doesn't reflect current JSON editor content- External code accessing
creator.textgets stale data - Only tab switching triggers synchronization
Current Behavior
- ❌ Cannot save immediately after editing JSON
- ❌ Multiple rapid edits with tab switches lose data
- ❌
creator.textreturns stale data when on JSON tab - ❌ External save handlers get incorrect JSON
- ❌
isJSONChangedflag 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.textshould always return current JSON state - ✅ External handlers should access latest changes
- ✅ Validation should occur before setting
isJSONChangedflag
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
onModifiedevent to track changes
Related Tickets
- AD Ticket: https://surveyjs.answerdesk.io/ticket/details/t24492/survey-creator-on-change-json-editor-event
Proposed Solution
Implement automatic validation and synchronization after JSON editing:
- After the validation timeout fires (1 second after typing stops), validate JSON
- If JSON is syntactically and semantically valid, immediately sync to
creator.JSON - Set
isJSONChanged = trueonly after successful validation - Track last synced text to prevent redundant updates
- 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)