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.

feat(md): trigger element transformers on enter (#57)

Seamus412e5a831960
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/markdown-shortcuts/index.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/markdown-shortcuts/index.ts
new file mode 100644
index 0000000000..425263f76b
--- /dev/null
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/markdown-shortcuts/index.ts
@@ -0,0 +1 @@
+export * from './markdownShortcutsPlugin';
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/markdown-shortcuts/markdownShortcutsPlugin.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/markdown-shortcuts/markdownShortcutsPlugin.ts
new file mode 100644
index 0000000000..793cbc2b22
--- /dev/null
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/markdown-shortcuts/markdownShortcutsPlugin.ts
@@ -0,0 +1,92 @@
+import { registerMarkdownShortcuts, type Transformer } from '@lexical/markdown';
+import { mergeRegister } from '@lexical/utils';
+import {
+ $getSelection,
+ $isParagraphNode,
+ $isRangeSelection,
+ COMMAND_PRIORITY_NORMAL,
+ KEY_ENTER_COMMAND,
+ type LexicalEditor,
+} from 'lexical';
+
+function getRegExp(transformer: Transformer) {
+ const { type } = transformer;
+ switch (type) {
+ case 'element':
+ return transformer.regExp;
+ case 'multiline-element':
+ return transformer.regExpStart;
+ case 'text-format':
+ case 'text-match':
+ return null;
+ }
+}
+
+export type MarkdownShortcutsPluginProps = {
+ transformers: Transformer[];
+ triggerOnEnterTransformers: Transformer[];
+};
+
+function registerMarkdownShortcutsPlugins(
+ editor: LexicalEditor,
+ props: MarkdownShortcutsPluginProps
+) {
+ // Not all editor flavors support all nodes. Filter the available markdown shortcuts
+ // to only those with all dependencies available.
+ const transformers = props.transformers.filter((transformer) => {
+ if (
+ transformer.type === 'element' ||
+ transformer.type === 'multiline-element'
+ ) {
+ const deps = transformer.dependencies;
+ return deps.every((dep) => editor.hasNode(dep));
+ }
+ return true;
+ });
+
+ return mergeRegister(
+ registerMarkdownShortcuts(editor, transformers),
+ editor.registerCommand(
+ KEY_ENTER_COMMAND,
+ (e: KeyboardEvent) => {
+ const selection = $getSelection();
+ if (!$isRangeSelection(selection)) return false;
+ const anchor = selection.anchor;
+ const node = anchor.getNode();
+ const textContent = node.getTextContent();
+
+ for (const transformer of props.triggerOnEnterTransformers) {
+ const regExp = getRegExp(transformer);
+ if (!regExp) continue;
+
+ if (regExp.test(textContent)) {
+ const parent = node.getParent();
+ if (parent && $isParagraphNode(parent)) {
+ const match = textContent.match(regExp); // get the real match, since we care for the coercion here
+ if (match) {
+ if (transformer.type === 'multiline-element') {
+ transformer.replace(parent, [node], match, null, null, false);
+ node.remove();
+ e.preventDefault();
+ return true;
+ } else if (transformer.type === 'element') {
+ transformer.replace(parent, [node], match, false);
+ node.remove();
+ e.preventDefault();
+ return true;
+ }
+ }
+ }
+ }
+ }
+ return false;
+ },
+ COMMAND_PRIORITY_NORMAL
+ )
+ );
+}
+
+export function markdownShortcutsPlugin(props: MarkdownShortcutsPluginProps) {
+ return (editor: LexicalEditor) =>
+ registerMarkdownShortcutsPlugins(editor, props);
+}
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/pluginManager.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/pluginManager.ts
index f234e4c53e..c163845e74 100644
--- a/js/app/packages/core/component/LexicalMarkdown/plugins/pluginManager.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/pluginManager.ts
@@ -2,20 +2,18 @@ import { isInBlock } from '@core/block';
import { blockLoroManagerSignal } from '@core/signal/load';
import { createEmptyHistoryState, registerHistory } from '@lexical/history';
import { registerList } from '@lexical/list';
-import { registerMarkdownShortcuts } from '@lexical/markdown';
+import { CODE } from '@lexical/markdown';
import { registerPlainText } from '@lexical/plain-text';
import { registerRichText } from '@lexical/rich-text';
-import {
- ALL_TRANSFORMERS,
- type EditorType,
- RegisteredNodesByType,
-} from '@lexical-core';
+import { ALL_TRANSFORMERS, type EditorType } from '@lexical-core';
+import { HR } from '@lexical-core/transformers/transformers';
import type { EditorState, LexicalEditor, UpdateListener } from 'lexical';
import type { Setter } from 'solid-js';
import { registerLoroHistory } from '../collaboration/undo';
import { bindStateAs } from '../utils';
import { checklistPlugin } from './checklist/';
import { customDeletePlugin } from './custom-delete';
+import { markdownShortcutsPlugin } from './markdown-shortcuts';
/**
* Create a binding between a LexicalEditor and the ability to register plugins
@@ -60,19 +58,12 @@ export function createPluginManager(editor: LexicalEditor, type: EditorType) {
},
markdownShortcuts() {
- // Not all editor flavors support all nodes. Filter the available markdown shortcuts
- // to only those with all dependencies available.
- const transformers = ALL_TRANSFORMERS.filter((transformer) => {
- if (
- transformer.type === 'element' ||
- transformer.type === 'multiline-element'
- ) {
- const deps = transformer.dependencies;
- return deps.every((dep) => RegisteredNodesByType[type].includes(dep));
- }
- return true;
- });
- cleanupFunctions.push(registerMarkdownShortcuts(editor, transformers));
+ cleanupFunctions.push(
+ markdownShortcutsPlugin({
+ transformers: ALL_TRANSFORMERS,
+ triggerOnEnterTransformers: [HR, CODE],
+ })(editor)
+ );
return pluginManager;
},