@mzeahmed/rich-editor 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -11,6 +11,18 @@ Before a release, the pending entries below `[Unreleased]` are moved under a hea
11
11
 
12
12
  ## [Unreleased]
13
13
 
14
+ ## [0.1.1] - 2026-11-20
15
+
16
+ ### Fixed
17
+
18
+ - The mention bubble now opens when the letters follow the `@` without a pause: it used to open only
19
+ if the `@` was seen alone by the debounced key handler, so typing `@le` quickly opened nothing.
20
+ - An `@` inside a word (such as `jean@…` in an e-mail address) no longer opens the bubble.
21
+ - The bubble now closes when the text after the `@` contains a space, even if no Space key release
22
+ was seen (paste, fast typing).
23
+ - A bubble dismissed with Escape, the cross or a click outside no longer reopens by itself while
24
+ the same `@term` is still being typed; typing a new `@` opens it again.
25
+
14
26
  ## [0.1.0] - 2026-11-20
15
27
 
16
28
  First public release.
package/README.md CHANGED
@@ -141,7 +141,9 @@ profileUrl: string}`.
141
141
 
142
142
  **How it behaves**
143
143
 
144
- - Typing `@`, then at least two letters opens a bubble with the matching users.
144
+ - Typing `@` at the start of a word, then at least two letters, opens a bubble with the matching users.
145
+ Typing fast works (`@le` typed at once opens it); an `@` inside a word, such as in an e-mail
146
+ address, does not.
145
147
  - Choosing a user inserts `<a class="mention" href="/path/of/profile" data-user-id="…"
146
148
  target="_blank">First Last</a>`. Only the **path** of `profileUrl` is kept in the link.
147
149
  - The bubble closes when you choose a user, press Space, Enter or Escape, click its **×** button, or
package/dist/Editor.d.ts CHANGED
@@ -208,6 +208,11 @@ export default class Editor {
208
208
  private static _saveSelection;
209
209
  private static _showEmojiPicker;
210
210
  private static _setupMentions;
211
+ /**
212
+ * The "@term" that ends at the caret: an "@" at the start of a word (not inside an e-mail
213
+ * address) followed by characters with no space. `index` is the position of the "@".
214
+ */
215
+ private static _findMentionTrigger;
211
216
  private static _handleMentionSearch;
212
217
  /**
213
218
  * Creates the bubble on document.body with fixed positioning — works around the overflow:hidden of
package/dist/Editor.js CHANGED
@@ -599,53 +599,64 @@ export default class Editor {
599
599
  activeIndex: -1,
600
600
  listId: null,
601
601
  stopOutsideClose: null,
602
+ dismissed: null,
602
603
  };
603
604
  // In capture phase: before the editor's handler (otherwise Tab inserts spaces).
604
605
  content.addEventListener('keydown', (e) => {
605
606
  Editor._handleMentionKeydown(e, content, state, integration);
606
607
  }, true);
607
- content.addEventListener('keyup', integration.debounce((e) => {
608
- Editor._handleMentionSearch(e, content, state, integration);
608
+ // Debounced: a burst of key releases ("@le" typed quickly) reaches the handler only once, with
609
+ // the last one. It therefore looks at the text before the caret, not at the key that was pressed.
610
+ content.addEventListener('keyup', integration.debounce(() => {
611
+ Editor._handleMentionSearch(content, state, integration);
609
612
  }, 300));
610
613
  }
611
- static _handleMentionSearch(e, content, state, integration) {
614
+ /**
615
+ * The "@term" that ends at the caret: an "@" at the start of a word (not inside an e-mail
616
+ * address) followed by characters with no space. `index` is the position of the "@".
617
+ */
618
+ static _findMentionTrigger(textBeforeCursor) {
619
+ const match = /(^|\s)@([^\s@]*)$/.exec(textBeforeCursor);
620
+ return match ? { index: match.index + match[1].length, term: match[2] } : null;
621
+ }
622
+ static _handleMentionSearch(content, state, integration) {
612
623
  const sel = window.getSelection();
613
624
  if (!sel?.rangeCount)
614
625
  return;
615
626
  const range = sel.getRangeAt(0);
616
627
  const node = range.endContainer;
617
- if (node.nodeType !== Node.TEXT_NODE)
628
+ if (node.nodeType !== Node.TEXT_NODE) {
629
+ if (state.isSearch)
630
+ Editor._closeMentionDropdown(state);
618
631
  return;
632
+ }
619
633
  const textNode = node;
620
- const textBeforeCursor = (textNode.textContent ?? '').substring(0, range.endOffset);
621
- const lastChar = textBeforeCursor[textBeforeCursor.length - 1];
622
- if (lastChar === '@') {
634
+ const trigger = Editor._findMentionTrigger((textNode.textContent ?? '').substring(0, range.endOffset));
635
+ if (!trigger) {
636
+ if (state.isSearch)
637
+ Editor._closeMentionDropdown(state);
638
+ return;
639
+ }
640
+ const isCurrent = state.isSearch && state.atNode === textNode && state.atOffset === trigger.index;
641
+ if (!isCurrent) {
642
+ // A "@" the user dismissed stays dismissed while they keep typing after it; a freshly typed
643
+ // "@" (nothing after it yet) always opens, even at the same position.
644
+ const dismissed = state.dismissed;
645
+ if (trigger.term !== '' && dismissed?.node === textNode && dismissed.offset === trigger.index)
646
+ return;
623
647
  // A new "@" while a bubble is open: the old one must not stay on screen.
624
648
  Editor._closeMentionDropdown(state);
649
+ state.dismissed = null;
625
650
  state.isSearch = true;
626
651
  state.atNode = textNode;
627
- state.atOffset = range.endOffset - 1;
652
+ state.atOffset = trigger.index;
628
653
  state.previousTerm = null;
629
654
  state.dropdown = Editor._showMentionDropdown(content, state, integration);
630
655
  Editor._closeOnOutsidePointer(state);
631
- return;
632
- }
633
- if (!state.isSearch)
634
- return;
635
- if (e.code === 'Space' || e.code === 'Escape' || e.code === 'Enter' ||
636
- content.innerText.trim() === '' || state.atNode !== textNode) {
637
- Editor._closeMentionDropdown(state);
638
- return;
639
- }
640
- const atIndex = textBeforeCursor.lastIndexOf('@');
641
- if (atIndex === -1) {
642
- Editor._closeMentionDropdown(state);
643
- return;
644
656
  }
645
- const term = textBeforeCursor.substring(atIndex + 1).trim();
646
- if (term.length >= 2 && term !== state.previousTerm) {
647
- state.previousTerm = term;
648
- Editor._performMentionSearch(term, content, state, integration);
657
+ if (trigger.term.length >= 2 && trigger.term !== state.previousTerm) {
658
+ state.previousTerm = trigger.term;
659
+ Editor._performMentionSearch(trigger.term, content, state, integration);
649
660
  }
650
661
  }
651
662
  /**
@@ -720,6 +731,7 @@ export default class Editor {
720
731
  state.stopOutsideClose = () => document.removeEventListener('pointerdown', onPointerDown, true);
721
732
  }
722
733
  static _closeMentionDropdown(state) {
734
+ state.dismissed = state.atNode ? { node: state.atNode, offset: state.atOffset } : null;
723
735
  state.isSearch = false;
724
736
  state.atNode = null;
725
737
  state.previousTerm = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mzeahmed/rich-editor",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "author": "mzeahmed",