Skip to content

Commit c60f72e

Browse files
authored
feat: support uploading files dragged and dropped from device in markup mode (#226)
1 parent 237c77c commit c60f72e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/bundle/Editor.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
289289
this.#markupEditor.codemirror.on('changes', this.onCMChanges);
290290
this.#markupEditor.codemirror.on('cursorActivity', this.onCMCursorActivity);
291291
this.#markupEditor.codemirror.on('paste', this.onCMPaste);
292+
this.#markupEditor.codemirror.on('drop', this.onCMDrop);
292293
this.#markupEditor.codemirror.on('beforeChange', this.onCMBeforeChange);
293294

294295
if (this.#fileUploadHandler) {
@@ -362,6 +363,7 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
362363
this.#markupEditor.codemirror.off('changes', this.onCMChanges);
363364
this.#markupEditor.codemirror.off('cursorActivity', this.onCMCursorActivity);
364365
this.#markupEditor.codemirror.off('paste', this.onCMPaste);
366+
this.#markupEditor.codemirror.off('drop', this.onCMDrop);
365367
this.#markupEditor.codemirror.off('beforeChange', this.onCMBeforeChange);
366368
this.#markupEditor.codemirror.getWrapperElement().remove();
367369
}
@@ -496,6 +498,21 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
496498
}
497499
};
498500

501+
private onCMDrop = (cm: CodeMirror.Editor, event: DragEvent) => {
502+
if (!event.dataTransfer) return;
503+
504+
const {
505+
dataTransfer: {files},
506+
} = event;
507+
508+
if (files.length) {
509+
event.preventDefault();
510+
const pos = cm.coordsChar({left: event.pageX, top: event.pageY}, 'page');
511+
cm.setCursor(pos);
512+
this.#cmFilesUploader?.upload(files);
513+
}
514+
};
515+
499516
private onCMBeforeChange = (
500517
_cm: CodeMirror.Editor,
501518
event: CodeMirror.EditorChangeCancellable,

0 commit comments

Comments
 (0)