Skip to content

Commit

Permalink
VideoPress: clean video attributes that are not options when replacin…
Browse files Browse the repository at this point in the history
…g the video file (#28249)

* clean attrs that are not options when replacing

* changelog

* fix setting defauls attribute values issue

* replace block when replacing with a new file
  • Loading branch information
retrofox authored Jan 10, 2023
1 parent 34028b2 commit 18103d2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

VideoPress: clean video attributes that are not options when replacing the video file
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/**
* WordPress dependencies
*/

import { store as blockEditorStore } from '@wordpress/block-editor';
import {
BlockIcon,
useBlockProps,
InspectorControls,
BlockControls,
} from '@wordpress/block-editor';
import { createBlock } from '@wordpress/blocks';
import { Spinner, Placeholder, Button, withNotices } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect, useDispatch } from '@wordpress/data';
Expand All @@ -32,13 +33,14 @@ import TracksControl from './components/tracks-control';
import VideoPressPlayer from './components/videopress-player';
import VideoPressUploader from './components/videopress-uploader';
import { description, title } from '.';
import './editor.scss';
/**
* Types
*/
import type { VideoBlockAttributes } from './types';
import type React from 'react';

import './editor.scss';

const debug = debugFactory( 'videopress:video:edit' );

const VIDEO_PREVIEW_ATTEMPTS_LIMIT = 10;
Expand Down Expand Up @@ -334,6 +336,8 @@ export default function VideoPressEdit( {
const [ isUploadingFile, setIsUploadingFile ] = useState( ! guid );
const [ fileToUpload, setFileToUpload ] = useState( null );

const { replaceBlock } = useDispatch( blockEditorStore );

// Replace video state
const [ isReplacingFile, setIsReplacingFile ] = useState< {
isReplacing: boolean;
Expand Down Expand Up @@ -370,6 +374,10 @@ export default function VideoPressEdit( {
if ( isUploadingFile ) {
const handleDoneUpload = () => {
setIsUploadingFile( false );
if ( isReplacingFile.isReplacing ) {
setIsReplacingFile( { isReplacing: false, prevAttrs: {} } );
replaceBlock( clientId, createBlock( 'videopress/video', { ...attributes } ) );
}
};

return (
Expand Down Expand Up @@ -458,13 +466,17 @@ export default function VideoPressEdit( {
setAttributes={ setAttributes }
attributes={ attributes }
onUploadFileStart={ media => {
// Store current attributes in case we wanto to cancel the replacing.
setIsReplacingFile( {
isReplacing: true,
prevAttrs: attributes,
} );

setAttributes( { id: null, guid: null } );
setIsUploadingFile( true );

// Clean relevant attributes to show the uploader placeholder.
setAttributes( { id: null, guid: null, cacheHtml: '', videoRatio: null } );

// Pass, through local state, the new file to upload.
setFileToUpload( media );
} }
onSelectVideoFromLibrary={ media => {
Expand Down

0 comments on commit 18103d2

Please sign in to comment.