2020-11-06 15:47:22 +03:00
|
|
|
import { saveAs } from 'file-saver';
|
2021-05-06 01:22:08 +03:00
|
|
|
import bel from '../../../logic/lib/bel';
|
2020-11-06 15:47:22 +03:00
|
|
|
|
2020-05-09 06:51:18 +03:00
|
|
|
export default class Store {
|
2021-05-06 07:20:34 +03:00
|
|
|
state: any;
|
|
|
|
api: any;
|
|
|
|
setState: any;
|
2020-05-01 05:54:12 +03:00
|
|
|
constructor() {
|
2020-05-08 00:15:59 +03:00
|
|
|
this.state = this.initialState();
|
|
|
|
}
|
|
|
|
|
|
|
|
initialState() {
|
|
|
|
return {
|
2020-10-24 02:25:44 +03:00
|
|
|
lines: [''],
|
2021-05-06 01:22:08 +03:00
|
|
|
cursor: 0
|
2020-05-01 05:54:12 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-12 00:58:13 +03:00
|
|
|
clear() {
|
2021-05-06 01:22:08 +03:00
|
|
|
this.setState(this.initialState());
|
2020-05-12 00:58:13 +03:00
|
|
|
}
|
|
|
|
|
2020-05-01 05:54:12 +03:00
|
|
|
handleEvent(data) {
|
2020-11-11 22:59:45 +03:00
|
|
|
// process slogs
|
|
|
|
//
|
|
|
|
if (data.slog) {
|
2021-05-06 01:22:08 +03:00
|
|
|
this.state.lines.splice(this.state.lines.length-1, 0, { lin: [data.slog] });
|
2020-11-11 22:59:45 +03:00
|
|
|
this.setState({ lines: this.state.lines });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// process blits
|
|
|
|
//
|
2020-10-24 02:25:44 +03:00
|
|
|
const blit = data.data;
|
|
|
|
switch (Object.keys(blit)[0]) {
|
|
|
|
case 'bel':
|
2020-11-10 23:11:58 +03:00
|
|
|
bel.play();
|
2020-10-24 02:25:44 +03:00
|
|
|
break;
|
|
|
|
case 'clr':
|
2020-11-07 00:35:38 +03:00
|
|
|
this.state.lines = this.state.lines.slice(-1);
|
|
|
|
this.setState({ lines: this.state.lines });
|
2020-10-24 02:25:44 +03:00
|
|
|
break;
|
2020-05-01 05:54:12 +03:00
|
|
|
case 'hop':
|
2020-11-10 22:42:28 +03:00
|
|
|
// since lines are lists of characters that might span multiple
|
|
|
|
// codepoints, we need to calculate the byte-wise cursor position
|
|
|
|
// to avoid incorrect cursor rendering.
|
2020-11-11 22:59:45 +03:00
|
|
|
//
|
2021-05-06 01:22:08 +03:00
|
|
|
const line = this.state.lines[this.state.lines.length - 1];
|
2020-11-10 22:42:28 +03:00
|
|
|
let hops;
|
|
|
|
if (line.lin) {
|
|
|
|
hops = line.lin.slice(0, blit.hop);
|
2021-05-06 01:22:08 +03:00
|
|
|
} else if (line.klr) {
|
2020-11-10 22:42:28 +03:00
|
|
|
hops = line.klr.reduce((h, p) => {
|
2021-05-06 01:22:08 +03:00
|
|
|
if (h.length >= blit.hop)
|
|
|
|
return h;
|
|
|
|
return [...h, ...p.text.slice(0, blit.hop - h.length)];
|
2020-11-10 22:42:28 +03:00
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
this.setState({ cursor: hops.join('').length });
|
2020-10-24 02:25:44 +03:00
|
|
|
break;
|
|
|
|
case 'lin':
|
|
|
|
this.state.lines[this.state.lines.length - 1] = blit;
|
|
|
|
this.setState({ lines: this.state.lines });
|
|
|
|
break;
|
|
|
|
case 'klr':
|
|
|
|
this.state.lines[this.state.lines.length - 1] = blit;
|
|
|
|
this.setState({ lines: this.state.lines });
|
2020-05-01 05:54:12 +03:00
|
|
|
break;
|
2020-10-24 02:25:44 +03:00
|
|
|
case 'mor':
|
|
|
|
this.state.lines.push('');
|
|
|
|
this.setState({ lines: this.state.lines });
|
|
|
|
break;
|
2020-11-06 15:47:22 +03:00
|
|
|
case 'sag':
|
|
|
|
blit.sav = blit.sag;
|
2021-05-06 07:20:34 +03:00
|
|
|
break;
|
2020-11-06 15:47:22 +03:00
|
|
|
case 'sav':
|
2021-05-06 01:22:08 +03:00
|
|
|
const name = blit.sav.path.split('/').slice(-2).join('.');
|
|
|
|
const buff = new Buffer(blit.sav.file, 'base64');
|
|
|
|
const blob = new Blob([buff], { type: 'application/octet-stream' });
|
2020-11-06 15:47:22 +03:00
|
|
|
saveAs(blob, name);
|
|
|
|
break;
|
2020-10-24 02:25:44 +03:00
|
|
|
case 'url':
|
2021-05-06 01:22:08 +03:00
|
|
|
// TODO too invasive? just print as <a>?
|
2020-10-24 02:25:44 +03:00
|
|
|
window.open(blit.url);
|
|
|
|
break;
|
|
|
|
default: console.log('weird blit', blit);
|
2020-05-01 05:54:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setStateHandler(setState) {
|
|
|
|
this.setState = setState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|