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(md): fix programmatic restore focus with checkbox bug (#570)
Seamus6462fde69ae5
diff --git a/js/app/packages/block-channel/component/MarkdownArea.tsx b/js/app/packages/block-channel/component/MarkdownArea.tsxindex 4082d6aec1..b71bc26577 100644--- a/js/app/packages/block-channel/component/MarkdownArea.tsx+++ b/js/app/packages/block-channel/component/MarkdownArea.tsx@@ -28,6 +28,7 @@ import {} from '@core/component/LexicalMarkdown/plugins';import { codePlugin } from '@core/component/LexicalMarkdown/plugins/code/codePlugin';import { emojisPlugin } from '@core/component/LexicalMarkdown/plugins/emojis/emojisPlugin';+import { restoreFocusPlugin } from '@core/component/LexicalMarkdown/plugins/restore-focus';import { createMenuOperations } from '@core/component/LexicalMarkdown/shared/inlineMenu';import {editorIsEmpty,@@ -239,6 +240,7 @@ function MarkdownArea(props: MarkdownAreaProps & ConsumableMarkdownAreaProps) {.use(textPastePlugin()).use(markdownPastePlugin()).use(normalizeEnterPlugin())+ .use(restoreFocusPlugin()).use(tablePlugin({hasCellMerge: true,@@ -332,14 +334,6 @@ function MarkdownArea(props: MarkdownAreaProps & ConsumableMarkdownAreaProps) {));- // better focus in handling. preserves selection on regain focus!- autoRegister(- registerRootEventListener(editor, 'focusin', (e) => {- e.preventDefault();- editor.focus();- })- );-let disposeBlurFn: () => void = () => {};createEffect(() => {disposeBlurFn();diff --git a/js/app/packages/block-md/component/MarkdownEditor.tsx b/js/app/packages/block-md/component/MarkdownEditor.tsxindex db4ed2a56f..b81e6e87c4 100644--- a/js/app/packages/block-md/component/MarkdownEditor.tsx+++ b/js/app/packages/block-md/component/MarkdownEditor.tsx@@ -84,6 +84,7 @@ import {mediaPlugin,} from '@core/component/LexicalMarkdown/plugins/media';import { createAccessoryStore } from '@core/component/LexicalMarkdown/plugins/node-accessory';+import { restoreFocusPlugin } from '@core/component/LexicalMarkdown/plugins/restore-focus';import { createMenuOperations } from '@core/component/LexicalMarkdown/shared/inlineMenu';import {$insertWrappedAfter,@@ -150,7 +151,6 @@ import {autoRegister,lazyRegister,registerInternalLayoutShiftListener,- registerRootEventListener,} from 'core/component/LexicalMarkdown/plugins/shared/utils';import { createMethodRegistration } from 'core/orchestrator';import {@@ -159,7 +159,6 @@ import {$isElementNode,type EditorState,} from 'lexical';-import {type Accessor,createEffect,@@ -704,6 +703,7 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {})).use(textPastePlugin())+ .use(restoreFocusPlugin()).use(markdownPastePlugin()).use(normalizeEnterPlugin()).use(@@ -766,6 +766,10 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {setClickTargetHeight(Math.max(targetHeight, EDITOR_PADDING_BOTTOM));};+ autoRegister(+ registerInternalLayoutShiftListener(editor, observeClickTargetHeight)+ );+onMount(() => {setMdStore('selection', lexicalWrapper.selection);editor.setRootElement(mountRef);@@ -786,27 +790,6 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {});});- // HACK: We need to distinguish click-based focus events from programmatic- // ones (el.focus()). We want to maintain the previous selection (editor.focus)- // only if we are regaining focus programmatically. If click, let browser handle- // focus and let lexical catch up.- let clickFocusFlag = false;- autoRegister(- registerRootEventListener(editor, 'pointerdown', () => {- clickFocusFlag = true;- setTimeout(() => {- clickFocusFlag = false; // negate the flag after tasks- });- }),- registerRootEventListener(editor, 'focusin', (e) => {- if (clickFocusFlag) return;- e.preventDefault();- editor.focus(undefined, { defaultSelection: 'rootStart' });- }),- // adjust click target height on layout shift- registerInternalLayoutShiftListener(editor, observeClickTargetHeight)- );-const additionalCleanups: Array<() => void> = [];onCleanup(() => {diff --git a/js/app/packages/core/component/AI/component/input/useChatMarkdownArea.tsx b/js/app/packages/core/component/AI/component/input/useChatMarkdownArea.tsxindex 2bbf58db26..3a59414dfa 100644--- a/js/app/packages/core/component/AI/component/input/useChatMarkdownArea.tsx+++ b/js/app/packages/core/component/AI/component/input/useChatMarkdownArea.tsx@@ -24,7 +24,6 @@ import {mentionsPlugin,NODE_TRANSFORM,type NodeTransformType,- registerRootEventListener,type SelectionData,tabIndentationPlugin,tableCellResizerPlugin,@@ -33,6 +32,7 @@ import {import { codePlugin } from '@core/component/LexicalMarkdown/plugins/code/codePlugin';import { emojisPlugin } from '@core/component/LexicalMarkdown/plugins/emojis/emojisPlugin';import { normalizeEnterPlugin } from '@core/component/LexicalMarkdown/plugins/normalize-enter/';+import { restoreFocusPlugin } from '@core/component/LexicalMarkdown/plugins/restore-focus';import { createMenuOperations } from '@core/component/LexicalMarkdown/shared/inlineMenu';import {$traverseNodes,@@ -340,6 +340,7 @@ function MarkdownArea().use(textPastePlugin()).use(markdownPastePlugin())+ .use(restoreFocusPlugin()).use(normalizeEnterPlugin()).use(tablePlugin({@@ -401,14 +402,6 @@ function MarkdownArea();}- // better focus in handling. preserves selection on regain focus!- autoRegister(- registerRootEventListener(editor, 'focusin', (e) => {- e.preventDefault();- editor.focus();- })- );-onCleanup(() => {cleanup();});diff --git a/js/app/packages/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsx b/js/app/packages/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsxindex bbe400d48e..f88050e013 100644--- a/js/app/packages/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsx+++ b/js/app/packages/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsx@@ -32,11 +32,11 @@ import {type ItemMention,keyboardFocusPlugin,mentionsPlugin,- registerRootEventListener,type SelectionData,selectionDataPlugin,tabIndentationPlugin,} from '../../plugins';+import { restoreFocusPlugin } from '../../plugins/restore-focus';import { createMenuOperations } from '../../shared/inlineMenu';import {editorIsEmpty,@@ -124,14 +124,6 @@ export function MarkdownTextarea(props: MarkdownTextareaProps) {}});- // better focus in handling. preserves selection on regain focus!- autoRegister(- registerRootEventListener(editor, 'focusin', (e) => {- e.preventDefault();- editor.focus();- })- );-createEffect(() => {editor.setEditable(props.editable());});@@ -160,6 +152,7 @@ export function MarkdownTextarea(props: MarkdownTextareaProps) {.delete().state<string>(setMarkdownState, 'markdown').history(400)+ .use(restoreFocusPlugin()).use(props.formatState && props.setFormatState? customSelectionDataPlugin(diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/restore-focus/index.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/restore-focus/index.tsnew file mode 100644index 0000000000..347dbc30fe--- /dev/null+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/restore-focus/index.ts@@ -0,0 +1 @@+export { restoreFocusPlugin } from './restoreFocusPlugin';diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/restore-focus/restoreFocusPlugin.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/restore-focus/restoreFocusPlugin.tsnew file mode 100644index 0000000000..6af49ba086--- /dev/null+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/restore-focus/restoreFocusPlugin.ts@@ -0,0 +1,30 @@+/**+ * @file A plugin allow editors to keep their previous selection when being+ * programmatically focused by element.focus() api.+ */+import { mergeRegister } from '@lexical/utils';+import type { LexicalEditor } from 'lexical';+import { registerRootEventListener } from '../shared';++export function restoreFocusPlugin() {+ // We need to distinguish click-based focus events from programmatic+ // ones (el.focus()). We want to maintain the previous selection (editor.focus)+ // only if we are regaining focus programmatically. If click, let browser handle+ // focus and let lexical catch up.+ let clickFlag = false;+ return (editor: LexicalEditor) => {+ return mergeRegister(+ registerRootEventListener(editor, 'pointerdown', () => {+ clickFlag = true;+ setTimeout(() => {+ clickFlag = false;+ });+ }),+ registerRootEventListener(editor, 'focusin', (e) => {+ if (clickFlag) return;+ e.preventDefault();+ editor.focus(undefined, { defaultSelection: 'rootStart' });+ })+ );+ };+}