|
| 1 | +// Load AceEditor for Textareas |
| 2 | +$(document).on('rex:ready', function () { |
| 3 | + |
| 4 | + // Select Textareas |
| 5 | + let aceTextAreas = document.querySelectorAll(rex.aceeditor_selectors); |
| 6 | + |
| 7 | + // Load AceEditor only if needed |
| 8 | + if (aceTextAreas.length > 0) { |
| 9 | + |
| 10 | + // Load AceEditor-Script |
| 11 | + var script = document.createElement('script'); |
| 12 | + script.src = '../assets/addons/aceeditor/vendor/aceeditor/ace.js'; |
| 13 | + document.head.appendChild(script); |
| 14 | + script.onload = function () { |
| 15 | + |
| 16 | + for (var i = 0; i < aceTextAreas.length; i++) { |
| 17 | + let textArea = aceTextAreas[i]; |
| 18 | + |
| 19 | + // Insert DIV for AceEditor |
| 20 | + let editorNode = document.createElement('div'); |
| 21 | + editorNode.style.width = '100%'; |
| 22 | + editorNode.style.height = $(textArea).height() + 'px' || '250px'; |
| 23 | + textArea.parentNode.insertBefore(editorNode, textArea); |
| 24 | + |
| 25 | + // Hide Textarea |
| 26 | + textArea.style.display = 'none'; |
| 27 | + |
| 28 | + // Initiate AceEditor and set Value |
| 29 | + let editor = ace.edit(editorNode); |
| 30 | + editor.getSession().setValue(textArea.value); |
| 31 | + |
| 32 | + // General Options from Settings-Page |
| 33 | + try { |
| 34 | + ace_options = JSON.parse(rex.aceeditor_options); |
| 35 | + } catch (e) { |
| 36 | + // Default on Error |
| 37 | + ace_options = { |
| 38 | + "showLineNumbers": true, |
| 39 | + "showGutter": true, |
| 40 | + "showInvisibles": false, |
| 41 | + "fontSize": 15, |
| 42 | + "mode": "ace/mode/php" |
| 43 | + }; |
| 44 | + console.warn('Addon aceeditor: Error in aceeditor-Options! Using minimum default configuration!'); |
| 45 | + } |
| 46 | + |
| 47 | + // Additional Settings from Attribute aceeditor-options |
| 48 | + add_options = textArea.getAttribute('aceeditor-options'); |
| 49 | + if (null !== add_options) { |
| 50 | + try { |
| 51 | + new_options = JSON.parse(add_options); |
| 52 | + } catch (e) { |
| 53 | + // Default on Error |
| 54 | + new_options = {}; |
| 55 | + console.warn('Addon aceeditor: Error in Attribute aceeditor-options! Options ignored!'); |
| 56 | + } |
| 57 | + ace_options = $.extend(ace_options, new_options); |
| 58 | + } |
| 59 | + |
| 60 | + // Set options |
| 61 | + editor.setOptions(ace_options); |
| 62 | + |
| 63 | + // Set theme Default / Darkmode |
| 64 | + let theme = rex.aceeditor_defaulttheme; |
| 65 | + let darkmode = false; |
| 66 | + |
| 67 | + let systemDarkModeDetector = null; |
| 68 | + if (window.matchMedia) { |
| 69 | + systemDarkModeDetector = window.matchMedia('(prefers-color-scheme: dark)'); |
| 70 | + // Query system settings |
| 71 | + if (systemDarkModeDetector.matches) { |
| 72 | + theme = rex.aceeditor_defaultdarktheme; |
| 73 | + darkmode = true; |
| 74 | + } |
| 75 | + // Setting from profile |
| 76 | + if (document.body.classList.contains('rex-theme-light')) { |
| 77 | + theme = rex.aceeditor_defaulttheme; |
| 78 | + } else if (document.body.classList.contains('rex-theme-dark')) { |
| 79 | + theme = rex.aceeditor_defaultdarktheme; |
| 80 | + darkmode = true; |
| 81 | + } |
| 82 | + // Detect dark/light switching on the system side |
| 83 | + systemDarkModeDetector.addEventListener('change', function (e) { |
| 84 | + if (systemDarkModeDetector.matches) { |
| 85 | + theme = rex.aceeditor_defaultdarktheme; |
| 86 | + darkmode = true; |
| 87 | + } else { |
| 88 | + theme = rex.aceeditor_defaulttheme; |
| 89 | + } |
| 90 | + if (document.body.classList.contains('rex-theme-light')) { |
| 91 | + theme = rex.aceeditor_defaulttheme; |
| 92 | + } else if (document.body.classList.contains('rex-theme-dark')) { |
| 93 | + theme = rex.aceeditor_defaultdarktheme; |
| 94 | + darkmode = true; |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + // Set theme from Attribute aceeditor-theme |
| 100 | + let attrtheme = textArea.getAttribute('aceeditor-theme'); |
| 101 | + if (null !== attrtheme) { |
| 102 | + theme = attrtheme; |
| 103 | + } |
| 104 | + |
| 105 | + // Set theme |
| 106 | + editor.setTheme('ace/theme/' + theme); |
| 107 | + |
| 108 | + // Set mode from Attribute aceeditor-mode |
| 109 | + let mode = textArea.getAttribute('aceeditor-mode'); |
| 110 | + if (null !== mode) { |
| 111 | + editor.getSession().setMode('ace/mode/' + mode); |
| 112 | + } |
| 113 | + |
| 114 | + // Set border color |
| 115 | + if (darkmode === true) { |
| 116 | + editor.container.classList.add('acerexdarkmode'); |
| 117 | + } else { |
| 118 | + editor.container.classList.add('acerexmode'); |
| 119 | + } |
| 120 | + |
| 121 | + // Set lineHeight |
| 122 | + editor.container.style.lineHeight = 1.5; |
| 123 | + editor.renderer.updateFontSize(); |
| 124 | + |
| 125 | + // Fullscreen-Toggle F11 + ESC |
| 126 | + editor.commands.addCommand({ |
| 127 | + name: 'fullScreenF11', |
| 128 | + bindKey: { win: 'F11', mac: 'F11' }, |
| 129 | + exec: function (editor) { |
| 130 | + editor.container.classList.toggle('acefullscreen'); |
| 131 | + document.body.classList.toggle('acefullscreenbody'); |
| 132 | + editor.resize(); |
| 133 | + } |
| 134 | + }); |
| 135 | + editor.commands.addCommand({ |
| 136 | + name: 'fullScreenESC', |
| 137 | + bindKey: { win: 'ESC', mac: 'ESC' }, |
| 138 | + exec: function (editor) { |
| 139 | + editor.container.classList.toggle('acefullscreen'); |
| 140 | + document.body.classList.toggle('acefullscreenbody'); |
| 141 | + editor.resize(); |
| 142 | + } |
| 143 | + }); |
| 144 | + |
| 145 | + // Changes direct to Textarea |
| 146 | + editor.session.addEventListener('change', function (item) { |
| 147 | + let textArea = editorNode.nextElementSibling; |
| 148 | + textArea.value = editor.getSession().getValue(); |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + }; |
| 153 | + } |
| 154 | + |
| 155 | +}); |
0 commit comments