Skip to content

The open alternative to Salesforce, designed for AI.

fix(rich-text): keep selection after image autosave (#26969)

Wael Antarb6248558dea1
diff --git a/packages/twenty-front/src/modules/blocknote-editor/utils/__tests__/prepareBodyWithSignedUrls.test.ts b/packages/twenty-front/src/modules/blocknote-editor/utils/__tests__/prepareBodyWithSignedUrls.test.ts
deleted file mode 100644
index cdcacec31f9..00000000000
--- a/packages/twenty-front/src/modules/blocknote-editor/utils/__tests__/prepareBodyWithSignedUrls.test.ts
+++ /dev/null
@@ -1,46 +0,0 @@
-import { prepareBodyWithSignedUrls } from '@/blocknote-editor/utils/prepareBodyWithSignedUrls';
-
-describe('prepareBodyWithSignedUrls', () => {
- it('should return empty string as-is', () => {
- expect(prepareBodyWithSignedUrls('')).toBe('');
- });
-
- it('should parse and re-stringify blocks', () => {
- const input = JSON.stringify([{ type: 'paragraph', content: 'text' }]);
- const result = JSON.parse(prepareBodyWithSignedUrls(input));
- expect(result).toEqual([{ type: 'paragraph', content: 'text' }]);
- });
-
- it('should pass through non-image blocks unchanged', () => {
- const blocks = [
- { type: 'paragraph', content: 'text' },
- { type: 'heading', content: 'title' },
- { type: 'bulletListItem', content: 'item' },
- ];
- const result = JSON.parse(
- prepareBodyWithSignedUrls(JSON.stringify(blocks)),
- );
- expect(result).toEqual(blocks);
- });
-
- it('should skip image blocks without props', () => {
- const input = JSON.stringify([{ type: 'image' }]);
- const result = JSON.parse(prepareBodyWithSignedUrls(input));
- expect(result).toEqual([{ type: 'image' }]);
- });
-
- it('should skip image blocks without url in props', () => {
- const input = JSON.stringify([{ type: 'image', props: { alt: 'test' } }]);
- const result = JSON.parse(prepareBodyWithSignedUrls(input));
- expect(result).toEqual([{ type: 'image', props: { alt: 'test' } }]);
- });
-
- it('should process image blocks with valid URLs', () => {
- const input = JSON.stringify([
- { type: 'image', props: { url: 'https://example.com/image.png' } },
- ]);
- const result = JSON.parse(prepareBodyWithSignedUrls(input));
- expect(result[0].type).toBe('image');
- expect(result[0].props.url).toContain('example.com');
- });
-});
diff --git a/packages/twenty-front/src/modules/blocknote-editor/utils/__tests__/stripImageUrlTokens.test.ts b/packages/twenty-front/src/modules/blocknote-editor/utils/__tests__/stripImageUrlTokens.test.ts
new file mode 100644
index 00000000000..4936119b7d4
--- /dev/null
+++ b/packages/twenty-front/src/modules/blocknote-editor/utils/__tests__/stripImageUrlTokens.test.ts
@@ -0,0 +1,51 @@
+import { stripImageUrlTokens } from '@/blocknote-editor/utils/stripImageUrlTokens';
+
+describe('stripImageUrlTokens', () => {
+ it('should return empty string as-is', () => {
+ expect(stripImageUrlTokens('')).toBe('');
+ });
+
+ it('should pass through non-image blocks unchanged', () => {
+ const blocks = [
+ { type: 'paragraph', content: 'text' },
+ { type: 'heading', content: 'title' },
+ { type: 'image', props: { alt: 'no url' } },
+ ];
+
+ expect(JSON.parse(stripImageUrlTokens(JSON.stringify(blocks)))).toEqual(
+ blocks,
+ );
+ });
+
+ it('should make two signatures of the same image identical', () => {
+ const signedWith = (token: string) =>
+ JSON.stringify([
+ {
+ type: 'image',
+ props: {
+ url: `https://example.com/file/files-field/file-id?token=${token}`,
+ },
+ },
+ ]);
+
+ expect(stripImageUrlTokens(signedWith('first'))).toBe(
+ stripImageUrlTokens(signedWith('second')),
+ );
+ expect(JSON.parse(stripImageUrlTokens(signedWith('first')))).toEqual([
+ {
+ type: 'image',
+ props: { url: 'https://example.com/file/files-field/file-id' },
+ },
+ ]);
+ });
+
+ it('should keep a malformed image URL', () => {
+ const input = JSON.stringify([
+ { type: 'image', props: { url: 'not-a-url' } },
+ ]);
+
+ expect(JSON.parse(stripImageUrlTokens(input))).toEqual([
+ { type: 'image', props: { url: 'not-a-url' } },
+ ]);
+ });
+});
diff --git a/packages/twenty-front/src/modules/blocknote-editor/utils/prepareBodyWithSignedUrls.ts b/packages/twenty-front/src/modules/blocknote-editor/utils/prepareBodyWithSignedUrls.ts
deleted file mode 100644
index 3c54a251394..00000000000
--- a/packages/twenty-front/src/modules/blocknote-editor/utils/prepareBodyWithSignedUrls.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import { parseInitialBlocknote } from '@/blocknote-editor/utils/parseInitialBlocknote';
-
-// TODO: This function is extracted but its not doing what it is supposed to do. It is not signing the urls. It is just parsing the image urls.
-// tracking issue - https://github.com/twentyhq/twenty/issues/8351
-export const prepareBodyWithSignedUrls = (
- newStringifiedBody: string,
-): string => {
- if (!newStringifiedBody) return newStringifiedBody;
-
- const body = parseInitialBlocknote(newStringifiedBody);
-
- if (!body) return newStringifiedBody;
-
- const bodyWithSignedPayload = body.map((block) => {
- if (block.type !== 'image' || !block.props?.url) {
- return block;
- }
-
- const imageUrl = block.props.url;
- const parsedImageUrl = new URL(imageUrl);
-
- return {
- ...block,
- props: {
- ...block.props,
- url: parsedImageUrl.toString(),
- },
- };
- });
-
- return JSON.stringify(bodyWithSignedPayload);
-};
diff --git a/packages/twenty-front/src/modules/blocknote-editor/utils/stripImageUrlTokens.ts b/packages/twenty-front/src/modules/blocknote-editor/utils/stripImageUrlTokens.ts
new file mode 100644
index 00000000000..99848ba538e
--- /dev/null
+++ b/packages/twenty-front/src/modules/blocknote-editor/utils/stripImageUrlTokens.ts
@@ -0,0 +1,28 @@
+import { parseInitialBlocknote } from '@/blocknote-editor/utils/parseInitialBlocknote';
+
+export const stripImageUrlTokens = (stringifiedBody: string): string => {
+ const blocks = parseInitialBlocknote(stringifiedBody);
+
+ if (!blocks) return stringifiedBody;
+
+ const blocksWithoutTokens = blocks.map((block) => {
+ if (block.type !== 'image' || !block.props?.url) {
+ return block;
+ }
+
+ try {
+ const imageUrl = new URL(block.props.url);
+
+ imageUrl.searchParams.delete('token');
+
+ return {
+ ...block,
+ props: { ...block.props, url: imageUrl.toString() },
+ };
+ } catch {
+ return block;
+ }
+ });
+
+ return JSON.stringify(blocksWithoutTokens);
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/RichTextFieldEditor.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/RichTextFieldEditor.tsx
index 51f00e5535f..4f3b21aba2d 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/RichTextFieldEditor.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/RichTextFieldEditor.tsx
@@ -7,7 +7,7 @@ import { BLOCK_EDITOR_GLOBAL_HOTKEYS_CONFIG } from '@/blocknote-editor/constants
import { useAttachmentSync } from '@/blocknote-editor/hooks/useAttachmentSync';
import { useReplaceBlockEditorContent } from '@/blocknote-editor/hooks/useReplaceBlockEditorContent';
import { parseInitialBlocknote } from '@/blocknote-editor/utils/parseInitialBlocknote';
-import { prepareBodyWithSignedUrls } from '@/blocknote-editor/utils/prepareBodyWithSignedUrls';
+import { stripImageUrlTokens } from '@/blocknote-editor/utils/stripImageUrlTokens';
import { type Attachment } from '@/activities/files/types/Attachment';
import { useUploadAttachmentFile } from '@/activities/files/hooks/useUploadAttachmentFile';
import { getActivityTargetObjectFieldIdName } from '@/activities/utils/getActivityTargetObjectFieldIdName';
@@ -149,16 +149,16 @@ export const RichTextFieldEditor = ({
const { updateDraft, markDirty, flush, draftResyncKey } =
useRecordSeededDraft({
- upstreamDraft: { blocknote: fieldValue?.blocknote ?? '' },
+ upstreamDraft: {
+ blocknote: stripImageUrlTokens(fieldValue?.blocknote ?? ''),
+ },
persistDebounceMs: 300,
resetKey: recordId,
onPersist: ({ blocknote }) => {
if (isRecordFieldReadOnly === true) return;
- const preparedBlocknote = prepareBodyWithSignedUrls(blocknote);
-
if (onPersistBody) {
- onPersistBody(preparedBlocknote);
+ onPersistBody(blocknote);
return;
}
@@ -167,7 +167,7 @@ export const RichTextFieldEditor = ({
objectNameSingular,
updateOneRecordInput: {
[fieldName]: {
- blocknote: preparedBlocknote,
+ blocknote,
markdown: null,
},
},
@@ -236,7 +236,7 @@ export const RichTextFieldEditor = ({
| undefined;
// Only after capturing the pre-edit body: persisting rewrites the record, so the diff would miss removed attachments.
- updateDraft({ blocknote: newStringifiedBody });
+ updateDraft({ blocknote: stripImageUrlTokens(newStringifiedBody) });
await syncAttachments(newStringifiedBody, oldFieldValue?.blocknote);
};