]> Untitled Git - lemmy.git/commitdiff
Allow pasting images into comment boxes and post url box. Fixes #472
authorDessalines <tyhou13@gmx.com>
Tue, 28 Jan 2020 02:59:38 +0000 (21:59 -0500)
committerDessalines <tyhou13@gmx.com>
Tue, 28 Jan 2020 02:59:38 +0000 (21:59 -0500)
ui/src/components/comment-form.tsx
ui/src/components/post-form.tsx

index e4543d6653dee05ca742ad6375cde71e8b6d7862..7eb30f502b302233d58cdc5fbbc54922210ddbb0 100644 (file)
@@ -96,6 +96,7 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
                 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<CommentFormProps, CommentFormState> {
     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);
index ebc7d7b7656e95f773c8b84df1bc24fe27882371..57d9a9646dfc546b3231f352febb992217ba9f5c 100644 (file)
@@ -160,6 +160,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
                 class="form-control"
                 value={this.state.postForm.url}
                 onInput={linkEvent(this, this.handlePostUrlChange)}
+                onPaste={linkEvent(this, this.handleImageUploadPaste)}
               />
               {this.state.suggestedTitle && (
                 <div
@@ -442,9 +443,22 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
     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);