From 68a9d5f915d4779ab2ac68f14b92b03bd134882e Mon Sep 17 00:00:00 2001 From: Donna Peplinskie Date: Tue, 28 Feb 2017 15:09:07 -0500 Subject: [PATCH] Update Redux state when video has loaded --- .../detail/detail-preview-videopress.jsx | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/client/post-editor/media-modal/detail/detail-preview-videopress.jsx b/client/post-editor/media-modal/detail/detail-preview-videopress.jsx index 54e06ce0f98b9..4332198cf95f2 100644 --- a/client/post-editor/media-modal/detail/detail-preview-videopress.jsx +++ b/client/post-editor/media-modal/detail/detail-preview-videopress.jsx @@ -13,6 +13,7 @@ import debug from 'debug'; import { loadScript, removeScriptCallback } from 'lib/load-script'; import { setVideoEditorHasScriptLoadError, + setVideoEditorVideoHasLoaded, } from 'state/ui/editor/video-editor/actions'; /** @@ -36,6 +37,7 @@ class EditorMediaModalDetailPreviewVideoPress extends Component { componentDidMount() { this.loadInitializeScript(); + window.addEventListener( 'message', this.receiveMessage, false ); } componentWillUnmount() { @@ -93,7 +95,27 @@ class EditorMediaModalDetailPreviewVideoPress extends Component { } }; + receiveMessage = ( event ) => { + if ( event.origin && event.origin !== location.origin ) { + return; + } + + const data = 'string' === typeof event.data + ? JSON.parse( event.data ) + : event.data; + + if ( ! data || 'videopress_loading_state' !== data.event || ! ( 'state' in data ) ) { + return; + } + + if ( 'loaded' === data.state ) { + this.props.setVideoHasLoaded(); + } + } + destroy() { + window.removeEventListener( 'message', this.receiveMessage ); + if ( ! this.player ) { return; } @@ -140,5 +162,6 @@ export default connect( null, { setHasScriptLoadError: setVideoEditorHasScriptLoadError, + setVideoHasLoaded: setVideoEditorVideoHasLoaded, } )( EditorMediaModalDetailPreviewVideoPress );