canopy/www/js/channel/chatPreprocessor.js

76 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class chatPreprocessor{
constructor(){
}
preprocess(chatEntry){
//Set current chat nodes
this.chatEntry = chatEntry;
this.chatBody = this.chatEntry.querySelector(".chat-entry-body");
//Split the chat body into an array of objects representing each word
//We could pass this through arguments but these functions wont be very interoperable anyways since they expect a purpose-made hashtable
this.splitBody();
//Inject whitespace into un-processed words
this.addWhitespace();
//Inject the pre-processed chat into the chatEntry node
this.injectBody();
//Return the pre-processed node
return this.chatEntry;
}
splitBody(){
//Create an empty array to hold the body
this.bodyArray = [];
//Split string by words
const splitString = this.chatBody.innerHTML.split(/\b/g); //Group words together
//for each word in the splitstring
splitString.forEach((string) => {
//create a word object
const wordObj = {
string: string,
type: "word"
}
//Add it to our body array
this.bodyArray.push(wordObj);
});
}
injectBody(){
//Create empty array to hold strings
var stringArray = [];
//Extract strings into the empty array
this.bodyArray.forEach((wordObj) => {
stringArray.push(wordObj.string);
});
//Join the strings to fill the chat entry
this.chatBody.innerHTML = stringArray.join("");
}
addWhitespace(){
//for each word object in the body
this.bodyArray.forEach((wordObj, wordIndex) => {
//if the word object hasn't been pre-processed elsewhere
if(wordObj.type == "word"){
var wordArray = [];
//Add invisible whitespace in-between characters to keep it from breaking page layout
//this.bodyArray[index].string = wordObj.string.split("").join("");
this.bodyArray[wordIndex].string.split("").forEach((char, charIndex) => {
wordArray.push(char);
//After eight characters
if(charIndex > 8){
//Push an invisible line-break character between every character
wordArray.push("");
}
});
this.bodyArray[wordIndex].string = wordArray.join("");
}
});
}
}