Skip to content

Commit 9245b9c

Browse files
priosshrsthclaude
andcommitted
fix(editor): insert dynamic fields into the last focused editor
Sidebar dynamic fields always targeted the body editor, so with the cursor in the heading/subheading the field landed at the body's stale selection. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent e98adfd commit 9245b9c

4 files changed

Lines changed: 12 additions & 3 deletions

File tree

‎src/features/editor/components/Editor/Editor.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const Editor = ({ content }: EditorProps) => {
5454
content,
5555
immediatelyRender: false, // Avoid SSR & hydration issues
5656
editorProps: { attributes: { class: 'text-custom-xs' } },
57+
onFocus: ({ editor }) => useEditorStore.getState().setFocusedEditor(editor),
5758
onUpdate: ({ editor }) => {
5859
settingsStoreApi?.getState().setSettings({
5960
content: editor.getHTML(),

‎src/features/editor/components/Editor/MinimalEditor.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use client'
22

3+
import { useEditorStore } from '@editor/stores/editorStore'
34
import { getMinimalExtensions } from '@extensions/minimal-extensions'
45
import { EditorContent, useEditor } from '@tiptap/react'
56
import { useEffect, useRef } from 'react'
@@ -54,6 +55,7 @@ export const MinimalEditor = ({
5455
return true
5556
},
5657
},
58+
onFocus: ({ editor }) => useEditorStore.getState().setFocusedEditor(editor),
5759
onUpdate: ({ editor }) => onChange?.(editor.getHTML()),
5860
})
5961

‎src/features/editor/components/Sidebar/DynamicFields/DynamicFieldItem.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ type DynamicFieldItemProps = {
99
}
1010

1111
export const DynamicFieldItem = ({ fieldContent, value, name, icon }: DynamicFieldItemProps) => {
12-
const editor = useEditorStore((s) => s.editor)
13-
1412
const handleClick = () => {
15-
editor?.chain().focus().insertAutofillField({ value }).run()
13+
const { editor, focusedEditor } = useEditorStore.getState()
14+
const target = focusedEditor && !focusedEditor.isDestroyed ? focusedEditor : editor
15+
target?.chain().focus().insertAutofillField({ value }).run()
1616
}
1717

1818
return (

‎src/features/editor/stores/editorStore.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ interface EditorState {
66
setEditor: (editor: Editor) => void
77
destroyEditor: () => void
88

9+
focusedEditor: Editor | null
10+
setFocusedEditor: (editor: Editor) => void
11+
912
showEmbedInput: boolean
1013
setShowEmbedInput: (showEmbedInput: boolean) => void
1114

@@ -22,6 +25,9 @@ export const useEditorStore = create<EditorState>()((set) => ({
2225
setEditor: (editor: Editor) => set({ editor }),
2326
destroyEditor: () => set({ editor: null }),
2427

28+
focusedEditor: null,
29+
setFocusedEditor: (focusedEditor: Editor) => set({ focusedEditor }),
30+
2531
showEmbedInput: false,
2632
setShowEmbedInput: (showEmbedInput: boolean) => set({ showEmbedInput }),
2733

0 commit comments

Comments
 (0)