@wordpress/media-editor 0.23.0 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/build/components/media-editor/index.cjs +157 -107
- package/build/components/media-editor/index.cjs.map +1 -1
- package/build/components/media-editor/use-save-media-editor.cjs +18 -8
- package/build/components/media-editor/use-save-media-editor.cjs.map +1 -1
- package/build/components/media-editor-canvas/index.cjs +9 -7
- package/build/components/media-editor-canvas/index.cjs.map +1 -1
- package/build/components/media-editor-crop-panel/index.cjs +9 -7
- package/build/components/media-editor-crop-panel/index.cjs.map +1 -1
- package/build/components/media-editor-fine-rotation/index.cjs +9 -7
- package/build/components/media-editor-fine-rotation/index.cjs.map +1 -1
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +32 -33
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +1 -1
- package/build/components/media-editor-modal/index.cjs +10 -7
- package/build/components/media-editor-modal/index.cjs.map +1 -1
- package/build/components/media-editor-panel/error-boundary.cjs +68 -0
- package/build/components/media-editor-panel/error-boundary.cjs.map +1 -0
- package/build/components/media-editor-panel/index.cjs +158 -0
- package/build/components/media-editor-panel/index.cjs.map +1 -0
- package/build/components/media-preview/index.cjs +143 -36
- package/build/components/media-preview/index.cjs.map +1 -1
- package/build/components/rotation-ruler/index.cjs +9 -7
- package/build/components/rotation-ruler/index.cjs.map +1 -1
- package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -1
- package/build/hooks/use-crop-gesture-handlers.cjs.map +1 -1
- package/build/index.cjs +10 -8
- package/build/private-apis.cjs.map +1 -1
- package/build/state/aspect-ratio-presets.cjs +2 -2
- package/build/state/aspect-ratio-presets.cjs.map +1 -1
- package/build/state/composite-reducer.cjs +9 -7
- package/build/state/composite-reducer.cjs.map +1 -1
- package/build/state/use-media-editor-state.cjs.map +1 -1
- package/build/store/index.cjs +11 -9
- package/build/store/index.cjs.map +1 -1
- package/build/utils/get-media-type.cjs +8 -0
- package/build/utils/get-media-type.cjs.map +1 -1
- package/build/utils/index.cjs +6 -0
- package/build-module/components/media-editor/index.mjs +142 -95
- package/build-module/components/media-editor/index.mjs.map +1 -1
- package/build-module/components/media-editor/use-save-media-editor.mjs +9 -1
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +1 -1
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +32 -33
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +1 -1
- package/build-module/components/media-editor-modal/index.mjs +1 -0
- package/build-module/components/media-editor-modal/index.mjs.map +1 -1
- package/build-module/components/media-editor-panel/error-boundary.mjs +63 -0
- package/build-module/components/media-editor-panel/error-boundary.mjs.map +1 -0
- package/build-module/components/media-editor-panel/index.mjs +123 -0
- package/build-module/components/media-editor-panel/index.mjs.map +1 -0
- package/build-module/components/media-preview/index.mjs +123 -40
- package/build-module/components/media-preview/index.mjs.map +1 -1
- package/build-module/state/aspect-ratio-presets.mjs +2 -2
- package/build-module/state/aspect-ratio-presets.mjs.map +1 -1
- package/build-module/state/use-media-editor-state.mjs.map +1 -1
- package/build-module/utils/get-media-type.mjs +8 -1
- package/build-module/utils/get-media-type.mjs.map +1 -1
- package/build-module/utils/index.mjs +1 -1
- package/build-style/style-rtl.css +18 -30
- package/build-style/style.css +18 -30
- package/build-types/components/media-editor/index.d.ts +8 -0
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-panel/error-boundary.d.ts +23 -0
- package/build-types/components/media-editor-panel/error-boundary.d.ts.map +1 -0
- package/build-types/components/media-editor-panel/index.d.ts +46 -0
- package/build-types/components/media-editor-panel/index.d.ts.map +1 -0
- package/build-types/components/media-preview/index.d.ts.map +1 -1
- package/build-types/state/use-media-editor-state.d.ts +7 -2
- package/build-types/state/use-media-editor-state.d.ts.map +1 -1
- package/build-types/utils/get-media-type.d.ts +3 -1
- package/build-types/utils/get-media-type.d.ts.map +1 -1
- package/build-types/utils/index.d.ts +1 -1
- package/build-types/utils/index.d.ts.map +1 -1
- package/package.json +26 -22
- package/src/components/media-editor/index.tsx +209 -114
- package/src/components/media-editor/style.scss +21 -1
- package/src/components/media-editor-keyboard-shortcuts-modal/index.tsx +55 -63
- package/src/components/media-editor-modal/index.tsx +3 -0
- package/src/components/media-editor-panel/error-boundary.tsx +88 -0
- package/src/components/media-editor-panel/index.tsx +211 -0
- package/src/components/media-preview/index.tsx +195 -36
- package/src/components/media-preview/style.scss +11 -32
- package/src/state/README.md +3 -1
- package/src/state/use-media-editor-state.ts +7 -2
- package/src/utils/get-media-type.ts +9 -1
- package/src/utils/index.ts +1 -1
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { Component } from '@wordpress/element';
|
|
2
|
+
import { speak } from '@wordpress/a11y';
|
|
3
|
+
import { useCopyToClipboard } from '@wordpress/compose';
|
|
4
|
+
import { __, _x, sprintf } from '@wordpress/i18n';
|
|
5
|
+
import { Notice } from '@wordpress/ui';
|
|
6
|
+
import type { ReactNode } from 'react';
|
|
7
|
+
|
|
8
|
+
interface Props {
|
|
9
|
+
title: string;
|
|
10
|
+
children: ReactNode;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface State {
|
|
14
|
+
/** Separate from `error`: a panel can throw a falsy value. */
|
|
15
|
+
hasError: boolean;
|
|
16
|
+
error: unknown;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function getMessage( title: string ) {
|
|
20
|
+
return {
|
|
21
|
+
title: sprintf(
|
|
22
|
+
/* translators: %s: the panel's title. */
|
|
23
|
+
__( 'The "%s" panel could not be shown' ),
|
|
24
|
+
title
|
|
25
|
+
),
|
|
26
|
+
description: __(
|
|
27
|
+
'An unknown error occurred. Reload your browser to try again, or copy the error to report the problem or search.'
|
|
28
|
+
),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// A component, because the copy hook cannot run in the class below.
|
|
33
|
+
function CopyErrorButton( { error }: { error: unknown } ) {
|
|
34
|
+
const ref = useCopyToClipboard< HTMLButtonElement >( () => {
|
|
35
|
+
// A boundary catches whatever was thrown, which is not always an `Error`.
|
|
36
|
+
const { message, stack } = ( error ?? {} ) as {
|
|
37
|
+
message?: unknown;
|
|
38
|
+
stack?: unknown;
|
|
39
|
+
};
|
|
40
|
+
const text = [ error, message ].find(
|
|
41
|
+
( part ) => typeof part === 'string' && part
|
|
42
|
+
);
|
|
43
|
+
return [ text ?? 'An unknown error occurred.', stack ]
|
|
44
|
+
.filter( ( part ) => typeof part === 'string' && part )
|
|
45
|
+
.join( '\n\n' );
|
|
46
|
+
} );
|
|
47
|
+
return (
|
|
48
|
+
<Notice.ActionButton variant="outline" ref={ ref }>
|
|
49
|
+
{ _x( 'Copy error', 'button label' ) }
|
|
50
|
+
</Notice.ActionButton>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Keeps a panel's failure inside its tab, so the rest of the editor stays
|
|
56
|
+
* usable.
|
|
57
|
+
*/
|
|
58
|
+
export default class MediaEditorPanelErrorBoundary extends Component<
|
|
59
|
+
Props,
|
|
60
|
+
State
|
|
61
|
+
> {
|
|
62
|
+
state: State = { hasError: false, error: null };
|
|
63
|
+
|
|
64
|
+
static getDerivedStateFromError( error: unknown ): State {
|
|
65
|
+
return { hasError: true, error };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
componentDidCatch() {
|
|
69
|
+
const { title, description } = getMessage( this.props.title );
|
|
70
|
+
speak( `${ title }. ${ description }`, 'assertive' );
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
render() {
|
|
74
|
+
if ( ! this.state.hasError ) {
|
|
75
|
+
return this.props.children;
|
|
76
|
+
}
|
|
77
|
+
const { title, description } = getMessage( this.props.title );
|
|
78
|
+
return (
|
|
79
|
+
<Notice.Root intent="error">
|
|
80
|
+
<Notice.Title>{ title }</Notice.Title>
|
|
81
|
+
<Notice.Description>{ description }</Notice.Description>
|
|
82
|
+
<Notice.Actions>
|
|
83
|
+
<CopyErrorButton error={ this.state.error } />
|
|
84
|
+
</Notice.Actions>
|
|
85
|
+
</Notice.Root>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import fastDeepEqual from 'fast-deep-equal/es6/index.js';
|
|
2
|
+
import { Fill } from '@wordpress/components';
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
useCallback,
|
|
6
|
+
useContext,
|
|
7
|
+
useLayoutEffect,
|
|
8
|
+
useMemo,
|
|
9
|
+
useRef,
|
|
10
|
+
useState,
|
|
11
|
+
} from '@wordpress/element';
|
|
12
|
+
import warning from '@wordpress/warning';
|
|
13
|
+
import type { ReactNode } from 'react';
|
|
14
|
+
import { MEDIA_TYPES, type MediaType } from '../../utils';
|
|
15
|
+
import MediaEditorPanelErrorBoundary from './error-boundary';
|
|
16
|
+
|
|
17
|
+
export interface MediaEditorPanelFillProps {
|
|
18
|
+
/** `true` while a save is running. */
|
|
19
|
+
disabled: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface MediaEditorPanelRegistration {
|
|
23
|
+
/** Unique, namespaced name, for example `core/crop`. */
|
|
24
|
+
name: string;
|
|
25
|
+
/** Tab label. */
|
|
26
|
+
title: string;
|
|
27
|
+
/** Omitted means every type. */
|
|
28
|
+
mediaTypes?: readonly MediaType[ 'type' ][];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface MediaEditorPanelProps extends MediaEditorPanelRegistration {
|
|
32
|
+
children:
|
|
33
|
+
ReactNode | ( ( fillProps: MediaEditorPanelFillProps ) => ReactNode );
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface MediaEditorPanelsContextValue {
|
|
37
|
+
register: ( panel: MediaEditorPanelRegistration, instance: object ) => void;
|
|
38
|
+
unregister: ( name: string, instance: object ) => void;
|
|
39
|
+
/** Panel name to the mounted panel that has it. */
|
|
40
|
+
owners: Record< string, object >;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const MediaEditorPanelsContext =
|
|
44
|
+
createContext< MediaEditorPanelsContextValue | null >( null );
|
|
45
|
+
|
|
46
|
+
export const MediaEditorPanelsProvider = MediaEditorPanelsContext.Provider;
|
|
47
|
+
|
|
48
|
+
/** The `registerPlugin` scope the editor mounts. */
|
|
49
|
+
export const MEDIA_EDITOR_PLUGIN_SCOPE = 'media-editor';
|
|
50
|
+
|
|
51
|
+
/** `vendor/panel`: lowercase letters, digits and dashes, with one slash. */
|
|
52
|
+
const PANEL_NAME_PATTERN = /^[a-z][a-z0-9-]*\/[a-z][a-z0-9-]*$/;
|
|
53
|
+
|
|
54
|
+
export function getPanelSlotName( name: string ) {
|
|
55
|
+
return `MediaEditorPanel/${ name }`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The mounted panels of one editor.
|
|
60
|
+
*
|
|
61
|
+
* @return The panels, in the order they mounted, and the context value to provide to them.
|
|
62
|
+
*/
|
|
63
|
+
export function useMediaEditorPanels() {
|
|
64
|
+
const [ panels, setPanels ] = useState<
|
|
65
|
+
Record<
|
|
66
|
+
string,
|
|
67
|
+
{ registration: MediaEditorPanelRegistration; owner: object }
|
|
68
|
+
>
|
|
69
|
+
>( {} );
|
|
70
|
+
// The first panel to announce a name keeps it. A ref, so panels
|
|
71
|
+
// announcing in the same commit see each other.
|
|
72
|
+
const ownersRef = useRef( new Map< string, object >() );
|
|
73
|
+
const register = useCallback(
|
|
74
|
+
( panel: MediaEditorPanelRegistration, instance: object ) => {
|
|
75
|
+
const owner = ownersRef.current.get( panel.name );
|
|
76
|
+
if ( owner && owner !== instance ) {
|
|
77
|
+
warning(
|
|
78
|
+
`A media editor panel named "${ panel.name }" is already registered, so this one is not shown. Give each panel a unique name.`
|
|
79
|
+
);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
ownersRef.current.set( panel.name, instance );
|
|
83
|
+
setPanels( ( current ) => {
|
|
84
|
+
// Skip unchanged details: an inline `mediaTypes` array would
|
|
85
|
+
// otherwise re-render the editor and the panel in a loop.
|
|
86
|
+
const entry = current[ panel.name ];
|
|
87
|
+
if (
|
|
88
|
+
entry?.owner === instance &&
|
|
89
|
+
fastDeepEqual( entry.registration, panel )
|
|
90
|
+
) {
|
|
91
|
+
return current;
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
...current,
|
|
95
|
+
[ panel.name ]: { registration: panel, owner: instance },
|
|
96
|
+
};
|
|
97
|
+
} );
|
|
98
|
+
},
|
|
99
|
+
[]
|
|
100
|
+
);
|
|
101
|
+
const unregister = useCallback( ( name: string, instance: object ) => {
|
|
102
|
+
if ( ownersRef.current.get( name ) !== instance ) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
ownersRef.current.delete( name );
|
|
106
|
+
setPanels( ( current ) => {
|
|
107
|
+
if ( ! Object.hasOwn( current, name ) ) {
|
|
108
|
+
return current;
|
|
109
|
+
}
|
|
110
|
+
const next = { ...current };
|
|
111
|
+
delete next[ name ];
|
|
112
|
+
return next;
|
|
113
|
+
} );
|
|
114
|
+
}, [] );
|
|
115
|
+
const owners = useMemo(
|
|
116
|
+
() =>
|
|
117
|
+
Object.fromEntries(
|
|
118
|
+
Object.entries( panels ).map( ( [ name, { owner } ] ) => [
|
|
119
|
+
name,
|
|
120
|
+
owner,
|
|
121
|
+
] )
|
|
122
|
+
),
|
|
123
|
+
[ panels ]
|
|
124
|
+
);
|
|
125
|
+
const contextValue = useMemo(
|
|
126
|
+
() => ( { register, unregister, owners } ),
|
|
127
|
+
[ register, unregister, owners ]
|
|
128
|
+
);
|
|
129
|
+
const registrations = useMemo(
|
|
130
|
+
() =>
|
|
131
|
+
Object.values( panels ).map( ( { registration } ) => registration ),
|
|
132
|
+
[ panels ]
|
|
133
|
+
);
|
|
134
|
+
return { panels: registrations, contextValue };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** A tab in the media editor's sidebar, filled with its children. */
|
|
138
|
+
export default function MediaEditorPanel( {
|
|
139
|
+
name,
|
|
140
|
+
title,
|
|
141
|
+
mediaTypes,
|
|
142
|
+
children,
|
|
143
|
+
}: MediaEditorPanelProps ) {
|
|
144
|
+
const context = useContext( MediaEditorPanelsContext );
|
|
145
|
+
if ( ! context ) {
|
|
146
|
+
throw new Error(
|
|
147
|
+
'MediaEditorPanel is rendered outside the media editor.'
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
const { register, unregister, owners } = context;
|
|
151
|
+
// Identifies this mounted panel.
|
|
152
|
+
const [ instance ] = useState( () => ( {} ) );
|
|
153
|
+
const owner = owners[ name ];
|
|
154
|
+
// Layout effects, so the tab exists before the sidebar paints. Two of
|
|
155
|
+
// them: details may change on any render, but the tab only goes when
|
|
156
|
+
// the panel unmounts or changes its name.
|
|
157
|
+
useLayoutEffect( () => {
|
|
158
|
+
if ( ! PANEL_NAME_PATTERN.test( name ) ) {
|
|
159
|
+
warning(
|
|
160
|
+
`The media editor panel name "${ name }" should be namespaced, like "my-plugin/my-panel": lowercase letters, digits and dashes, with one slash.`
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
mediaTypes?.forEach( ( type ) => {
|
|
164
|
+
if ( ! MEDIA_TYPES.includes( type ) ) {
|
|
165
|
+
warning(
|
|
166
|
+
`The media editor panel "${ name }" lists the media type "${ type }", which the editor does not know, so the panel would never show for it. Known types: ${ MEDIA_TYPES.join( ', ' ) }.`
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
} );
|
|
170
|
+
register( { name, title, mediaTypes }, instance );
|
|
171
|
+
// When two panels share a name, the second waits for the first to
|
|
172
|
+
// unmount. `owner` changes at that point, which re-runs this effect
|
|
173
|
+
// and gives the second panel the tab.
|
|
174
|
+
}, [ name, title, mediaTypes, register, owner, instance ] );
|
|
175
|
+
useLayoutEffect( () => {
|
|
176
|
+
return () => unregister( name, instance );
|
|
177
|
+
}, [ name, unregister, instance ] );
|
|
178
|
+
|
|
179
|
+
if ( owner !== instance ) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return (
|
|
184
|
+
<Fill name={ getPanelSlotName( name ) }>
|
|
185
|
+
{ ( fillProps ) => (
|
|
186
|
+
<MediaEditorPanelErrorBoundary title={ title }>
|
|
187
|
+
<MediaEditorPanelContent
|
|
188
|
+
// The slot passes exactly these props; `Fill` types them loosely.
|
|
189
|
+
fillProps={ fillProps as MediaEditorPanelFillProps }
|
|
190
|
+
>
|
|
191
|
+
{ children }
|
|
192
|
+
</MediaEditorPanelContent>
|
|
193
|
+
</MediaEditorPanelErrorBoundary>
|
|
194
|
+
) }
|
|
195
|
+
</Fill>
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Calls a function child inside the error boundary, so one that throws is
|
|
201
|
+
* caught there.
|
|
202
|
+
*/
|
|
203
|
+
function MediaEditorPanelContent( {
|
|
204
|
+
fillProps,
|
|
205
|
+
children,
|
|
206
|
+
}: {
|
|
207
|
+
fillProps: MediaEditorPanelFillProps;
|
|
208
|
+
children: MediaEditorPanelProps[ 'children' ];
|
|
209
|
+
} ) {
|
|
210
|
+
return typeof children === 'function' ? children( fillProps ) : children;
|
|
211
|
+
}
|
|
@@ -1,7 +1,19 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import clsx from 'clsx';
|
|
2
|
+
import { speak } from '@wordpress/a11y';
|
|
3
|
+
import { Button, Spinner as WCSpinner } from '@wordpress/components';
|
|
4
|
+
import { useCopyToClipboard } from '@wordpress/compose';
|
|
5
|
+
import { useEffect, useState } from '@wordpress/element';
|
|
6
|
+
import { __, _x, sprintf } from '@wordpress/i18n';
|
|
7
|
+
import {
|
|
8
|
+
check,
|
|
9
|
+
copy,
|
|
10
|
+
download,
|
|
11
|
+
error as errorIcon,
|
|
12
|
+
folder,
|
|
13
|
+
} from '@wordpress/icons';
|
|
14
|
+
import { EmptyState } from '@wordpress/ui';
|
|
4
15
|
import { useMediaEditorContext } from '../media-editor-provider';
|
|
16
|
+
import type { Media } from '../media-editor-provider';
|
|
5
17
|
import { getMediaTypeFromMimeType } from '../../utils';
|
|
6
18
|
|
|
7
19
|
/**
|
|
@@ -19,36 +31,142 @@ interface MediaPreviewContentProps {
|
|
|
19
31
|
mediaUrl: string;
|
|
20
32
|
altText?: string;
|
|
21
33
|
displayTitle?: string;
|
|
22
|
-
|
|
34
|
+
fileName?: string;
|
|
35
|
+
fileDetails?: string;
|
|
23
36
|
onLoad: () => void;
|
|
24
37
|
onError: () => void;
|
|
25
38
|
loadingState: 'loading' | 'loaded' | 'error';
|
|
26
39
|
}
|
|
27
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Returns the value of one of the attachment fields for the media, or an
|
|
43
|
+
* empty string if the field isn't available.
|
|
44
|
+
*
|
|
45
|
+
* @param fields The attachment fields.
|
|
46
|
+
* @param fieldId The id of the field to read.
|
|
47
|
+
* @param media The media record.
|
|
48
|
+
*/
|
|
49
|
+
function getFieldValue(
|
|
50
|
+
fields: Array< {
|
|
51
|
+
id: string;
|
|
52
|
+
getValue?: ( args: { item: Media } ) => unknown;
|
|
53
|
+
} >,
|
|
54
|
+
fieldId: string,
|
|
55
|
+
media: Media
|
|
56
|
+
): string {
|
|
57
|
+
const field = fields.find( ( { id } ) => id === fieldId );
|
|
58
|
+
const value = field?.getValue?.( { item: media } );
|
|
59
|
+
return typeof value === 'string' ? value : '';
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const COPY_FEEDBACK_TIMEOUT = 2000;
|
|
63
|
+
|
|
64
|
+
type CopyResult = 'idle' | 'copied' | 'error';
|
|
65
|
+
|
|
66
|
+
const COPY_RESULT_ICONS = {
|
|
67
|
+
idle: copy,
|
|
68
|
+
copied: check,
|
|
69
|
+
error: errorIcon,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Copies the text and shows the result on the copy button, following the
|
|
74
|
+
* Copy to Clipboard design system pattern.
|
|
75
|
+
*
|
|
76
|
+
* @param text The text to copy.
|
|
77
|
+
*/
|
|
78
|
+
function useCopyWithFeedback( text: string ) {
|
|
79
|
+
// Bump the id on every result, so a repeat of the same result restarts
|
|
80
|
+
// the time the icon stays visible.
|
|
81
|
+
const [ feedback, setFeedback ] = useState< {
|
|
82
|
+
result: CopyResult;
|
|
83
|
+
id: number;
|
|
84
|
+
} >( { result: 'idle', id: 0 } );
|
|
85
|
+
|
|
86
|
+
useEffect( () => {
|
|
87
|
+
if ( feedback.result === 'idle' ) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const timeoutId = setTimeout(
|
|
91
|
+
() => setFeedback( { result: 'idle', id: 0 } ),
|
|
92
|
+
COPY_FEEDBACK_TIMEOUT
|
|
93
|
+
);
|
|
94
|
+
return () => clearTimeout( timeoutId );
|
|
95
|
+
}, [ feedback ] );
|
|
96
|
+
|
|
97
|
+
function report( result: Exclude< CopyResult, 'idle' >, message: string ) {
|
|
98
|
+
setFeedback( ( current ) => ( { result, id: current.id + 1 } ) );
|
|
99
|
+
speak( message );
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const ref = useCopyToClipboard(
|
|
103
|
+
text,
|
|
104
|
+
() => report( 'copied', __( 'Copied URL to clipboard.' ) ),
|
|
105
|
+
() => report( 'error', __( "Couldn't copy URL to clipboard." ) )
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
return { result: feedback.result, ref };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function MediaPreviewFileActions( { mediaUrl }: { mediaUrl: string } ) {
|
|
112
|
+
const { result, ref } = useCopyWithFeedback( mediaUrl );
|
|
113
|
+
|
|
114
|
+
return (
|
|
115
|
+
<>
|
|
116
|
+
{ /*
|
|
117
|
+
* Browsers ignore `download` for cross-origin URLs, e.g. media
|
|
118
|
+
* offloaded to a CDN, and show the file instead. Open it in a new
|
|
119
|
+
* tab so it doesn't replace the editor.
|
|
120
|
+
*/ }
|
|
121
|
+
<Button
|
|
122
|
+
__next40pxDefaultSize
|
|
123
|
+
variant="primary"
|
|
124
|
+
href={ mediaUrl }
|
|
125
|
+
target="_blank"
|
|
126
|
+
download
|
|
127
|
+
icon={ download }
|
|
128
|
+
>
|
|
129
|
+
{ __( 'Download' ) }
|
|
130
|
+
</Button>
|
|
131
|
+
{ /* The label stays the same, and the icon shows the result. */ }
|
|
132
|
+
<Button
|
|
133
|
+
__next40pxDefaultSize
|
|
134
|
+
variant="secondary"
|
|
135
|
+
ref={ ref }
|
|
136
|
+
icon={ COPY_RESULT_ICONS[ result ] }
|
|
137
|
+
>
|
|
138
|
+
{ __( 'Copy URL' ) }
|
|
139
|
+
</Button>
|
|
140
|
+
</>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
28
144
|
interface MediaPreviewFileInfoProps {
|
|
29
145
|
mediaUrl: string;
|
|
30
|
-
|
|
31
|
-
|
|
146
|
+
fileName?: string;
|
|
147
|
+
fileDetails?: string;
|
|
32
148
|
}
|
|
33
149
|
|
|
34
150
|
function MediaPreviewFileInfo( {
|
|
35
151
|
mediaUrl,
|
|
36
|
-
|
|
37
|
-
|
|
152
|
+
fileName,
|
|
153
|
+
fileDetails,
|
|
38
154
|
}: MediaPreviewFileInfoProps ) {
|
|
39
155
|
return (
|
|
40
|
-
<
|
|
41
|
-
<
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
156
|
+
<EmptyState.Root>
|
|
157
|
+
<EmptyState.Icon icon={ folder } />
|
|
158
|
+
{ !! fileName && (
|
|
159
|
+
<EmptyState.Title className="media-editor-preview__file-name">
|
|
160
|
+
{ fileName }
|
|
161
|
+
</EmptyState.Title>
|
|
162
|
+
) }
|
|
163
|
+
{ !! fileDetails && (
|
|
164
|
+
<EmptyState.Description>{ fileDetails }</EmptyState.Description>
|
|
165
|
+
) }
|
|
166
|
+
<EmptyState.Actions className="media-editor-preview__actions">
|
|
167
|
+
<MediaPreviewFileActions mediaUrl={ mediaUrl } />
|
|
168
|
+
</EmptyState.Actions>
|
|
169
|
+
</EmptyState.Root>
|
|
52
170
|
);
|
|
53
171
|
}
|
|
54
172
|
|
|
@@ -57,7 +175,8 @@ function MediaPreviewContent( {
|
|
|
57
175
|
mediaUrl,
|
|
58
176
|
altText,
|
|
59
177
|
displayTitle,
|
|
60
|
-
|
|
178
|
+
fileName,
|
|
179
|
+
fileDetails,
|
|
61
180
|
onLoad,
|
|
62
181
|
onError,
|
|
63
182
|
loadingState,
|
|
@@ -100,8 +219,8 @@ function MediaPreviewContent( {
|
|
|
100
219
|
>
|
|
101
220
|
<MediaPreviewFileInfo
|
|
102
221
|
mediaUrl={ mediaUrl }
|
|
103
|
-
|
|
104
|
-
|
|
222
|
+
fileName={ fileName }
|
|
223
|
+
fileDetails={ fileDetails }
|
|
105
224
|
/>
|
|
106
225
|
</object>
|
|
107
226
|
);
|
|
@@ -109,8 +228,8 @@ function MediaPreviewContent( {
|
|
|
109
228
|
return (
|
|
110
229
|
<MediaPreviewFileInfo
|
|
111
230
|
mediaUrl={ mediaUrl }
|
|
112
|
-
|
|
113
|
-
|
|
231
|
+
fileName={ fileName }
|
|
232
|
+
fileDetails={ fileDetails }
|
|
114
233
|
/>
|
|
115
234
|
);
|
|
116
235
|
}
|
|
@@ -127,7 +246,7 @@ export default function MediaPreview( props: MediaPreviewProps ) {
|
|
|
127
246
|
const [ loadingState, setLoadingState ] = useState<
|
|
128
247
|
'loading' | 'loaded' | 'error'
|
|
129
248
|
>( 'loading' );
|
|
130
|
-
const { media } = useMediaEditorContext();
|
|
249
|
+
const { media, fields } = useMediaEditorContext();
|
|
131
250
|
|
|
132
251
|
const {
|
|
133
252
|
source_url: mediaUrl,
|
|
@@ -147,28 +266,67 @@ export default function MediaPreview( props: MediaPreviewProps ) {
|
|
|
147
266
|
|
|
148
267
|
if ( ! mediaUrl ) {
|
|
149
268
|
return (
|
|
150
|
-
<div className="media-editor-preview media-editor-preview--empty">
|
|
151
|
-
<
|
|
269
|
+
<div className="media-editor-preview media-editor-preview--empty is-placeholder">
|
|
270
|
+
<EmptyState.Root>
|
|
271
|
+
<EmptyState.Icon icon={ folder } />
|
|
272
|
+
<EmptyState.Title>
|
|
273
|
+
{ __( 'No media file available.' ) }
|
|
274
|
+
</EmptyState.Title>
|
|
275
|
+
</EmptyState.Root>
|
|
152
276
|
</div>
|
|
153
277
|
);
|
|
154
278
|
}
|
|
155
279
|
|
|
280
|
+
const displayTitle =
|
|
281
|
+
typeof title === 'string' ? title : title?.rendered || title?.raw;
|
|
282
|
+
|
|
283
|
+
// Read through the attachment fields, so the values match the Details
|
|
284
|
+
// panel. Without a filename field, fall back to the title.
|
|
285
|
+
const fileName =
|
|
286
|
+
( media && getFieldValue( fields, 'filename', media ) ) || displayTitle;
|
|
287
|
+
const fileSize = media ? getFieldValue( fields, 'filesize', media ) : '';
|
|
288
|
+
const fileDetails =
|
|
289
|
+
mimeType && fileSize
|
|
290
|
+
? sprintf(
|
|
291
|
+
/* translators: 1: File type, e.g. "application/zip". 2: File size, e.g. "2.4 MB". */
|
|
292
|
+
_x( '%1$s · %2$s', 'file type and size' ),
|
|
293
|
+
mimeType,
|
|
294
|
+
fileSize
|
|
295
|
+
)
|
|
296
|
+
: mimeType || fileSize;
|
|
297
|
+
|
|
156
298
|
if ( loadingState === 'error' ) {
|
|
157
299
|
return (
|
|
158
|
-
<div className="media-editor-preview media-editor-preview--error">
|
|
159
|
-
<
|
|
160
|
-
|
|
300
|
+
<div className="media-editor-preview media-editor-preview--error is-placeholder">
|
|
301
|
+
<EmptyState.Root>
|
|
302
|
+
<EmptyState.Icon icon={ folder } />
|
|
303
|
+
<EmptyState.Title>
|
|
304
|
+
{ __( 'Failed to load media file.' ) }
|
|
305
|
+
</EmptyState.Title>
|
|
306
|
+
{ !! fileName && (
|
|
307
|
+
<EmptyState.Description className="media-editor-preview__file-name">
|
|
308
|
+
{ fileName }
|
|
309
|
+
</EmptyState.Description>
|
|
310
|
+
) }
|
|
311
|
+
<EmptyState.Actions className="media-editor-preview__actions">
|
|
312
|
+
<MediaPreviewFileActions mediaUrl={ mediaUrl } />
|
|
313
|
+
</EmptyState.Actions>
|
|
314
|
+
</EmptyState.Root>
|
|
161
315
|
</div>
|
|
162
316
|
);
|
|
163
317
|
}
|
|
164
318
|
|
|
165
|
-
const displayTitle =
|
|
166
|
-
typeof title === 'string' ? title : title?.rendered || title?.raw;
|
|
167
|
-
|
|
168
319
|
return (
|
|
169
320
|
<div
|
|
170
321
|
{ ...props }
|
|
171
|
-
className={
|
|
322
|
+
className={ clsx(
|
|
323
|
+
'media-editor-preview',
|
|
324
|
+
`media-editor-preview--${ previewType }`,
|
|
325
|
+
{
|
|
326
|
+
// Media with no preview of its own shows the file info.
|
|
327
|
+
'is-placeholder': previewType === 'application',
|
|
328
|
+
}
|
|
329
|
+
) }
|
|
172
330
|
>
|
|
173
331
|
{ previewType === 'image' && loadingState === 'loading' && (
|
|
174
332
|
<div className="media-editor-preview__spinner">
|
|
@@ -180,7 +338,8 @@ export default function MediaPreview( props: MediaPreviewProps ) {
|
|
|
180
338
|
mediaUrl={ mediaUrl }
|
|
181
339
|
altText={ altText }
|
|
182
340
|
displayTitle={ displayTitle }
|
|
183
|
-
|
|
341
|
+
fileName={ fileName }
|
|
342
|
+
fileDetails={ fileDetails }
|
|
184
343
|
onLoad={ () => setLoadingState( 'loaded' ) }
|
|
185
344
|
onError={ () => setLoadingState( 'error' ) }
|
|
186
345
|
loadingState={ loadingState }
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
@use "@wordpress/base-styles/animations" as *;
|
|
2
|
-
@use "@wordpress/base-styles/colors" as *;
|
|
3
2
|
@use "@wordpress/base-styles/variables" as *;
|
|
4
3
|
|
|
5
4
|
.media-editor-preview {
|
|
@@ -65,39 +64,19 @@
|
|
|
65
64
|
border-radius: $radius-small;
|
|
66
65
|
}
|
|
67
66
|
|
|
68
|
-
|
|
69
|
-
text-align: center;
|
|
70
|
-
background: $gray-100;
|
|
71
|
-
padding: $grid-unit-30;
|
|
72
|
-
border-radius: 2px;
|
|
73
|
-
}
|
|
74
|
-
|
|
67
|
+
// Long filenames often have no spaces to break on.
|
|
75
68
|
&__file-name {
|
|
76
|
-
|
|
77
|
-
margin-bottom: $grid-unit-10;
|
|
69
|
+
overflow-wrap: anywhere;
|
|
78
70
|
}
|
|
79
71
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
&--error,
|
|
92
|
-
&--empty {
|
|
93
|
-
color: $gray-700;
|
|
94
|
-
text-align: center;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
&__url {
|
|
98
|
-
font-size: 0.9em;
|
|
99
|
-
color: $gray-600;
|
|
100
|
-
word-break: break-all;
|
|
101
|
-
margin-top: $grid-unit-10;
|
|
72
|
+
// EmptyState.Actions switches between a column and a row on the viewport
|
|
73
|
+
// width, and the row doesn't wrap. The canvas can be narrow on a wide
|
|
74
|
+
// viewport, e.g. with the Details panel open, so wrap a row instead. Also
|
|
75
|
+
// widen its 4px gap between buttons.
|
|
76
|
+
&__actions {
|
|
77
|
+
flex-direction: row;
|
|
78
|
+
flex-wrap: wrap;
|
|
79
|
+
justify-content: center;
|
|
80
|
+
gap: var(--wpds-dimension-gap-sm);
|
|
102
81
|
}
|
|
103
82
|
}
|
package/src/state/README.md
CHANGED
|
@@ -4,10 +4,12 @@ The media editor's session owns its undo and redo history. The image editor's st
|
|
|
4
4
|
|
|
5
5
|
`useMediaEditorState()` creates a media editor session; `<MediaEditorStateProvider>` calls it, and `useMediaEditor()` reads the one it made. Calling `useMediaEditorState()` again inside the provider makes a second session that shares nothing with the first, so components use `useMediaEditor()`. The session owns the source image, every editing slice, the shared undo/redo history, gesture boundaries, and whether the edits change the saved image (`hasOutputEdits`). The image editor is one participant: `session.imageEditor` is a geometry-only `ImageEditorController` that an `<ImageEditorCanvas>` takes via its `controller` prop, and its `isDirty` covers geometry alone. Replacing the source image (`setSourceImage`, which the image editor's `setImage` also calls) starts a fresh session with default geometry and crop options, a clean baseline, and no history. Save code asks the session, not the image editor, whether image output needs saving.
|
|
6
6
|
|
|
7
|
-
The session also holds named edits other than crop: `session.edits`, set with `session.setEdit( name, value )` and removed by passing `undefined`. They share the crop history, so undo, redo and gestures treat them like any crop change, and a different source image clears them. Values are plain data (`EditValue`: strings, numbers, booleans, `null`, and arrays and objects of those), because the session compares them by content to tell a real change from a no-op.
|
|
7
|
+
The session also holds named edits other than crop: `session.edits`, set with `session.setEdit( name, value )` and removed by passing `undefined`. They share the crop history, so undo, redo and gestures treat them like any crop change, and a different source image clears them. Values are plain data (`EditValue`: strings, numbers, booleans, `null`, and arrays and objects of those), because the session compares them by content to tell a real change from a no-op. They are session state for the editor's own edit pipeline, not an extension API. `isDirty` counts them, so Reset clears them; `hasOutputEdits` does not, so the editor's unsaved-changes check and Save ignore them, and nothing persists them. That holds until the pipeline applies them to the output, and a panel has no reason to set one before then.
|
|
8
8
|
|
|
9
9
|
`session.reset()` puts every slice back to its starting state in one undo entry: default geometry and crop options, no edits, same image. The Reset button calls it; it does not compose per-slice resets itself.
|
|
10
10
|
|
|
11
|
+
Sidebar panels are React: a `MediaEditorPanel` rendered anywhere inside the editor announces `{ name, title, mediaTypes }` while mounted and fills a slot with its content, and the sidebar turns the mounted panels into tabs. Core's Crop and Details panels are rendered with that component, so the editor itself exercises the extension point. The editor also mounts `registerPlugin` callbacks in the `media-editor` scope through `PluginArea`, inside the session provider. This follows the post editor's `PluginSidebar`: plugin UI arrives as components through Slot/Fill, not as entries in a store.
|
|
12
|
+
|
|
11
13
|
## Use the Session With the Canvas
|
|
12
14
|
|
|
13
15
|
Use the media editor session when image editor state must share history with controls such as aspect ratio. The session owns history and gesture boundaries; pass its `imageEditor` controller to `<ImageEditorCanvas>`.
|