asciibird/src/Dashboard.vue

625 lines
20 KiB
Vue
Raw Normal View History

<template>
<div id="app">
<t-modal
name="create-ascii-modal"
header="Create new ASCII"
2021-01-16 03:03:14 +00:00
:clickToClose="false"
:escToClose="false"
@before-closed="closeNewASCII"
>
2021-03-31 23:29:55 +00:00
2021-01-30 02:33:11 +00:00
Width
2021-01-16 03:03:14 +00:00
<t-input
type="number"
name="width"
v-model="forms.createAscii.width"
2021-04-02 00:19:33 +00:00
min="1"
2021-01-16 03:03:14 +00:00
/>
2021-01-30 02:33:11 +00:00
Height
2021-01-16 03:03:14 +00:00
<t-input
type="number"
name="height"
v-model="forms.createAscii.height"
2021-04-02 00:19:33 +00:00
min="1"
2021-01-16 03:03:14 +00:00
/>
2021-01-30 02:33:11 +00:00
Title
2021-01-16 03:03:14 +00:00
<t-input
type="text"
name="title"
v-model="forms.createAscii.title"
max="128"
/>
<template v-slot:footer>
2021-01-16 03:03:14 +00:00
<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>
2021-01-09 01:57:48 +00:00
<div>
<t-button @click="createClick()" class="ml-1">New ASCII</t-button>
<t-button @click="clearCache()" class="ml-1"
>Clear and Refresh</t-button
>
<t-button @click="startImport('mirc')" class="ml-1">Import mIRC</t-button>
2021-03-31 23:29:55 +00:00
<t-button @click="exportMirc()" class="ml-1" v-if="this.$store.getters.asciibirdMeta.length"
2021-03-30 08:36:53 +00:00
>Export ASCII to mIRC</t-button
>
<t-button @click="exportAsciibirdState()" class="ml-1" v-if="this.$store.getters.asciibirdMeta.length"
>Save Asciibird State</t-button
>
<t-button @click="startImport('asb')" class="ml-1"
>Load Asciibird State</t-button
>
<!-- <t-button @click="startImport('ansi')" class="ml-1">Import ANSI</t-button> -->
2021-01-16 03:03:14 +00:00
<input
type="file"
style="display: none"
ref="asciiInput"
@change="onImport()"
2021-01-16 03:03:14 +00:00
/>
<t-button
2021-03-31 23:29:55 +00:00
v-for="(value, key) in this.$store.getters.asciibirdMeta"
2021-01-16 03:03:14 +00:00
v-bind:key="key"
class="ml-1"
@click="changeTab(key, value)"
2021-01-30 02:33:11 +00:00
:disabled="false"
2021-01-16 03:03:14 +00:00
>
{{ value.title }}
2021-01-09 01:57:48 +00:00
</t-button>
2021-03-30 08:36:53 +00:00
<Toolbar
:canvas-x="canvasX"
:canvas-y="canvasY"
2021-03-31 23:29:55 +00:00
v-if="this.$store.getters.asciibirdMeta.length"
2021-03-30 08:36:53 +00:00
/>
<DebugPanel
:canvas-x="canvasX"
:canvas-y="canvasY"
2021-03-31 23:29:55 +00:00
v-if="this.$store.getters.asciibirdMeta.length"
2021-03-30 08:36:53 +00:00
/>
2021-03-31 23:29:55 +00:00
<Editor @coordsupdate="updateCoords" v-if="this.$store.getters.asciibirdMeta.length" />
2021-03-29 08:44:42 +00:00
2021-03-30 08:36:53 +00:00
<CharPicker v-if="$store.getters.getToolbarState.isChoosingChar" />
<ColourPicker
v-if="
$store.getters.getToolbarState.isChoosingFg ||
$store.getters.getToolbarState.isChoosingBg
"
/>
2021-01-09 01:57:48 +00:00
</div>
</div>
</template>
<style>
html {
cursor: url('assets/mouse-pointer-solid.svg'), auto;
}
</style>
<script>
2021-03-31 23:29:55 +00:00
import Toolbar from './components/Toolbar.vue';
import DebugPanel from './components/DebugPanel.vue';
import Editor from './views/Editor.vue';
// import * as Anser from "anser";
2021-03-31 23:29:55 +00:00
import CharPicker from './components/parts/CharPicker.vue';
import ColourPicker from './components/parts/ColourPicker.vue';
// import AsciiCursor from './components/parts/AsciiCursor.vue';
// import pako from 'pako';
export default {
async created() {
2021-03-31 23:29:55 +00:00
// Load from irc watch if present in the URL bar
const asciiUrl = new URL(location.href).searchParams.get('ircwatch');
if (asciiUrl) {
2021-03-31 23:29:55 +00:00
const res = await fetch(`https://irc.watch/ascii/txt/${asciiUrl}`);
const asciiData = await res.text();
this.mircAsciiImport(asciiData, asciiUrl);
2021-03-31 23:29:55 +00:00
window.location.href = '/';
}
// console.log(this.currentTool.svgPath)
// document.getElementsByTagName("body")[0].style.setProperty('cursor', "url('assets/fill-drip-solid.svg'), auto")
},
2021-03-31 23:29:55 +00:00
components: {
Toolbar, DebugPanel, Editor, CharPicker, ColourPicker
2021-03-31 23:29:55 +00:00
},
name: 'Dashboard',
data: () => ({
forms: {
createAscii: {
width: 5,
height: 5,
2021-03-31 23:29:55 +00:00
title: 'ascii',
},
},
currentTab: 1,
2021-03-29 05:10:31 +00:00
canvasX: null,
canvasY: null,
2021-04-02 02:07:49 +00:00
dashboardX: 0,
dashboardY: 0,
importType: null,
}),
computed: {
currentTool() {
return this.$store.getters.getToolbarIcons[this.$store.getters.getCurrentTool] ?? null
},
icon() {
return [this.currentTool.fa ?? 'fas', this.currentTool.icon ?? 'mouse-pointer']
},
},
methods: {
2021-03-29 05:10:31 +00:00
updateCoords(value) {
2021-03-30 08:36:53 +00:00
this.canvasX = value.x;
this.canvasY = value.y;
2021-03-29 05:10:31 +00:00
},
onImport() {
2021-01-23 02:50:32 +00:00
const { files } = this.$refs.asciiInput;
const filename = files[0].name;
2021-01-16 03:03:14 +00:00
const fileReader = new FileReader();
var _importType = this.importType
2021-03-31 23:29:55 +00:00
fileReader.addEventListener('load', () => {
switch(_importType) {
case 'asb':
this.importAsciibirdState(fileReader.result, filename)
break;
case 'mirc':
this.mircAsciiImport(fileReader.result, filename);
break;
}
});
// This will fire the file reader 'load' event
const asciiImport = fileReader.readAsText(files[0]);
},
startImport(type) {
2021-03-31 23:29:55 +00:00
// For ANSI we'll need to add back in the
// type cariable here
this.importType = type
console.log(this.importType)
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 = {
2021-03-29 05:10:31 +00:00
// 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;
// }
2021-03-29 05:10:31 +00:00
// 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) {
2021-03-31 23:29:55 +00:00
const MIRC_MAX_COLOURS = this.$store.getters.mircColours.length;
2021-03-29 05:10:31 +00:00
// The current state of the Colours
let curBlock = {
fg: null,
bg: null,
char: null,
};
// set asciiImport as the entire file contents as a string
2021-03-31 23:29:55 +00:00
const asciiImport = contents.split('\u0003\u0003').join('\u0003');
// This will end up in the asciibirdMeta
2021-03-31 23:29:55 +00:00
const finalAscii = {
width: false, // defined in: switch (curChar) case "\n":
2021-03-31 23:29:55 +00:00
height: asciiImport.split('\n').length,
title: filename,
key: this.$store.getters.nextTabValue,
blockWidth: 8 * this.$store.getters.blockSizeMultiplier,
blockHeight: 13 * this.$store.getters.blockSizeMultiplier,
2021-03-31 23:29:55 +00:00
blocks: this.create2DArray(asciiImport.split('\n').length),
2021-04-02 00:19:33 +00:00
x: 247, // the dragable ascii canvas x
y: 24, // the dragable ascii canvas y
};
// Turn the entire ascii string into an array
2021-03-31 23:29:55 +00:00
let asciiStringArray = asciiImport.split('');
// The proper X and Y value of the block inside the ASCII
let asciiX = 0;
let asciiY = 0;
2021-03-30 08:36:53 +00:00
// used to determine colours
let colourChar1 = null;
let colourChar2 = null;
2021-03-31 23:29:55 +00:00
let parsedColour = null;
2021-03-30 08:36:53 +00:00
// This variable just counts the amount of colour and char codes to minus
// to get the real width
2021-03-31 23:29:55 +00:00
let widthOfColCodes = 0;
2021-03-20 00:39:17 +00:00
while (asciiStringArray.length) {
2021-03-31 23:29:55 +00:00
const curChar = asciiStringArray[0];
// Defining a small finite state machine
// to detect the colour code
switch (curChar) {
2021-03-31 23:29:55 +00:00
case '\n':
// Reset the colours here on a new line
curBlock = {
fg: null,
bg: null,
char: null,
};
2021-03-31 23:29:55 +00:00
// the Y value of the ascii
asciiY++;
// We can determine the width at the end of the first line
2021-03-31 23:29:55 +00:00
// If for some reason like in aphex.txt the first line is
// trimmed it will trim the entire ascii!
if (!finalAscii.width) {
finalAscii.width = asciiImport.split('\n')[0].length - 1 - widthOfColCodes; // minus \n for the proper width
}
// Resets the X value
asciiX = 0;
asciiStringArray.shift();
break;
2021-03-31 23:29:55 +00:00
case '\u0003':
// Remove the colour char
asciiStringArray.shift();
2021-03-31 23:29:55 +00:00
widthOfColCodes++;
2021-03-31 23:29:55 +00:00
// Attempt to work out bg
2021-03-30 08:36:53 +00:00
colourChar1 = `${asciiStringArray[0]}`;
colourChar2 = `${asciiStringArray[1]}`;
parsedColour = parseInt(`${colourChar1}${colourChar2}`);
2021-03-31 23:29:55 +00:00
// Work out the 01, 02 double digit codes
if (parseInt(colourChar1) === 0 && parseInt(colourChar2) >= 0) {
asciiStringArray.shift();
2021-03-31 23:29:55 +00:00
widthOfColCodes += 1;
}
2021-03-29 05:10:31 +00:00
if (isNaN(parsedColour)) {
2021-03-30 08:36:53 +00:00
curBlock.bg = parseInt(colourChar1);
2021-03-31 23:29:55 +00:00
widthOfColCodes += 1;
2021-03-27 04:54:44 +00:00
asciiStringArray.shift();
2021-03-31 23:29:55 +00:00
} else if (parsedColour <= MIRC_MAX_COLOURS && parsedColour >= 0) {
2021-03-29 05:10:31 +00:00
curBlock.fg = parseInt(parsedColour);
2021-03-31 23:29:55 +00:00
widthOfColCodes += parsedColour.toString().length;
2021-03-27 04:54:44 +00:00
asciiStringArray = asciiStringArray.slice(
2021-03-29 05:10:31 +00:00
parsedColour.toString().length,
2021-03-31 23:29:55 +00:00
asciiStringArray.length,
2021-03-27 04:54:44 +00:00
);
}
// No background colour
2021-03-31 23:29:55 +00:00
if (asciiStringArray[0] !== ',') {
break;
} else {
2021-03-20 09:59:32 +00:00
// Remove , from array
asciiStringArray.shift();
}
2021-02-20 02:16:45 +00:00
2021-03-31 23:29:55 +00:00
// Attempt to work out bg
2021-03-30 08:36:53 +00:00
colourChar1 = `${asciiStringArray[0]}`;
colourChar2 = `${asciiStringArray[1]}`;
parsedColour = parseInt(`${colourChar1}${colourChar2}`);
2021-03-20 09:59:32 +00:00
if (
2021-03-31 23:29:55 +00:00
!isNaN(colourChar1)
&& !isNaN(colourChar2)
&& parseInt(colourChar2) > parseInt(colourChar1)
&& !isNaN(parsedColour)
&& parseInt(parsedColour) < 10
) {
2021-03-30 08:36:53 +00:00
parsedColour = parseInt(colourChar2);
asciiStringArray.shift();
}
2021-03-29 05:10:31 +00:00
if (isNaN(parsedColour)) {
2021-03-30 08:36:53 +00:00
curBlock.bg = parseInt(colourChar1);
2021-03-31 23:29:55 +00:00
widthOfColCodes += 1;
asciiStringArray.shift();
2021-03-31 23:29:55 +00:00
} else if (parsedColour <= MIRC_MAX_COLOURS && parsedColour >= 0) {
2021-03-29 05:10:31 +00:00
curBlock.bg = parseInt(parsedColour);
2021-03-31 23:29:55 +00:00
widthOfColCodes += parsedColour.toString().length;
2021-02-20 02:16:45 +00:00
asciiStringArray = asciiStringArray.slice(
2021-03-29 05:10:31 +00:00
parsedColour.toString().length,
2021-03-31 23:29:55 +00:00
asciiStringArray.length,
);
2021-02-20 02:16:45 +00:00
break;
}
break;
default:
curBlock.char = curChar;
asciiStringArray.shift();
asciiX++;
2021-03-31 23:29:55 +00:00
finalAscii.blocks[asciiY][asciiX - 1] = { ...curBlock };
break;
} // End Switch
} // End loop charPos
2021-03-31 23:29:55 +00:00
// Store the ASCII
this.$store.commit('newAsciibirdMeta', finalAscii);
2021-03-27 02:07:33 +00:00
2021-03-31 23:29:55 +00:00
// To show the ASCII after importing we get the last key
// from the asciiBirdMeta array
const keys = this.$store.getters.asciibirdMeta
2021-03-27 04:54:44 +00:00
.map((v, k) => k)
.filter((i) => i !== undefined);
2021-03-27 02:07:33 +00:00
2021-03-31 23:29:55 +00:00
// Set the current tab and pop the array for the last value
2021-03-27 02:07:33 +00:00
this.currentTab = keys.pop();
2021-03-31 23:29:55 +00:00
this.$store.commit('changeTab', this.currentTab);
// Update the browsers title to the ASCII filename
document.title = `asciibird - ${this.$store.getters.currentAscii.title}`;
2021-01-16 03:03:14 +00:00
},
importAsciibirdState(fileContents, fileName) {
// Import from a gzipped json file
// let input;
// try {
// // convert the incoming base64 -> binary
// const strData = fileContents;
// // split it into an array rather than a "string"
// const charData = strData.split('').map(function(x){return x.charCodeAt(0); });
// // var strData = String.fromCharCode.apply(null, pako.inflate(String.fromCharCode.apply(null, input).split("").map(function(x){return x.charCodeAt(0);})));
// input = pako.inflate(charData, { to: 'string' });
// console.log(input);
// } catch (err) {
// console.log(err);
// }
// console.log(fileContents)
// No gz for now unless can get the above working
this.$store.commit("changeState", Object.assign({},JSON.parse(fileContents)));
},
exportAsciibirdState() {
// Download to a gzipped json file
let output;
try {
// Make a gzipped JSON of the asciibird app state
// While making a gzip bellow works, had some trouble gunzipping in importAsciibirdState(fileContents, fileName)
// output = pako.gzip(JSON.stringify(this.$store.getters.getState), {level:"9"});
output = JSON.stringify(this.$store.getters.getState);
// Default timestamp for filename
let today = new Date();
let y = today.getFullYear();
let m = today.getMonth() + 1; // JavaScript months are 0-based.
let d = today.getDate();
let h = today.getHours();
let mi = today.getMinutes();
let s = today.getSeconds();
this.downloadToFile(output, `asciibird-${y}-${m}-${d}-${h}-${mi}-${s}.asb`, 'application/json');
} catch (err) {
console.log(err);
}
},
2021-03-30 08:36:53 +00:00
exportMirc() {
2021-03-31 23:29:55 +00:00
const { currentAscii } = this.$store.getters;
const output = [];
let curBlock = null;
let prevBlock = { bg: -1, fg: -1 };
2021-03-30 08:36:53 +00:00
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];
2021-03-31 23:29:55 +00:00
// If we have a difference between our previous block
// we'll put a colour codes and continue as normal
2021-03-31 00:46:33 +00:00
if (curBlock.bg !== prevBlock.bg || curBlock.fg !== prevBlock.fg) {
Object.assign(curBlock, currentAscii.blocks[y][x]);
2021-03-31 23:29:55 +00:00
const zeroPad = (num, places) => String(num).padStart(places, '0');
output.push(`\u0003${zeroPad(curBlock.fg ?? this.$store.getstate.options.defaultFg, 2)},${zeroPad(curBlock.bg ?? this.$store.getstate.options.defaultBg, 2)}`);
2021-03-30 08:36:53 +00:00
}
2021-03-31 23:29:55 +00:00
// null .chars will end up as space
output.push(curBlock.char ?? ' ');
2021-03-30 08:36:53 +00:00
prevBlock = currentAscii.blocks[y][x];
}
2021-03-31 23:29:55 +00:00
// We can never have a -1 colour code so we'll always
// write one at the start of each line
2021-03-31 00:46:33 +00:00
prevBlock = { bg: -1, fg: -1 };
2021-03-31 23:29:55 +00:00
// New line except for the very last line
2021-03-30 08:36:53 +00:00
if (y < currentAscii.blocks.length - 1) {
2021-03-31 23:29:55 +00:00
output.push('\n');
}
2021-03-30 08:36:53 +00:00
}
// Download to a txt file
// Check if txt already exists and append it
const filename = currentAscii.title.slice(currentAscii.title.length - 3) === 'txt' ? currentAscii.title : `${currentAscii.title}.txt`;
this.downloadToFile(output.join(''), filename, 'text/plain');
},
downloadToFile(content, filename, contentType) {
2021-03-30 08:36:53 +00:00
const downloadToFile = (content, filename, contentType) => {
2021-03-31 23:29:55 +00:00
const a = document.createElement('a');
2021-03-30 08:36:53 +00:00
const file = new Blob([content], { type: contentType });
a.href = URL.createObjectURL(file);
a.download = filename;
a.click();
URL.revokeObjectURL(a.href);
};
return downloadToFile(content, filename, contentType)
2021-03-30 08:36:53 +00:00
},
createClick() {
2021-03-31 23:29:55 +00:00
this.forms.createAscii.title = `New ASCII ${this.$store.getters.asciibirdMeta.length}`;
this.$modal.show('create-ascii-modal');
},
changeTab(key, value) {
// Update the tab index in vuex store
2021-03-13 03:30:58 +00:00
this.currentTab = key;
2021-03-31 23:29:55 +00:00
this.$store.commit('changeTab', key);
},
2021-01-30 02:33:11 +00:00
clearCache() {
localStorage.clear();
2021-03-31 23:29:55 +00:00
window.location.href = '/';
2021-01-30 02:33:11 +00:00
},
createNewASCII() {
const payload = {
title: this.forms.createAscii.title,
2021-03-31 23:29:55 +00:00
key: this.$store.getters.asciibirdMeta.length,
width: this.forms.createAscii.width,
height: this.forms.createAscii.height,
2021-03-27 02:07:33 +00:00
blockWidth: 8,
blockHeight: 13,
2021-04-02 00:19:33 +00:00
x: 247, // the dragable ascii canvas x
y: 24, // the dragable ascii canvas y
blocks: this.create2DArray(this.forms.createAscii.height),
};
// Push all the default ASCII blocks
2021-01-30 02:33:11 +00:00
for (let x = 0; x < payload.width; x++) {
for (let y = 0; y < payload.height; y++) {
payload.blocks[y].push({
2021-03-27 02:07:33 +00:00
bg: null,
fg: null,
char: null,
});
}
}
2021-03-31 23:29:55 +00:00
this.$store.commit('newAsciibirdMeta', payload);
this.$modal.hide('create-ascii-modal');
},
closeNewASCII({ params, cancel }) {
this.forms.createAscii.width = 5;
this.forms.createAscii.height = 5;
2021-03-31 23:29:55 +00:00
this.forms.createAscii.title = 'New ASCII';
},
create2DArray(rows) {
const arr = [];
for (let i = 0; i < rows; i++) {
arr[i] = [];
}
return arr;
},
2021-04-02 02:07:49 +00:00
captureMouse(event) {
// clientX/Y gives the coordinates relative to the viewport in CSS pixels.
// console.log("viewport", event.clientX);
// console.log("viewport", event.clientY);
// // pageX/Y gives the coordinates relative to the <html> element in CSS pixels.
// console.log("element", event.pageX);
// console.log("element", event.pageY);
this.dashboardX = event.pageX
this.dashboardY = event.pageY
// // screenX/Y gives the coordinates relative to the screen in device pixels.
// console.log("screen", event.screenX);
// console.log("screen", event.screenY);
}
},
};
</script>