asciibird/src/Dashboard.vue

543 lines
16 KiB
Vue

<template>
<div id="app">
<t-modal
name="create-ascii-modal"
header="Create new ASCII"
:clickToClose="false"
:escToClose="false"
@before-closed="closeNewASCII"
>
<!-- Main Menu -->
Width
<t-input
type="number"
name="width"
v-model="forms.createAscii.width"
max="3"
/>
Height
<t-input
type="number"
name="height"
v-model="forms.createAscii.height"
max="4"
/>
Title
<t-input
type="text"
name="title"
v-model="forms.createAscii.title"
max="128"
/>
<template v-slot:footer>
<div
class="flex justify-between"
@click="$modal.hide('create-ascii-modal')"
>
<t-button type="button"> Cancel </t-button>
<t-button type="button" @click="createNewASCII()"> Ok </t-button>
</div>
</template>
</t-modal>
<div>
<t-button @click="createClick()" class="ml-1">New ASCII</t-button>
<t-button @click="clearCache()" v-if="asciibirdMeta.length" class="ml-1"
>Clear and Refresh</t-button
>
<t-button @click="startImport('mirc')" class="ml-1">Import mIRC</t-button>
<t-button @click="exportMirc()" class="ml-1" v-if="asciibirdMeta.length"
>Export ASCII to mIRC</t-button
>
<!-- <t-button @click="startImport('ansi')" class="ml-1">Import ANSI</t-button> -->
<input
type="file"
style="display: none"
ref="asciiInput"
@change="onImport()"
/>
<t-button
v-for="(value, key) in asciibirdMeta"
v-bind:key="key"
class="ml-1"
@click="changeTab(key, value)"
:disabled="false"
>
{{ value.title }}
</t-button>
<Toolbar
:canvas-x="canvasX"
:canvas-y="canvasY"
v-if="asciibirdMeta.length"
/>
<DebugPanel
:canvas-x="canvasX"
:canvas-y="canvasY"
v-if="asciibirdMeta.length"
/>
<Editor @coordsupdate="updateCoords" v-if="asciibirdMeta.length" />
<CharPicker v-if="$store.getters.getToolbarState.isChoosingChar" />
<ColourPicker
v-if="
$store.getters.getToolbarState.isChoosingFg ||
$store.getters.getToolbarState.isChoosingBg
"
/>
</div>
</div>
</template>
<script>
import Toolbar from "./components/Toolbar.vue";
import DebugPanel from "./components/DebugPanel.vue";
import Editor from "./views/Editor.vue";
// import * as Anser from "anser";
import CharPicker from "./components/parts/CharPicker.vue";
import ColourPicker from "./components/parts/ColourPicker.vue";
export default {
async created() {
this.mircColours = this.$store.getters.mircColours;
this.charCodes = this.$store.getters.charCodes;
this.asciibirdMeta = this.$store.getters.asciibirdMeta;
let asciiUrl = new URL(location.href).searchParams.get("ircwatch");
if (asciiUrl) {
let res = await fetch(`https://irc.watch/ascii/txt/${asciiUrl}`);
let asciiData = await res.text();
this.mircAsciiImport(asciiData, asciiUrl);
window.location.href = "/";
}
},
components: { Toolbar, DebugPanel, Editor, CharPicker, ColourPicker },
name: "Dashboard",
data: () => ({
forms: {
createAscii: {
width: 5,
height: 5,
title: "ascii",
},
},
formsDefault: null,
status: {
createNew: false,
},
text: "ASCII",
currentTab: 1,
asciibirdMeta: [],
mircColours: null,
charCodes: null,
refresh: false,
asciiImport: "",
finalAscii: null,
asciiArray: [],
imageUrl: null,
parseColour: false,
ColourCode: false,
importBlocks: null,
importFormat: null,
canvasX: null,
canvasY: null,
dashboardX: null,
dashboardY: null,
}),
methods: {
updateCoords(value) {
this.canvasX = value.x;
this.canvasY = value.y;
},
onImport() {
const { files } = this.$refs.asciiInput;
const filename = files[0].name;
const fileReader = new FileReader();
this.asciiImport = fileReader.readAsText(files[0]);
fileReader.addEventListener("load", () => {
switch (this.importFormat) {
// case "ansi":
// this.asniImport(fileReader.result, filename);
// break;
case "mirc":
this.mircAsciiImport(fileReader.result, filename);
break;
}
this.importFormat = null;
});
this.asciiImportFile = files[0];
},
startImport(type) {
this.importFormat = type;
this.$refs.asciiInput.click();
},
// We can maybe try something different to import ANSI
// asniImport(contents, filename) {
// let ansiArray = contents.split("\n");
// let ansiWidth = 0;
// this.finalAscii = {
// width: false, // defined in: switch (curChar) case "\n":
// height: contents.split("\r\n").length,
// title: filename,
// key: this.$store.getters.nextTabValue,
// blockWidth: 8 * this.$store.getters.blockSizeMultiplier,
// blockHeight: 13 * this.$store.getters.blockSizeMultiplier,
// blocks: this.create2DArray(contents.split("\r\n").length),
// };
// for (let i = 0; i <= ansiArray.length - 1; i++) {
// if (ansiWidth > 0 && this.finalAscii.width === false) {
// this.finalAscii.width = ansiWidth;
// }
// ansiWidth = 0;
// for (let j = 0; j <= ansiArray[i].length - 1; j++) {
// let ansiParse = Anser.ansiToJson(ansiArray[i]);
// for (let l = 0; l <= ansiParse.length - 1; l++) {
// var contentArray = ansiParse[l].content.split("");
// var curBlock = {
// fg: this.mircColours.indexOf(`rgb(${ansiParse[l].fg})`),
// bg: this.mircColours.indexOf(`rgb(${ansiParse[l].bg})`),
// char: null,
// };
// // If we had no matches in our mIRC RGB lookup, then we have to try match
// // the ASNI colours to the best mIRC colour
// if (curBlock.fg === -1) {
// switch (ansiParse[l].fg) {
// case "187, 187, 0": // orangeish yellow
// curBlock.fg = 8;
// break;
// case "187, 0, 0": // red
// curBlock.fg = 4;
// break;
// }
// }
// if (curBlock.bg === -1) {
// switch (ansiParse[l].bg) {
// case "187, 187, 0": // orangeish yellow
// curBlock.bg = 8;
// break;
// case "187, 0, 0": // red
// curBlock.bg = 4;
// break;
// }
// }
// for (let k = 0; k <= contentArray.length - 1; k++) {
// if (contentArray[k] === "\r") {
// continue;
// }
// this.mircColours.indexOf(`rgb(${ansiParse[l].fg})`);
// curBlock.char = contentArray[k];
// this.finalAscii.blocks[i][ansiWidth] = JSON.parse(
// JSON.stringify(curBlock)
// );
// ansiWidth++;
// }
// }
// }
// }
// this.$store.commit("newAsciibirdMeta", this.finalAscii);
// },
mircAsciiImport(contents, filename) {
const MIRC_MAX_ColourS = this.mircColours.length;
// The current state of the Colours
let curBlock = {
fg: null,
bg: null,
char: null,
};
// set asciiImport as the entire file contents as a string
this.asciiImport = contents.split("\u0003\u0003").join("\u0003");
// This will end up in the asciibirdMeta
this.finalAscii = {
width: false, // defined in: switch (curChar) case "\n":
height: this.asciiImport.split("\n").length,
title: filename,
key: this.$store.getters.nextTabValue,
blockWidth: 8 * this.$store.getters.blockSizeMultiplier,
blockHeight: 13 * this.$store.getters.blockSizeMultiplier,
blocks: this.create2DArray(this.asciiImport.split("\n").length),
};
// Turn the entire ascii string into an array
let asciiStringArray = this.asciiImport.split("");
// The proper X and Y value of the block inside the ASCII
let asciiX = 0;
let asciiY = 0;
// used to determine colours
let colourChar1 = null;
let colourChar2 = null;
var parsedColour = null;
// This variable just counts the amount of colour and char codes to minus
// to get the real width
var theWidth = 0;
// for (let charPos = 0; charPos <= this.asciiImport.length - 1; charPos++) {
while (asciiStringArray.length) {
let curChar = asciiStringArray[0];
// Defining a small finite state machine
// to detect the colour code
switch (curChar) {
case "\n":
// Reset the colours here on a new line
curBlock = {
fg: null,
bg: null,
char: null,
};
//
asciiY++;
// We can determine the width at the end of the first line
if (!this.finalAscii.width) {
this.finalAscii.width =
this.asciiImport.split("\n")[0].length - 1 - theWidth; // minus \n for the proper width
}
// Resets the X value
asciiX = 0;
asciiStringArray.shift();
break;
case "\u0003":
asciiStringArray.shift();
theWidth++;
colourChar1 = `${asciiStringArray[0]}`;
colourChar2 = `${asciiStringArray[1]}`;
parsedColour = parseInt(`${colourChar1}${colourChar2}`);
if (parseInt(colourChar1) === 0 && parseInt(colourChar2) >= 0) {
asciiStringArray.shift();
theWidth += 1;
}
if (isNaN(parsedColour)) {
curBlock.bg = parseInt(colourChar1);
theWidth += 1;
asciiStringArray.shift();
} else if (parsedColour <= MIRC_MAX_ColourS && parsedColour >= 0) {
curBlock.fg = parseInt(parsedColour);
theWidth += parsedColour.toString().length;
asciiStringArray = asciiStringArray.slice(
parsedColour.toString().length,
asciiStringArray.length
);
}
colourChar1 = null;
colourChar2 = null;
parsedColour = null;
// No background colour
if (asciiStringArray[0] !== ",") {
break;
} else {
// Remove , from array
asciiStringArray.shift();
}
colourChar1 = `${asciiStringArray[0]}`;
colourChar2 = `${asciiStringArray[1]}`;
parsedColour = parseInt(`${colourChar1}${colourChar2}`);
// Work out the 01, 02
if (
!isNaN(colourChar1) &&
!isNaN(colourChar2) &&
parseInt(colourChar2) > parseInt(colourChar1) &&
!isNaN(parsedColour) &&
parseInt(parsedColour) < 10
) {
parsedColour = parseInt(colourChar2);
asciiStringArray.shift();
}
if (isNaN(parsedColour)) {
curBlock.bg = parseInt(colourChar1);
theWidth += 1;
asciiStringArray.shift();
} else if (parsedColour <= MIRC_MAX_ColourS && parsedColour >= 0) {
curBlock.bg = parseInt(parsedColour);
theWidth += parsedColour.toString().length;
asciiStringArray = asciiStringArray.slice(
parsedColour.toString().length,
asciiStringArray.length
);
break;
}
break;
default:
curBlock.char = curChar;
asciiStringArray.shift();
asciiX++;
// Fk this js shit, serialising the curBlock works much better. Lost hours on this bs, fk.
this.finalAscii.blocks[asciiY][asciiX - 1] = JSON.parse(
JSON.stringify(curBlock)
);
break;
} // End Switch
// break;
} // End loop charPos
this.$store.commit("newAsciibirdMeta", this.finalAscii);
this.asciibirdMeta = this.$store.getters.asciibirdMeta;
let keys = this.asciibirdMeta
.map((v, k) => k)
.filter((i) => i !== undefined);
this.currentTab = keys.pop();
this.$store.commit("changeTab", this.currentTab);
document.title = `asciibird - ${this.$store.getters.currentAscii.title}`;
},
exportMirc() {
let currentAscii = this.$store.getters.currentAscii;
let output = [];
var curBlock = null;
var prevBlock = { bg: -1, fg: -1 };
for (let y = 0; y <= currentAscii.blocks.length - 1; y++) {
for (let x = 0; x <= currentAscii.blocks[y].length - 1; x++) {
curBlock = currentAscii.blocks[y][x];
if (curBlock.bg !== prevBlock.bg || curBlock.fg !== prevBlock.fg) {
Object.assign(curBlock, currentAscii.blocks[y][x]);
const zeroPad = (num, places) => String(num).padStart(places, "0");
output.push(
`\u0003${zeroPad(curBlock.fg ?? 0, 2)},${zeroPad(
curBlock.bg ?? 1,
2
)}`
);
}
output.push(curBlock.char ?? " ");
prevBlock = currentAscii.blocks[y][x];
}
prevBlock = { bg: -1, fg: -1 };
// New line except for last line
if (y < currentAscii.blocks.length - 1) {
output.push("\n");
}
}
// Download to a txt file
const downloadToFile = (content, filename, contentType) => {
const a = document.createElement("a");
const file = new Blob([content], { type: contentType });
a.href = URL.createObjectURL(file);
a.download = filename;
a.click();
URL.revokeObjectURL(a.href);
};
// Check if txt already exists and append it
var filename =
currentAscii.title.slice(currentAscii.title.length - 3) === "txt"
? currentAscii.title
: `${currentAscii.title}.txt`;
downloadToFile(output.join(""), filename, "text/plain");
},
createClick() {
this.forms.createAscii.title = `New ASCII ${this.asciibirdMeta.length}`;
this.$modal.show("create-ascii-modal");
},
changeTab(key, value) {
// Update the tab index in vuex store
this.currentTab = key;
this.refresh = !this.refresh;
this.$store.commit("changeTab", key);
},
clearCache() {
localStorage.clear();
window.location.href = "/";
},
createNewASCII() {
const payload = {
title: this.forms.createAscii.title,
key: this.asciibirdMeta.length,
width: this.forms.createAscii.width,
height: this.forms.createAscii.height,
blockWidth: 8,
blockHeight: 13,
blocks: this.create2DArray(this.forms.createAscii.height),
};
// Push all the default ASCII blocks
for (let x = 0; x < payload.width; x++) {
for (let y = 0; y < payload.height; y++) {
payload.blocks[y].push({
bg: null,
fg: null,
char: null,
});
}
}
this.$store.commit("newAsciibirdMeta", payload);
this.$modal.hide("create-ascii-modal");
this.refresh = !this.refresh;
},
closeNewASCII({ params, cancel }) {
this.forms.createAscii.width = 5;
this.forms.createAscii.height = 5;
this.forms.createAscii.title = "New ASCII";
},
create2DArray(rows) {
const arr = [];
for (let i = 0; i < rows; i++) {
arr[i] = [];
}
return arr;
},
},
};
</script>