|
192 | 192 | document.head.append(style); |
193 | 193 | } |
194 | 194 |
|
| 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 | + |
195 | 203 | // on plugin load |
196 | 204 | const mainPlugin = function(editor) { |
197 | 205 |
|
|
450 | 458 | modal.element.style.display = 'none'; |
451 | 459 | }, 10) |
452 | 460 | }; |
| 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 | + }; |
453 | 473 |
|
454 | 474 | const onSaveHandler = () => { |
455 | 475 | 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(); |
460 | 486 | } |
461 | | - editor.setContent(value); |
462 | | - }); |
463 | | - editor.selection.setCursorLocation(); |
464 | | - editor.nodeChanged(); |
465 | 487 | editor.execCommand('ToggleView', false, 'supercode'); |
466 | | - } |
| 488 | + }; |
467 | 489 |
|
468 | 490 | const onKeyDownHandler = (e) => { |
469 | 491 | if((e.key === ' ' && e.ctrlKey) || e.key === 'Escape'){ |
|
484 | 506 | if(!session){ |
485 | 507 | session = ace.createEditSession(content, `ace/mode/${Config.language}`); |
486 | 508 | 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 | + } |
487 | 514 | } |
488 | 515 | aceEditor.setSession(session); |
489 | 516 | session.setValue(content); |
|
0 commit comments