diff --git a/docs/manifest-devhub.json b/docs/manifest-devhub.json index bfdf2e267fef6..657fd0c83b0a5 100644 --- a/docs/manifest-devhub.json +++ b/docs/manifest-devhub.json @@ -1295,6 +1295,12 @@ "markdown_source": "../packages/list-reusable-blocks/README.md", "parent": "packages" }, + { + "title": "@wordpress/media-utils", + "slug": "packages-media-utils", + "markdown_source": "../packages/media-utils/README.md", + "parent": "packages" + }, { "title": "@wordpress/notices", "slug": "packages-notices", diff --git a/docs/manifest.json b/docs/manifest.json index 429fd5bc835ae..f997afe61b911 100644 --- a/docs/manifest.json +++ b/docs/manifest.json @@ -449,6 +449,12 @@ "markdown_source": "https://raw.githubusercontent.com/WordPress/gutenberg/master/packages/list-reusable-blocks/README.md", "parent": "packages" }, + { + "title": "@wordpress/media-utils", + "slug": "packages-media-utils", + "markdown_source": "https://raw.githubusercontent.com/WordPress/gutenberg/master/packages/media-utils/README.md", + "parent": "packages" + }, { "title": "@wordpress/notices", "slug": "packages-notices", @@ -1331,4 +1337,4 @@ "markdown_source": "https://raw.githubusercontent.com/WordPress/gutenberg/master/docs/contributors/outreach.md", "parent": "contributors" } -] \ No newline at end of file +] diff --git a/package-lock.json b/package-lock.json index 8d51cc11b6bb2..c38f286c365e1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3584,6 +3584,7 @@ "@wordpress/html-entities": "file:packages/html-entities", "@wordpress/i18n": "file:packages/i18n", "@wordpress/keycodes": "file:packages/keycodes", + "@wordpress/media-utils": "file:packages/media-utils", "@wordpress/notices": "file:packages/notices", "@wordpress/nux": "file:packages/nux", "@wordpress/url": "file:packages/url", @@ -3725,6 +3726,17 @@ "lodash": "^4.17.11" } }, + "@wordpress/media-utils": { + "version": "file:packages/media-utils", + "requires": { + "@babel/runtime": "^7.4.4", + "@wordpress/api-fetch": "file:packages/api-fetch", + "@wordpress/blob": "file:packages/blob", + "@wordpress/element": "file:packages/element", + "@wordpress/i18n": "file:packages/i18n", + "lodash": "^4.17.11" + } + }, "@wordpress/notices": { "version": "file:packages/notices", "requires": { diff --git a/package.json b/package.json index 78d57862f8c22..421a6d9580abd 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "@wordpress/is-shallow-equal": "file:packages/is-shallow-equal", "@wordpress/keycodes": "file:packages/keycodes", "@wordpress/list-reusable-blocks": "file:packages/list-reusable-blocks", + "@wordpress/media-utils": "file:packages/media-utils", "@wordpress/notices": "file:packages/notices", "@wordpress/nux": "file:packages/nux", "@wordpress/plugins": "file:packages/plugins", diff --git a/packages/edit-post/src/hooks/components/index.js b/packages/edit-post/src/hooks/components/index.js deleted file mode 100644 index 20f16a7461def..0000000000000 --- a/packages/edit-post/src/hooks/components/index.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * WordPress dependencies - */ -import { addFilter } from '@wordpress/hooks'; - -/** - * Internal dependencies - */ -import MediaUpload from './media-upload'; - -const replaceMediaUpload = () => MediaUpload; - -addFilter( - 'editor.MediaUpload', - 'core/edit-post/components/media-upload/replace-media-upload', - replaceMediaUpload -); diff --git a/packages/edit-post/src/hooks/index.js b/packages/edit-post/src/hooks/index.js index dc90f62760a7d..5564fc50fdf48 100644 --- a/packages/edit-post/src/hooks/index.js +++ b/packages/edit-post/src/hooks/index.js @@ -1,5 +1,18 @@ +/** + * WordPress dependencies + */ +import { addFilter } from '@wordpress/hooks'; +import { components } from '@wordpress/media-utils'; + /** * Internal dependencies */ -import './components'; import './validate-multiple-use'; + +const replaceMediaUpload = () => components.MediaUpload; + +addFilter( + 'editor.MediaUpload', + 'wordpress/media-utils/replace-media-upload', + replaceMediaUpload +); diff --git a/packages/edit-widgets/src/components/edit-widgets-initializer/index.js b/packages/edit-widgets/src/components/edit-widgets-initializer/index.js index 41e15dc5b4110..7d9efdf7f686c 100644 --- a/packages/edit-widgets/src/components/edit-widgets-initializer/index.js +++ b/packages/edit-widgets/src/components/edit-widgets-initializer/index.js @@ -4,6 +4,10 @@ import { compose } from '@wordpress/compose'; import { useEffect } from '@wordpress/element'; import { withDispatch } from '@wordpress/data'; +import { addFilter, removeFilter } from '@wordpress/hooks'; +import { components } from '@wordpress/media-utils'; + +const replaceMediaUpload = () => components.MediaUpload; /** * Internal dependencies @@ -13,6 +17,17 @@ import Layout from '../layout'; function EditWidgetsInitializer( { setupWidgetAreas, settings } ) { useEffect( () => { setupWidgetAreas(); + addFilter( + 'editor.MediaUpload', + 'wordpress/media-utils/replace-media-upload', + replaceMediaUpload + ); + return () => { + removeFilter( + 'editor.MediaUpload', + 'wordpress/media-utils/replace-media-upload' + ); + }; }, [] ); return ( { + if ( ! hasUploadPermissions ) { + return blockEditorSettings; + } + const mediaUploadBlockEditor = ( { onError, ...argumentsObject } ) => { + mediaUpload( { + wpAllowedMimeTypes: settings.allowedMimeTypes, + onError: ( { message } ) => onError( message ), + ...argumentsObject, + } ); + }; + return { + ...blockEditorSettings, + __experimentalMediaUpload: mediaUploadBlockEditor, + }; + }, + [ blockEditorSettings, hasUploadPermissions ] + ); return ( @@ -41,11 +70,13 @@ export default compose( [ getBlocksFromWidgetArea, getWidgetArea, } = select( 'core/edit-widgets' ); + const { canUser } = select( 'core' ); const blocks = getBlocksFromWidgetArea( id ); const widgetAreaName = ( getWidgetArea( id ) || {} ).name; return { blocks, widgetAreaName, + hasUploadPermissions: defaultTo( canUser( 'create', 'media' ), true ), }; } ), withDispatch( ( dispatch, { id } ) => { diff --git a/packages/editor/package.json b/packages/editor/package.json index 74384ff0ce390..9578fdf41caf1 100644 --- a/packages/editor/package.json +++ b/packages/editor/package.json @@ -38,6 +38,7 @@ "@wordpress/html-entities": "file:../html-entities", "@wordpress/i18n": "file:../i18n", "@wordpress/keycodes": "file:../keycodes", + "@wordpress/media-utils": "file:../media-utils", "@wordpress/notices": "file:../notices", "@wordpress/nux": "file:../nux", "@wordpress/url": "file:../url", diff --git a/packages/editor/src/utils/media-upload/index.js b/packages/editor/src/utils/media-upload/index.js index f6572e29c6b0c..347f6be0bb5cc 100644 --- a/packages/editor/src/utils/media-upload/index.js +++ b/packages/editor/src/utils/media-upload/index.js @@ -7,11 +7,7 @@ import { noop } from 'lodash'; * WordPress dependencies */ import { select } from '@wordpress/data'; - -/** - * Internal dependencies - */ -import { mediaUpload } from './media-upload'; +import { utils } from '@wordpress/media-utils'; /** * Upload a media file when the file upload button is activated. @@ -37,7 +33,7 @@ export default function( { const wpAllowedMimeTypes = getEditorSettings().allowedMimeTypes; maxUploadFileSize = maxUploadFileSize || getEditorSettings().maxUploadFileSize; - mediaUpload( { + utils.mediaUpload( { allowedTypes, filesList, onFileChange, diff --git a/packages/media-utils/.npmrc b/packages/media-utils/.npmrc new file mode 100644 index 0000000000000..43c97e719a5a8 --- /dev/null +++ b/packages/media-utils/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/packages/media-utils/CHANGELOG.md b/packages/media-utils/CHANGELOG.md new file mode 100644 index 0000000000000..08666d6e2cf0c --- /dev/null +++ b/packages/media-utils/CHANGELOG.md @@ -0,0 +1,5 @@ +## 0.1.0 (2019-01-03) + +### New Features + +- Implemented first version of the package. diff --git a/packages/media-utils/README.md b/packages/media-utils/README.md new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/packages/media-utils/package.json b/packages/media-utils/package.json new file mode 100644 index 0000000000000..b56795b5d6597 --- /dev/null +++ b/packages/media-utils/package.json @@ -0,0 +1,35 @@ +{ + "name": "@wordpress/media-utils", + "version": "0.1.0", + "description": "WordPress Media Upload Utils.", + "author": "The WordPress Contributors", + "license": "GPL-2.0-or-later", + "keywords": [ + "wordpress", + "media", + "upload", + "media-upload" + ], + "homepage": "https://github.com/WordPress/gutenberg/master/packages/media-utils/README.md", + "repository": { + "type": "git", + "url": "https://github.com/WordPress/gutenberg.git", + "directory": "packages/url" + }, + "bugs": { + "url": "https://github.com/WordPress/gutenberg/issues" + }, + "main": "build/index.js", + "module": "build-module/index.js", + "dependencies": { + "@babel/runtime": "^7.4.4", + "@wordpress/api-fetch": "file:../api-fetch", + "@wordpress/blob": "file:../blob", + "@wordpress/element": "file:../element", + "@wordpress/i18n": "file:../i18n", + "lodash": "^4.17.11" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/media-utils/src/components/index.js b/packages/media-utils/src/components/index.js new file mode 100644 index 0000000000000..5b90761105b9f --- /dev/null +++ b/packages/media-utils/src/components/index.js @@ -0,0 +1,8 @@ +/** + * Internal dependencies + */ +import MediaUpload from './media-upload'; + +export const components = { + MediaUpload, +}; diff --git a/packages/edit-post/src/hooks/components/media-upload/index.js b/packages/media-utils/src/components/media-upload/index.js similarity index 100% rename from packages/edit-post/src/hooks/components/media-upload/index.js rename to packages/media-utils/src/components/media-upload/index.js diff --git a/packages/media-utils/src/index.js b/packages/media-utils/src/index.js new file mode 100644 index 0000000000000..dc7b1dbe1fd55 --- /dev/null +++ b/packages/media-utils/src/index.js @@ -0,0 +1,2 @@ +export { components } from './components'; +export { utils } from './utils'; diff --git a/packages/media-utils/src/utils/index.js b/packages/media-utils/src/utils/index.js new file mode 100644 index 0000000000000..92f12f9cf17a7 --- /dev/null +++ b/packages/media-utils/src/utils/index.js @@ -0,0 +1,8 @@ +/** + * Internal dependencies + */ +import { mediaUpload } from './media-upload'; + +export const utils = { + mediaUpload, +}; diff --git a/packages/editor/src/utils/media-upload/media-upload.js b/packages/media-utils/src/utils/media-upload.js similarity index 100% rename from packages/editor/src/utils/media-upload/media-upload.js rename to packages/media-utils/src/utils/media-upload.js diff --git a/packages/editor/src/utils/media-upload/test/media-upload.js b/packages/media-utils/src/utils/test/media-upload.test.js similarity index 100% rename from packages/editor/src/utils/media-upload/test/media-upload.js rename to packages/media-utils/src/utils/test/media-upload.test.js