* Dynamic updates for highlights

This commit is contained in:
Daniel Legt 2022-06-07 00:53:25 +03:00
parent 0a3b5d50f2
commit 30dc23c847
1 changed files with 29 additions and 4 deletions

View File

@ -184,15 +184,27 @@ function updatePadContent(newContent, textArea = true) {
const prev = document.getElementById(`textarea-preview`); const prev = document.getElementById(`textarea-preview`);
const shouldScroll = prev.scrollTop >= (prev.scrollHeight - Number(getComputedStyle(prev).height.replace(/px/g, ''))) * 0.98; 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 // Check if we should follow the bottom scrolling
if (shouldScroll) { if (shouldScroll) {
prev.scrollTop = prev.scrollHeight; prev.scrollTop = prev.scrollHeight;
} }
// TODO: Re-run the syntax highlight
} }
function updatePadViewers(vc) { function updatePadViewers(vc) {
@ -223,4 +235,17 @@ function connectSocket() {
// wait for the whole window to load // wait for the whole window to load
window.addEventListener(`load`, e => { window.addEventListener(`load`, e => {
connectSocket() connectSocket()
}) })
// 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;
}