From a3574f464dd2a37d1a279d37cf6497ee3add894a Mon Sep 17 00:00:00 2001 From: santoslgl01-web Date: Mon, 27 Apr 2026 08:34:26 -0300 Subject: [PATCH] fix: preserve cursor position while editing posts Resolves #31 --- app/components/editor/Tiptap.tsx | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/components/editor/Tiptap.tsx b/app/components/editor/Tiptap.tsx index a687746..8470c90 100644 --- a/app/components/editor/Tiptap.tsx +++ b/app/components/editor/Tiptap.tsx @@ -1,5 +1,5 @@ 'use client'; -import { useEffect } from 'react'; +import { useEffect, useRef } from 'react'; import { EditorContent, useEditor } from '@tiptap/react'; import StarterKit from '@tiptap/starter-kit'; import Underline from '@tiptap/extension-underline'; @@ -9,11 +9,16 @@ const Tiptap: React.FC<{ onChange: (newContent: string) => void; content: string; }> = ({ onChange, content }) => { + const lastAppliedContentRef = useRef(content); + const handleChange = (newContent: string) => { + lastAppliedContentRef.current = newContent; onChange(newContent); }; + const editor = useEditor({ extensions: [StarterKit, Underline], + content, editorProps: { attributes: { class: @@ -24,12 +29,13 @@ const Tiptap: React.FC<{ handleChange(editor.getHTML()); }, }); + useEffect(() => { if (!editor) return; - const value = content; - if (value) { - editor.commands.setContent(value); - } + if (content === lastAppliedContentRef.current) return; + + editor.commands.setContent(content, false); + lastAppliedContentRef.current = content; }, [editor, content]); return (