Skip to content

Commit bf90bba

Browse files
committed
image drop onto textarea
1 parent 1a01adc commit bf90bba

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

client/src/components/ImagePreview.vue

+6
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626
2727
.btn-paste {
2828
color: #222;
29+
2930
&:hover {
3031
color: #22222255;
3132
}
3233
}
3334
3435
.btn-delete {
3536
color: #a00;
37+
3638
&:hover {
3739
color: #e00;
3840
}
@@ -113,6 +115,10 @@ const getMarkdownString = () => {
113115
114116
const onDragStart = (event: DragEvent) => {
115117
if (props.showPaste) {
118+
const ghostImage = event.target as HTMLImageElement;
119+
const horizPos = ghostImage.width / 2;
120+
const vertPos = ghostImage.height / 2;
121+
event.dataTransfer?.setDragImage(ghostImage, horizPos, vertPos);
116122
event.dataTransfer?.setData("text/markdown", getMarkdownString());
117123
}
118124
};

client/src/views/PostFormView.vue

+17-7
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<div class="form-floating mb-3">
4646
<textarea
4747
v-model="form.markdown"
48-
class="form-control"
48+
class="form-control md-area"
4949
placeholder="Blogpost"
5050
ref="markdownArea"
5151
style="height: 40vh; min-height: 200px"
@@ -422,11 +422,22 @@ const dropMarkdown = (evt: DragEvent) => {
422422
const textArea = evt.target as HTMLTextAreaElement;
423423
if (items && textArea) {
424424
for (const item of items) {
425-
if (item.kind === "string" && item.type === "text/markdown") {
426-
evt.preventDefault();
427-
item.getAsString((markdown) => {
428-
form.markdown = insertIntoTextarea(markdown, textArea, "beforeCursor");
429-
});
425+
// evt.preventDefault();
426+
// We cannot use preventDefault(), because we will be unable to get the cursor position to drop to.
427+
// instead we have to pase everything and remove the base64 string afterwards
428+
if (item.kind === "string") {
429+
if (item.type === "text/markdown") {
430+
item.getAsString((markdown_img_link) => {
431+
form.markdown = insertIntoTextarea(markdown_img_link, textArea, "beforeCursor");
432+
});
433+
} else {
434+
// Remove base64 string from drop events default behaviour
435+
item.getAsString((str) => {
436+
setTimeout(() => {
437+
form.markdown = form.markdown.replace(str, "");
438+
}, 0);
439+
});
440+
}
430441
}
431442
}
432443
}
@@ -549,7 +560,6 @@ const insertIntoTextarea = (
549560
const text = area.value;
550561
const before = text.substring(0, insertPosition === "afterCursor" ? end : start);
551562
const after = text.substring(insertPosition === "beforeCursor" ? start : end);
552-
553563
return before + insertedText + after;
554564
};
555565

0 commit comments

Comments
 (0)