64 lines
2 KiB
JavaScript
64 lines
2 KiB
JavaScript
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(/(\W)+/g); //Group not-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, index) => {
|
||
//if the word object hasn't been pre-processed elsewhere
|
||
if(wordObj.type == "word"){
|
||
//Add invisible whitespace in-between characters to keep it from breaking page layout
|
||
this.bodyArray[index].string = wordObj.string.split("").join("ㅤ");
|
||
}
|
||
});
|
||
}
|
||
} |