@mzeahmed/rich-editor 0.1.0 → 0.1.2

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,26 @@ Before a release, the pending entries below `[Unreleased]` are moved under a hea
11
11
 
12
12
  ## [Unreleased]
13
13
 
14
+ ## [0.1.2] - 2026-11-20
15
+
16
+ ### Fixed
17
+
18
+ - The mention bubble now follows the writing area when the page (or a scrolling container such as a
19
+ modal) scrolls or the window is resized. It used to stay where it opened. It is hidden while the
20
+ writing area is out of the viewport.
21
+
22
+ ## [0.1.1] - 2026-11-20
23
+
24
+ ### Fixed
25
+
26
+ - The mention bubble now opens when the letters follow the `@` without a pause: it used to open only
27
+ if the `@` was seen alone by the debounced key handler, so typing `@le` quickly opened nothing.
28
+ - An `@` inside a word (such as `jean@…` in an e-mail address) no longer opens the bubble.
29
+ - The bubble now closes when the text after the `@` contains a space, even if no Space key release
30
+ was seen (paste, fast typing).
31
+ - A bubble dismissed with Escape, the cross or a click outside no longer reopens by itself while
32
+ the same `@term` is still being typed; typing a new `@` opens it again.
33
+
14
34
  ## [0.1.0] - 2026-11-20
15
35
 
16
36
  First public release.
package/README.md CHANGED
@@ -141,9 +141,13 @@ 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.
149
+ - The bubble stays next to the writing area while the page or a scrolling container moves, and is
150
+ hidden while the writing area is out of view.
147
151
  - The bubble closes when you choose a user, press Space, Enter or Escape, click its **×** button, or
148
152
  click anywhere outside of it.
149
153
  - Users without a usable `profileUrl` (`http(s)` or relative) are not offered. An avatar that is not
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
@@ -220,6 +225,13 @@ export default class Editor {
220
225
  */
221
226
  private static _showMentionDropdown;
222
227
  private static _positionDropdown;
228
+ /**
229
+ * Keeps the bubble next to the writing area while the page scrolls or the window is resized. The
230
+ * bubble is fixed on `document.body`, so it does not move with the page by itself. `scroll` does
231
+ * not bubble: listening in the capture phase of the window also catches the scroll of a container
232
+ * (a modal, a panel). The listeners are removed by _closeMentionDropdown().
233
+ */
234
+ private static _followWritingArea;
223
235
  /**
224
236
  * Closes the bubble as soon as the user presses (mouse or finger) anywhere but on it, including in the text
225
237
  * of the editor. The listener is added on open and removed by _closeMentionDropdown().
package/dist/Editor.js CHANGED
@@ -599,53 +599,66 @@ export default class Editor {
599
599
  activeIndex: -1,
600
600
  listId: null,
601
601
  stopOutsideClose: null,
602
+ dismissed: null,
603
+ stopFollow: null,
602
604
  };
603
605
  // In capture phase: before the editor's handler (otherwise Tab inserts spaces).
604
606
  content.addEventListener('keydown', (e) => {
605
607
  Editor._handleMentionKeydown(e, content, state, integration);
606
608
  }, true);
607
- content.addEventListener('keyup', integration.debounce((e) => {
608
- Editor._handleMentionSearch(e, content, state, integration);
609
+ // Debounced: a burst of key releases ("@le" typed quickly) reaches the handler only once, with
610
+ // the last one. It therefore looks at the text before the caret, not at the key that was pressed.
611
+ content.addEventListener('keyup', integration.debounce(() => {
612
+ Editor._handleMentionSearch(content, state, integration);
609
613
  }, 300));
610
614
  }
611
- static _handleMentionSearch(e, content, state, integration) {
615
+ /**
616
+ * The "@term" that ends at the caret: an "@" at the start of a word (not inside an e-mail
617
+ * address) followed by characters with no space. `index` is the position of the "@".
618
+ */
619
+ static _findMentionTrigger(textBeforeCursor) {
620
+ const match = /(^|\s)@([^\s@]*)$/.exec(textBeforeCursor);
621
+ return match ? { index: match.index + match[1].length, term: match[2] } : null;
622
+ }
623
+ static _handleMentionSearch(content, state, integration) {
612
624
  const sel = window.getSelection();
613
625
  if (!sel?.rangeCount)
614
626
  return;
615
627
  const range = sel.getRangeAt(0);
616
628
  const node = range.endContainer;
617
- if (node.nodeType !== Node.TEXT_NODE)
629
+ if (node.nodeType !== Node.TEXT_NODE) {
630
+ if (state.isSearch)
631
+ Editor._closeMentionDropdown(state);
618
632
  return;
633
+ }
619
634
  const textNode = node;
620
- const textBeforeCursor = (textNode.textContent ?? '').substring(0, range.endOffset);
621
- const lastChar = textBeforeCursor[textBeforeCursor.length - 1];
622
- if (lastChar === '@') {
635
+ const trigger = Editor._findMentionTrigger((textNode.textContent ?? '').substring(0, range.endOffset));
636
+ if (!trigger) {
637
+ if (state.isSearch)
638
+ Editor._closeMentionDropdown(state);
639
+ return;
640
+ }
641
+ const isCurrent = state.isSearch && state.atNode === textNode && state.atOffset === trigger.index;
642
+ if (!isCurrent) {
643
+ // A "@" the user dismissed stays dismissed while they keep typing after it; a freshly typed
644
+ // "@" (nothing after it yet) always opens, even at the same position.
645
+ const dismissed = state.dismissed;
646
+ if (trigger.term !== '' && dismissed?.node === textNode && dismissed.offset === trigger.index)
647
+ return;
623
648
  // A new "@" while a bubble is open: the old one must not stay on screen.
624
649
  Editor._closeMentionDropdown(state);
650
+ state.dismissed = null;
625
651
  state.isSearch = true;
626
652
  state.atNode = textNode;
627
- state.atOffset = range.endOffset - 1;
653
+ state.atOffset = trigger.index;
628
654
  state.previousTerm = null;
629
655
  state.dropdown = Editor._showMentionDropdown(content, state, integration);
630
656
  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;
657
+ Editor._followWritingArea(state, content);
644
658
  }
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);
659
+ if (trigger.term.length >= 2 && trigger.term !== state.previousTerm) {
660
+ state.previousTerm = trigger.term;
661
+ Editor._performMentionSearch(trigger.term, content, state, integration);
649
662
  }
650
663
  }
651
664
  /**
@@ -693,6 +706,8 @@ export default class Editor {
693
706
  const dropH = 300; // estimated max height of the dropdown
694
707
  dropdown.style.left = `${Math.round(rect.left)}px`;
695
708
  dropdown.style.width = `${width}px`;
709
+ // The bubble is fixed on the page: out of sight while the writing area is, instead of floating alone.
710
+ dropdown.style.visibility = rect.bottom > 0 && rect.top < window.innerHeight ? '' : 'hidden';
696
711
  const spaceBelow = window.innerHeight - rect.bottom;
697
712
  if (spaceBelow >= dropH || spaceBelow >= 150) {
698
713
  // Below the writing area
@@ -705,6 +720,32 @@ export default class Editor {
705
720
  dropdown.style.top = 'auto';
706
721
  }
707
722
  }
723
+ /**
724
+ * Keeps the bubble next to the writing area while the page scrolls or the window is resized. The
725
+ * bubble is fixed on `document.body`, so it does not move with the page by itself. `scroll` does
726
+ * not bubble: listening in the capture phase of the window also catches the scroll of a container
727
+ * (a modal, a panel). The listeners are removed by _closeMentionDropdown().
728
+ */
729
+ static _followWritingArea(state, content) {
730
+ let frame = 0;
731
+ // At most one repositioning per frame, however many scroll events the browser sends.
732
+ const update = () => {
733
+ if (frame)
734
+ return;
735
+ frame = requestAnimationFrame(() => {
736
+ frame = 0;
737
+ if (state.dropdown)
738
+ Editor._positionDropdown(state.dropdown, content);
739
+ });
740
+ };
741
+ window.addEventListener('scroll', update, true);
742
+ window.addEventListener('resize', update);
743
+ state.stopFollow = () => {
744
+ cancelAnimationFrame(frame);
745
+ window.removeEventListener('scroll', update, true);
746
+ window.removeEventListener('resize', update);
747
+ };
748
+ }
708
749
  /**
709
750
  * Closes the bubble as soon as the user presses (mouse or finger) anywhere but on it, including in the text
710
751
  * of the editor. The listener is added on open and removed by _closeMentionDropdown().
@@ -720,11 +761,14 @@ export default class Editor {
720
761
  state.stopOutsideClose = () => document.removeEventListener('pointerdown', onPointerDown, true);
721
762
  }
722
763
  static _closeMentionDropdown(state) {
764
+ state.dismissed = state.atNode ? { node: state.atNode, offset: state.atOffset } : null;
723
765
  state.isSearch = false;
724
766
  state.atNode = null;
725
767
  state.previousTerm = null;
726
768
  state.stopOutsideClose?.();
727
769
  state.stopOutsideClose = null;
770
+ state.stopFollow?.();
771
+ state.stopFollow = null;
728
772
  state.dropdown?.remove();
729
773
  state.dropdown = null;
730
774
  state.activeIndex = -1;
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.2",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "author": "mzeahmed",