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
feat(md): trailing/trapped paragraph plugin (#6186)
Seamus03eafb9ec56e
diff --git a/apps/web/src/features/block-md/component/MarkdownEditor.tsx b/apps/web/src/features/block-md/component/MarkdownEditor.tsxindex 7f80d88f2d..3a5deffb7f 100644--- a/apps/web/src/features/block-md/component/MarkdownEditor.tsx+++ b/apps/web/src/features/block-md/component/MarkdownEditor.tsx@@ -72,6 +72,7 @@ import {tableTouchSelectionPlugin,tagsPlugin,textPastePlugin,+ trailingParagraphPlugin,wordcountPlugin,} from '@core/component/LexicalMarkdown/plugins';import { actionsPlugin } from '@core/component/LexicalMarkdown/plugins/actions/actionsPlugin';@@ -615,6 +616,7 @@ export function MarkdownEditor(props: {.use(restoreFocusPlugin()).use(markdownPastePlugin()).use(normalizeEnterPlugin())+ .use(trailingParagraphPlugin()).use(checkboxToTaskPlugin({currentUserId: userId(),diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/builder/buildHandleFromConfig.ts b/apps/web/src/lib/core/component/LexicalMarkdown/builder/buildHandleFromConfig.tsindex 3cfa5bed51..6426c6ad0d 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/builder/buildHandleFromConfig.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/builder/buildHandleFromConfig.ts@@ -7,7 +7,6 @@ import {actionsPlugin,agentCommandsPlugin,awaitPlugin,- blockDecoratorTrailingParagraphPlugin,codePlugin,createAccessoryStore,createDraggableBlockStore,@@ -28,6 +27,7 @@ import {tabIndentationPlugin,tagsPlugin,textPastePlugin,+ trailingParagraphPlugin,} from '../plugins';import { checkboxToTaskPlugin } from '../plugins/checkbox-to-task';import { normalizeEnterPlugin } from '../plugins/normalize-enter';@@ -129,7 +129,7 @@ export function buildHandleFromConfig(config: EditorConfig): EditorHandle {}if (config.type !== 'plain-text' && !config.singleLine) {- plugins.use(blockDecoratorTrailingParagraphPlugin());+ plugins.use(trailingParagraphPlugin());}// Historydiff --git a/apps/web/src/lib/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsx b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsxindex bc8bcc505b..864ed57d64 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsx+++ b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsx@@ -33,7 +33,6 @@ import {type AutoLinkMatchMode,autoRegister,awaitPlugin,- blockDecoratorTrailingParagraphPlugin,codePlugin,createAccessoryStore,customSelectionDataPlugin,@@ -49,6 +48,7 @@ import {snippetsPlugin,tabIndentationPlugin,textPastePlugin,+ trailingParagraphPlugin,} from '../../plugins';import { checkboxToTaskPlugin } from '../../plugins/checkbox-to-task';import { restoreFocusPlugin } from '../../plugins/restore-focus';@@ -218,7 +218,7 @@ export function MarkdownTextarea(props: MarkdownTextareaProps) {.delete().state<string>(setMarkdownState, 'markdown').history(400)- .use(blockDecoratorTrailingParagraphPlugin())+ .use(trailingParagraphPlugin()).use(restoreFocusPlugin()).use(checkboxToTaskPlugin()).use(mediaPlugin())diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/block-decorator-trailing-paragraph/blockDecoratorTrailingParagraphPlugin.test.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/block-decorator-trailing-paragraph/blockDecoratorTrailingParagraphPlugin.test.tsdeleted file mode 100644index 34d5bf4b5e..0000000000--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/block-decorator-trailing-paragraph/blockDecoratorTrailingParagraphPlugin.test.ts+++ /dev/null@@ -1,106 +0,0 @@-/**- * @vitest-environment jsdom- */--import {- $createReplyTargetNode,- ReplyTargetNode,-} from '@macro-inc/lexical-core';-import {- $createParagraphNode,- $getRoot,- $isParagraphNode,- createEditor,-} from 'lexical';-import { describe, expect, it } from 'vitest';-import { blockDecoratorTrailingParagraphPlugin } from './blockDecoratorTrailingParagraphPlugin';--const quoteReplyData = {- channelId: 'channel-1',- targetMessageId: 'message-1',- targetThreadId: 'thread-1',- displayText: 'Reply preview',- senderId: 'sender-1',-};--function createTestEditor() {- const editor = createEditor({- namespace: 'block-decorator-trailing-paragraph-test',- nodes: [ReplyTargetNode],- onError: (error) => {- throw error;- },- });- const cleanup = blockDecoratorTrailingParagraphPlugin()(editor);- return { editor, cleanup };-}--describe('blockDecoratorTrailingParagraphPlugin', () => {- it('adds a paragraph after a trailing block decorator', () => {- const { editor, cleanup } = createTestEditor();-- editor.update(- () => {- $getRoot().append($createReplyTargetNode(quoteReplyData));- },- { discrete: true }- );-- editor.read(() => {- const children = $getRoot().getChildren();- expect(children).toHaveLength(2);- expect(children[0]).toBeInstanceOf(ReplyTargetNode);- expect($isParagraphNode(children[1])).toBe(true);- });-- cleanup();- });-- it('recreates the caret target when its paragraph is deleted', () => {- const { editor, cleanup } = createTestEditor();-- editor.update(- () => {- $getRoot().append(- $createReplyTargetNode(quoteReplyData),- $createParagraphNode()- );- },- { discrete: true }- );- editor.update(- () => {- $getRoot().getLastChild()?.remove();- },- { discrete: true }- );-- editor.read(() => {- const children = $getRoot().getChildren();- expect(children).toHaveLength(2);- expect($isParagraphNode(children[1])).toBe(true);- });-- cleanup();- });-- it('does not add another paragraph when one already trails the decorator', () => {- const { editor, cleanup } = createTestEditor();-- editor.update(- () => {- $getRoot().append(- $createReplyTargetNode(quoteReplyData),- $createParagraphNode()- );- },- { discrete: true }- );-- editor.read(() => {- expect($getRoot().getChildren()).toHaveLength(2);- });-- cleanup();- });-});diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/block-decorator-trailing-paragraph/blockDecoratorTrailingParagraphPlugin.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/block-decorator-trailing-paragraph/blockDecoratorTrailingParagraphPlugin.tsdeleted file mode 100644index af063cacff..0000000000--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/block-decorator-trailing-paragraph/blockDecoratorTrailingParagraphPlugin.ts+++ /dev/null@@ -1,19 +0,0 @@-import {- $createParagraphNode,- $isDecoratorNode,- type LexicalEditor,- RootNode,-} from 'lexical';--/**- * Keep an editable caret target after a block decorator at the end of the- * document. Inline decorators do not need a separate paragraph.- */-export function blockDecoratorTrailingParagraphPlugin() {- return (editor: LexicalEditor) =>- editor.registerNodeTransform(RootNode, (root) => {- const lastChild = root.getLastChild();- if (!$isDecoratorNode(lastChild) || lastChild.isInline()) return;- root.append($createParagraphNode());- });-}diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/block-decorator-trailing-paragraph/index.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/block-decorator-trailing-paragraph/index.tsdeleted file mode 100644index 58c4f4abf2..0000000000--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/block-decorator-trailing-paragraph/index.ts+++ /dev/null@@ -1 +0,0 @@-export * from './blockDecoratorTrailingParagraphPlugin';diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/index.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/index.tsindex 1d3a4c2a26..8b659fda5a 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/index.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/index.ts@@ -2,7 +2,6 @@ export * from './actions';export * from './agent-commands';export * from './await';export * from './blame-tooltip';-export * from './block-decorator-trailing-paragraph';export * from './checklist';export * from './code';export * from './comments';@@ -45,4 +44,5 @@ export * from './tab-indentation';export * from './tables';export * from './tags';export * from './text-paste';+export * from './trailing-paragraph';export * from './wordcount';diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/trailing-paragraph/index.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/trailing-paragraph/index.tsnew file mode 100644index 0000000000..d6f5d921ea--- /dev/null+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/trailing-paragraph/index.ts@@ -0,0 +1 @@+export * from './trailingParagraphPlugin';diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/trailing-paragraph/trailingParagraphPlugin.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/trailing-paragraph/trailingParagraphPlugin.tsnew file mode 100644index 0000000000..395918e213--- /dev/null+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/trailing-paragraph/trailingParagraphPlugin.ts@@ -0,0 +1,114 @@+import { $isCodeNode } from '@lexical/code';+import { $isTableNode } from '@lexical/table';+import { mergeRegister } from '@lexical/utils';+import {+ $createParagraphNode,+ $getNodeByKey,+ $getRoot,+ $isDecoratorNode,+ HISTORY_PUSH_TAG,+ type LexicalEditor,+ type LexicalNode,+ type NodeKey,+ RootNode,+} from 'lexical';++type BlockGap = {+ previousKey: NodeKey;+ nextKey: NodeKey;+};++function $needsTrailingParagraph(+ node: LexicalNode | null | undefined+): node is LexicalNode {+ const isBlockDecorator = $isDecoratorNode(node) && !node.isInline();+ return isBlockDecorator || $isCodeNode(node) || $isTableNode(node);+}++function getBlockGapAtPoint(+ editor: LexicalEditor,+ clientY: number+): BlockGap | null {+ return editor.read(() => {+ const children = $getRoot().getChildren();+ for (let index = 0; index < children.length - 1; index++) {+ const previous = children[index];+ const next = children[index + 1];+ if (+ !$needsTrailingParagraph(previous) ||+ !$needsTrailingParagraph(next)+ ) {+ continue;+ }++ const previousElement = editor.getElementByKey(previous.getKey());+ const nextElement = editor.getElementByKey(next.getKey());+ if (!previousElement || !nextElement) continue;++ const previousRect = previousElement.getBoundingClientRect();+ const nextRect = nextElement.getBoundingClientRect();+ if (clientY > previousRect.bottom && clientY < nextRect.top) {+ return {+ previousKey: previous.getKey(),+ nextKey: next.getKey(),+ };+ }+ }+ return null;+ });+}++/**+ * Keep an editable caret target after a block decorator, code block, or table+ * at the end of the document. Clicking the gap between two such blocks also+ * creates an editable paragraph there. Inline decorators do not need a+ * separate paragraph.+ */+export function trailingParagraphPlugin() {+ return (editor: LexicalEditor) => {+ const handleClick = (event: MouseEvent) => {+ const rootElement = editor.getRootElement();+ if (+ !editor.isEditable() ||+ event.button !== 0 ||+ event.defaultPrevented ||+ event.target !== rootElement+ ) {+ return;+ }++ const gap = getBlockGapAtPoint(editor, event.clientY);+ if (!gap) return;++ event.preventDefault();+ editor.update(+ () => {+ const previous = $getNodeByKey(gap.previousKey);+ const next = $getNodeByKey(gap.nextKey);+ if (+ !$needsTrailingParagraph(previous) ||+ !$needsTrailingParagraph(next) ||+ previous.getNextSibling() !== next+ ) {+ return;+ }+ previous.insertAfter($createParagraphNode()).selectStart();+ },+ { discrete: true, tag: HISTORY_PUSH_TAG }+ );+ editor.focus();+ };++ return mergeRegister(+ editor.registerNodeTransform(RootNode, (root) => {+ if ($needsTrailingParagraph(root.getLastChild())) {+ root.append($createParagraphNode());+ }+ }),+ editor.registerRootListener((root, previousRoot) => {+ previousRoot?.removeEventListener('click', handleClick);+ root?.addEventListener('click', handleClick);+ })+ );+ };+}