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(md): plugins.useReactive util to correctly re-register plugins on reactive props (#587)

Seamus5b564f656989
diff --git a/js/app/packages/block-canvas/component/nodes/TextBox.tsx b/js/app/packages/block-canvas/component/nodes/TextBox.tsx
index 905ea479b1..d5b5b07550 100644
--- a/js/app/packages/block-canvas/component/nodes/TextBox.tsx
+++ b/js/app/packages/block-canvas/component/nodes/TextBox.tsx
@@ -171,14 +171,19 @@ function TextBoxEditor(props: {
};
});
- if (props.focusOnMount) {
- plugins.use((editor: LexicalEditor) => {
- return editor.registerRootListener((root) => {
- if (root === null) return;
- Object.assign(root.style, textwrapStyles());
- });
- });
- }
+ plugins.useReactive(
+ () => props.focusOnMount,
+ () => {
+ if (props.focusOnMount) {
+ return (editor) => {
+ return editor.registerRootListener((root) => {
+ if (root === null) return;
+ Object.assign(root.style, textwrapStyles());
+ });
+ };
+ }
+ }
+ );
createEffect(() => {
const val = markdownState();
diff --git a/js/app/packages/block-md/component/MarkdownEditor.tsx b/js/app/packages/block-md/component/MarkdownEditor.tsx
index b81e6e87c4..fa46527af7 100644
--- a/js/app/packages/block-md/component/MarkdownEditor.tsx
+++ b/js/app/packages/block-md/component/MarkdownEditor.tsx
@@ -831,11 +831,13 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
editor.setEditable(canEdit() ?? false);
});
- createEffect(() => {
- const titleEditor = md.titleEditor;
- if (!titleEditor) return;
- plugins.use(keyNavigationPlugin(titleEditor, isInlineMenuOpen));
- });
+ plugins.useReactive(
+ () => md.titleEditor,
+ () => {
+ if (md.titleEditor)
+ return keyNavigationPlugin(md.titleEditor, isInlineMenuOpen);
+ }
+ );
const [editorHasNoContent, setEditorHasNoContent] = createSignal(false);
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsx b/js/app/packages/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsx
index f88050e013..9dea8b3caf 100644
--- a/js/app/packages/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsx
+++ b/js/app/packages/core/component/LexicalMarkdown/component/core/MarkdownTextarea.tsx
@@ -179,16 +179,19 @@ export function MarkdownTextarea(props: MarkdownTextareaProps) {
})
);
- if (props.onFocusLeaveEnd && props.onFocusLeaveStart) {
- plugins.use(
- keyboardFocusPlugin({
- onFocusLeaveStart: props.onFocusLeaveStart,
- onFocusLeaveEnd: props.onFocusLeaveEnd,
- ignoreKeys: () =>
- mentionsMenuOperations.isOpen() || emojisMenuOperations.isOpen(),
- })
- );
- }
+ plugins.useReactive(
+ [() => props.onFocusLeaveEnd, () => props.onFocusLeaveStart],
+ () => {
+ if (props.onFocusLeaveEnd && props.onFocusLeaveStart) {
+ return keyboardFocusPlugin({
+ onFocusLeaveStart: props.onFocusLeaveStart,
+ onFocusLeaveEnd: props.onFocusLeaveEnd,
+ ignoreKeys: () =>
+ mentionsMenuOperations.isOpen() || emojisMenuOperations.isOpen(),
+ });
+ }
+ }
+ );
let cleanupEnterListener: () => void = () => {};
createEffect(() => {
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/pluginManager.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/pluginManager.ts
index c163845e74..81cdf17d9a 100644
--- a/js/app/packages/core/component/LexicalMarkdown/plugins/pluginManager.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/pluginManager.ts
@@ -8,13 +8,22 @@ import { registerRichText } from '@lexical/rich-text';
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 {
+ type Accessor,
+ type AccessorArray,
+ createEffect,
+ createRoot,
+ on,
+ 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';
+type PluginFunction = (editor: LexicalEditor) => () => void;
+
/**
* Create a binding between a LexicalEditor and the ability to register plugins
* without having to manually track clean up functions.
@@ -77,12 +86,38 @@ export function createPluginManager(editor: LexicalEditor, type: EditorType) {
return pluginManager;
},
- use(pluginFn: (editor: LexicalEditor) => () => void) {
+ use(pluginFn: PluginFunction) {
const cleanup = pluginFn(editor);
cleanupFunctions.push(cleanup);
return pluginManager;
},
+ useReactive<T>(
+ deps: AccessorArray<T> | Accessor<T>,
+ pluginAccessor: Accessor<PluginFunction | undefined>
+ ) {
+ let disposeRoot!: () => void;
+ let lastCleanup = pluginAccessor()?.(editor);
+ createRoot((dispose) => {
+ disposeRoot = dispose;
+ createEffect(
+ on(
+ deps,
+ () => {
+ lastCleanup?.();
+ lastCleanup = pluginAccessor()?.(editor);
+ },
+ { defer: true }
+ )
+ );
+ });
+ cleanupFunctions.push(() => {
+ lastCleanup?.();
+ disposeRoot();
+ });
+ return pluginManager;
+ },
+
cleanup() {
cleanupFunctions.forEach((cleanup) => {
cleanup();