Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(web): let iOS handle Backspace inside text (#7166)

Wolf Mermelsteinf481bf44a732
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/native-ios-backspace/nativeIosBackspacePlugin.test.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/native-ios-backspace/nativeIosBackspacePlugin.test.ts
new file mode 100644
index 0000000000..13811c9f4f
--- /dev/null
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/native-ios-backspace/nativeIosBackspacePlugin.test.ts
@@ -0,0 +1,73 @@
+import { registerRichText } from '@lexical/rich-text';
+import {
+ $createParagraphNode,
+ $createTextNode,
+ $getRoot,
+ createEditor,
+ type LexicalEditor,
+ ParagraphNode,
+ TextNode,
+} from 'lexical';
+import { describe, expect, test, vi } from 'vitest';
+import { nativeIosBackspacePlugin } from './nativeIosBackspacePlugin';
+
+vi.mock('lexical', async (importOriginal) => ({
+ ...(await importOriginal<typeof import('lexical')>()),
+ IS_IOS: true,
+}));
+
+function createTestEditor(text: string, offset: number) {
+ const editor = createEditor({
+ namespace: 'native-ios-backspace-test',
+ nodes: [ParagraphNode, TextNode],
+ onError: (error) => {
+ throw error;
+ },
+ });
+ const root = document.createElement('div');
+ root.contentEditable = 'true';
+ document.body.appendChild(root);
+ editor.setRootElement(root);
+ registerRichText(editor);
+ nativeIosBackspacePlugin()(editor);
+ editor.update(
+ () => {
+ const node = $createTextNode(text);
+ $getRoot().clear().append($createParagraphNode().append(node));
+ node.select(offset, offset);
+ },
+ { discrete: true }
+ );
+ return { editor, root };
+}
+
+function pressBackspace(root: HTMLElement) {
+ const event = new KeyboardEvent('keydown', {
+ bubbles: true,
+ cancelable: true,
+ key: 'Backspace',
+ });
+ root.dispatchEvent(event);
+ return event;
+}
+
+function readText(editor: LexicalEditor) {
+ return editor.read(() => $getRoot().getTextContent());
+}
+
+describe('nativeIosBackspacePlugin', () => {
+ test('leaves a Backspace inside text to the iOS keyboard', () => {
+ const { editor, root } = createTestEditor('hello world', 11);
+
+ expect(pressBackspace(root).defaultPrevented).toBe(false);
+ expect(readText(editor)).toBe('hello world');
+ });
+
+ test('keeps Lexical handling at the start of a block', () => {
+ // jsdom has no Selection.modify, which Lexical's delete path calls.
+ Selection.prototype.modify ??= () => {};
+ const { root } = createTestEditor('hello', 0);
+
+ expect(pressBackspace(root).defaultPrevented).toBe(true);
+ });
+});
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/native-ios-backspace/nativeIosBackspacePlugin.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/native-ios-backspace/nativeIosBackspacePlugin.ts
new file mode 100644
index 0000000000..e886a10ff1
--- /dev/null
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/native-ios-backspace/nativeIosBackspacePlugin.ts
@@ -0,0 +1,35 @@
+import {
+ $getSelection,
+ $isRangeSelection,
+ $isTextNode,
+ COMMAND_PRIORITY_LOW,
+ IS_IOS,
+ KEY_BACKSPACE_COMMAND,
+ type LexicalEditor,
+} from 'lexical';
+
+function $isCaretInsideText() {
+ const selection = $getSelection();
+ if (!$isRangeSelection(selection) || !selection.isCollapsed()) return false;
+ return $isTextNode(selection.anchor.getNode()) && selection.anchor.offset > 0;
+}
+
+/**
+ * The rich-text Backspace handler cancels the keydown and deletes one
+ * character. On iOS that also cancels the keyboard's own deletion: the whole
+ * word after glide typing, and the speed-up while Backspace is held. Both
+ * arrive as `beforeinput` events (a word-sized target range, or
+ * `deleteWordBackward`) that Lexical already applies, so let the keydown
+ * through while the caret is inside text. Block boundaries keep Lexical's
+ * handling for list items, indentation, and decorators.
+ */
+export function nativeIosBackspacePlugin() {
+ return (editor: LexicalEditor) => {
+ if (!IS_IOS) return () => {};
+ return editor.registerCommand(
+ KEY_BACKSPACE_COMMAND,
+ $isCaretInsideText,
+ COMMAND_PRIORITY_LOW
+ );
+ };
+}
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/pluginManager.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/pluginManager.ts
index ecf4ac9ba8..9e0d70a525 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/pluginManager.ts
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/pluginManager.ts
@@ -20,6 +20,7 @@ import { bindStateAs } from '../utils';
import { checklistPlugin } from './checklist/';
import { customDeletePlugin } from './custom-delete';
import { markdownShortcutsPlugin } from './markdown-shortcuts';
+import { nativeIosBackspacePlugin } from './native-ios-backspace/nativeIosBackspacePlugin';
import { normalizeTripleClickPlugin } from './normalize-triple-click';
import { wordNavigationPlugin } from './word-navigation/wordNavigationPlugin';
@@ -91,6 +92,7 @@ export function createPluginManager(editor: LexicalEditor, type: EditorType) {
// register it explicitly here.
cleanupFunctions.push(normalizeTripleClickPlugin()(editor));
cleanupFunctions.push(wordNavigationPlugin()(editor));
+ cleanupFunctions.push(nativeIosBackspacePlugin()(editor));
return pluginManager;
},