Skip to content

Added selection-aware word count plugin for Koenig#1976

Open
kevinfengcs88 wants to merge 2 commits into
TryGhost:mainfrom
kevinfengcs88:selection-word-count
Open

Added selection-aware word count plugin for Koenig#1976
kevinfengcs88 wants to merge 2 commits into
TryGhost:mainfrom
kevinfengcs88:selection-word-count

Conversation

@kevinfengcs88

Copy link
Copy Markdown

Discussed with Steve (@9larsons) over Ghost support email, who suggested opening this in the Koenig repo.

Adds a headless SelectionWordCountPlugin that reports the word count of the current selection. onChange(count) fires with an integer (so a word count of 0 is allowed) while a non-collapsed range selection exists, and with null once the selection collapses.

  • Counting reuses the same countWords util as WordCountPlugin, so selection counts should stay consistent with the document total
  • Selections inside nested editors (image captions, callouts) work through the same context-ref pattern WordCountPlugin uses in KoenigNestedComposer
  • This is purely additive. WordCountPlugin and its API are not touched
  • The demo app renders "X of Y words" while text is selected (e.g. "2 of 9 words", and "0 of 9 words" for a whitespace-only selection). e2e coverage spans selection, whitespace, partial-word fragments, nested editors, select-all parity, and collapse.

Video demo:

ghost_koenig_word_counter.mp4

@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 29916f7e-c645-4193-981c-a17789e1da69

📥 Commits

Reviewing files that changed from the base of the PR and between 1b37e67 and 017f74f.

📒 Files selected for processing (7)
  • packages/koenig-lexical/demo/DemoApp.jsx
  • packages/koenig-lexical/demo/components/WordCount.jsx
  • packages/koenig-lexical/src/components/KoenigComposer.jsx
  • packages/koenig-lexical/src/components/KoenigNestedComposer.jsx
  • packages/koenig-lexical/src/index.js
  • packages/koenig-lexical/src/plugins/SelectionWordCountPlugin.jsx
  • packages/koenig-lexical/test/e2e/plugins/SelectionWordCountPlugin.test.js

Walkthrough

This PR adds a new SelectionWordCountPlugin to track word counts of active text selections in Lexical editors. The plugin registers update listeners to monitor selection state, computes word counts for non-collapsed selections, and emits deduplicated combined counts across editor instances using shared composer refs with 200ms throttling. The feature is wired into the Koenig composer infrastructure through context-exposed refs, mounted in demo editor flows, integrated into the WordCount UI component, and validated with comprehensive e2e tests covering single and nested editor selection scenarios.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding a selection-aware word count plugin for Koenig, which is the primary purpose of this changeset.
Description check ✅ Passed The description is well-related to the changeset, explaining the plugin's purpose, behavior, implementation details, and test coverage with concrete examples.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@kevinfengcs88

kevinfengcs88 commented Jun 11, 2026

Copy link
Copy Markdown
Author

@9larsons

tagging you because I don't think I have the power to add you as a reviewer as an external contributor

Thanks!

@9larsons 9larsons self-requested a review June 11, 2026 22:12
@9larsons

Copy link
Copy Markdown
Collaborator

Thanks! I'll take a look when I'm able.

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.

2 participants