Skip to content

Commit b2cd20f

Browse files
committed
✨ Added live save feature in in-view modal
1 parent 300a227 commit b2cd20f

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/plugin.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@
192192
document.head.append(style);
193193
}
194194

195+
const debounce = (func, delay) => {
196+
let timeoutId;
197+
return (...args) => {
198+
clearTimeout(timeoutId);
199+
timeoutId = setTimeout(() => func.apply(null, args), delay);
200+
};
201+
};
202+
195203
// on plugin load
196204
const mainPlugin = function(editor) {
197205

@@ -450,20 +458,34 @@
450458
modal.element.style.display = 'none';
451459
}, 10)
452460
};
461+
462+
// Save editor content to tinymce editor on ace editor change
463+
const liveSave = () => {
464+
editor.undoManager.transact(function() {
465+
let value = aceEditor.getValue();
466+
if(Config.renderer){
467+
value = Config.renderer(value);
468+
}
469+
editor.setContent(value);
470+
});
471+
editor.nodeChanged();
472+
};
453473

454474
const onSaveHandler = () => {
455475
editor.focus();
456-
editor.undoManager.transact(function() {
457-
let value = aceEditor.getValue();
458-
if(Config.renderer){
459-
value = Config.renderer(value);
476+
if (Config.fallbackModal) {
477+
editor.undoManager.transact(function() {
478+
let value = aceEditor.getValue();
479+
if(Config.renderer){
480+
value = Config.renderer(value);
481+
}
482+
editor.setContent(value);
483+
});
484+
editor.selection.setCursorLocation();
485+
editor.nodeChanged();
460486
}
461-
editor.setContent(value);
462-
});
463-
editor.selection.setCursorLocation();
464-
editor.nodeChanged();
465487
editor.execCommand('ToggleView', false, 'supercode');
466-
}
488+
};
467489

468490
const onKeyDownHandler = (e) => {
469491
if((e.key === ' ' && e.ctrlKey) || e.key === 'Escape'){
@@ -484,6 +506,11 @@
484506
if(!session){
485507
session = ace.createEditSession(content, `ace/mode/${Config.language}`);
486508
session.setUseWrapMode(Config.wrap);
509+
// Attach live save to ace editor for in-editor source view
510+
if (!Config.fallbackModal) {
511+
const debouncedSave = debounce(liveSave, 300);
512+
session.on('change', debouncedSave);
513+
}
487514
}
488515
aceEditor.setSession(session);
489516
session.setValue(content);

0 commit comments

Comments
 (0)