Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(editor): keep Ctrl+Left on the current word after a leading mention (#7049)
Wolf Mermelstein83ccab38e393
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/pluginManager.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/pluginManager.tsindex ca92468d82..ecf4ac9ba8 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/pluginManager.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/pluginManager.ts@@ -21,6 +21,7 @@ import { checklistPlugin } from './checklist/';import { customDeletePlugin } from './custom-delete';import { markdownShortcutsPlugin } from './markdown-shortcuts';import { normalizeTripleClickPlugin } from './normalize-triple-click';+import { wordNavigationPlugin } from './word-navigation/wordNavigationPlugin';export type PluginFunction = (editor: LexicalEditor) => () => void;@@ -89,6 +90,7 @@ export function createPluginManager(editor: LexicalEditor, type: EditorType) {// selection normalization the way the newer RichTextExtension does, so// register it explicitly here.cleanupFunctions.push(normalizeTripleClickPlugin()(editor));+ cleanupFunctions.push(wordNavigationPlugin()(editor));return pluginManager;},diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/word-navigation/wordNavigationPlugin.test.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/word-navigation/wordNavigationPlugin.test.tsnew file mode 100644index 0000000000..cbbbefbefe--- /dev/null+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/word-navigation/wordNavigationPlugin.test.ts@@ -0,0 +1,222 @@+import { HeadingNode, QuoteNode, registerRichText } from '@lexical/rich-text';+import {+ $createParagraphNode,+ $createTextNode,+ $getRoot,+ $getSelection,+ $isRangeSelection,+ createEditor,+ DecoratorNode,+ IS_APPLE,+ type LexicalEditor,+ MOVE_TO_END,+ MOVE_TO_START,+ ParagraphNode,+ TextNode,+} from 'lexical';+import { describe, expect, test } from 'vitest';+import { wordNavigationPlugin } from './wordNavigationPlugin';++class MentionNode extends DecoratorNode<null> {+ static getType() {+ return 'test-mention';+ }++ static clone(node: MentionNode) {+ return new MentionNode(node.__key);+ }++ createDOM() {+ const span = document.createElement('span');+ span.textContent = '@Cursor';+ return span;+ }++ updateDOM() {+ return false;+ }++ decorate() {+ return null;+ }++ isInline() {+ return true;+ }+}++function createTestEditor(): LexicalEditor {+ const editor = createEditor({+ namespace: 'word-navigation-plugin-test',+ nodes: [HeadingNode, ParagraphNode, QuoteNode, TextNode, MentionNode],+ onError: (error) => {+ throw error;+ },+ });+ const root = document.createElement('div');+ root.contentEditable = 'true';+ document.body.appendChild(root);+ editor.setRootElement(root);+ registerRichText(editor);+ wordNavigationPlugin()(editor);+ return editor;+}++function selectionPoint() {+ const selection = $getSelection();+ if (!$isRangeSelection(selection)) return null;+ return {+ type: selection.anchor.type,+ offset: selection.anchor.offset,+ key: selection.anchor.key,+ nodeType: selection.anchor.getNode().getType(),+ };+}++describe.skipIf(IS_APPLE)('word navigation with a leading mention', () => {+ test('ctrl+left stays on the current word instead of jumping to the block start', () => {+ const editor = createTestEditor();+ editor.update(+ () => {+ const paragraph = $createParagraphNode();+ const text = $createTextNode(+ ' on firefox you will need to download this'+ );+ paragraph.append(new MentionNode(), text);+ $getRoot().clear().append(paragraph);+ text.select(37, 37);+ },+ { discrete: true }+ );++ const event = new KeyboardEvent('keydown', {+ key: 'ArrowLeft',+ ctrlKey: true,+ });+ editor.dispatchCommand(MOVE_TO_START, event);++ expect(event.defaultPrevented).toBe(false);+ editor.read(() => {+ expect(selectionPoint()).toEqual({+ type: 'text',+ offset: 37,+ key: expect.any(String),+ nodeType: 'text',+ });+ });+ });++ test('ctrl+left in ordinary text is left to the browser', () => {+ const editor = createTestEditor();+ editor.update(+ () => {+ const paragraph = $createParagraphNode();+ const text = $createTextNode('hello on firefox');+ paragraph.append(text);+ $getRoot().clear().append(paragraph);+ text.select(8, 8);+ },+ { discrete: true }+ );++ const event = new KeyboardEvent('keydown', {+ key: 'ArrowLeft',+ ctrlKey: true,+ });+ editor.dispatchCommand(MOVE_TO_START, event);++ expect(event.defaultPrevented).toBe(false);+ editor.read(() => {+ expect(selectionPoint()?.offset).toBe(8);+ expect(selectionPoint()?.type).toBe('text');+ });+ });++ test('ctrl+left from just after the mention steps in front of it', () => {+ const editor = createTestEditor();+ editor.update(+ () => {+ const paragraph = $createParagraphNode();+ const text = $createTextNode(' on firefox');+ paragraph.append(new MentionNode(), text);+ $getRoot().clear().append(paragraph);+ text.select(0, 0);+ },+ { discrete: true }+ );++ const event = new KeyboardEvent('keydown', {+ key: 'ArrowLeft',+ ctrlKey: true,+ });+ editor.dispatchCommand(MOVE_TO_START, event);++ editor.read(() => {+ expect(selectionPoint()).toMatchObject({+ type: 'element',+ offset: 0,+ nodeType: 'paragraph',+ });+ });+ });++ test('shift+ctrl+left from just after the mention extends back over it', () => {+ const editor = createTestEditor();+ editor.update(+ () => {+ const paragraph = $createParagraphNode();+ const text = $createTextNode(' on firefox');+ paragraph.append(new MentionNode(), text);+ $getRoot().clear().append(paragraph);+ text.select(0, 0);+ },+ { discrete: true }+ );++ const event = new KeyboardEvent('keydown', {+ key: 'ArrowLeft',+ ctrlKey: true,+ shiftKey: true,+ });+ editor.dispatchCommand(MOVE_TO_START, event);++ editor.read(() => {+ const selection = $getSelection();+ if (!$isRangeSelection(selection)) throw new Error('expected a range');+ expect(selection.anchor).toMatchObject({ type: 'text', offset: 0 });+ expect(selection.focus).toMatchObject({ type: 'element', offset: 0 });+ expect(selection.isCollapsed()).toBe(false);+ });+ });++ test('ctrl+right from before the mention does not jump to the end of the block', () => {+ const editor = createTestEditor();+ editor.update(+ () => {+ const paragraph = $createParagraphNode();+ paragraph.append(+ new MentionNode(),+ $createTextNode(' on firefox you will need to download this')+ );+ $getRoot().clear().append(paragraph);+ paragraph.select(0, 0);+ },+ { discrete: true }+ );++ const event = new KeyboardEvent('keydown', {+ key: 'ArrowRight',+ ctrlKey: true,+ });+ editor.dispatchCommand(MOVE_TO_END, event);++ expect(event.defaultPrevented).toBe(false);+ editor.read(() => {+ expect(selectionPoint()).toMatchObject({+ type: 'element',+ offset: 0,+ nodeType: 'paragraph',+ });+ });+ });+});diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/word-navigation/wordNavigationPlugin.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/word-navigation/wordNavigationPlugin.tsnew file mode 100644index 0000000000..3adaa17f0f--- /dev/null+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/word-navigation/wordNavigationPlugin.ts@@ -0,0 +1,98 @@+import { $findMatchingParent, mergeRegister } from '@lexical/utils';+import { $isSelectionInsideCode } from '@macro-inc/lexical-core';+import {+ $getSelection,+ $isDecoratorNode,+ $isElementNode,+ $isRangeSelection,+ COMMAND_PRIORITY_HIGH,+ type ElementNode,+ IS_APPLE,+ type LexicalEditor,+ MOVE_TO_END,+ MOVE_TO_START,+} from 'lexical';++function $leadingInlineDecorator(block: ElementNode | null) {+ const first = block?.getFirstChild() ?? null;+ if (!$isDecoratorNode(first) || !first.isInline()) return null;+ return first;+}++function $focusBlock() {+ const selection = $getSelection();+ if (!$isRangeSelection(selection) || $isSelectionInsideCode(selection)) {+ return null;+ }+ return $findMatchingParent(+ selection.focus.getNode(),+ (node): node is ElementNode => $isElementNode(node) && !node.isInline()+ );+}++/**+ * Ctrl+Left and Ctrl+Right move by word. Lexical reports those keys as+ * MOVE_TO_START and MOVE_TO_END, and the rich-text handlers then pin the+ * caret to the block edge when the block opens with an inline decorator.+ * Swallow those commands so the browser keeps moving by word.+ *+ * Firefox will not step backward out of the text node that follows a+ * contenteditable=false chip, so from that offset move the caret to before+ * the chip.+ *+ * On Apple, Cmd+Left and Cmd+Right move to the line edge. Those stay with+ * the rich-text handlers, which can place the caret before a+ * contenteditable=false chip when the browser will not.+ */+export function wordNavigationPlugin() {+ return (editor: LexicalEditor) => {+ return mergeRegister(+ editor.registerCommand(+ MOVE_TO_START,+ (event) => {+ if (IS_APPLE) return false;+ const block = $focusBlock();+ const decorator = $leadingInlineDecorator(block);+ if (!decorator || !block) return false;+ const selection = $getSelection();+ const next = decorator.getNextSibling();+ if (+ $isRangeSelection(selection) &&+ selection.isCollapsed() &&+ next !== null &&+ selection.anchor.getNode().is(next) &&+ selection.anchor.offset === 0+ ) {+ event.preventDefault();+ selection.focus.set(block.getKey(), 0, 'element');+ if (!event.shiftKey) {+ selection.anchor.set(block.getKey(), 0, 'element');+ }+ return true;+ }+ return true;+ },+ COMMAND_PRIORITY_HIGH+ ),+ editor.registerCommand(+ MOVE_TO_END,+ () => {+ if (IS_APPLE) return false;+ const selection = $getSelection();+ if (+ !$isRangeSelection(selection) ||+ $isSelectionInsideCode(selection)+ ) {+ return false;+ }+ const { anchor } = selection;+ if (anchor.type !== 'element' || anchor.offset !== 0) return false;+ const element = anchor.getNode();+ if (!$isElementNode(element) || element.isInline()) return false;+ return $leadingInlineDecorator(element) !== null;+ },+ COMMAND_PRIORITY_HIGH+ )+ );+ };+}