-
Notifications
You must be signed in to change notification settings - Fork 4
/
artify.js
42 lines (35 loc) · 1.15 KB
/
artify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
import { logger } from '/lib/logger.js';
import { data } from '../data.js';
import { artify } from '../views/artify.js';
/**
* creates a mirror-image rendering of the user input and displays the growing artwork
* @param {Event} event - triggered whenever a user releases a key in the input area
*/
export const artifyHandler = event => {
// ignore the event if the user is correcting a mistake
if (event.keyCode === 8 || event.keyCode === 46) {
return;
}
// read & process user input
// remove newlines from the input value
// keeps things pretty for the art
// and allows user to use enter to submit the same input unchanged
const target = event.target;
const newEntry =
event.keyCode === 13 ? target.value.split('\n').join('') : target.value;
// update state
data.lines.push(newEntry);
// render state & update UI
const renderedArtwork = artify(data.lines, data.separator);
const outputContainer = document.getElementById('output');
outputContainer.innerHTML = '';
outputContainer.appendChild(renderedArtwork);
logger.add({
handler: 'artify',
newEntry,
renderedArtwork,
data,
event,
});
};