From beb55a471f2dfbebe79cf2f79d8582bc04929a8d Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 27 Jan 2020 21:59:38 -0500 Subject: [PATCH] Allow pasting images into comment boxes and post url box. Fixes #472 --- ui/src/components/comment-form.tsx | 18 ++++++++++++++++-- ui/src/components/post-form.tsx | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/ui/src/components/comment-form.tsx b/ui/src/components/comment-form.tsx index e4543d66..7eb30f50 100644 --- a/ui/src/components/comment-form.tsx +++ b/ui/src/components/comment-form.tsx @@ -96,6 +96,7 @@ export class CommentForm extends Component { className={`form-control ${this.state.previewMode && 'd-none'}`} value={this.state.commentForm.content} onInput={linkEvent(this, this.handleCommentContentChange)} + onPaste={linkEvent(this, this.handleImageUploadPaste)} required disabled={this.props.disabled} rows={2} @@ -208,9 +209,22 @@ export class CommentForm extends Component { i.props.onReplyCancel(); } + handleImageUploadPaste(i: CommentForm, event: any) { + let image = event.clipboardData.files[0]; + if (image) { + i.handleImageUpload(i, image); + } + } + handleImageUpload(i: CommentForm, event: any) { - event.preventDefault(); - let file = event.target.files[0]; + let file: any; + if (event.target) { + event.preventDefault(); + file = event.target.files[0]; + } else { + file = event; + } + const imageUploadUrl = `/pictshare/api/upload.php`; const formData = new FormData(); formData.append('file', file); diff --git a/ui/src/components/post-form.tsx b/ui/src/components/post-form.tsx index ebc7d7b7..57d9a964 100644 --- a/ui/src/components/post-form.tsx +++ b/ui/src/components/post-form.tsx @@ -160,6 +160,7 @@ export class PostForm extends Component { class="form-control" value={this.state.postForm.url} onInput={linkEvent(this, this.handlePostUrlChange)} + onPaste={linkEvent(this, this.handleImageUploadPaste)} /> {this.state.suggestedTitle && (
{ i.setState(i.state); } + handleImageUploadPaste(i: PostForm, event: any) { + let image = event.clipboardData.files[0]; + if (image) { + i.handleImageUpload(i, image); + } + } + handleImageUpload(i: PostForm, event: any) { - event.preventDefault(); - let file = event.target.files[0]; + let file: any; + if (event.target) { + event.preventDefault(); + file = event.target.files[0]; + } else { + file = event; + } + const imageUploadUrl = `/pictshare/api/upload.php`; const formData = new FormData(); formData.append('file', file);