Skip to content

Commit cabbaf2

Browse files
mejo-max-nextcloud
authored andcommitted
Unselect uploaded image in case it is the first element in editor
Tiptap `setImage()` marks the added image as selected when added as first element in the document), which leads to the image being overwritten by subsequent `insertContent()` command. See ueberdosis/tiptap#3355 for the relevant upstream bug. Mitigate this bug by explicitly placing the cursor at the end of the selection between adding the image and the line break. Signed-off-by: Jonas <jonas@freesources.org>
1 parent 75f4571 commit cabbaf2

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

src/components/Editor/MediaHandler.vue

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,21 @@ export default {
194194
? this.$editor.chain().focus(position)
195195
: this.$editor.chain()
196196
197-
chain.setImage({ src, alt }).insertContent('<br />').focus().run()
197+
chain.setImage({ src, alt }).run()
198+
199+
const selection = this.$editor.view.state.selection
200+
if (!selection.empty) {
201+
// If inserted image is first element, it is selected and would get overwritten by
202+
// subsequent editor inserts (see tiptap#3355). So unselect the image by placing
203+
// the cursor at the end of the selection.
204+
this.$editor.commands.focus(selection.to)
205+
} else {
206+
// Place the cursor after the inserted image node
207+
this.$editor.commands.focus(selection.to + 2)
208+
}
209+
210+
// Insert a newline to allow placing the cursor in between subsequent images
211+
this.$editor.chain().insertContent('<br />').focus().run()
198212
},
199213
},
200214
}

0 commit comments

Comments
 (0)