From 30dc23c847a3271d8b20d8aa213f33da67c9879f Mon Sep 17 00:00:00 2001 From: Kato Twofold Date: Tue, 7 Jun 2022 00:53:25 +0300 Subject: [PATCH] * Dynamic updates for highlights --- static/js/ws.js | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/static/js/ws.js b/static/js/ws.js index 75a95f5..3076608 100644 --- a/static/js/ws.js +++ b/static/js/ws.js @@ -184,15 +184,27 @@ function updatePadContent(newContent, textArea = true) { const prev = document.getElementById(`textarea-preview`); const shouldScroll = prev.scrollTop >= (prev.scrollHeight - Number(getComputedStyle(prev).height.replace(/px/g, ''))) * 0.98; - prev.innerHTML = newContent; + prev.innerHTML = escapeHtml(newContent); + + prev.classList.remove(`language-undefined`); + + prev.classList.forEach( c => { + if ( c.indexOf(`language-`) != -1 ) { + prev.classList.remove(c); + } + }) + + try { // highlights + hljs.highlightElement(document.getElementById(`textarea-preview`)); + } catch ( err ) { + console.err(err); + } // Check if we should follow the bottom scrolling if (shouldScroll) { prev.scrollTop = prev.scrollHeight; } - // TODO: Re-run the syntax highlight - } function updatePadViewers(vc) { @@ -223,4 +235,17 @@ function connectSocket() { // wait for the whole window to load window.addEventListener(`load`, e => { connectSocket() -}) \ No newline at end of file +}) + + +// lol +function escapeHtml(html){ + const text = document.createTextNode(html); + const p = document.createElement('p'); + + p.appendChild(text); + const content = p.innerHTML; + p.remove(); + + return content; + } \ No newline at end of file