From 38739ca7ad89a3b743696e9b3b95c26d1a50b07a Mon Sep 17 00:00:00 2001 From: "caleb.mabry.15@cnu.edu" Date: Sun, 20 Mar 2022 14:08:10 -0400 Subject: Adding custom markdown based on asset file --- webAO/client.js | 10 ++++-- webAO/utils/aoml.js | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 webAO/utils/aoml.js (limited to 'webAO') diff --git a/webAO/client.js b/webAO/client.js index 14bfd08..a59508e 100644 --- a/webAO/client.js +++ b/webAO/client.js @@ -10,7 +10,7 @@ import fileExistsSync from './utils/fileExistsSync'; import { escapeChat, encodeChat, prepChat, safeTags, } from './encoding.js'; - +import mlConfig from './utils/aoml'; // Load some defaults for the background and evidence dropdowns import vanilla_character_arr from './constants/characters.js'; import vanilla_music_arr from './constants/music.js'; @@ -43,6 +43,8 @@ const DEFAULT_HOST = 'http://attorneyoffline.de/base/'; let AO_HOST = asset || DEFAULT_HOST; const THEME = theme || 'default'; +const attorneyMarkdown = mlConfig(AO_HOST) + const UPDATE_INTERVAL = 60; /** @@ -1938,6 +1940,7 @@ class Viewport { * @param {object} chatmsg the new chat message */ async say(chatmsg) { + this.chatmsg = chatmsg; this.textnow = ''; this.sfxplayed = 0; @@ -2089,7 +2092,7 @@ class Viewport { if (soundChecks.some((check) => this.chatmsg.sound === check)) { this.chatmsg.sound = this.chatmsg.effects[2]; } - + this.chatmsg.parsed = attorneyMarkdown.applyMarkdown(chatmsg.content) this.tick(); } @@ -2242,7 +2245,7 @@ class Viewport { } this.textnow = this.chatmsg.content.substring(0, this.textnow.length + 1); - chatBoxInner.innerText = this.textnow; + chatBoxInner.appendChild(this.chatmsg.parsed[this.textnow.length - 1]); // scroll to bottom chatBox.scrollTop = chatBox.scrollHeight; @@ -2325,6 +2328,7 @@ export function onEnter(event) { if (emote_mod === 0) { emote_mod = 1; } } else if (emote_mod === 1) { emote_mod = 0; } + client.sendIC( 'chat', myemo.preanim, diff --git a/webAO/utils/aoml.js b/webAO/utils/aoml.js new file mode 100644 index 0000000..6c4cafb --- /dev/null +++ b/webAO/utils/aoml.js @@ -0,0 +1,100 @@ +import request from "../services/request" + + +const aomlParser = (text) => { + let parsed = {} + let currentHeader = '' + for (const line of text.split(/\r?\n/)) { + if (line === '') { + currentHeader = '' + continue; + } + const content = line.split(' = ') + const contentName = content[0] + const contentValue = content[1] + if (currentHeader === '') { + currentHeader = contentName + parsed[currentHeader] = { + color: contentValue + } + } else { + const contentKey = contentName.split('_')[1] + parsed[currentHeader][contentKey] = contentValue + } + } + return parsed +} + + + + +const mlConfig = (AO_HOST) => { + const defaultUrl = `${AO_HOST}themes/default/chat_config.ini` + let aomlParsed = {} + + request(defaultUrl).then((data) => { + aomlParsed = aomlParser(data) + } + ); + + const createIdentifiers = () => { + const identifiers = new Map() + for (const [ruleName, value] of Object.entries(aomlParsed)) { + if (identifiers.has(value.start)) { + throw new Error() + } + else if (value.start) { + identifiers.set(value.start, value.end) + } + } + return identifiers + } + const colorIdentifiers = () => { + let colorIdentifier = {} + for (const [ruleName, value] of Object.entries(aomlParsed)) { + colorIdentifier[value.start] = value.color + } + return colorIdentifier + } + const applyMarkdown = (text) => { + const identifiers = createIdentifiers(aomlParsed) + const startIdentifiers = new Set(identifiers.keys()) + const colorIdentifier = colorIdentifiers() + const identifierClosingStack = [] + const colorStack = [[255, 255, 255]] + // each value in output will be an html element + let output = [] + for (const letter of text) { + let currentSelector = document.createElement('span') + + const lastItem = identifierClosingStack.length + const closingToLookFor = identifierClosingStack[lastItem-1] + if (letter === closingToLookFor) { + identifierClosingStack.pop() + colorStack.pop() + } + else if (startIdentifiers.has(letter)) { + identifierClosingStack.push(identifiers.get(letter)) + const colors = colorIdentifier[letter].split(',') + const r = colors[0] + const g = colors[1] + const b = colors[2] + colorStack.push([r,g,b]) + } else { + currentSelector.innerHTML = letter + } + const r = colorStack[colorStack.length-1][0] + const g = colorStack[colorStack.length-1][1] + const b = colorStack[colorStack.length-1][2] + const currentColor = `color: rgb(${r},${g},${b});` + currentSelector.setAttribute('style', currentColor) + output.push(currentSelector) + } + return output + } + return { + applyMarkdown + } +} + +export default mlConfig \ No newline at end of file -- cgit