Skip to content

Commit

Permalink
image drop onto textarea
Browse files Browse the repository at this point in the history
  • Loading branch information
thooyork committed Jun 14, 2024
1 parent 1a01adc commit bf90bba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 6 additions & 0 deletions client/src/components/ImagePreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
.btn-paste {
color: #222;
&:hover {
color: #22222255;
}
}
.btn-delete {
color: #a00;
&:hover {
color: #e00;
}
Expand Down Expand Up @@ -113,6 +115,10 @@ const getMarkdownString = () => {
const onDragStart = (event: DragEvent) => {
if (props.showPaste) {
const ghostImage = event.target as HTMLImageElement;
const horizPos = ghostImage.width / 2;
const vertPos = ghostImage.height / 2;
event.dataTransfer?.setDragImage(ghostImage, horizPos, vertPos);
event.dataTransfer?.setData("text/markdown", getMarkdownString());
}
};
Expand Down
24 changes: 17 additions & 7 deletions client/src/views/PostFormView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<div class="form-floating mb-3">
<textarea
v-model="form.markdown"
class="form-control"
class="form-control md-area"
placeholder="Blogpost"
ref="markdownArea"
style="height: 40vh; min-height: 200px"
Expand Down Expand Up @@ -422,11 +422,22 @@ const dropMarkdown = (evt: DragEvent) => {
const textArea = evt.target as HTMLTextAreaElement;
if (items && textArea) {
for (const item of items) {
if (item.kind === "string" && item.type === "text/markdown") {
evt.preventDefault();
item.getAsString((markdown) => {
form.markdown = insertIntoTextarea(markdown, textArea, "beforeCursor");
});
// evt.preventDefault();
// We cannot use preventDefault(), because we will be unable to get the cursor position to drop to.
// instead we have to pase everything and remove the base64 string afterwards
if (item.kind === "string") {
if (item.type === "text/markdown") {
item.getAsString((markdown_img_link) => {
form.markdown = insertIntoTextarea(markdown_img_link, textArea, "beforeCursor");
});
} else {
// Remove base64 string from drop events default behaviour
item.getAsString((str) => {
setTimeout(() => {
form.markdown = form.markdown.replace(str, "");
}, 0);
});
}
}
}
}
Expand Down Expand Up @@ -549,7 +560,6 @@ const insertIntoTextarea = (
const text = area.value;
const before = text.substring(0, insertPosition === "afterCursor" ? end : start);
const after = text.substring(insertPosition === "beforeCursor" ? start : end);
return before + insertedText + after;
};
Expand Down

0 comments on commit bf90bba

Please sign in to comment.