Connect Stack with the UI
Implement the script to connect the Stack class with the HTML web page and finish the project.
We'll cover the following...
Introduction
So far, we've completed the implementation of the Stack
class to store the operations made by the user in the text editor. Now, we need to connect the class with UI to perform the undo operation from the HTML web page. We'll follow the steps below:
Access all the required HTML elements from the web page using the
getElementById()
function.Attach two event listeners to the text editor: one for the click event and the other for the input event (when there is some input in the text editor).
Attach an event listener to the button to clear all the text from the text editor.
Attach an event listener to the undo button to undo the operations one by one.
Implementing the script to connect the stack to the UI
We'll execute all the steps mentioned above when the web page is loaded. Here's the implementation:
import { Stack } from './stack.js';// Function to executed when the web page is loadedonload = function () {// Store the required HTML elementsconst textbox = document.getElementById('comment');const undo = document.getElementById('undo');const clear = document.getElementById('clear');const temptext = document.getElementById('temptext');// Initialize the variablestextbox.value = "";let text = "";let stack = new Stack();// Attach a click event listener to the button to clear the text editorclear.onclick = function () {stack.clear(); // Call the clear() function from the Stack classtext = ""; // Set the text to empty stringtextbox.value = ""; // Set the value of the text box to empty stringtemptext.innerHTML = "Sequence of operations will be shown here !"; // Set the message displaying the operation performed};// Attach an input event listener to the text box to insert data to the stack// when the user enters text and removes the text by pressing the backspace keytextbox.oninput = function(event){switch(event.inputType){// Case when the character is inserted in the text boxcase "insertText":stack.push(0, event.data); // Store the operation type and the data in the stackbreak;// Case when the character is removed from the text boxcase "deleteContentBackward":stack.push(1, text[text.length - 1]); // Store the operation type and the last character removed from the text editor in the stackbreak;}// Set the message on the right side of the web page to display the data in the stacktemptext.innerHTML = "Stack data: Operation " + stack.top() + "<br>" + temptext.innerHTML;// Store the current text data in the text boxtext = textbox.value;};// Attach a click event listener to button to perform the undo operationundo.onclick = function () {// Remove the top element from the stacklet operation = stack.pop();// Check if the stack is not empty since the pop() function returns -1 as operation type if the stack is emptyif(operation[0] !== -1){// Set the message to display the undo operation performedtemptext.innerHTML = "Performing undo operation<br>"+temptext.innerHTML;// Check if the operation type was insertif(operation[0] === 0){// Store the length of the stringlet len = operation[1].length;// Update the text box valuetextbox.value = textbox.value.substring(0,textbox.value.length-len);}// If the operation type was deleteelse{// Update the text valuetextbox.value += operation[1];}// Update the variable to store the current text in the text boxtext = textbox.value;}};};