From c17f20d39ccbd5a7ef7c34ef7451cf83086b8940 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 10 May 2024 12:54:32 +0200 Subject: [PATCH] refactoring Signed-off-by: Matteo Collina --- static/scripts/chat.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/static/scripts/chat.js b/static/scripts/chat.js index 592a406..efe8641 100644 --- a/static/scripts/chat.js +++ b/static/scripts/chat.js @@ -252,9 +252,7 @@ async function drawStreamedMessageContents (parent, message) { const lines = tokenString.split('\n') if (newLine) { - lines[0] = lines[0].replace(/^ +/g, (spaces) => { - return spaces.split('').map(() => ' ').join('') - }) + lines[0] = addNonBreakingSpaces(lines[0]) newLine = false } @@ -264,13 +262,10 @@ async function drawStreamedMessageContents (parent, message) { current.scrollIntoView(false) for (let i = 1; i < lines.length; i++) { - // console.log(`line (newline=${newLine})`, JSON.stringify(lines[i])) current = document.createElement('p') parent.appendChild(current) current.scrollIntoView(false) - lines[i] = lines[i].replace(/^ +/g, (spaces) => { - return spaces.split('').map(() => ' ').join('') - }) + lines[i] = addNonBreakingSpaces(lines[i]) current.innerHTML += lines[i] newLine = true } @@ -285,6 +280,16 @@ async function drawStreamedMessageContents (parent, message) { promptLock = false } +/** + * @param {string} message + * @returns {string} + */ +function addNonBreakingSpaces (str) { + return str.replace(/^ +/g, (spaces) => { + return spaces.split('').map(() => ' ').join('') + }) +} + /** * @param {{ prompt: string, response: string, errored: boolean | undefined }} message * @returns {HTMLParagraphElement}