X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/c6ad16dba657c82512ae495a4a38b99b8cfa9eeb..refs/pull/3556/head:/resources/js/components/code-editor.js diff --git a/resources/js/components/code-editor.js b/resources/js/components/code-editor.js index a8a2c0c6f..27ff56395 100644 --- a/resources/js/components/code-editor.js +++ b/resources/js/components/code-editor.js @@ -33,10 +33,11 @@ class CodeEditor { onSelect(this.languageLinks, event => { const language = event.target.dataset.lang; this.languageInput.value = language; - this.updateEditorMode(language); + this.languageInputChange(language); }); onEnterPress(this.languageInput, e => this.save()); + this.languageInput.addEventListener('input', e => this.languageInputChange(this.languageInput.value)); onSelect(this.saveButton, e => this.save()); onChildEvent(this.historyList, 'button', 'click', (event, elem) => { @@ -59,12 +60,10 @@ class CodeEditor { this.languageInput.value = language; this.callback = callback; - this.show(); - this.updateEditorMode(language); - - window.importVersioned('code').then(Code => { - Code.setContent(this.editor, code); - }); + this.show() + .then(() => this.languageInputChange(language)) + .then(() => window.importVersioned('code')) + .then(Code => Code.setContent(this.editor, code)); } async show() { @@ -92,13 +91,29 @@ class CodeEditor { Code.setMode(this.editor, language, this.editor.getValue()); } + languageInputChange(language) { + this.updateEditorMode(language); + const inputLang = language.toLowerCase(); + let matched = false; + + for (const link of this.languageLinks) { + const lang = link.dataset.lang.toLowerCase().trim(); + const isMatch = inputLang && lang.startsWith(inputLang); + link.classList.toggle('active', isMatch); + if (isMatch && !matched) { + link.scrollIntoView({block: "center", behavior: "smooth"}); + matched = true; + } + } + } + loadHistory() { this.history = JSON.parse(window.sessionStorage.getItem(this.historyKey) || '{}'); const historyKeys = Object.keys(this.history).reverse(); this.historyDropDown.classList.toggle('hidden', historyKeys.length === 0); this.historyList.innerHTML = historyKeys.map(key => { const localTime = (new Date(parseInt(key))).toLocaleTimeString(); - return `
`; + return ``; }).join(''); }