// tslint:disable:react-a11y-anchors import React from 'react'; import * as GoogleChrome from '../util/GoogleChrome'; import { AttachmentType } from './conversation/types'; import { Localizer } from '../types/Util'; interface Props { attachment: AttachmentType; i18n: Localizer; url: string; caption?: string; onChangeCaption?: (caption: string) => void; close?: () => void; } export class CaptionEditor extends React.Component { private handleKeyUpBound: () => void; private setFocusBound: () => void; private captureRefBound: () => void; private inputRef: React.Ref | null; constructor(props: Props) { super(props); this.handleKeyUpBound = this.handleKeyUp.bind(this); this.setFocusBound = this.setFocus.bind(this); this.captureRefBound = this.captureRef.bind(this); this.inputRef = null; } public handleKeyUp(event: React.KeyboardEvent) { const { close } = this.props; if (close && (event.key === 'Escape' || event.key === 'Enter')) { close(); } } public setFocus() { if (this.inputRef) { // @ts-ignore this.inputRef.focus(); } } public captureRef(ref: React.Ref) { this.inputRef = ref; // Forcing focus after a delay due to some focus contention with ConversationView setTimeout(() => { this.setFocus(); }, 200); } public renderObject() { const { url, i18n, attachment } = this.props; const { contentType } = attachment || { contentType: null }; const isImageTypeSupported = GoogleChrome.isImageTypeSupported(contentType); if (isImageTypeSupported) { return ( {i18n('imageAttachmentAlt')} ); } const isVideoTypeSupported = GoogleChrome.isVideoTypeSupported(contentType); if (isVideoTypeSupported) { return ( ); } return
; } public render() { const { caption, i18n, close, onChangeCaption } = this.props; return (
{this.renderObject()}
{ if (onChangeCaption) { onChangeCaption(event.target.value); } }} />
); } }