Skip to content

Commit 53988f6

Browse files
Merge pull request #36 from cesardeazevedo/handle-paste
Prevent default paste handling when an image is detected on paste
2 parents e6f4561 + ddc35dd commit 53988f6

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/extensions/FileUploadExtension.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,22 @@ export const FileUploadExtension = Extension.create<FileUploadOptions, FileUploa
145145
},
146146
props: {
147147
handleDrop: (_, event) => {
148-
uploader.handleDrop(event)
148+
const handled = uploader.handleDrop(event)
149+
149150
if (this.options.immediateUpload) {
150151
uploader.start()
151152
}
153+
154+
return handled
152155
},
153156
handlePaste: (_, event) => {
154-
uploader.handlePaste(event)
155-
if (this.options.immediateUpload) {
157+
const handled = uploader.handlePaste(event)
158+
159+
if (handled && this.options.immediateUpload) {
156160
uploader.start()
157161
}
162+
163+
return handled
158164
},
159165
},
160166
}),
@@ -305,12 +311,16 @@ class Uploader {
305311

306312
const pos = this.view.posAtCoords({ left: event.clientX, top: event.clientY })?.pos
307313

308-
if (!pos) return
309-
if (!event.dataTransfer) return
314+
if (!pos) return false
315+
if (!event.dataTransfer) return false
310316

317+
let handled = false
311318
for (const file of event.dataTransfer.files) {
312319
this.addFile(file, pos)
320+
handled = true
313321
}
322+
323+
return handled
314324
}
315325

316326
handlePaste(event: ClipboardEvent) {

src/helpers/utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ export function replaceTextContent(content: string): string {
5454
* From: https://stackoverflow.com/q/79019919/1467342
5555
*/
5656
function handlePaste(view: EditorView, event: ClipboardEvent) {
57-
// Prevent the default paste behavior
58-
event.preventDefault()
57+
// If there are files, let the FileUploadExtension handle them
58+
if (event.clipboardData?.files && event.clipboardData.files.length > 0) {
59+
return false
60+
}
5961

6062
// Get plain text from clipboard
6163
const text = event.clipboardData?.getData('text/plain')
@@ -80,7 +82,7 @@ function handlePaste(view: EditorView, event: ClipboardEvent) {
8082
view.dispatch(tr.scrollIntoView().setMeta('paste', true).setMeta('uiEvent', 'paste'))
8183
}
8284

83-
return true
85+
return Boolean(text)
8486
}
8587

8688
/**

0 commit comments

Comments
 (0)