Skip to content

Commit

Permalink
handle image paste, ignore the event's text if it contains a file (#2265
Browse files Browse the repository at this point in the history
)

Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
Julien Veyssier authored Mar 31, 2022
1 parent 6f619c0 commit 6c444cd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/components/EditorWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<div v-if="tiptap"
id="editor"
:class="{ draggedOver }"
@image-paste="onPaste"
@dragover.prevent.stop="draggedOver = true"
@dragleave.prevent.stop="draggedOver = false"
@drop.prevent.stop="onEditorDrop">
Expand Down Expand Up @@ -566,6 +567,9 @@ export default {
hideHelp() {
this.displayHelp = false
},
onPaste(e) {
this.uploadImageFiles(e.detail.files)
},
onEditorDrop(e) {
this.uploadImageFiles(e.dataTransfer.files)
this.draggedOver = false
Expand Down
24 changes: 24 additions & 0 deletions src/nodes/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

import TiptapImage from '@tiptap/extension-image'
import { Plugin } from 'prosemirror-state'
import ImageView from './ImageView'
import { VueNodeViewRenderer } from '@tiptap/vue-2'

Expand All @@ -39,6 +40,29 @@ const Image = TiptapImage.extend({
return VueNodeViewRenderer(ImageView)
},

addProseMirrorPlugins() {
return [
new Plugin({
props: {
handlePaste: (view, event, slice) => {
// only prevent the paste if it contains files
if (event.clipboardData.files && event.clipboardData.files.length > 0) {
// let the editor wrapper catch this custom event
const customEvent = new CustomEvent('image-paste', {
bubbles: true,
detail: {
files: event.clipboardData.files,
},
})
event.target.dispatchEvent(customEvent)
return true
}
},
},
}),
]
},

})

export default Image

0 comments on commit 6c444cd

Please sign in to comment.