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:
- Rich Text Editor Foundation: Create an editable area using
contenteditable that allows users to type and delete free-form text.
- Variable Insertion: Provide a mechanism (e.g., buttons) to insert predefined variables into the editor at the current cursor position.
- 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.
- 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.