SHARE
TWEET

Untitled

a guest Mar 20th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as React from 'react';
  2. import { Modal } from 'react-bootstrap';
  3.  
  4. import { Attachment } from 'src/contrib/aidbox';
  5.  
  6. interface Props {}
  7. interface State {
  8.     previewModal: null | { url: string; isFile: boolean };
  9. }
  10.  
  11. export class AttachmentPreviewModal extends React.Component<Props, State> {
  12.     constructor(props: Props) {
  13.         super(props);
  14.  
  15.         this.state = { previewModal: null };
  16.     }
  17.  
  18.     public showPreview(attachment: File | Attachment) {
  19.         if (attachment instanceof File) {
  20.             this.setState({ previewModal: { url: URL.createObjectURL(attachment), isFile: true } });
  21.         } else {
  22.             this.setState({ previewModal: { url: attachment.url, isFile: false } });
  23.         }
  24.     }
  25.  
  26.     public render() {
  27.         const { previewModal } = this.state;
  28.  
  29.         return (
  30.             <Modal
  31.                 size="lg"
  32.                 show={!!previewModal}
  33.                 onHide={() => {
  34.                     if (previewModal!.isFile) {
  35.                         URL.revokeObjectURL(previewModal!.url);
  36.                     }
  37.                     this.setState({ previewModal: null });
  38.                 }}
  39.                 centered
  40.             >
  41.                 {previewModal && (
  42.                     <Modal.Body>
  43.                         <img src={previewModal!.url} width="100%" />
  44.                     </Modal.Body>
  45.                 )}
  46.             </Modal>
  47.         );
  48.     }
  49. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top