Frontend School

Dynamic Content Editor with Inline Variable Insertion

Machine Coding
hard

Dynamic Content Editor with Inline Variable Insertion

Hard
React
TypeScript
Rich Text Editor
UI/UX
State Management
DOM Manipulation

Overview

Salesforce applications frequently require dynamic content generation, such as personalized email templates, marketing messages, or interactive flow screens. A common feature in these scenarios is the ability for users to insert predefined variables (merge fields) directly into rich text. This problem challenges you to build a simplified rich text editor that supports basic text input alongside the seamless insertion and atomic manipulation of inline variables.

What you need to do / Task

Your task is to implement a React component that functions as a content editor with the following capabilities:

  1. Rich Text Editor Foundation: Create an editable area using contenteditable that allows users to type and delete free-form text.
  2. Variable Insertion: Provide a mechanism (e.g., buttons) to insert predefined variables into the editor at the current cursor position.
  3. Atomic Variables: Ensure that inserted variables are treated as atomic units. They should be visually distinct (e.g., styled as a 'pill' or 'badge') and cannot be partially edited. A user should only be able to select and delete an entire variable, not individual characters within it.
  4. Cursor Management: The cursor position should be correctly maintained and restored after inserting a variable, allowing for continuous typing.

UI/UX expectations

  • Editor Area: A clearly defined div with contenteditable="true" that serves as the main input area.
  • Variable Buttons: A set of buttons, each representing an available variable (e.g., {{FirstName}}, {{Company}}), positioned above or below the editor.
  • Variable Pill Style: When a variable is inserted, it should appear as a distinct, non-editable element (e.g., a rounded background, different font) that stands out from regular text.
  • Interaction: Standard text editing (typing, backspace, delete) should function intuitively around the inserted variables. Selecting a variable should highlight the entire pill, and pressing backspace/delete should remove the entire variable.
  • Accessibility: Consider basic keyboard navigation for inserting variables and ensure the editor remains usable for users who rely on keyboard input.

Acceptance criteria

  • The user can type arbitrary text into the editor.
  • Clicking a variable button inserts the corresponding variable at the current cursor position within the editor.
  • Inserted variables are visually distinct from regular text.
  • Variables cannot be partially edited; attempts to place the cursor inside a variable or delete individual characters from it should not be possible.
  • Pressing backspace or delete when a variable is selected removes the entire variable.
  • The cursor position is correctly maintained and appears logically after variable insertion, allowing the user to continue typing immediately.
  • Basic text selection and deletion work as expected for free-form text.

Constraints

Constraints & assumptions

  • Use React with TypeScript.
  • You are not allowed to use external rich text editor libraries (e.g., Slate, Quill, Draft.js). Implement the core functionality using native browser APIs (contenteditable, Selection API, Range API).
  • Focus on the core functionality described; advanced rich text formatting (bold, italic, lists, etc.) is out of scope.
  • The list of available variables is predefined: ['{{FirstName}}', '{{LastName}}', '{{Company}}', '{{Email}}'].
  • Assume a modern browser environment (Chrome, Firefox, Safari).

Out of scope

  • Saving or loading the editor's content (no persistence required).
  • Complex undo/redo stack management (browser's native undo/redo is acceptable).
  • Drag-and-drop functionality for variables.
  • Image or media embedding.

What interviewers evaluate

  • Correctness of DOM Manipulation: Effective and accurate use of contenteditable, Selection, and Range APIs to manage content and insert atomic elements.
  • Component Design & State Management: Clean, modular React component structure and thoughtful management of editor state and cursor position.
  • Robustness of Atomic Elements: The ability to reliably insert, select, and delete variables as single, immutable units without unintended side effects.
  • User Experience & Accessibility: A smooth and intuitive interaction flow for both text and variable manipulation, including proper cursor handling and basic keyboard usability.

Loading editor...