Files
wp-agentic-writer/MENTION_AUTOCOMPLETE_FEATURE.md
2026-01-28 00:26:00 +07:00

8.0 KiB

Mention Autocomplete Feature - Implementation Complete

Summary

Added intelligent autocomplete dropdown when typing @ in the chat input. Shows all available blocks with truncated content previews so users can easily select the right block to refine.


What Was Added

1. State Management (assets/js/sidebar.js)

New State Variables:

  • showMentionAutocomplete - Controls dropdown visibility
  • mentionQuery - Current search query after @
  • mentionOptions - Filtered list of available blocks
  • mentionCursorIndex - Keyboard navigation position
  • inputRef - Reference to textarea element

2. Core Functions

getMentionOptions(query) - Builds list of available mentions:

  • Special mentions: @this, @previous, @next, @all
  • Numbered blocks: @paragraph-1, @heading-2, @list-1
  • Filters by query string
  • Shows truncated content preview (40 chars max)
  • Limits to 10 options

handleInputChange(value) - Detects when user types @:

  • Finds @ symbol using regex: /@(\w*)$/
  • Shows/hides autocomplete dropdown
  • Filters options based on what user types after @

handleKeyDown(e) - Keyboard navigation:

  • ↑/↓ arrows - Navigate options
  • Enter - Select current option
  • Escape - Close dropdown
  • Ctrl+Enter - Send message (when dropdown closed)

insertMention(option) - Inserts selected mention:

  • Replaces @query with selected option
  • Adds space after mention
  • Closes dropdown
  • Returns focus to textarea

3. UI Components

Autocomplete Dropdown:

  • Positioned above textarea (bottom: 100%)
  • Full width of input area
  • Scrollable (max-height: 200px)
  • White background with shadow

Mention Options:

  • Bold label: @paragraph-1
  • Smaller sublabel with content preview
  • Hover effect: Light blue background (#e7f3ff)
  • Selected state: Blue left border
  • Smooth transitions

4. CSS Enhancements (assets/css/sidebar.css)

Updated Styles (Lines 568-604):

  • .wpaw-mention-autocomplete - Dropdown container
  • .wpaw-mention-option - Individual option styling
  • .wpaw-mention-option:hover - Hover effect
  • .wpaw-mention-option.selected - Keyboard navigation indicator
  • Smooth 0.15s transitions

User Experience

Workflow

  1. User types @ → Dropdown appears immediately

  2. Shows all options (up to 10):

    • Special mentions first (@this, @previous, @next, @all)
    • Then all blocks (@paragraph-1, @heading-1, @list-1, etc.)
  3. User continues typing → List filters:

    • Type "p" → Shows @paragraph-1, @paragraph-2, @previous
    • Type "h" → Shows @heading-1, @heading-2
    • Type "1" → Shows @paragraph-1, @heading-1, @list-1
  4. User selects option (click or Enter):

    • Mention inserted: @paragraph-1
    • Dropdown closes
    • Cursor ready after mention
  5. User can add more mentions:

    • "Refine @paragraph-1 and @paragraph-2 to be more concise"

Visual Feedback

Dropdown Appearance:

┌─────────────────────────────────────┐
│ @this                                │
│ Currently selected block             │
├─────────────────────────────────────┤
│ @paragraph-1                         │
│ This is the text from the first...   │
├─────────────────────────────────────┤
│ @heading-1                           │
│ 📌 Introduction to the topic         │
├─────────────────────────────────────┤
│ @list-1                              │
│ 📝 3 items                           │
└─────────────────────────────────────┘

Keyboard Navigation:

  • ↑/↓ arrows move selection
  • Selected option highlighted with blue left border
  • Enter to select
  • Escape to close

Examples

Example 1: Single Block Refinement

User types: "Refine @"
Dropdown shows: @this, @previous, @next, @all, @paragraph-1, @heading-1, ...
User selects: @paragraph-1
Result: "Refine @paragraph-1 "

Example 2: Filtering

User types: "Refine @pa"
Dropdown shows: @paragraph-1, @paragraph-2, @paragraph-3, @paragraph-4
User selects: @paragraph-2
Result: "Refine @paragraph-2 "

Example 3: Multi-Block

User types: "Refine @paragraph-1 and @"
Dropdown shows all options again
User selects: @paragraph-2
Result: "Refine @paragraph-1 and @paragraph-2 "

Example 4: Special Mention

User types: "Make @"
User types: "Make @prev"
Dropdown shows: @previous
Result: "Make @previous "

Technical Details

Block Detection

Paragraphs:

  • Label: @paragraph-N
  • Preview: First 40 chars of content
  • Example: "This is the text from the first paragraph..."

Headings:

  • Label: @heading-N
  • Preview: 📌 emoji + heading text (40 chars)
  • Example: "📌 Introduction to the topic"

Lists:

  • Label: @list-N
  • Preview: 📝 emoji + item count
  • Example: "📝 3 items"

Filtering Logic

  • Case-insensitive matching
  • Searches both label type AND number
  • @p matches all paragraphs
  • @h1 matches @heading-1
  • @2 matches @paragraph-2, @heading-2, @list-2

Keyboard Shortcuts

Key Action
@ Open autocomplete
↑/↓ Navigate options
Enter Select option (or send if dropdown closed)
Escape Close dropdown
Ctrl+Enter Send message

Benefits

Discoverability - Users see all available blocks immediately No memorization - Don't need to remember block numbers Content preview - See truncated content to identify blocks Fast - Keyboard navigation (↑/↓/Enter) Filtering - Type to narrow down options Visual - Clear selection indication Intuitive - Works like GitHub/Slack @mentions


Edge Cases Handled

Empty query (@) → Shows all options No matches → Dropdown closes Partial matches → Shows filtered results Multiple @ symbols → Only activates on last @ Cursor in middle of text → Detects @ before cursor position Click outside → Dropdown closes (on blur) Block with no content → Shows "..." as preview


Future Enhancements

Possible Improvements:

  • Fuzzy matching - "intro" could match @heading-1 about "Introduction"
  • Block icons - Show block type icons in dropdown
  • Group by type - Section headers for "Special", "Paragraphs", "Headings", "Lists"
  • Recent mentions - Show recently used blocks first
  • Search content - Search within block content, not just labels
  • Multi-select - Select multiple blocks with Shift+click

Files Modified

  1. assets/js/sidebar.js

    • Added 5 state variables
    • Added 4 new functions (~150 lines)
    • Updated TextareaControl with ref and handlers
    • Added autocomplete dropdown UI
  2. assets/css/sidebar.css

    • Updated mention autocomplete styles
    • Added hover and selected states
    • Added smooth transitions

Testing Checklist

Basic Functionality:

  • Typing @ shows dropdown
  • All special mentions appear (@this, @previous, @next, @all)
  • All numbered blocks appear (@paragraph-N, @heading-N, @list-N)
  • Content truncation works (40 chars max)
  • Maximum 10 options shown

Filtering:

  • Typing after @ filters options
  • Case-insensitive filtering works
  • No matches closes dropdown

Navigation:

  • ↑/↓ arrows navigate options
  • Enter selects option
  • Escape closes dropdown
  • Clicking selects option

Insertion:

  • Selected mention inserts correctly
  • Space added after mention
  • Focus returns to textarea
  • Can type another @ for multi-block

UI:

  • Dropdown positioned above input
  • Proper z-index (above other elements)
  • Scrollbar when needed
  • Selected state has blue border
  • Hover effect works

Implementation Date: 2026-01-18 Status: Complete and ready for testing