Skip to content

The open alternative to Salesforce, designed for AI.

Fix the rich text editor crash on the full screen toggle (#25071)

Félix Malfait536afd76bb45
diff --git a/packages/twenty-front/src/modules/activities/emails/editor/components/EmailEditorCanvas.tsx b/packages/twenty-front/src/modules/activities/emails/editor/components/EmailEditorCanvas.tsx
index 093a1e09d9d..294da205104 100644
--- a/packages/twenty-front/src/modules/activities/emails/editor/components/EmailEditorCanvas.tsx
+++ b/packages/twenty-front/src/modules/activities/emails/editor/components/EmailEditorCanvas.tsx
@@ -1,7 +1,7 @@
import { AdvancedTextEditor } from '@/advanced-text-editor/components/AdvancedTextEditor';
+import { useLiveEditorState } from '@/advanced-text-editor/hooks/useLiveEditorState';
import { type AdvancedTextEditorComponentProps } from '@/advanced-text-editor/types/AdvancedTextEditorComponentProps';
import { styled } from '@linaria/react';
-import { useEditorState } from '@tiptap/react';
import { CANVAS_THEME_DEFAULTS, resolveCanvasTheme } from 'twenty-shared/utils';
import { themeCssVariables } from 'twenty-ui/theme-constants';
@@ -37,11 +37,9 @@ export const EmailEditorCanvas = ({
readonly,
minHeight,
}: EmailEditorCanvasProps) => {
- const storedCanvasTheme = useEditorState({
- editor,
- selector: ({ editor: currentEditor }) =>
- resolveCanvasTheme(currentEditor.state.doc.attrs.canvasTheme),
- });
+ const storedCanvasTheme = useLiveEditorState(editor, (currentEditor) =>
+ resolveCanvasTheme(currentEditor.state.doc.attrs.canvasTheme),
+ );
const canvasTheme = storedCanvasTheme ?? CANVAS_THEME_DEFAULTS;
return (
diff --git a/packages/twenty-front/src/modules/activities/emails/hooks/useCampaignCanvasWidth.ts b/packages/twenty-front/src/modules/activities/emails/hooks/useCampaignCanvasWidth.ts
index 1312552fe7f..35623e76d64 100644
--- a/packages/twenty-front/src/modules/activities/emails/hooks/useCampaignCanvasWidth.ts
+++ b/packages/twenty-front/src/modules/activities/emails/hooks/useCampaignCanvasWidth.ts
@@ -13,10 +13,13 @@ import {
export const useCampaignCanvasWidth = (editor: Editor | null): string => {
const canvasWidth = useEditorState({
editor,
- selector: ({ editor: currentEditor }) =>
- isDefined(currentEditor)
- ? (resolveCanvasTheme(currentEditor.state.doc.attrs.canvasTheme)
- ?.width ?? CANVAS_THEME_DEFAULTS.width)
+ // Reading the editor passed in rather than the snapshot one, which keeps
+ // pointing at the instance `useEditor` destroyed when it recreated the
+ // editor, until the next transaction refreshes the snapshot.
+ selector: () =>
+ isDefined(editor)
+ ? (resolveCanvasTheme(editor.state.doc.attrs.canvasTheme)?.width ??
+ CANVAS_THEME_DEFAULTS.width)
: CANVAS_THEME_DEFAULTS.width,
});
diff --git a/packages/twenty-front/src/modules/advanced-text-editor/components/ImageBubbleMenu.tsx b/packages/twenty-front/src/modules/advanced-text-editor/components/ImageBubbleMenu.tsx
index 3b9278c7353..745a9000d16 100644
--- a/packages/twenty-front/src/modules/advanced-text-editor/components/ImageBubbleMenu.tsx
+++ b/packages/twenty-front/src/modules/advanced-text-editor/components/ImageBubbleMenu.tsx
@@ -1,7 +1,7 @@
import { BubbleMenuIconButton } from '@/advanced-text-editor/components/BubbleMenuIconButton';
import { StyledBubbleMenuContainer } from '@/advanced-text-editor/components/StyledBubbleMenuContainer';
+import { useLiveEditorState } from '@/advanced-text-editor/hooks/useLiveEditorState';
import { type Editor } from '@tiptap/core';
-import { useEditorState } from '@tiptap/react';
import { BubbleMenu } from '@tiptap/react/menus';
import {
IconAlignCenter,
@@ -15,13 +15,10 @@ type ImageBubbleMenuProps = {
};
export const ImageBubbleMenu = ({ editor }: ImageBubbleMenuProps) => {
- const state = useEditorState({
- editor,
- selector: (ctx) => {
- return {
- align: ctx.editor.getAttributes('image').align || 'left',
- };
- },
+ const state = useLiveEditorState(editor, (currentEditor) => {
+ return {
+ align: currentEditor.getAttributes('image').align || 'left',
+ };
});
const handleDelete = () => {
diff --git a/packages/twenty-front/src/modules/advanced-text-editor/components/LinkBubbleMenu.tsx b/packages/twenty-front/src/modules/advanced-text-editor/components/LinkBubbleMenu.tsx
index f6b92722bdc..6f7e36bd775 100644
--- a/packages/twenty-front/src/modules/advanced-text-editor/components/LinkBubbleMenu.tsx
+++ b/packages/twenty-front/src/modules/advanced-text-editor/components/LinkBubbleMenu.tsx
@@ -1,8 +1,8 @@
import { BubbleMenuIconButton } from '@/advanced-text-editor/components/BubbleMenuIconButton';
import { EditLinkPopover } from '@/advanced-text-editor/components/EditLinkPopover';
import { StyledBubbleMenuContainer } from '@/advanced-text-editor/components/StyledBubbleMenuContainer';
+import { useLiveEditorState } from '@/advanced-text-editor/hooks/useLiveEditorState';
import { type Editor } from '@tiptap/core';
-import { useEditorState } from '@tiptap/react';
import { BubbleMenu } from '@tiptap/react/menus';
import { IconExternalLink, IconLinkOff } from 'twenty-ui/icon';
import { getSafeUrl } from 'twenty-shared/utils';
@@ -12,13 +12,10 @@ type LinkBubbleMenuProps = {
};
export const LinkBubbleMenu = ({ editor }: LinkBubbleMenuProps) => {
- const state = useEditorState({
- editor,
- selector: (ctx) => {
- return {
- linkHref: ctx.editor.getAttributes('link').href || '',
- };
- },
+ const state = useLiveEditorState(editor, (currentEditor) => {
+ return {
+ linkHref: currentEditor.getAttributes('link').href || '',
+ };
});
const handleShouldShow = () => {
diff --git a/packages/twenty-front/src/modules/advanced-text-editor/components/__tests__/FormAdvancedTextFieldInput.test.tsx b/packages/twenty-front/src/modules/advanced-text-editor/components/__tests__/FormAdvancedTextFieldInput.test.tsx
new file mode 100644
index 00000000000..94c3225c094
--- /dev/null
+++ b/packages/twenty-front/src/modules/advanced-text-editor/components/__tests__/FormAdvancedTextFieldInput.test.tsx
@@ -0,0 +1,44 @@
+import { i18n } from '@lingui/core';
+import { I18nProvider } from '@lingui/react';
+import { render, screen } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { Provider as JotaiProvider } from 'jotai';
+import { type ReactNode } from 'react';
+import { MemoryRouter } from 'react-router-dom';
+
+import { FormAdvancedTextFieldInput } from '@/advanced-text-editor/components/FormAdvancedTextFieldInput';
+import { type AdvancedTextEditorProfile } from '@/advanced-text-editor/types/AdvancedTextEditorProfile';
+import { buildFullRichTextExtensions } from '@/advanced-text-editor/utils/buildFullRichTextExtensions';
+import { jotaiStore } from '@/ui/utilities/state/jotai/jotaiStore';
+
+const TEST_EDITOR_PROFILE = {
+ chrome: 'field',
+ minHeight: 200,
+ enableFullScreen: true,
+ buildExtensions: buildFullRichTextExtensions,
+} satisfies AdvancedTextEditorProfile;
+
+const Wrapper = ({ children }: { children: ReactNode }) => (
+ <I18nProvider i18n={i18n}>
+ <JotaiProvider store={jotaiStore}>
+ <MemoryRouter>{children}</MemoryRouter>
+ </JotaiProvider>
+ </I18nProvider>
+);
+
+describe('FormAdvancedTextFieldInput', () => {
+ it('should keep the editor working when entering full screen', async () => {
+ render(
+ <FormAdvancedTextFieldInput
+ label="Body"
+ defaultValue=""
+ profile={TEST_EDITOR_PROFILE}
+ />,
+ { wrapper: Wrapper },
+ );
+
+ await userEvent.click(screen.getByRole('button'));
+
+ expect(screen.getByRole('textbox')).toBeVisible();
+ });
+});
diff --git a/packages/twenty-front/src/modules/advanced-text-editor/hooks/__tests__/useLiveEditorState.test.tsx b/packages/twenty-front/src/modules/advanced-text-editor/hooks/__tests__/useLiveEditorState.test.tsx
new file mode 100644
index 00000000000..fd11164de1c
--- /dev/null
+++ b/packages/twenty-front/src/modules/advanced-text-editor/hooks/__tests__/useLiveEditorState.test.tsx
@@ -0,0 +1,37 @@
+import { renderHook } from '@testing-library/react';
+import { Editor } from '@tiptap/core';
+import { Document } from '@tiptap/extension-document';
+import { Heading } from '@tiptap/extension-heading';
+import { Paragraph } from '@tiptap/extension-paragraph';
+import { Text } from '@tiptap/extension-text';
+
+import { useLiveEditorState } from '@/advanced-text-editor/hooks/useLiveEditorState';
+import { hasEditorExtension } from '@/advanced-text-editor/utils/hasEditorExtension';
+
+describe('useLiveEditorState', () => {
+ it('should select from the editor that replaced a destroyed one', () => {
+ const destroyedEditor = new Editor({
+ extensions: [Document, Paragraph, Text],
+ });
+ const recreatedEditor = new Editor({
+ extensions: [Document, Paragraph, Text, Heading],
+ });
+
+ const { result, rerender } = renderHook(
+ ({ editor }) =>
+ useLiveEditorState(editor, (currentEditor) =>
+ hasEditorExtension(currentEditor, 'heading'),
+ ),
+ { initialProps: { editor: destroyedEditor } },
+ );
+
+ expect(result.current).toBe(false);
+
+ destroyedEditor.destroy();
+ rerender({ editor: recreatedEditor });
+
+ expect(result.current).toBe(true);
+
+ recreatedEditor.destroy();
+ });
+});
diff --git a/packages/twenty-front/src/modules/advanced-text-editor/hooks/useLiveEditorState.ts b/packages/twenty-front/src/modules/advanced-text-editor/hooks/useLiveEditorState.ts
new file mode 100644
index 00000000000..4dd0d6790f9
--- /dev/null
+++ b/packages/twenty-front/src/modules/advanced-text-editor/hooks/useLiveEditorState.ts
@@ -0,0 +1,17 @@
+import { type Editor } from '@tiptap/core';
+import { useEditorState } from '@tiptap/react';
+
+// `useEditorState` caches its snapshot until the next transaction: a selector
+// reading the snapshot editor still gets the previous instance after
+// `useEditor` recreated the editor on a dependency change, and that instance is
+// destroyed, so it has dropped its extension and command managers. Selecting
+// from the instance the caller holds keeps the selection on the live editor.
+// Drop this hook once https://github.com/ueberdosis/tiptap/issues/7346 ships.
+export const useLiveEditorState = <TSelectorResult>(
+ editor: Editor,
+ select: (editor: Editor) => TSelectorResult,
+): TSelectorResult =>
+ useEditorState({
+ editor,
+ selector: () => select(editor),
+ });
diff --git a/packages/twenty-front/src/modules/advanced-text-editor/hooks/useTextBubbleState.ts b/packages/twenty-front/src/modules/advanced-text-editor/hooks/useTextBubbleState.ts
index 3cdcdc14fb1..22007089f28 100644
--- a/packages/twenty-front/src/modules/advanced-text-editor/hooks/useTextBubbleState.ts
+++ b/packages/twenty-front/src/modules/advanced-text-editor/hooks/useTextBubbleState.ts
@@ -1,21 +1,18 @@
+import { useLiveEditorState } from '@/advanced-text-editor/hooks/useLiveEditorState';
import { type Editor } from '@tiptap/core';
-import { useEditorState } from '@tiptap/react';
export const useTextBubbleState = (editor: Editor) => {
- const state = useEditorState({
- editor,
- selector: (ctx) => {
- return {
- isBold: ctx.editor.isActive('bold'),
- isItalic: ctx.editor.isActive('italic'),
- isStrike: ctx.editor.isActive('strike'),
- isUnderline: ctx.editor.isActive('underline'),
- isLink: ctx.editor.isActive('link'),
- linkHref: ctx.editor.getAttributes('link').href || '',
- isBulletList: ctx.editor.isActive('bulletList'),
- isOrderedList: ctx.editor.isActive('orderedList'),
- };
- },
+ const state = useLiveEditorState(editor, (currentEditor) => {
+ return {
+ isBold: currentEditor.isActive('bold'),
+ isItalic: currentEditor.isActive('italic'),
+ isStrike: currentEditor.isActive('strike'),
+ isUnderline: currentEditor.isActive('underline'),
+ isLink: currentEditor.isActive('link'),
+ linkHref: currentEditor.getAttributes('link').href || '',
+ isBulletList: currentEditor.isActive('bulletList'),
+ isOrderedList: currentEditor.isActive('orderedList'),
+ };
});
return state;
diff --git a/packages/twenty-front/src/modules/advanced-text-editor/hooks/useTurnIntoBlockOptions.ts b/packages/twenty-front/src/modules/advanced-text-editor/hooks/useTurnIntoBlockOptions.ts
index b41035cd3ef..2b8f8c41bcf 100644
--- a/packages/twenty-front/src/modules/advanced-text-editor/hooks/useTurnIntoBlockOptions.ts
+++ b/packages/twenty-front/src/modules/advanced-text-editor/hooks/useTurnIntoBlockOptions.ts
@@ -1,6 +1,7 @@
+import { useLiveEditorState } from '@/advanced-text-editor/hooks/useLiveEditorState';
import { hasEditorExtension } from '@/advanced-text-editor/utils/hasEditorExtension';
import { useLingui } from '@lingui/react/macro';
-import { type Editor, useEditorState } from '@tiptap/react';
+import { type Editor } from '@tiptap/react';
import {
type IconComponent,
IconH1,
@@ -33,39 +34,36 @@ export const useTurnIntoBlockOptions = (editor: Editor) => {
3: t`Heading 3`,
};
- return useEditorState({
- editor,
- selector: ({ editor }): TurnIntoBlockOptions[] => [
- {
- id: 'paragraph',
- title: t`Paragraph`,
- icon: IconPilcrow,
- onClick: () => {
- return editor.chain().focus().setParagraph().run();
- },
- disabled: () => {
- return !editor.can().setParagraph();
- },
- isActive: () => {
- return editor.isActive('paragraph');
- },
+ return useLiveEditorState(editor, (currentEditor): TurnIntoBlockOptions[] => [
+ {
+ id: 'paragraph',
+ title: t`Paragraph`,
+ icon: IconPilcrow,
+ onClick: () => {
+ return currentEditor.chain().focus().setParagraph().run();
},
- ...(hasEditorExtension(editor, 'heading')
- ? ([1, 2, 3] as const).map((level) => ({
- id: `heading${level}`,
- title: headingTitles[level],
- icon: HEADING_ICONS[level],
- onClick: () => {
- return editor.chain().focus().setHeading({ level }).run();
- },
- disabled: () => {
- return !editor.can().setHeading({ level });
- },
- isActive: () => {
- return editor.isActive('heading', { level });
- },
- }))
- : []),
- ],
- });
+ disabled: () => {
+ return !currentEditor.can().setParagraph();
+ },
+ isActive: () => {
+ return currentEditor.isActive('paragraph');
+ },
+ },
+ ...(hasEditorExtension(currentEditor, 'heading')
+ ? ([1, 2, 3] as const).map((level) => ({
+ id: `heading${level}`,
+ title: headingTitles[level],
+ icon: HEADING_ICONS[level],
+ onClick: () => {
+ return currentEditor.chain().focus().setHeading({ level }).run();
+ },
+ disabled: () => {
+ return !currentEditor.can().setHeading({ level });
+ },
+ isActive: () => {
+ return currentEditor.isActive('heading', { level });
+ },
+ }))
+ : []),
+ ]);
};
diff --git a/packages/twenty-front/src/modules/advanced-text-editor/utils/__tests__/hasEditorExtension.test.ts b/packages/twenty-front/src/modules/advanced-text-editor/utils/__tests__/hasEditorExtension.test.ts
index 5fa5047623f..f6778b74836 100644
--- a/packages/twenty-front/src/modules/advanced-text-editor/utils/__tests__/hasEditorExtension.test.ts
+++ b/packages/twenty-front/src/modules/advanced-text-editor/utils/__tests__/hasEditorExtension.test.ts
@@ -23,4 +23,14 @@ describe('hasEditorExtension', () => {
expect(hasEditorExtension(editor, 'italic')).toBe(false);
expect(hasEditorExtension(editor, 'heading')).toBe(false);
});
+
+ it('should return false for a destroyed editor', () => {
+ const destroyedEditor = new Editor({
+ extensions: [Document, Paragraph, Text, Bold],
+ });
+
+ destroyedEditor.destroy();
+
+ expect(hasEditorExtension(destroyedEditor, 'bold')).toBe(false);
+ });
});
diff --git a/packages/twenty-front/src/modules/advanced-text-editor/utils/hasEditorExtension.ts b/packages/twenty-front/src/modules/advanced-text-editor/utils/hasEditorExtension.ts
index e3d79fef4a2..37cdaabec9f 100644
--- a/packages/twenty-front/src/modules/advanced-text-editor/utils/hasEditorExtension.ts
+++ b/packages/twenty-front/src/modules/advanced-text-editor/utils/hasEditorExtension.ts
@@ -1,6 +1,11 @@
import { type Editor } from '@tiptap/core';
+import { isDefined } from 'twenty-shared/utils';
+// A destroyed editor drops its extension manager, and components can still hold
+// a destroyed instance for a render, either from a state that has not been
+// updated yet or from a stale tiptap snapshot.
export const hasEditorExtension = (editor: Editor, extensionName: string) =>
+ isDefined(editor.extensionManager) &&
editor.extensionManager.extensions.some(
(extension) => extension.name === extensionName,
);
diff --git a/packages/twenty-front/src/modules/side-panel/pages/email-block-settings/components/EmailPageStyleSection.tsx b/packages/twenty-front/src/modules/side-panel/pages/email-block-settings/components/EmailPageStyleSection.tsx
index ea8d449b9e5..05678a0567e 100644
--- a/packages/twenty-front/src/modules/side-panel/pages/email-block-settings/components/EmailPageStyleSection.tsx
+++ b/packages/twenty-front/src/modules/side-panel/pages/email-block-settings/components/EmailPageStyleSection.tsx
@@ -1,7 +1,6 @@
import { styled } from '@linaria/react';
import { useLingui } from '@lingui/react/macro';
import { type Editor } from '@tiptap/core';
-import { useEditorState } from '@tiptap/react';
import {
type CanvasTheme,
isDefined,
@@ -9,6 +8,7 @@ import {
} from 'twenty-shared/utils';
import { themeCssVariables } from 'twenty-ui/theme-constants';
+import { useLiveEditorState } from '@/advanced-text-editor/hooks/useLiveEditorState';
import { EmailBlockSettingsFieldInput } from '@/side-panel/pages/email-block-settings/components/EmailBlockSettingsFieldInput';
import {
EmailBoxSidesInput,
@@ -88,11 +88,9 @@ export const EmailPageStyleSection = ({
}: EmailPageStyleSectionProps) => {
const { t, i18n } = useLingui();
- const canvasTheme = useEditorState({
- editor,
- selector: ({ editor: currentEditor }) =>
- resolveCanvasTheme(currentEditor.state.doc.attrs.canvasTheme),
- });
+ const canvasTheme = useLiveEditorState(editor, (currentEditor) =>
+ resolveCanvasTheme(currentEditor.state.doc.attrs.canvasTheme),
+ );
if (!isDefined(canvasTheme)) {
return (
diff --git a/packages/twenty-front/src/modules/side-panel/pages/email-block-settings/components/SidePanelEmailBlockSettingsPage.tsx b/packages/twenty-front/src/modules/side-panel/pages/email-block-settings/components/SidePanelEmailBlockSettingsPage.tsx
index 6263d09690a..e10e14aeae3 100644
--- a/packages/twenty-front/src/modules/side-panel/pages/email-block-settings/components/SidePanelEmailBlockSettingsPage.tsx
+++ b/packages/twenty-front/src/modules/side-panel/pages/email-block-settings/components/SidePanelEmailBlockSettingsPage.tsx
@@ -1,7 +1,6 @@
import { styled } from '@linaria/react';
import { useLingui } from '@lingui/react/macro';
import { type Editor } from '@tiptap/core';
-import { useEditorState } from '@tiptap/react';
import {
isDefined,
resolveCanvasTheme,
@@ -13,6 +12,7 @@ import { themeCssVariables } from 'twenty-ui/theme-constants';
import { activeEmailEditorState } from '@/activities/emails/states/activeEmailEditorState';
import { ADVANCED_TEXT_EDITOR_BLOCK_CATALOG } from '@/advanced-text-editor/constants/AdvancedTextEditorBlockCatalog';
+import { useLiveEditorState } from '@/advanced-text-editor/hooks/useLiveEditorState';
import { type AdvancedTextEditorBlockSetting } from '@/advanced-text-editor/types/AdvancedTextEditorBlockCatalog';
import { getBlockSelectionTarget } from '@/advanced-text-editor/utils/getBlockSelectionTarget';
import { getBlockStyle } from '@/advanced-text-editor/utils/getBlockStyle';
@@ -71,11 +71,9 @@ const BOX_FIELD_SIDE_PROPERTIES: Record<
const EmailBlockSettingsContent = ({ editor }: { editor: Editor }) => {
const { i18n, t } = useLingui();
- const target = useEditorState({
- editor,
- selector: ({ editor: currentEditor }) =>
- getBlockSelectionTarget(currentEditor),
- });
+ const target = useLiveEditorState(editor, (currentEditor) =>
+ getBlockSelectionTarget(currentEditor),
+ );
if (!isDefined(target)) {
return <EmailPageStyleSection editor={editor} />;