Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new private upload-media package #66290

Open
wants to merge 46 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
8a57140
Add new private `upload-media` package
swissspidy Oct 21, 2024
808700e
Add missing readme
swissspidy Oct 22, 2024
7a9a762
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Oct 22, 2024
6022cf0
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Oct 23, 2024
3f30935
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Oct 24, 2024
91f7687
Make `updateSettings` private
swissspidy Oct 24, 2024
b1b0cb5
Remove empty line
swissspidy Oct 24, 2024
5d00adb
Add back some of the missing utils
swissspidy Oct 24, 2024
72d474f
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Oct 24, 2024
07680ca
Update docs now
swissspidy Oct 24, 2024
38c3df6
Remove meetings (#66421)
ndiego Oct 24, 2024
06e345e
Site editor: remove "default" admin CSS (#66431)
ellatrix Oct 24, 2024
8f99999
Fix: JSON Schema Docgen doesn't work on Windows OS (#66414)
t-hamano Oct 25, 2024
a198bd2
Revise zoom layout shift fix (#66390)
stokesman Oct 25, 2024
4d4142c
Zoom out: Add keyboard shortcut in editor (#66400)
ntsekouras Oct 25, 2024
7f2549c
Compose: Fix React Complier error for 'useCopyToClipboard' (#66444)
Mamaduka Oct 25, 2024
f0b988c
Style Book: Fix React Compiler error (#66445)
Mamaduka Oct 25, 2024
0b066a9
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Oct 25, 2024
3cfa25a
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Oct 25, 2024
a1f044f
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Nov 4, 2024
57135d8
Add `ts-ignore`
swissspidy Nov 4, 2024
a76fcc7
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Nov 25, 2024
5b3c022
Block editor: Add `MediaUploadProvider` (#66380)
swissspidy Nov 25, 2024
1dc086d
Change order in `package.json`
swissspidy Nov 25, 2024
845cf78
Rip out most code
swissspidy Nov 25, 2024
8cd6177
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Nov 30, 2024
a1f0282
A bunch of fixes
swissspidy Dec 2, 2024
bae3e67
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Dec 2, 2024
93e9ec9
Fixes
swissspidy Dec 2, 2024
6176a66
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Dec 2, 2024
17dc47e
Move to folder for RN compat
swissspidy Dec 2, 2024
8b6f249
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Dec 2, 2024
4f5a0d0
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Dec 10, 2024
c772bd5
Remove default value
swissspidy Dec 10, 2024
addc7ff
Pass to `privateSettings` instead
swissspidy Dec 10, 2024
d2e8255
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Dec 10, 2024
d423b45
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Dec 11, 2024
d3ec207
Move validation to block-editor/upload-media
swissspidy Dec 11, 2024
9e73bd5
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Dec 11, 2024
ad01ff2
Use non-empty files in test
swissspidy Dec 11, 2024
e498986
fix api reference
swissspidy Dec 11, 2024
b77e527
Update readme a little bit
swissspidy Dec 12, 2024
00f7466
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Dec 12, 2024
4c1c576
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Dec 13, 2024
ba97bbf
Undo change to privateSettings
swissspidy Dec 13, 2024
e8b3755
Merge branch 'trunk' into add/61447-upload-media-pkg
swissspidy Dec 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/check-licenses.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { spawnSync } from 'node:child_process';
*/
import { checkDepsInTree } from '../packages/scripts/utils/license.js';

const ignored = [ '@ampproject/remapping' ];
const ignored = [ '@ampproject/remapping', 'webpack' ];

/*
* `wp-scripts check-licenses` uses prod and dev dependencies of the package to scan for dependencies. With npm workspaces, workspace packages (the @wordpress/* packages) are not listed in the main package json and this approach does not work.
Expand Down
59 changes: 59 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/private-apis/src/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const CORE_MODULES_USING_PRIVATE_APIS = [
'@wordpress/router',
'@wordpress/dataviews',
'@wordpress/fields',
'@wordpress/upload-media',
];

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/upload-media/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->

## Unreleased

Initial release.
147 changes: 147 additions & 0 deletions packages/upload-media/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# `@wordpress/upload-media`
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

Core media upload logic implemented with a custom `@wordpress/data` store.

## API Reference

### Actions

The following set of dispatching action creators are available on the object returned by `wp.data.dispatch( 'core/upload-media' )`:

<!-- START TOKEN(Autogenerated actions|src/store/actions.ts) -->

#### addItems

Adds a new item to the upload queue.

_Parameters_

- _$0_ `AddItemsArgs`:
- _$0.files_ `AddItemsArgs[ 'files' ]`: Files
- _$0.onChange_ `[AddItemsArgs[ 'onChange' ]]`: Function called each time a file or a temporary representation of the file is available.
- _$0.onSuccess_ `[AddItemsArgs[ 'onSuccess' ]]`: Function called after the file is uploaded.
- _$0.onBatchSuccess_ `[AddItemsArgs[ 'onBatchSuccess' ]]`: Function called after a batch of files is uploaded.
- _$0.onError_ `[AddItemsArgs[ 'onError' ]]`: Function called when an error happens.
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
- _$0.additionalData_ `[AddItemsArgs[ 'additionalData' ]]`: Additional data to include in the request.

#### cancelItem

Cancels an item in the queue based on an error.
youknowriad marked this conversation as resolved.
Show resolved Hide resolved

_Parameters_

- _id_ `QueueItemId`: Item ID.
- _error_ `Error`: Error instance.
- _silent_ Whether to cancel the item silently, without invoking its `onError` callback.

<!-- END TOKEN(Autogenerated actions|src/store/actions.ts) -->
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

### Selectors

The following selectors are available on the object returned by `wp.data.select( 'core/upload-media' )`:

<!-- START TOKEN(Autogenerated selectors|src/store/selectors.ts) -->

#### getComparisonDataForApproval

Returns data to compare the old file vs. the optimized file, given the attachment ID.

Includes both the URLs and the respective file sizes and the size difference in percentage.

_Parameters_

- _state_ `State`: Upload state.
- _attachmentId_ `number`: Attachment ID.

_Returns_

- `{ oldUrl: string | undefined; oldSize: number; newSize: number; newUrl: string | undefined; sizeDiff: number; } | null`: Comparison data.

#### getItems

Returns all items currently being uploaded, without sub-sizes (children).

_Parameters_

- _state_ `State`: Upload state.

_Returns_

- `QueueItem[]`: Queue items.

#### getSettings

Returns the media upload settings.

_Parameters_

- _state_ `State`: Upload state.

_Returns_

- `Settings`: Settings

#### isPendingApproval

Determines whether there is an item pending approval.

_Parameters_

- _state_ `State`: Upload state.

_Returns_

- `boolean`: Whether there is an item pending approval.

#### isPendingApprovalByAttachmentId

Determines whether an item is the first one pending approval given its associated attachment ID.

_Parameters_

- _state_ `State`: Upload state.
- _attachmentId_ `number`: Attachment ID.

_Returns_

- `boolean`: Whether the item is first in the list of items pending approval.

#### isUploading

Determines whether any upload is currently in progress.

_Parameters_

- _state_ `State`: Upload state.

_Returns_

- `boolean`: Whether any upload is currently in progress.

#### isUploadingById

Determines whether an upload is currently in progress given an attachment ID.

_Parameters_

- _state_ `State`: Upload state.
- _attachmentId_ `number`: Attachment ID.

_Returns_

- `boolean`: Whether upload is currently in progress for the given attachment.

#### isUploadingByUrl
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

Determines whether an upload is currently in progress given an attachment URL.

_Parameters_

- _state_ `State`: Upload state.
- _url_ `string`: Attachment URL.

_Returns_

- `boolean`: Whether upload is currently in progress for the given attachment.

<!-- END TOKEN(Autogenerated selectors|src/store/selectors.ts) -->
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
44 changes: 44 additions & 0 deletions packages/upload-media/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "@wordpress/upload-media",
"version": "1.0.0-prerelease",
"private": true,
"description": "Core media upload logic.",
"author": "The WordPress Contributors",
"license": "GPL-2.0-or-later",
"keywords": [
"wordpress",
"media"
],
"homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/packages/upload-media/README.md",
"repository": {
"type": "git",
"url": "https://github.com/WordPress/gutenberg.git",
"directory": "packages/upload-media"
},
"bugs": {
"url": "https://github.com/WordPress/gutenberg/issues"
},
"engines": {
"node": ">=18.12.0",
"npm": ">=8.19.2"
},
"main": "build/index.js",
"module": "build-module/index.js",
"types": "build-types",
"dependencies": {
"@shopify/web-worker": "^6.4.0",
"@wordpress/api-fetch": "file:../api-fetch",
"@wordpress/blob": "file:../blob",
"@wordpress/data": "file:../data",
"@wordpress/i18n": "file:../i18n",
"@wordpress/preferences": "file:../preferences",
"@wordpress/private-apis": "file:../private-apis",
"@wordpress/url": "file:../url",
"@wordpress/vips": "file:../vips",
"uuid": "^9.0.1"
},
"publishConfig": {
"access": "public"
},
"wpScript": true
}
1 change: 1 addition & 0 deletions packages/upload-media/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const PREFERENCES_NAME = 'core/media';
38 changes: 38 additions & 0 deletions packages/upload-media/src/image-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* ImageFile class.
*
* Small wrapper around the `File` class to hold
* information about current dimensions and original
* dimensions, in case the image was resized.
*/
export class ImageFile extends File {
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
width = 0;
height = 0;
originalWidth? = 0;
originalHeight? = 0;

get wasResized() {
return (
( this.originalWidth || 0 ) > this.width ||
( this.originalHeight || 0 ) > this.height
);
}

constructor(
file: File,
width: number,
height: number,
originalWidth?: number,
originalHeight?: number
) {
super( [ file ], file.name, {
type: file.type,
lastModified: file.lastModified,
} );

this.width = width;
this.height = height;
this.originalWidth = originalWidth;
this.originalHeight = originalHeight;
}
}
16 changes: 16 additions & 0 deletions packages/upload-media/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Internal dependencies
*/
import { store as uploadStore } from './store';
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

export { uploadStore as store };

export { UploadError } from './upload-error';

export type {
ImageFormat,
ImageSizeCrop,
ThumbnailGeneration,
VideoFormat,
AudioFormat,
} from './store/types';
10 changes: 10 additions & 0 deletions packages/upload-media/src/lock-unlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* WordPress dependencies
*/
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';

export const { lock, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',
'@wordpress/upload-media'
);
Loading
Loading