Skip to content

Commit

Permalink
Unselect uploaded image in case it is the first element in editor
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mejo- committed Oct 31, 2022
1 parent d06790f commit e4d4f6b
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/components/Editor/MediaHandler.vue
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,18 @@ export default {
? this.$editor.chain().focus(position)
: this.$editor.chain()

chain.setImage({ src, alt }).insertContent('<br />').focus().run()
chain.setImage({ src, alt }).run()

const selection = this.$editor.view.state.selection
if (selection.from === 0 && !selection.empty) {
// If inserted image is first element, it is selected and would get overwritten by
// subsequent editor inserts (see tiptap#3355). So unselect the image by placing
// the cursor at the end of the selection.
this.$editor.commands.focus(selection.to)
}

// Insert a newline to allow placing the cursor in between subsequent images
this.$editor.chain().insertContent('<br />').focus().run()
},
},
}
Expand Down

0 comments on commit e4d4f6b

Please sign in to comment.