// 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; onSave?: (caption: string) => void; close?: () => void; } interface State { caption: string; } export class CaptionEditor extends React.Component { private handleKeyUpBound: ( event: React.KeyboardEvent ) => void; private setFocusBound: () => void; // TypeScript doesn't like our React.Ref typing here, so we omit it private captureRefBound: () => void; private onChangeBound: () => void; private onSaveBound: () => void; private inputRef: React.Ref | null; constructor(props: Props) { super(props); const { caption } = props; this.state = { caption: caption || '', }; this.handleKeyUpBound = this.handleKeyUp.bind(this); this.setFocusBound = this.setFocus.bind(this); this.captureRefBound = this.captureRef.bind(this); this.onChangeBound = this.onChange.bind(this); this.onSaveBound = this.onSave.bind(this); this.inputRef = null; } public handleKeyUp(event: React.KeyboardEvent) { const { close, onSave } = this.props; if (close && event.key === 'Escape') { close(); } if (onSave && event.key === 'Enter') { const { caption } = this.state; onSave(caption); } } 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 onSave() { const { onSave } = this.props; const { caption } = this.state; if (onSave) { onSave(caption); } } public onChange(event: React.FormEvent) { // @ts-ignore const { value } = event.target; this.setState({ caption: value, }); } 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 { i18n, close } = this.props; const { caption } = this.state; return (
{this.renderObject()}
{caption ? (
{i18n('save')}
) : null}
); } }