Allow pasting images into composition box as attachments (#1616)

* Allow pasting images into composition box as attachments

* Fix linter errors

* Fix typo
pull/1622/head
shellhazard 4 years ago committed by GitHub
parent e6128fa5a7
commit 442b881438
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -153,12 +153,19 @@ export class SessionCompositionBox extends React.Component<Props, State> {
public componentDidMount() {
setTimeout(this.focusCompositionBox, 100);
const div = this.container;
div?.addEventListener('paste', this.handlePaste);
}
public componentWillUnmount() {
this.abortLinkPreviewFetch();
this.linkPreviewAbortController = undefined;
const div = this.container;
div?.removeEventListener('paste', this.handlePaste);
}
public componentDidUpdate(prevProps: Props, _prevState: State) {
// reset the state on new conversation key
if (prevProps.selectedConversationKey !== this.props.selectedConversationKey) {
@ -193,6 +200,24 @@ export class SessionCompositionBox extends React.Component<Props, State> {
this.hideEmojiPanel();
}
private handlePaste(e: any) {
const { items } = e.clipboardData;
let imgBlob = null;
for (const item of items) {
if (item.type.split('/')[0] === 'image') {
imgBlob = item.getAsFile();
}
}
if (imgBlob !== null) {
const file = imgBlob;
window.log.info('Adding attachment from clipboard', file);
this.props.onChoseAttachments([file]);
e.preventDefault();
e.stopPropagation();
}
}
private showEmojiPanel() {
document.addEventListener('mousedown', this.handleClick, false);

Loading…
Cancel
Save