xxxxxxxxxx
90
// A2Z F24
// Daniel Shiffman
// https://github.com/Programming-from-A-to-Z/A2Z-F24
// Many DOM elements
let dropZone, input, button, sample, clearButton;
// An array to keep track of all the new DOM elements being added
let paragraphs = [];
let inputText = '';
// An extra element, an input element
let seedInput;
function setup() {
noCanvas();
// Selecting the text field and button
input = select('#textinput');
button = select('#submit');
// What to do when button pressed
button.mousePressed(handleInput);
// Selected the div which will be the "drop zone"
// for dragging and dropping files
dropZone = select('#drop_zone');
// Here are the events to handle
dropZone.dragOver(highlight);
dropZone.drop(gotFile, unHighlight);
// This link allows quick testing with a file
// that's ready to load instantly
sample = select('#sample');
sample.mousePressed(loadFile);
// This button clears the new paragraph elements added
clearButton = select('#clearButton');
clearButton.mousePressed(clearText);
// Diastic Seed
seedInput = select('#seed');
}
// Load a file for quick testing
function loadFile() {
loadStrings('files/spam.txt', fileLoaded);
}
// When the file is loaded
function fileLoaded(data) {
const txt = data.join('\n');
input.html(txt);
}
// Handle dropzone events
function highlight() {
dropZone.style('background', '#AAA');
}
function unHighlight() {
dropZone.style('background','');
}
function gotFile(file) {
if (file.type === 'text') {
inputText += file.data + '\n\n';
input.html(inputText);
} else {
// In case it's some weird other kind of file
alert('this is not a text file.');
}
}
// Handle the text input field
function handleInput() {
process(input.value());
}
// Clear all the divs with remove()
function clearText() {
input.html('');
for (var i = 0; i < paragraphs.length; i++) {
paragraphs[i].remove();
}
paragraphs = [];
}