Hone logo
Hone
Problems

Implementing Undo/Redo Functionality in an Angular Application

This challenge focuses on building a robust undo/redo system within an Angular application. Implementing undo/redo functionality is crucial for providing a user-friendly experience in applications where users might make mistakes or want to experiment with different states. This challenge will test your understanding of Angular's reactivity, state management, and event handling.

Problem Description

You are tasked with creating an Angular component that allows a user to input text into a text area and provides undo and redo functionality. The component should maintain a history of the text area's content, allowing the user to revert to previous states (undo) and reapply actions they've undone (redo).

What needs to be achieved:

  • A text area where the user can input text.
  • "Undo" and "Redo" buttons.
  • Clicking "Undo" should revert the text area to the previous state in the history.
  • Clicking "Redo" should reapply the last undone action.
  • The "Undo" button should be disabled when there are no previous states to undo.
  • The "Redo" button should be disabled when there are no future states to redo.
  • The history should be updated whenever the user modifies the text in the text area.

Key Requirements:

  • Use Angular's reactive forms or a similar approach to manage the text area's value.
  • Implement a history array to store the states of the text area.
  • Implement undo and redo methods that manipulate the history array and update the text area's value accordingly.
  • Ensure the UI reflects the current state of the history (e.g., disabling buttons when appropriate).

Expected Behavior:

  1. Initially, the text area is empty, and both "Undo" and "Redo" buttons are disabled.
  2. When the user types text into the text area, the current text is added to the history.
  3. Clicking "Undo" reverts the text area to the previous state in the history and disables the "Redo" button.
  4. Clicking "Redo" reapplies the last undone action and disables the "Undo" button.
  5. If the user types more text after undoing, the history is updated, and both "Undo" and "Redo" buttons become available again.
  6. If the user attempts to undo when the history is empty, nothing should happen, and the "Undo" button remains disabled.
  7. If the user attempts to redo when there are no future states, nothing should happen, and the "Redo" button remains disabled.

Edge Cases to Consider:

  • Empty input: What happens when the user deletes all text?
  • Rapid undo/redo: How does the system handle multiple rapid undo/redo actions?
  • Large history: While not a primary concern for this challenge, consider how the history size might impact performance in a real-world application. (No need to implement history size limiting for this challenge).

Examples

Example 1:

Input: User types "Hello" into the text area.
Output: Text area displays "Hello". History: ["", "Hello"]
Explanation: The initial empty state is added to the history, followed by the new text.

Example 2:

Input: User types " World" after "Hello".
Output: Text area displays "Hello World". History: ["", "Hello", "Hello World"]
Explanation: The previous state "Hello" is preserved, and the new state "Hello World" is added.

Example 3:

Input: User clicks "Undo" after typing "Hello World".
Output: Text area displays "Hello". History: ["", "Hello", "Hello World"]
Explanation: The text area reverts to the previous state "Hello". The "Redo" button becomes enabled.

Example 4:

Input: User clicks "Redo" after clicking "Undo".
Output: Text area displays "Hello World". History: ["", "Hello", "Hello World"]
Explanation: The text area reapplies the last undone action, returning to "Hello World". The "Undo" button becomes enabled.

Constraints

  • The solution must be a functional Angular component.
  • The component should be self-contained and not rely on external services (unless absolutely necessary for demonstration purposes - e.g., a simple logging service).
  • The history array should not exceed a reasonable size (e.g., 20 states) for demonstration purposes. No need to implement a hard limit, but avoid unbounded growth.
  • The solution should be well-structured and easy to understand.
  • The code should be written in TypeScript.

Notes

  • Consider using Angular's FormControl to manage the text area's value.
  • Think about how to efficiently update the UI when the history changes.
  • Focus on the core undo/redo logic rather than complex styling or UI enhancements.
  • You can use a simple array to store the history.
  • The challenge is about demonstrating the undo/redo mechanism, not about creating a fully production-ready component.
Loading editor...
typescript