@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 13.4 kB
Source Map (JSON)
{"version":3,"file":"edit.cjs","names":["__","_x","useState","useBlockEditingMode","createUpgradedEmbedBlock","useDispatch","noticesStore","isBlobURL","useBlockProps","useToolsPanelDropdownMenuProps","MediaPlaceholder","BlockIcon","icon","BlockControls","MediaReplaceFlow","InspectorControls","ToolsPanel","Text","ToolsPanelItem","ToggleControl","SelectControl","inertValue","WCSpinner","Caption"],"sources":["../../src/audio/edit.jsx"],"sourcesContent":["import clsx from 'clsx';\nimport { isBlobURL } from '@wordpress/blob';\nimport {\n\tSpinner as WCSpinner,\n\tToggleControl,\n\t__experimentalToolsPanel as ToolsPanel,\n\t__experimentalToolsPanelItem as ToolsPanelItem,\n} from '@wordpress/components';\nimport { inertValue } from '@wordpress/react-inert-value';\nimport { SelectControl, Text } from '@wordpress/ui';\nimport {\n\tBlockControls,\n\tBlockIcon,\n\tInspectorControls,\n\tMediaPlaceholder,\n\tMediaReplaceFlow,\n\tuseBlockProps,\n\tuseBlockEditingMode,\n} from '@wordpress/block-editor';\nimport { __, _x } from '@wordpress/i18n';\nimport { useDispatch } from '@wordpress/data';\nimport { audio as icon } from '@wordpress/icons';\nimport { store as noticesStore } from '@wordpress/notices';\nimport { useState } from '@wordpress/element';\nimport { createUpgradedEmbedBlock } from '../embed/util';\nimport {\n\tuseUploadMediaFromBlobURL,\n\tuseToolsPanelDropdownMenuProps,\n} from '../utils/hooks';\nimport { Caption } from '../utils/caption';\n\nconst ALLOWED_MEDIA_TYPES = [ 'audio' ];\n\nconst PRELOAD_ITEMS = [\n\t{ value: null, label: __( 'Browser default' ) },\n\t{ value: 'auto', label: __( 'Auto' ) },\n\t{ value: 'metadata', label: __( 'Metadata' ) },\n\t{ value: 'none', label: _x( 'None', 'Preload value' ) },\n];\n\n/**\n * Renders the `core/audio` block on the editor.\n *\n * @param {Object} props React props.\n * @param {Object} props.attributes Block attributes: `blob`, `src`, `caption`, `id`, `autoplay`, `loop` and `preload`.\n * @param {string} props.className Class name generated for the block.\n * @param {Function} props.setAttributes Callback for updating block attributes.\n * @param {Function} props.onReplace Callback for replacing this block with other blocks.\n * @param {boolean} props.isSelected Whether the block is the only selected block.\n * @param {Function} props.insertBlocksAfter Callback for inserting blocks after this one.\n *\n * @return {React.JSX.Element} React element.\n */\nfunction AudioEdit( {\n\tattributes,\n\tclassName,\n\tsetAttributes,\n\tonReplace,\n\tisSelected: isSingleSelected,\n\tinsertBlocksAfter,\n} ) {\n\tconst { id, autoplay, loop, preload, src } = attributes;\n\tconst [ temporaryURL, setTemporaryURL ] = useState( attributes.blob );\n\tconst blockEditingMode = useBlockEditingMode();\n\tconst hasNonContentControls = blockEditingMode === 'default';\n\n\tuseUploadMediaFromBlobURL( {\n\t\turl: temporaryURL,\n\t\tallowedTypes: ALLOWED_MEDIA_TYPES,\n\t\tonChange: onSelectAudio,\n\t\tonError: onUploadError,\n\t} );\n\n\tfunction toggleAttribute( attribute ) {\n\t\treturn ( newValue ) => {\n\t\t\tsetAttributes( { [ attribute ]: newValue } );\n\t\t};\n\t}\n\n\tfunction onSelectURL( newSrc ) {\n\t\t// Set the block's src from the edit component's state, and switch off\n\t\t// the editing UI.\n\t\tif ( newSrc !== src ) {\n\t\t\t// Check if there's an embed block that handles this URL.\n\t\t\tconst embedBlock = createUpgradedEmbedBlock( {\n\t\t\t\tattributes: { url: newSrc },\n\t\t\t} );\n\t\t\tif ( undefined !== embedBlock && onReplace ) {\n\t\t\t\tonReplace( embedBlock );\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tsetAttributes( { src: newSrc, id: undefined, blob: undefined } );\n\t\t\tsetTemporaryURL();\n\t\t}\n\t}\n\n\tconst { createErrorNotice } = useDispatch( noticesStore );\n\tfunction onUploadError( message ) {\n\t\tcreateErrorNotice( message, { type: 'snackbar' } );\n\t}\n\n\tfunction onSelectAudio( media ) {\n\t\tif ( ! media || ! media.url ) {\n\t\t\t// The upload failed. Only clear the temporary blob URL, so a failed\n\t\t\t// replacement leaves the current audio and its caption intact.\n\t\t\tsetAttributes( { blob: undefined } );\n\t\t\tsetTemporaryURL();\n\t\t\treturn;\n\t\t}\n\n\t\tif ( isBlobURL( media.url ) ) {\n\t\t\tsetTemporaryURL( media.url );\n\t\t\treturn;\n\t\t}\n\n\t\t// Sets the block's attribute and updates the edit component from the\n\t\t// selected media, then switches off the editing UI.\n\t\tsetAttributes( {\n\t\t\tblob: undefined,\n\t\t\tsrc: media.url,\n\t\t\tid: media.id,\n\t\t\tcaption: media.caption,\n\t\t} );\n\t\tsetTemporaryURL();\n\t}\n\n\tfunction onResetAudio() {\n\t\tsetAttributes( {\n\t\t\tsrc: undefined,\n\t\t\tid: undefined,\n\t\t\tcaption: undefined,\n\t\t\tblob: undefined,\n\t\t} );\n\t\tsetTemporaryURL();\n\t}\n\n\tconst classes = clsx( className, {\n\t\t'is-transient': !! temporaryURL,\n\t} );\n\n\tconst blockProps = useBlockProps( {\n\t\tclassName: classes,\n\t} );\n\tconst dropdownMenuProps = useToolsPanelDropdownMenuProps();\n\n\tif ( ! src && ! temporaryURL ) {\n\t\treturn (\n\t\t\t<div { ...blockProps }>\n\t\t\t\t<MediaPlaceholder\n\t\t\t\t\ticon={ <BlockIcon icon={ icon } /> }\n\t\t\t\t\tonSelect={ onSelectAudio }\n\t\t\t\t\tonSelectURL={ onSelectURL }\n\t\t\t\t\taccept=\"audio/*\"\n\t\t\t\t\tallowedTypes={ ALLOWED_MEDIA_TYPES }\n\t\t\t\t\tvalue={ attributes }\n\t\t\t\t\tonError={ onUploadError }\n\t\t\t\t/>\n\t\t\t</div>\n\t\t);\n\t}\n\n\treturn (\n\t\t<>\n\t\t\t{ isSingleSelected && (\n\t\t\t\t<BlockControls group=\"other\">\n\t\t\t\t\t<MediaReplaceFlow\n\t\t\t\t\t\tmediaId={ id }\n\t\t\t\t\t\tmediaURL={ src }\n\t\t\t\t\t\tallowedTypes={ ALLOWED_MEDIA_TYPES }\n\t\t\t\t\t\taccept=\"audio/*\"\n\t\t\t\t\t\tonSelect={ onSelectAudio }\n\t\t\t\t\t\tonSelectURL={ onSelectURL }\n\t\t\t\t\t\tonError={ onUploadError }\n\t\t\t\t\t\tonReset={ onResetAudio }\n\t\t\t\t\t\tvariant=\"toolbar\"\n\t\t\t\t\t/>\n\t\t\t\t</BlockControls>\n\t\t\t) }\n\t\t\t<InspectorControls>\n\t\t\t\t<ToolsPanel\n\t\t\t\t\tlabel={ __( 'Settings' ) }\n\t\t\t\t\tresetAll={ () => {\n\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\tautoplay: false,\n\t\t\t\t\t\t\tloop: false,\n\t\t\t\t\t\t\tpreload: undefined,\n\t\t\t\t\t\t} );\n\t\t\t\t\t} }\n\t\t\t\t\tdropdownMenuProps={ dropdownMenuProps }\n\t\t\t\t>\n\t\t\t\t\t<Text className=\"wp-block-audio__autoplay-help-text\">\n\t\t\t\t\t\t{ __(\n\t\t\t\t\t\t\t'Most browsers block audio until a visitor interacts with the page, so autoplay often won’t work, especially on mobile. Unexpected sound can also be disruptive for screen reader users.'\n\t\t\t\t\t\t) }\n\t\t\t\t\t</Text>\n\t\t\t\t\t<ToolsPanelItem\n\t\t\t\t\t\tlabel={ __( 'Autoplay' ) }\n\t\t\t\t\t\tisShownByDefault\n\t\t\t\t\t\thasValue={ () => !! autoplay }\n\t\t\t\t\t\tonDeselect={ () =>\n\t\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\t\tautoplay: false,\n\t\t\t\t\t\t\t} )\n\t\t\t\t\t\t}\n\t\t\t\t\t>\n\t\t\t\t\t\t<ToggleControl\n\t\t\t\t\t\t\tlabel={ __( 'Autoplay' ) }\n\t\t\t\t\t\t\tonChange={ toggleAttribute( 'autoplay' ) }\n\t\t\t\t\t\t\tchecked={ !! autoplay }\n\t\t\t\t\t\t/>\n\t\t\t\t\t</ToolsPanelItem>\n\t\t\t\t\t<ToolsPanelItem\n\t\t\t\t\t\tlabel={ __( 'Loop' ) }\n\t\t\t\t\t\tisShownByDefault\n\t\t\t\t\t\thasValue={ () => !! loop }\n\t\t\t\t\t\tonDeselect={ () =>\n\t\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\t\tloop: false,\n\t\t\t\t\t\t\t} )\n\t\t\t\t\t\t}\n\t\t\t\t\t>\n\t\t\t\t\t\t<ToggleControl\n\t\t\t\t\t\t\tlabel={ __( 'Loop' ) }\n\t\t\t\t\t\t\tonChange={ toggleAttribute( 'loop' ) }\n\t\t\t\t\t\t\tchecked={ !! loop }\n\t\t\t\t\t\t/>\n\t\t\t\t\t</ToolsPanelItem>\n\t\t\t\t\t<ToolsPanelItem\n\t\t\t\t\t\tlabel={ __( 'Preload' ) }\n\t\t\t\t\t\tisShownByDefault\n\t\t\t\t\t\thasValue={ () => !! preload }\n\t\t\t\t\t\tonDeselect={ () =>\n\t\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\t\tpreload: undefined,\n\t\t\t\t\t\t\t} )\n\t\t\t\t\t\t}\n\t\t\t\t\t>\n\t\t\t\t\t\t<SelectControl\n\t\t\t\t\t\t\tlabel={ _x(\n\t\t\t\t\t\t\t\t'Preload',\n\t\t\t\t\t\t\t\t'noun; Audio block parameter'\n\t\t\t\t\t\t\t) }\n\t\t\t\t\t\t\titems={ PRELOAD_ITEMS }\n\t\t\t\t\t\t\tvalue={\n\t\t\t\t\t\t\t\tPRELOAD_ITEMS.find(\n\t\t\t\t\t\t\t\t\t( item ) =>\n\t\t\t\t\t\t\t\t\t\titem.value === ( preload || null )\n\t\t\t\t\t\t\t\t) ?? PRELOAD_ITEMS[ 0 ]\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// `undefined` is required for the preload attribute to be unset.\n\t\t\t\t\t\t\tonValueChange={ ( item ) =>\n\t\t\t\t\t\t\t\titem &&\n\t\t\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\t\t\tpreload: item.value || undefined,\n\t\t\t\t\t\t\t\t} )\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t/>\n\t\t\t\t\t</ToolsPanelItem>\n\t\t\t\t</ToolsPanel>\n\t\t\t</InspectorControls>\n\t\t\t<figure { ...blockProps }>\n\t\t\t\t<audio\n\t\t\t\t\tcontrols=\"controls\"\n\t\t\t\t\tinert={ inertValue( ! isSingleSelected ) }\n\t\t\t\t\tsrc={ src ?? temporaryURL }\n\t\t\t\t/>\n\t\t\t\t{ !! temporaryURL && <WCSpinner /> }\n\t\t\t\t<Caption\n\t\t\t\t\tattributes={ attributes }\n\t\t\t\t\tsetAttributes={ setAttributes }\n\t\t\t\t\tisSelected={ isSingleSelected }\n\t\t\t\t\tinsertBlocksAfter={ insertBlocksAfter }\n\t\t\t\t\tlabel={ __( 'Audio caption text' ) }\n\t\t\t\t\tshowToolbarButton={\n\t\t\t\t\t\tisSingleSelected && hasNonContentControls\n\t\t\t\t\t}\n\t\t\t\t/>\n\t\t\t</figure>\n\t\t</>\n\t);\n}\n\nexport default AudioEdit;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,MAAM,sBAAsB,CAAE,OAAQ;AAEtC,MAAM,gBAAgB;CACrB;EAAE,OAAO;EAAM,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,iBAAkB;CAAE;CAC9C;EAAE,OAAO;EAAQ,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,MAAO;CAAE;CACrC;EAAE,OAAO;EAAY,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,UAAW;CAAE;CAC7C;EAAE,OAAO;EAAQ,QAAA,GAAOC,gBAAAA,GAAAA,CAAI,QAAQ,eAAgB;CAAE;AACvD;;;;;;;;;;;;;;AAeA,SAAS,UAAW,EACnB,YACA,WACA,eACA,WACA,YAAY,kBACZ,qBACG;CACH,MAAM,EAAE,IAAI,UAAU,MAAM,SAAS,QAAQ;CAC7C,MAAM,CAAE,cAAc,oBAAA,GAAoBC,mBAAAA,SAAA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