X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/4b2654598c39d3613a4e05470470555a6f743988..refs/pull/3179/head:/resources/js/services/code.js diff --git a/resources/js/services/code.js b/resources/js/services/code.js index 21f331a63..d82db5271 100644 --- a/resources/js/services/code.js +++ b/resources/js/services/code.js @@ -15,6 +15,7 @@ import 'codemirror/mode/lua/lua'; import 'codemirror/mode/markdown/markdown'; import 'codemirror/mode/mllike/mllike'; import 'codemirror/mode/nginx/nginx'; +import 'codemirror/mode/perl/perl'; import 'codemirror/mode/pascal/pascal'; import 'codemirror/mode/php/php'; import 'codemirror/mode/powershell/powershell'; @@ -25,6 +26,8 @@ import 'codemirror/mode/rust/rust'; import 'codemirror/mode/shell/shell'; import 'codemirror/mode/sql/sql'; import 'codemirror/mode/toml/toml'; +import 'codemirror/mode/vb/vb'; +import 'codemirror/mode/vbscript/vbscript'; import 'codemirror/mode/xml/xml'; import 'codemirror/mode/yaml/yaml'; @@ -62,6 +65,8 @@ const modeMap = { markdown: 'markdown', ml: 'mllike', nginx: 'nginx', + perl: 'perl', + pl: 'perl', powershell: 'powershell', properties: 'properties', ocaml: 'mllike', @@ -81,6 +86,10 @@ const modeMap = { bash: 'shell', toml: 'toml', sql: 'text/x-sql', + vbs: 'vbscript', + vbscript: 'vbscript', + 'vb.net': 'text/x-vb', + vbnet: 'text/x-vb', xml: 'xml', yaml: 'yaml', yml: 'yaml', @@ -188,7 +197,8 @@ function getMode(suggestion, content) { * @returns {*|string} */ function getTheme() { - return window.codeTheme || 'default'; + const darkMode = document.documentElement.classList.contains('dark-mode'); + return window.codeTheme || (darkMode ? 'darcula' : 'default'); } /** @@ -201,9 +211,9 @@ function wysiwygView(elem) { const doc = elem.ownerDocument; const codeElem = elem.querySelector('code'); - let lang = (elem.className || '').replace('language-', ''); - if (lang === '' && codeElem) { - lang = (codeElem.className || '').replace('language-', '') + let lang = getLanguageFromCssClasses(elem.className || ''); + if (!lang && codeElem) { + lang = getLanguageFromCssClasses(codeElem.className || ''); } elem.innerHTML = elem.innerHTML.replace(//gi ,'\n'); @@ -218,7 +228,7 @@ function wysiwygView(elem) { elem.parentNode.replaceChild(newWrap, elem); newWrap.appendChild(newTextArea); - newWrap.contentEditable = false; + newWrap.contentEditable = 'false'; newTextArea.textContent = content; let cm = CodeMirror(function(elt) { @@ -231,12 +241,20 @@ function wysiwygView(elem) { theme: getTheme(), readOnly: true }); - setTimeout(() => { - cm.refresh(); - }, 300); + return {wrap: newWrap, editor: cm}; } +/** + * Get the code language from the given css classes. + * @param {String} classes + * @return {String} + */ +function getLanguageFromCssClasses(classes) { + const langClasses = classes.split(' ').filter(cssClass => cssClass.startsWith('language-')); + return (langClasses[0] || '').replace('language-', ''); +} + /** * Create a CodeMirror instance to show in the WYSIWYG pop-up editor * @param {HTMLElement} elem