From 53576245aa5dd33f8fba61b90a512493deac64b9 Mon Sep 17 00:00:00 2001 From: Thomas Trompette Date: Thu, 19 Dec 2024 14:19:25 +0100 Subject: [PATCH] Fix version creation / update when opening an action (#9145) Actions using tiptap trigger updates when called. Flow is following: - component using tiptap receive default value - we separate the default value because we need to add specific attributes to the editor when inserting breaks or variables - editor performs an update for each value It means that initialize our editor performs several updates. We need to avoid persisting until the init finished. --- .../form-types/hooks/useTextVariableEditor.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/twenty-front/src/modules/object-record/record-field/form-types/hooks/useTextVariableEditor.ts b/packages/twenty-front/src/modules/object-record/record-field/form-types/hooks/useTextVariableEditor.ts index d184e1dab4..a00c52f26e 100644 --- a/packages/twenty-front/src/modules/object-record/record-field/form-types/hooks/useTextVariableEditor.ts +++ b/packages/twenty-front/src/modules/object-record/record-field/form-types/hooks/useTextVariableEditor.ts @@ -6,6 +6,7 @@ import Paragraph from '@tiptap/extension-paragraph'; import { default as Placeholder } from '@tiptap/extension-placeholder'; import Text from '@tiptap/extension-text'; import { Editor, useEditor } from '@tiptap/react'; +import { useState } from 'react'; import { isDefined } from 'twenty-ui'; type UseTextVariableEditorProps = { @@ -23,6 +24,8 @@ export const useTextVariableEditor = ({ defaultValue, onUpdate, }: UseTextVariableEditorProps) => { + const [isInitializing, setIsInitializing] = useState(true); + const editor = useEditor({ extensions: [ Document, @@ -45,8 +48,12 @@ export const useTextVariableEditor = ({ if (isDefined(defaultValue)) { initializeEditorContent(editor, defaultValue); } + setIsInitializing(false); }, onUpdate: ({ editor }) => { + if (isInitializing) { + return; + } onUpdate(editor); }, editorProps: { @@ -54,11 +61,10 @@ export const useTextVariableEditor = ({ if (event.key === 'Enter' && !event.shiftKey) { event.preventDefault(); - const { state } = view; - const { tr } = state; - // Insert hard break using the view's state and dispatch if (multiline === true) { + const { state } = view; + const { tr } = state; const transaction = tr.replaceSelectionWith( state.schema.nodes.hardBreak.create(), );