Skip to content

Conversation

@JatinSharma222
Copy link

@JatinSharma222 JatinSharma222 commented Dec 27, 2025

Add Built-in Dictionary Mode to BookReader

Overview

Closes #1392

This PR implements a built-in dictionary feature that allows users to click on words in the text layer to view definitions, pronunciation, synonyms, and translations. The feature enhances the reading experience by providing instant vocabulary assistance without leaving the book.

Features Implemented

Dictionary Mode Toggle

  • Added a checkbox control in the navigation bar to enable/disable dictionary mode
  • Visual indicator when dictionary mode is active
  • Helper overlay message that auto-dismisses after 3 seconds

Word Lookup

  • Single-click on any word to see its definition
  • Double-click support for additional selection flexibility
  • Interactive text layer with hover effects on words
  • Smart word extraction from text selections

Multi-language Support

Automatic language detection for:

  • English
  • Hindi (Devanagari)
  • Bengali
  • Telugu
  • Tamil
  • Arabic
  • Chinese
  • Japanese
  • Falls back to English for unrecognized languages

Definition Popup

The popup displays:

  • Word with phonetic pronunciation (when available)
  • Part of speech (noun, verb, adjective, etc.)
  • Definition from dictionary API
  • Example usage in a sentence
  • Synonyms (up to 3)
  • Pronunciation button using Web Speech API
  • Translation button for non-English words

User Interface

  • Modern, responsive popup design with gradient header
  • Smooth animations and transitions
  • Mobile-responsive layout
  • Dark mode support
  • Smart positioning (stays within viewport bounds)
  • Click outside to close

Technical Implementation

Modified Files

src/BookReader/Navbar.js

Added dictionary mode toggle methods:

  • enableDictionaryMode() - Activates dictionary features
  • disableDictionaryMode() - Deactivates and cleans up
  • enableTextLayers() - Makes text layers interactive
  • disableTextLayers() - Restores default behavior

Implemented word lookup functionality:

  • handleWordSelection() - Extracts clicked word
  • showDefinition() - Orchestrates the lookup process
  • detectLanguage() - Auto-detects word language using Unicode ranges
  • getDefinition() - Fetches definition from Free Dictionary API

Created popup display methods:

  • displayDefinitionPopup() - Renders the definition popup
  • showLoadingPopup() - Shows loading state
  • showErrorPopup() - Handles lookup failures
  • closeDefinitionPopup() - Cleanup method

Added auxiliary features:

  • speakWord() - Text-to-speech pronunciation
  • translateWord() - Translation via MyMemory API
  • showDictionaryModeMessage() - User guidance overlay

Updated init() method to:

  • Add dictionary checkbox to navbar
  • Bind event handlers properly after DOM insertion
  • Listen to textLayerRendered events for dynamic pages

src/css/BRnav.scss

  • Added .dictionary-button styles for the toggle control
  • Created .dictionary-mode-active state styles
  • Designed .dictionary-popup component with gradient header, structured body layout, and interactive footer buttons
  • Added responsive breakpoints for mobile
  • Created .dictionary-mode-overlay for helper messages
  • Implemented smooth animations (popupFadeIn, slideDown)
  • Added hover effects for better UX
  • Included dark mode support

APIs Used

Free Dictionary API (api.dictionaryapi.dev)

  • Free, no authentication required
  • English word definitions, phonetics, examples, synonyms
  • Reliable and well-maintained

MyMemory Translation API (api.mymemory.translated.net)

  • Free tier available
  • Supports multiple language pairs
  • Used for non-English word translation

Web Speech API (Browser native)

  • Text-to-speech for pronunciation
  • No external dependencies
  • Supported in modern browsers

How It Works

  1. User checks "Dictionary Mode" checkbox in navbar
  2. Pointer events enabled on .BRtextLayer and words become clickable with hover effects
  3. Page images disabled to prevent interference
  4. User clicks or double-clicks any word
  5. Language auto-detected via Unicode character ranges
  6. API call to fetch definition with loading indicator shown during fetch
  7. Popup appears near cursor with definition
  8. User can click pronunciation button to hear the word or click translate button for non-English words
  9. Click outside or close button to dismiss popup

User Experience Improvements

  • Non-intrusive: Only activates when toggled on
  • Fast: Uses cached text layers, no re-parsing needed
  • Accessible: Keyboard-friendly, semantic HTML
  • Responsive: Works on mobile and desktop
  • Forgiving: Handles API failures gracefully
  • Visual Feedback: Loading states, hover effects, animations
  • Smart Positioning: Popup stays within viewport bounds

Browser Compatibility

  • Chrome/Edge (latest) - Full support
  • Firefox (latest) - Full support
  • Safari (latest) - Full support
  • Mobile browsers (iOS Safari, Chrome Mobile) - Full support
  • Web Speech API may not be available in all browsers (graceful fallback)

Testing Recommendations

Manual Testing Steps

  1. Load a book with text layer (OCR data)
  2. Enable "Dictionary Mode" checkbox
  3. Click on various words to verify definition popup appears
  4. Test pronunciation button
  5. Test translation for non-English words
  6. Test on different pages
  7. Test on mobile viewport
  8. Verify disable functionality works

Edge Cases to Test

  • Words with special characters
  • Multi-word selections (should show first word)
  • Very short words (1-2 letters)
  • Non-dictionary words
  • API failures (network error)
  • Rapid clicking
  • Page navigation while popup is open

Future Enhancements (Not in this PR)

  • Add support for more dictionary APIs (Wiktionary, etc.)
  • Cache definitions locally for offline use
  • Add user preference for dictionary API choice
  • Support for idioms and phrases
  • History of looked-up words
  • Flashcard creation from definitions
  • Integration with Google Translate for more languages
  • Custom styling options

Breaking Changes

None. This is a purely additive feature.

Dependencies

No new npm dependencies added. Uses:

  • Existing jQuery (already in BookReader)
  • Native Fetch API
  • Native Web Speech API (optional)

Checklist

  • Code follows existing style guidelines
  • Self-review completed
  • Comments added for complex logic
  • No console errors in browser
  • Responsive design tested
  • Works with existing BookReader features
  • No breaking changes to existing API
  • Dictionary mode is opt-in (disabled by default)

Related Issues

Closes #1392

Demo Instructions

To test this feature:

  1. Check out this branch
  2. Run npm install && npm run serve-dev
  3. Open http://localhost:8000
  4. Open "From Internet Archive"
  5. Enable "Dictionary Mode" in the navbar
  6. Click on any word in the book

Reviewer Notes

  • Main logic is in Navbar.js (dictionary methods)
  • Styling is in BRnav.scss (dictionary classes)
  • Feature is completely opt-in and doesn't affect existing functionality
  • No changes to core BookReader rendering logic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Built-in dictionary

1 participant