UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 12.6 kB
{ "version": 3, "sources": ["../../../src/components/provider/index.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useDispatch } from '@wordpress/data';\nimport { useEffect, useMemo, useRef } from '@wordpress/element';\nimport { SlotFillProvider } from '@wordpress/components';\nimport {\n\tMediaUploadProvider,\n\tstore as uploadStore,\n\tdetectClientSideMediaSupport,\n} from '@wordpress/upload-media';\n\n/**\n * Internal dependencies\n */\nimport withRegistryProvider from './with-registry-provider';\nimport useBlockSync from './use-block-sync';\nimport { store as blockEditorStore } from '../../store';\nimport { BlockRefsProvider } from './block-refs-provider';\nimport { unlock } from '../../lock-unlock';\nimport KeyboardShortcuts from '../keyboard-shortcuts';\nimport useMediaUploadSettings from './use-media-upload-settings';\nimport { mediaUploadOnSuccessKey } from '../../store/private-keys';\nimport { SelectionContext } from './selection-context';\n\n/** @typedef {import('@wordpress/data').WPDataRegistry} WPDataRegistry */\n\nconst noop = () => {};\n\n/**\n * Flag to track if we've already logged the fallback message.\n */\nlet hasLoggedFallback = false;\n\n/**\n * Cached result of whether client-side media processing should be enabled.\n * This is computed once per session for efficiency and stability.\n */\nlet isClientSideMediaEnabledCache = null;\n\n/**\n * Checks if client-side media processing should be enabled.\n *\n * Returns true only if:\n * 1. The client-side media processing flag is enabled\n * 2. The browser supports WebAssembly, SharedArrayBuffer, cross-origin isolation, and CSP allows blob workers\n *\n * The result is cached for the session to ensure stability during React renders.\n *\n * @return {boolean} Whether client-side media processing should be enabled.\n */\nfunction shouldEnableClientSideMediaProcessing() {\n\t// Return cached result if available.\n\tif ( isClientSideMediaEnabledCache !== null ) {\n\t\treturn isClientSideMediaEnabledCache;\n\t}\n\n\t// Check if the client-side media processing flag is enabled first.\n\tif ( ! window.__clientSideMediaProcessing ) {\n\t\tisClientSideMediaEnabledCache = false;\n\t\treturn false;\n\t}\n\n\t// Safety check in case the import is unavailable.\n\tif ( typeof detectClientSideMediaSupport !== 'function' ) {\n\t\tisClientSideMediaEnabledCache = false;\n\t\treturn false;\n\t}\n\n\tconst detection = detectClientSideMediaSupport();\n\tif ( ! detection || ! detection.supported ) {\n\t\t// Only log once per session to avoid console spam.\n\t\tif ( ! hasLoggedFallback ) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.info(\n\t\t\t\t`Client-side media processing unavailable: ${ detection.reason }. Using server-side processing.`\n\t\t\t);\n\t\t\thasLoggedFallback = true;\n\t\t}\n\t\tisClientSideMediaEnabledCache = false;\n\t\treturn false;\n\t}\n\n\tisClientSideMediaEnabledCache = true;\n\treturn true;\n}\n\n/**\n * Upload a media file when the file upload button is activated\n * or when adding a file to the editor via drag & drop.\n *\n * @param {WPDataRegistry} registry\n * @param {Object} settings Block editor settings.\n * @param {Object} $3 Parameters object passed to the function.\n * @param {Array} $3.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.\n * @param {Object} $3.additionalData Additional data to include in the request.\n * @param {Array<File>} $3.filesList List of files.\n * @param {Function} $3.onError Function called when an error happens.\n * @param {Function} $3.onFileChange Function called each time a file or a temporary representation of the file is available.\n * @param {Function} $3.onSuccess Function called once a file has completely finished uploading, including thumbnails.\n * @param {Function} $3.onBatchSuccess Function called once all files in a group have completely finished uploading, including thumbnails.\n */\nfunction mediaUpload(\n\tregistry,\n\tsettings,\n\t{\n\t\tallowedTypes,\n\t\tadditionalData = {},\n\t\tfilesList,\n\t\tonError = noop,\n\t\tonFileChange,\n\t\tonSuccess,\n\t\tonBatchSuccess,\n\t}\n) {\n\tvoid registry.dispatch( uploadStore ).addItems( {\n\t\tfiles: Array.from( filesList ),\n\t\tonChange: onFileChange,\n\t\tonSuccess: ( attachments ) => {\n\t\t\tsettings?.[ mediaUploadOnSuccessKey ]?.( attachments );\n\t\t\tonSuccess?.( attachments );\n\t\t},\n\t\tonBatchSuccess,\n\t\tonError: ( error ) =>\n\t\t\tonError( typeof error === 'string' ? error : error?.message ?? '' ),\n\t\tadditionalData,\n\t\tallowedTypes,\n\t} );\n}\n\n/**\n * Calls useBlockSync as a child of SelectionContext.Provider so that the\n * hook can read selection state from the context provided by this tree\n * rather than from a parent provider (which may not exist for the root).\n *\n * @param {Object} props Props forwarded to useBlockSync.\n */\nfunction BlockSyncEffect( props ) {\n\tuseBlockSync( props );\n\treturn null;\n}\n\nexport const ExperimentalBlockEditorProvider = withRegistryProvider(\n\t( props ) => {\n\t\tconst {\n\t\t\tsettings: _settings,\n\t\t\tregistry,\n\t\t\tstripExperimentalSettings = false,\n\t\t} = props;\n\n\t\tconst mediaUploadSettings = useMediaUploadSettings( _settings );\n\n\t\tconst isClientSideMediaEnabled =\n\t\t\tshouldEnableClientSideMediaProcessing();\n\n\t\t// Nested providers (e.g. from useBlockPreview) inherit settings\n\t\t// where mediaUpload has already been replaced with the\n\t\t// interceptor. Detect this so we skip the replacement and\n\t\t// MediaUploadProvider for them \u2014 see the longer comment below.\n\t\tconst isMediaUploadIntercepted =\n\t\t\t!! _settings?.mediaUpload?.__isMediaUploadInterceptor;\n\n\t\tconst settings = useMemo( () => {\n\t\t\tif (\n\t\t\t\tisClientSideMediaEnabled &&\n\t\t\t\t_settings?.mediaUpload &&\n\t\t\t\t! isMediaUploadIntercepted\n\t\t\t) {\n\t\t\t\t// Create a new object so that the original props.settings.mediaUpload is not modified.\n\t\t\t\tconst interceptor = mediaUpload.bind(\n\t\t\t\t\tnull,\n\t\t\t\t\tregistry,\n\t\t\t\t\t_settings\n\t\t\t\t);\n\t\t\t\tinterceptor.__isMediaUploadInterceptor = true;\n\t\t\t\treturn {\n\t\t\t\t\t..._settings,\n\t\t\t\t\tmediaUpload: interceptor,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn _settings;\n\t\t}, [\n\t\t\t_settings,\n\t\t\tregistry,\n\t\t\tisClientSideMediaEnabled,\n\t\t\tisMediaUploadIntercepted,\n\t\t] );\n\n\t\tconst { __experimentalUpdateSettings } = unlock(\n\t\t\tuseDispatch( blockEditorStore )\n\t\t);\n\t\tuseEffect( () => {\n\t\t\t__experimentalUpdateSettings(\n\t\t\t\t{\n\t\t\t\t\t...settings,\n\t\t\t\t\t__internalIsInitialized: true,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tstripExperimentalSettings,\n\t\t\t\t\treset: true,\n\t\t\t\t}\n\t\t\t);\n\t\t}, [\n\t\t\tsettings,\n\t\t\tstripExperimentalSettings,\n\t\t\t__experimentalUpdateSettings,\n\t\t] );\n\n\t\t// Store selection and onChangeSelection in refs and expose\n\t\t// stable getters/callers so that the context value is a\n\t\t// complete constant. This prevents re-rendering the entire\n\t\t// block tree (including async-rendered off-screen blocks)\n\t\t// when either value changes.\n\t\tconst selectionRef = useRef( props.selection );\n\t\tselectionRef.current = props.selection;\n\t\tconst onChangeSelectionRef = useRef( props.onChangeSelection ?? noop );\n\t\tonChangeSelectionRef.current = props.onChangeSelection ?? noop;\n\n\t\tconst selectionContextValue = useMemo(\n\t\t\t() => ( {\n\t\t\t\tgetSelection: () => selectionRef.current,\n\t\t\t\tonChangeSelection: ( ...args ) =>\n\t\t\t\t\tonChangeSelectionRef.current( ...args ),\n\t\t\t} ),\n\t\t\t[]\n\t\t);\n\n\t\tconst children = (\n\t\t\t<SlotFillProvider passthrough>\n\t\t\t\t{ ! settings?.isPreviewMode && <KeyboardShortcuts.Register /> }\n\t\t\t\t<BlockRefsProvider>{ props.children }</BlockRefsProvider>\n\t\t\t</SlotFillProvider>\n\t\t);\n\n\t\tconst content = (\n\t\t\t<SelectionContext.Provider value={ selectionContextValue }>\n\t\t\t\t<BlockSyncEffect\n\t\t\t\t\tclientId={ props.clientId }\n\t\t\t\t\tvalue={ props.value }\n\t\t\t\t\tonChange={ props.onChange }\n\t\t\t\t\tonInput={ props.onInput }\n\t\t\t\t/>\n\t\t\t\t{ children }\n\t\t\t</SelectionContext.Provider>\n\t\t);\n\n\t\t// MediaUploadProvider writes the mediaUpload function from\n\t\t// _settings into the shared upload-media store so the store can\n\t\t// hand files off to the server. useMediaUploadSettings extracts\n\t\t// mediaUpload from the original _settings prop \u2014 *before* the\n\t\t// interceptor replacement above \u2014 so the store receives the\n\t\t// real server-side upload function.\n\t\t//\n\t\t// Only the first (outermost) provider should do this.\n\t\t// Nested providers (e.g. from useBlockPreview in\n\t\t// core/post-template) inherit settings that already contain\n\t\t// the interceptor, so their MediaUploadProvider would\n\t\t// overwrite the store's server-side function with the\n\t\t// interceptor, causing uploads to loop instead of reaching\n\t\t// the server.\n\t\tif ( isClientSideMediaEnabled && ! isMediaUploadIntercepted ) {\n\t\t\treturn (\n\t\t\t\t<MediaUploadProvider\n\t\t\t\t\tsettings={ mediaUploadSettings }\n\t\t\t\t\tuseSubRegistry={ false }\n\t\t\t\t>\n\t\t\t\t\t{ content }\n\t\t\t\t</MediaUploadProvider>\n\t\t\t);\n\t\t}\n\n\t\treturn content;\n\t}\n);\n\nexport const BlockEditorProvider = ( props ) => {\n\treturn (\n\t\t<ExperimentalBlockEditorProvider { ...props } stripExperimentalSettings>\n\t\t\t{ props.children }\n\t\t</ExperimentalBlockEditorProvider>\n\t);\n};\n\nexport default BlockEditorProvider;\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA4B;AAC5B,qBAA2C;AAC3C,wBAAiC;AACjC,0BAIO;AAKP,oCAAiC;AACjC,4BAAyB;AACzB,mBAA0C;AAC1C,iCAAkC;AAClC,yBAAuB;AACvB,gCAA8B;AAC9B,uCAAmC;AACnC,0BAAwC;AACxC,+BAAiC;AA6M9B;AAzMH,IAAM,OAAO,MAAM;AAAC;AAKpB,IAAI,oBAAoB;AAMxB,IAAI,gCAAgC;AAapC,SAAS,wCAAwC;AAEhD,MAAK,kCAAkC,MAAO;AAC7C,WAAO;AAAA,EACR;AAGA,MAAK,CAAE,OAAO,6BAA8B;AAC3C,oCAAgC;AAChC,WAAO;AAAA,EACR;AAGA,MAAK,OAAO,qDAAiC,YAAa;AAC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