Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #131 from hyperspacedev/copy-paste-image
Browse files Browse the repository at this point in the history
[HD-4] Add copy and paste support for images to composer
  • Loading branch information
alicerunsonfedora authored Nov 24, 2019
2 parents ee865cd + 8b1142f commit 14a06dd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/components/AudioPlayer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import AudioPlayer from "./AudioPlayer";

export default AudioPlayer;
export default AudioPlayer;
79 changes: 50 additions & 29 deletions src/pages/Compose.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ class Composer extends Component<any, IComposerState> {
: 99999999
});
});

window.addEventListener("paste", (evt: Event) => {
let thePasteEvent = evt as ClipboardEvent;
let fileList: File[] = [];
if (thePasteEvent.clipboardData != null) {
let clipitems = thePasteEvent.clipboardData.items;
if (clipitems != undefined) {
for (let i = 0; i < clipitems.length; i++) {
if (clipitems[i].type.indexOf("image") != -1) {
let clipfile = clipitems[i].getAsFile();
if (clipfile != null) {
fileList.push(clipfile);
}
}
}
this.actuallyUploadMedia(fileList);
}
}
});
}

componentWillReceiveProps(props: any) {
Expand Down Expand Up @@ -176,35 +195,7 @@ class Composer extends Component<any, IComposerState> {
multiple: false,
accept: ".jpeg,.jpg,.png,.gif,.webm,.mp4,.mov,.ogg,.wav,.mp3,.flac"
})
.then((media: FileList) => {
let mediaForm = new FormData();
mediaForm.append("file", media[0]);
this.props.enqueueSnackbar("Uploading media...", {
persist: true,
key: "media-upload"
});
this.client
.post("/media", mediaForm)
.then((resp: any) => {
let attachment: Attachment = resp.data;
let attachments = this.state.attachments;
if (attachments) {
attachments.push(attachment);
} else {
attachments = [attachment];
}
this.setState({ attachments });
this.props.closeSnackbar("media-upload");
this.props.enqueueSnackbar("Media uploaded.");
})
.catch((err: Error) => {
this.props.closeSnackbar("media-upload");
this.props.enqueueSnackbar(
"Couldn't upload media: " + err.name,
{ variant: "error" }
);
});
})
.then((media: FileList) => this.actuallyUploadMedia(media))
.catch((err: Error) => {
this.props.enqueueSnackbar("Couldn't get media: " + err.name, {
variant: "error"
Expand All @@ -213,6 +204,36 @@ class Composer extends Component<any, IComposerState> {
});
}

actuallyUploadMedia(media: FileList | File[]) {
let mediaForm = new FormData();
mediaForm.append("file", media[0]);
this.props.enqueueSnackbar("Uploading media...", {
persist: true,
key: "media-upload"
});
this.client
.post("/media", mediaForm)
.then((resp: any) => {
let attachment: Attachment = resp.data;
let attachments = this.state.attachments;
if (attachments) {
attachments.push(attachment);
} else {
attachments = [attachment];
}
this.setState({ attachments });
this.props.closeSnackbar("media-upload");
this.props.enqueueSnackbar("Media uploaded.");
})
.catch((err: Error) => {
this.props.closeSnackbar("media-upload");
this.props.enqueueSnackbar(
"Couldn't upload media: " + err.name,
{ variant: "error" }
);
});
}

getOnlyMediaIds() {
let ids: string[] = [];
if (this.state.attachments) {
Expand Down

0 comments on commit 14a06dd

Please sign in to comment.