Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pasting an image from the clipboard #2265

Merged
merged 1 commit into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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