cursor is placed on end of entry when date is selected
authorPhiTux <redacted>
Wed, 22 Oct 2025 14:22:28 +0000 (16:22 +0200)
committerPhiTux <redacted>
Wed, 22 Oct 2025 14:22:28 +0000 (16:22 +0200)
frontend/src/routes/(authed)/write/+page.svelte

index 9d3f1ecfdeb167b6f008c07f7a11f795169d0634..f9298c570a00f10da40e7d3682161a920fdf7487 100644 (file)
 
                        // Update editor content
                        tinyMDE.setContent(currentLog);
-                       // Only auto-focus/select on non-mobile devices
+                       // Only auto-focus and place caret at end on non-mobile devices
                        if (!isMobile) {
                                try {
-                                       tinyMDE.setSelection({ row: 0, col: 0 });
-                                       // Ensure the underlying editable div gets focus
+                                       const content =
+                                               typeof tinyMDE.getContent === 'function' ? tinyMDE.getContent() : currentLog || '';
+                                       const text = typeof content === 'string' ? content : currentLog || '';
+                                       const lines = text.split('\n');
+                                       const lastRow = Math.max(0, lines.length - 1);
+                                       const lastCol = (lines[lastRow] && lines[lastRow].length) || 0;
+
+                                       // Focus the underlying editable element first
                                        const editorEl = document.querySelector(
                                                '#editor .TinyMDE textarea, #editor .TinyMDE [contenteditable="true"]'
                                        );
                                        if (editorEl) editorEl.focus();
+
+                                       // Then set the selection at the end on the next frame
+                                       requestAnimationFrame(() => {
+                                               try {
+                                                       tinyMDE.setSelection({ row: lastRow, col: lastCol });
+                                               } catch {}
+                                       });
                                } catch {}
                        }
 
git clone https://git.99rst.org/PROJECT