@wordpress/media-editor 0.18.1-next.v.202609171837.0 → 0.20.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 +25 -0
- package/build/components/media-editor/index.cjs +132 -50
- package/build/components/media-editor/index.cjs.map +2 -2
- package/build/components/media-editor/use-crop-options.cjs +1 -1
- package/build/components/media-editor/use-crop-options.cjs.map +2 -2
- package/build/components/media-editor/use-save-media-editor.cjs +36 -20
- package/build/components/media-editor/use-save-media-editor.cjs.map +2 -2
- package/build/components/media-editor-canvas/index.cjs +8 -8
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-fine-rotation/index.cjs +1 -1
- package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
- package/build/components/media-editor-image-controls/index.cjs +46 -29
- package/build/components/media-editor-image-controls/index.cjs.map +2 -2
- package/build/components/media-editor-modal/index.cjs +17 -9
- package/build/components/media-editor-modal/index.cjs.map +2 -2
- package/build/hooks/use-crop-gesture-handlers.cjs +1 -1
- package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs +5 -0
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs +3 -3
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +1 -1
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +3 -1
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs +1 -1
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build/state/composite-reducer.cjs +3 -3
- package/build/state/composite-reducer.cjs.map +2 -2
- package/build/state/index.cjs.map +1 -1
- package/build/state/media-editor-state-provider.cjs +1 -6
- package/build/state/media-editor-state-provider.cjs.map +2 -2
- package/build/state/use-media-editor-state.cjs +38 -36
- package/build/state/use-media-editor-state.cjs.map +2 -2
- package/build/store/actions.cjs +2 -0
- package/build/store/actions.cjs.map +2 -2
- package/build/store/reducer.cjs +3 -1
- package/build/store/reducer.cjs.map +2 -2
- package/build/store/selectors.cjs +5 -0
- package/build/store/selectors.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +145 -52
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +1 -1
- package/build-module/components/media-editor/use-crop-options.mjs.map +2 -2
- package/build-module/components/media-editor/use-save-media-editor.mjs +36 -20
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +2 -2
- package/build-module/components/media-editor-canvas/index.mjs +8 -8
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs +1 -1
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
- package/build-module/components/media-editor-image-controls/index.mjs +47 -36
- package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
- package/build-module/components/media-editor-modal/index.mjs +17 -9
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/hooks/use-crop-gesture-handlers.mjs +1 -1
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs +5 -0
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +3 -3
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +1 -1
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +3 -1
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-interaction.mjs +1 -1
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-module/state/composite-reducer.mjs +3 -3
- package/build-module/state/composite-reducer.mjs.map +2 -2
- package/build-module/state/index.mjs.map +1 -1
- package/build-module/state/media-editor-state-provider.mjs +1 -6
- package/build-module/state/media-editor-state-provider.mjs.map +2 -2
- package/build-module/state/use-media-editor-state.mjs +38 -36
- package/build-module/state/use-media-editor-state.mjs.map +2 -2
- package/build-module/store/actions.mjs +2 -0
- package/build-module/store/actions.mjs.map +2 -2
- package/build-module/store/reducer.mjs +3 -1
- package/build-module/store/reducer.mjs.map +2 -2
- package/build-module/store/selectors.mjs +4 -0
- package/build-module/store/selectors.mjs.map +2 -2
- package/build-style/style-rtl.css +26 -27
- package/build-style/style.css +11 -5
- package/build-types/components/media-editor/index.d.ts +2 -2
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-save-media-editor.d.ts +15 -3
- package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/build-types/state/composite-reducer.d.ts +5 -5
- package/build-types/state/composite-reducer.d.ts.map +1 -1
- package/build-types/state/index.d.ts +1 -1
- package/build-types/state/index.d.ts.map +1 -1
- package/build-types/state/media-editor-state-provider.d.ts +8 -16
- package/build-types/state/media-editor-state-provider.d.ts.map +1 -1
- package/build-types/state/use-media-editor-state.d.ts +37 -30
- package/build-types/state/use-media-editor-state.d.ts.map +1 -1
- package/build-types/store/actions.d.ts +9 -1
- package/build-types/store/actions.d.ts.map +1 -1
- package/build-types/store/reducer.d.ts +3 -0
- package/build-types/store/reducer.d.ts.map +1 -1
- package/build-types/store/selectors.d.ts +1 -0
- package/build-types/store/selectors.d.ts.map +1 -1
- package/package.json +29 -25
- package/src/components/media-editor/index.tsx +203 -59
- package/src/components/media-editor/test/restore-original.browser.test.tsx +227 -0
- package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +12 -10
- package/src/components/media-editor/use-crop-options.ts +1 -1
- package/src/components/media-editor/use-save-media-editor.ts +61 -20
- package/src/components/media-editor-canvas/index.tsx +7 -8
- package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +2 -4
- package/src/components/media-editor-fine-rotation/index.tsx +1 -1
- package/src/components/media-editor-image-controls/index.tsx +41 -41
- package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +19 -13
- package/src/components/media-editor-modal/index.tsx +18 -10
- package/src/components/media-editor-modal/test/index.jsdom.test.tsx +18 -1
- package/src/components/rotation-ruler/stories/index.story.tsx +2 -2
- package/src/components/rotation-ruler/style.scss +9 -0
- package/src/hooks/use-crop-gesture-handlers.ts +3 -1
- package/src/image-editor/core/interaction-controller.ts +8 -0
- package/src/image-editor/core/test/interaction-controller.ts +45 -0
- package/src/image-editor/docs/architecture.md +6 -2
- package/src/image-editor/docs/recipes.md +8 -8
- package/src/image-editor/react/components/cropper.scss +11 -5
- package/src/image-editor/react/components/cropper.tsx +8 -3
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +3 -1
- package/src/image-editor/react/components/test/cropper.browser.test.tsx +7 -5
- package/src/image-editor/react/hooks/use-aria-announcer.ts +3 -1
- package/src/image-editor/react/hooks/use-interaction.ts +3 -2
- package/src/image-editor/stories/rectangle-crop.story.tsx +15 -14
- package/src/image-editor/stories/{style.css → style.module.css} +8 -37
- package/src/state/composite-reducer.ts +6 -7
- package/src/state/index.ts +1 -1
- package/src/state/media-editor-state-provider.tsx +9 -22
- package/src/state/test/use-media-editor-state.browser.test.tsx +79 -0
- package/src/state/test/use-media-editor-state.jsdom.test.ts +126 -64
- package/src/state/use-media-editor-state.ts +79 -68
- package/src/store/actions.ts +9 -0
- package/src/store/reducer.ts +6 -1
- package/src/store/selectors.ts +4 -0
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import clsx from 'clsx';
|
|
2
2
|
import {
|
|
3
3
|
Button,
|
|
4
|
+
DropdownMenu,
|
|
5
|
+
MenuGroup,
|
|
6
|
+
MenuItem,
|
|
4
7
|
Spinner,
|
|
5
8
|
__experimentalConfirmDialog as ConfirmDialog,
|
|
6
9
|
} from '@wordpress/components';
|
|
@@ -19,7 +22,15 @@ import {
|
|
|
19
22
|
useState,
|
|
20
23
|
} from '@wordpress/element';
|
|
21
24
|
import { __ } from '@wordpress/i18n';
|
|
22
|
-
import {
|
|
25
|
+
import {
|
|
26
|
+
backup,
|
|
27
|
+
close,
|
|
28
|
+
drawerRight,
|
|
29
|
+
keyboard,
|
|
30
|
+
moreVertical,
|
|
31
|
+
redo,
|
|
32
|
+
undo,
|
|
33
|
+
} from '@wordpress/icons';
|
|
23
34
|
import {
|
|
24
35
|
displayShortcut,
|
|
25
36
|
isAppleOS,
|
|
@@ -27,7 +38,11 @@ import {
|
|
|
27
38
|
} from '@wordpress/keycodes';
|
|
28
39
|
import { SnackbarNotices, store as noticesStore } from '@wordpress/notices';
|
|
29
40
|
import type { Field } from '@wordpress/dataviews';
|
|
30
|
-
import type {
|
|
41
|
+
import type {
|
|
42
|
+
KeyboardEvent as ReactKeyboardEvent,
|
|
43
|
+
ReactElement,
|
|
44
|
+
ReactNode,
|
|
45
|
+
} from 'react';
|
|
31
46
|
import { MediaEditorProvider } from '../media-editor-provider';
|
|
32
47
|
import type { Media } from '../media-editor-provider';
|
|
33
48
|
import MediaPreview from '../media-preview';
|
|
@@ -71,7 +86,7 @@ const CROP_PANEL = 'crop';
|
|
|
71
86
|
interface MediaEditorTab {
|
|
72
87
|
id: string;
|
|
73
88
|
title: string;
|
|
74
|
-
render: () =>
|
|
89
|
+
render: () => ReactElement;
|
|
75
90
|
}
|
|
76
91
|
|
|
77
92
|
export interface MediaEditorFrameProps {
|
|
@@ -105,7 +120,7 @@ export interface MediaEditorProps {
|
|
|
105
120
|
aspectRatioPresets?: AspectRatioPreset[];
|
|
106
121
|
onClose?: () => void;
|
|
107
122
|
onSaved?: ( result: MediaEditorSaveResult ) => void;
|
|
108
|
-
renderFrame: ( props: MediaEditorFrameProps ) =>
|
|
123
|
+
renderFrame: ( props: MediaEditorFrameProps ) => ReactElement;
|
|
109
124
|
noticesClassName?: string;
|
|
110
125
|
noticesPortalElement?: Element | null;
|
|
111
126
|
shouldCloseOnEsc?: boolean;
|
|
@@ -204,6 +219,19 @@ interface MediaEditorFrameContextValue {
|
|
|
204
219
|
onCancel: () => void;
|
|
205
220
|
onSave: () => void;
|
|
206
221
|
onReset: () => void;
|
|
222
|
+
/**
|
|
223
|
+
* When `true`, the media has a lineage root to restore to, so the
|
|
224
|
+
* "Restore original image" menu item is shown.
|
|
225
|
+
*/
|
|
226
|
+
canRestoreOriginal: boolean;
|
|
227
|
+
/**
|
|
228
|
+
* When `true`, another attachment has replaced the edited one in the
|
|
229
|
+
* cropper this session (today, only the original), so the menu item is
|
|
230
|
+
* disabled.
|
|
231
|
+
*/
|
|
232
|
+
isSourceReplaced: boolean;
|
|
233
|
+
/** Load the lineage root into the cropper as a dirty preview. */
|
|
234
|
+
onRestoreOriginal: () => void;
|
|
207
235
|
}
|
|
208
236
|
|
|
209
237
|
const MediaEditorFrameContext =
|
|
@@ -237,8 +265,17 @@ export interface HeaderActionsProps {
|
|
|
237
265
|
}
|
|
238
266
|
|
|
239
267
|
function HeaderActions( { showCloseButton = false }: HeaderActionsProps ) {
|
|
240
|
-
const {
|
|
241
|
-
|
|
268
|
+
const {
|
|
269
|
+
isImage,
|
|
270
|
+
isSaving,
|
|
271
|
+
onCancel,
|
|
272
|
+
isWide,
|
|
273
|
+
activePanel,
|
|
274
|
+
onTogglePanel,
|
|
275
|
+
canRestoreOriginal,
|
|
276
|
+
isSourceReplaced,
|
|
277
|
+
onRestoreOriginal,
|
|
278
|
+
} = useMediaEditorFrameContext();
|
|
242
279
|
const isPanelOpen = !! activePanel;
|
|
243
280
|
const [ isShortcutsModalOpen, setIsShortcutsModalOpen ] = useState( false );
|
|
244
281
|
return (
|
|
@@ -248,14 +285,6 @@ function HeaderActions( { showCloseButton = false }: HeaderActionsProps ) {
|
|
|
248
285
|
align="center"
|
|
249
286
|
gap="sm"
|
|
250
287
|
>
|
|
251
|
-
{ isImage && (
|
|
252
|
-
<Button
|
|
253
|
-
size="compact"
|
|
254
|
-
icon={ keyboard }
|
|
255
|
-
label={ __( 'Keyboard shortcuts' ) }
|
|
256
|
-
onClick={ () => setIsShortcutsModalOpen( true ) }
|
|
257
|
-
/>
|
|
258
|
-
) }
|
|
259
288
|
{ /* The sidebar holds more than one panel, so this opens and
|
|
260
289
|
closes the sidebar rather than naming a panel: reopening
|
|
261
290
|
returns to whichever tab was last showing, and the tab strip
|
|
@@ -271,6 +300,50 @@ function HeaderActions( { showCloseButton = false }: HeaderActionsProps ) {
|
|
|
271
300
|
aria-expanded={ isWide ? isPanelOpen : undefined }
|
|
272
301
|
onClick={ onTogglePanel }
|
|
273
302
|
/>
|
|
303
|
+
{ isImage && (
|
|
304
|
+
<DropdownMenu
|
|
305
|
+
icon={ moreVertical }
|
|
306
|
+
label={ __( 'More options' ) }
|
|
307
|
+
toggleProps={ { size: 'compact' } }
|
|
308
|
+
>
|
|
309
|
+
{ ( { onClose: closeMenu } ) => (
|
|
310
|
+
<>
|
|
311
|
+
{ canRestoreOriginal && (
|
|
312
|
+
<MenuGroup>
|
|
313
|
+
<MenuItem
|
|
314
|
+
icon={ backup }
|
|
315
|
+
iconPosition="left"
|
|
316
|
+
disabled={
|
|
317
|
+
isSourceReplaced || isSaving
|
|
318
|
+
}
|
|
319
|
+
info={ __(
|
|
320
|
+
'Discards unsaved changes and loads the original image.'
|
|
321
|
+
) }
|
|
322
|
+
onClick={ () => {
|
|
323
|
+
onRestoreOriginal();
|
|
324
|
+
closeMenu();
|
|
325
|
+
} }
|
|
326
|
+
>
|
|
327
|
+
{ __( 'Restore original image' ) }
|
|
328
|
+
</MenuItem>
|
|
329
|
+
</MenuGroup>
|
|
330
|
+
) }
|
|
331
|
+
<MenuGroup>
|
|
332
|
+
<MenuItem
|
|
333
|
+
icon={ keyboard }
|
|
334
|
+
iconPosition="left"
|
|
335
|
+
onClick={ () => {
|
|
336
|
+
setIsShortcutsModalOpen( true );
|
|
337
|
+
closeMenu();
|
|
338
|
+
} }
|
|
339
|
+
>
|
|
340
|
+
{ __( 'Keyboard shortcuts' ) }
|
|
341
|
+
</MenuItem>
|
|
342
|
+
</MenuGroup>
|
|
343
|
+
</>
|
|
344
|
+
) }
|
|
345
|
+
</DropdownMenu>
|
|
346
|
+
) }
|
|
274
347
|
{ showCloseButton && (
|
|
275
348
|
<Button
|
|
276
349
|
size="compact"
|
|
@@ -294,7 +367,7 @@ function HistoryActions() {
|
|
|
294
367
|
const { isImage, isUndoRedoDisabled, onReset, isWide, activePanel } =
|
|
295
368
|
useMediaEditorFrameContext();
|
|
296
369
|
const {
|
|
297
|
-
reset,
|
|
370
|
+
cropper: { reset },
|
|
298
371
|
isDirty,
|
|
299
372
|
hasUndo,
|
|
300
373
|
hasRedo,
|
|
@@ -435,7 +508,7 @@ function MediaEditorContent( {
|
|
|
435
508
|
noticesPortalElement,
|
|
436
509
|
shouldCloseOnEsc = false,
|
|
437
510
|
}: MediaEditorProps ) {
|
|
438
|
-
const
|
|
511
|
+
const session = useMediaEditor();
|
|
439
512
|
// Width decides whether the settings panel docks beside the canvas or
|
|
440
513
|
// takes the whole body. It docks from `small` (600px): the modal is the
|
|
441
514
|
// viewport less a 16px margin either side, so a 320px panel and the
|
|
@@ -483,41 +556,16 @@ function MediaEditorContent( {
|
|
|
483
556
|
// its own controls, so it moves that cluster elsewhere. The same width
|
|
484
557
|
// undocks the panel, so this is `isWide` rather than a second query.
|
|
485
558
|
const layout: 'wide' | 'narrow' = isWide ? 'wide' : 'narrow';
|
|
486
|
-
const
|
|
487
|
-
( select ) =>
|
|
488
|
-
|
|
489
|
-
getEditedEntityRecord,
|
|
490
|
-
getEntityRecord,
|
|
491
|
-
hasEditsForEntityRecord,
|
|
492
|
-
} = select( coreStore );
|
|
493
|
-
// Trigger an _embed fetch so `_embedded.author` and
|
|
494
|
-
// `_embedded['wp:attached-to']` land on the record for the Details
|
|
495
|
-
// fields to read. `getEditedEntityRecord` doesn't formally accept a
|
|
496
|
-
// query, so we can't embed via that selector directly.
|
|
497
|
-
getEntityRecord(
|
|
559
|
+
const sourceMedia = useSelect(
|
|
560
|
+
( select ) =>
|
|
561
|
+
select( coreStore ).getEntityRecord(
|
|
498
562
|
'postType',
|
|
499
563
|
'attachment',
|
|
500
|
-
id
|
|
501
|
-
|
|
502
|
-
);
|
|
503
|
-
return {
|
|
504
|
-
media: getEditedEntityRecord(
|
|
505
|
-
'postType',
|
|
506
|
-
'attachment',
|
|
507
|
-
id
|
|
508
|
-
) as Media,
|
|
509
|
-
hasEdits: hasEditsForEntityRecord(
|
|
510
|
-
'postType',
|
|
511
|
-
'attachment',
|
|
512
|
-
id
|
|
513
|
-
),
|
|
514
|
-
};
|
|
515
|
-
},
|
|
564
|
+
id
|
|
565
|
+
) as Media,
|
|
516
566
|
[ id ]
|
|
517
567
|
);
|
|
518
568
|
|
|
519
|
-
const hasChanges = cropper.isCropperDirty || hasEdits;
|
|
520
|
-
|
|
521
569
|
const { clearEntityRecordEdits, editEntityRecord, invalidateResolution } =
|
|
522
570
|
useDispatch( coreStore );
|
|
523
571
|
const { removeAllNotices } = useDispatch( noticesStore );
|
|
@@ -526,8 +574,15 @@ function MediaEditorContent( {
|
|
|
526
574
|
const [ isPlacementActive, setIsPlacementActive ] = useState( false );
|
|
527
575
|
const [ isCanvasGestureActive, setIsCanvasGestureActive ] =
|
|
528
576
|
useState( false );
|
|
529
|
-
|
|
530
|
-
|
|
577
|
+
// Whether another attachment has replaced `id` in the cropper this
|
|
578
|
+
// session (today, the original via "Restore original image"). Stays a
|
|
579
|
+
// distinct flag (not derived from the cropper) so a bare replacement
|
|
580
|
+
// counts as a change even though swapping the source resets the
|
|
581
|
+
// cropper's own dirty baseline.
|
|
582
|
+
const [ isSourceReplaced, setIsSourceReplaced ] = useState( false );
|
|
583
|
+
const placementControlTimerRef = useRef<
|
|
584
|
+
ReturnType< typeof setTimeout > | undefined
|
|
585
|
+
>( undefined );
|
|
531
586
|
|
|
532
587
|
const signalPlacementControlInteraction = useCallback( () => {
|
|
533
588
|
setIsPlacementActive( true );
|
|
@@ -553,19 +608,103 @@ function MediaEditorContent( {
|
|
|
553
608
|
useEffect( () => {
|
|
554
609
|
setIsPlacementActive( false );
|
|
555
610
|
setIsCanvasGestureActive( false );
|
|
611
|
+
setIsSourceReplaced( false );
|
|
556
612
|
}, [ id ] );
|
|
557
613
|
|
|
558
|
-
//
|
|
559
|
-
//
|
|
560
|
-
//
|
|
614
|
+
// Restore-original: the edit root the edited attachment descends from,
|
|
615
|
+
// exposed by the server as the root-level `edit_root` id on the
|
|
616
|
+
// attachment (edit context, embeddable via the `wp:edit-root` link).
|
|
617
|
+
// Fetch the original's record for the URL and natural dimensions the
|
|
618
|
+
// cropper needs to seed itself.
|
|
619
|
+
const originalId: number | undefined = sourceMedia?.edit_root;
|
|
620
|
+
const originalRecord = useSelect(
|
|
621
|
+
( select ) =>
|
|
622
|
+
originalId
|
|
623
|
+
? ( select( coreStore ).getEntityRecord(
|
|
624
|
+
'postType',
|
|
625
|
+
'attachment',
|
|
626
|
+
originalId
|
|
627
|
+
) as Media | undefined )
|
|
628
|
+
: undefined,
|
|
629
|
+
[ originalId ]
|
|
630
|
+
);
|
|
631
|
+
const originalWidth = Number( originalRecord?.media_details?.width );
|
|
632
|
+
const originalHeight = Number( originalRecord?.media_details?.height );
|
|
633
|
+
const originalUrl = originalRecord?.source_url;
|
|
634
|
+
const originalSource =
|
|
635
|
+
originalId &&
|
|
636
|
+
originalRecord &&
|
|
637
|
+
typeof originalUrl === 'string' &&
|
|
638
|
+
originalUrl.length > 0 &&
|
|
639
|
+
Number.isFinite( originalWidth ) &&
|
|
640
|
+
originalWidth > 0 &&
|
|
641
|
+
Number.isFinite( originalHeight ) &&
|
|
642
|
+
originalHeight > 0
|
|
643
|
+
? {
|
|
644
|
+
id: originalId,
|
|
645
|
+
url: originalUrl,
|
|
646
|
+
media: originalRecord,
|
|
647
|
+
}
|
|
648
|
+
: undefined;
|
|
649
|
+
const canRestoreOriginal = !! originalSource;
|
|
650
|
+
const replacementSource =
|
|
651
|
+
isSourceReplaced && originalSource
|
|
652
|
+
? {
|
|
653
|
+
id: originalSource.id,
|
|
654
|
+
url: originalSource.url,
|
|
655
|
+
media: originalSource.media,
|
|
656
|
+
}
|
|
657
|
+
: undefined;
|
|
658
|
+
// Details and pending edits follow the attachment shown on the canvas.
|
|
659
|
+
// Keep `sourceMedia` separate for the snackbar's previous attachment.
|
|
660
|
+
const activeId = replacementSource?.id ?? id;
|
|
661
|
+
const { media, hasEdits } = useSelect(
|
|
662
|
+
( select ) => {
|
|
663
|
+
const {
|
|
664
|
+
getEditedEntityRecord,
|
|
665
|
+
getEntityRecord,
|
|
666
|
+
hasEditsForEntityRecord,
|
|
667
|
+
} = select( coreStore );
|
|
668
|
+
// Fetch embedded author and attached-post data for Details.
|
|
669
|
+
getEntityRecord(
|
|
670
|
+
'postType',
|
|
671
|
+
'attachment',
|
|
672
|
+
activeId,
|
|
673
|
+
ATTACHMENT_EMBED_QUERY
|
|
674
|
+
);
|
|
675
|
+
return {
|
|
676
|
+
media: getEditedEntityRecord(
|
|
677
|
+
'postType',
|
|
678
|
+
'attachment',
|
|
679
|
+
activeId
|
|
680
|
+
) as Media,
|
|
681
|
+
hasEdits: hasEditsForEntityRecord(
|
|
682
|
+
'postType',
|
|
683
|
+
'attachment',
|
|
684
|
+
activeId
|
|
685
|
+
),
|
|
686
|
+
};
|
|
687
|
+
},
|
|
688
|
+
[ activeId ]
|
|
689
|
+
);
|
|
690
|
+
// Refresh embedded data on opening or switching to the original.
|
|
561
691
|
useEffect( () => {
|
|
562
692
|
invalidateResolution( 'getEntityRecord', [
|
|
563
693
|
'postType',
|
|
564
694
|
'attachment',
|
|
565
|
-
|
|
695
|
+
activeId,
|
|
566
696
|
ATTACHMENT_EMBED_QUERY,
|
|
567
697
|
] );
|
|
568
|
-
}, [
|
|
698
|
+
}, [ activeId, invalidateResolution ] );
|
|
699
|
+
const handleRestoreOriginal = useCallback( () => {
|
|
700
|
+
// Restoring discards every pending edit, as the menu item says, so
|
|
701
|
+
// nothing staged against the attachment being replaced reaches a save.
|
|
702
|
+
clearEntityRecordEdits( 'postType', 'attachment', id );
|
|
703
|
+
setIsSourceReplaced( true );
|
|
704
|
+
}, [ clearEntityRecordEdits, id ] );
|
|
705
|
+
|
|
706
|
+
// A bare replacement has no cropper diff, so OR the flag in explicitly.
|
|
707
|
+
const hasChanges = session.hasOutputEdits || hasEdits || isSourceReplaced;
|
|
569
708
|
|
|
570
709
|
const mediaType = getMediaTypeFromMimeType( media?.mime_type ).type;
|
|
571
710
|
const isImage = !! media && mediaType === 'image';
|
|
@@ -602,11 +741,12 @@ function MediaEditorContent( {
|
|
|
602
741
|
resetCropOptions,
|
|
603
742
|
} = useCropOptions( { aspectRatioPresets } );
|
|
604
743
|
const { isSaving, save: saveMediaEditor } = useSaveMediaEditor( {
|
|
605
|
-
|
|
744
|
+
session,
|
|
606
745
|
id,
|
|
607
746
|
isImage,
|
|
608
|
-
media,
|
|
747
|
+
media: sourceMedia,
|
|
609
748
|
onSaved,
|
|
749
|
+
replacementSource,
|
|
610
750
|
} );
|
|
611
751
|
|
|
612
752
|
const handleChange = ( updates: Partial< Media > ) => {
|
|
@@ -618,12 +758,13 @@ function MediaEditorContent( {
|
|
|
618
758
|
if ( isSaving ) {
|
|
619
759
|
return;
|
|
620
760
|
}
|
|
621
|
-
editEntityRecord( 'postType', 'attachment',
|
|
761
|
+
editEntityRecord( 'postType', 'attachment', activeId, updates );
|
|
622
762
|
};
|
|
623
763
|
|
|
624
764
|
const discardAndClose = () => {
|
|
625
765
|
removeAllNotices( 'snackbar', MEDIA_EDITOR_NOTICES_CONTEXT );
|
|
626
|
-
clearEntityRecordEdits( 'postType', 'attachment',
|
|
766
|
+
clearEntityRecordEdits( 'postType', 'attachment', activeId );
|
|
767
|
+
setIsSourceReplaced( false );
|
|
627
768
|
onClose?.();
|
|
628
769
|
};
|
|
629
770
|
|
|
@@ -656,9 +797,9 @@ function MediaEditorContent( {
|
|
|
656
797
|
return;
|
|
657
798
|
}
|
|
658
799
|
if ( isRedoShortcut ) {
|
|
659
|
-
|
|
800
|
+
session.redo();
|
|
660
801
|
} else {
|
|
661
|
-
|
|
802
|
+
session.undo();
|
|
662
803
|
}
|
|
663
804
|
}
|
|
664
805
|
}
|
|
@@ -850,6 +991,9 @@ function MediaEditorContent( {
|
|
|
850
991
|
onCancel: handleRequestClose,
|
|
851
992
|
onSave: saveMediaEditor,
|
|
852
993
|
onReset: resetCropOptions,
|
|
994
|
+
canRestoreOriginal,
|
|
995
|
+
isSourceReplaced,
|
|
996
|
+
onRestoreOriginal: handleRestoreOriginal,
|
|
853
997
|
};
|
|
854
998
|
|
|
855
999
|
return (
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { page, userEvent } from 'vitest/browser';
|
|
3
|
+
import { screen, waitFor } from '@testing-library/react';
|
|
4
|
+
import { cleanup, render } from 'vitest-browser-react';
|
|
5
|
+
import { createRegistry, RegistryProvider } from '@wordpress/data';
|
|
6
|
+
import { store as coreStore } from '@wordpress/core-data';
|
|
7
|
+
import { store as noticesStore } from '@wordpress/notices';
|
|
8
|
+
import '@wordpress/components/src/style.scss';
|
|
9
|
+
import MediaEditor, { type MediaEditorFrameProps } from '../index';
|
|
10
|
+
import type { Media } from '../../media-editor-provider';
|
|
11
|
+
import '../../../style.scss';
|
|
12
|
+
|
|
13
|
+
const original: Media = {
|
|
14
|
+
id: 10,
|
|
15
|
+
source_url:
|
|
16
|
+
'data:image/svg+xml,' +
|
|
17
|
+
encodeURIComponent(
|
|
18
|
+
'<svg xmlns="http://www.w3.org/2000/svg" width="600" height="400"><rect width="600" height="400" fill="black"/></svg>'
|
|
19
|
+
),
|
|
20
|
+
mime_type: 'image/svg+xml',
|
|
21
|
+
media_details: { width: 600, height: 400 },
|
|
22
|
+
alt_text: 'Original alternative text',
|
|
23
|
+
title: { raw: 'Original title', rendered: 'Original title' },
|
|
24
|
+
post: 5,
|
|
25
|
+
};
|
|
26
|
+
const cropped: Media = {
|
|
27
|
+
...original,
|
|
28
|
+
id: 11,
|
|
29
|
+
source_url: `${ original.source_url }#cropped`,
|
|
30
|
+
edit_root: 10,
|
|
31
|
+
alt_text: 'Cropped alternative text',
|
|
32
|
+
title: { raw: 'Cropped title', rendered: 'Cropped title' },
|
|
33
|
+
post: 6,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function Frame( { children }: MediaEditorFrameProps ) {
|
|
37
|
+
return (
|
|
38
|
+
<div
|
|
39
|
+
style={ {
|
|
40
|
+
display: 'flex',
|
|
41
|
+
flexDirection: 'column',
|
|
42
|
+
width: 900,
|
|
43
|
+
height: 650,
|
|
44
|
+
} }
|
|
45
|
+
>
|
|
46
|
+
<MediaEditor.HeaderActions />
|
|
47
|
+
{ children }
|
|
48
|
+
<MediaEditor.SaveActions />
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function setup() {
|
|
54
|
+
await page.viewport( 1000, 800 );
|
|
55
|
+
const writes = vi.fn();
|
|
56
|
+
vi.spyOn( window, 'fetch' ).mockImplementation(
|
|
57
|
+
async ( input, options ) => {
|
|
58
|
+
const url = new URL( String( input ), window.location.href );
|
|
59
|
+
const record = url.pathname.startsWith( '/wp/v2/media/10' )
|
|
60
|
+
? original
|
|
61
|
+
: cropped;
|
|
62
|
+
if ( options?.method === 'POST' ) {
|
|
63
|
+
const data = JSON.parse( String( options.body ) );
|
|
64
|
+
writes( url.pathname, data );
|
|
65
|
+
return Response.json( {
|
|
66
|
+
...record,
|
|
67
|
+
...data,
|
|
68
|
+
...( url.pathname.endsWith( '/edit' ) ? { id: 12 } : {} ),
|
|
69
|
+
} );
|
|
70
|
+
}
|
|
71
|
+
return Response.json( record );
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
const registry = createRegistry();
|
|
75
|
+
registry.register( coreStore );
|
|
76
|
+
registry.register( noticesStore );
|
|
77
|
+
registry.dispatch( coreStore ).addEntities( [
|
|
78
|
+
{
|
|
79
|
+
kind: 'postType',
|
|
80
|
+
name: 'attachment',
|
|
81
|
+
baseURL: '/wp/v2/media',
|
|
82
|
+
baseURLParams: { context: 'edit' },
|
|
83
|
+
rawAttributes: [ 'title', 'caption', 'description' ],
|
|
84
|
+
},
|
|
85
|
+
] );
|
|
86
|
+
registry
|
|
87
|
+
.dispatch( coreStore )
|
|
88
|
+
.receiveEntityRecords( 'postType', 'attachment', [
|
|
89
|
+
original,
|
|
90
|
+
cropped,
|
|
91
|
+
] );
|
|
92
|
+
const onSaved = vi.fn();
|
|
93
|
+
await render(
|
|
94
|
+
<RegistryProvider value={ registry }>
|
|
95
|
+
<MediaEditor
|
|
96
|
+
id={ 11 }
|
|
97
|
+
onSaved={ onSaved }
|
|
98
|
+
renderFrame={ Frame }
|
|
99
|
+
fields={ [
|
|
100
|
+
{
|
|
101
|
+
id: 'alt_text',
|
|
102
|
+
label: 'Alternative text',
|
|
103
|
+
type: 'text',
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
id: 'title',
|
|
107
|
+
label: 'Title',
|
|
108
|
+
type: 'text',
|
|
109
|
+
getValue: ( { item } ) =>
|
|
110
|
+
typeof item.title === 'string'
|
|
111
|
+
? item.title
|
|
112
|
+
: item.title?.raw,
|
|
113
|
+
},
|
|
114
|
+
] }
|
|
115
|
+
/>
|
|
116
|
+
</RegistryProvider>
|
|
117
|
+
);
|
|
118
|
+
await userEvent.click( screen.getByRole( 'tab', { name: 'Details' } ) );
|
|
119
|
+
return { registry, writes, onSaved };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function restoreOriginal() {
|
|
123
|
+
await userEvent.click(
|
|
124
|
+
screen.getByRole( 'button', { name: 'More options' } )
|
|
125
|
+
);
|
|
126
|
+
await userEvent.click(
|
|
127
|
+
screen.getByRole( 'menuitem', { name: /^Restore original image/ } )
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
afterEach( async () => {
|
|
132
|
+
await cleanup();
|
|
133
|
+
vi.restoreAllMocks();
|
|
134
|
+
} );
|
|
135
|
+
|
|
136
|
+
describe( 'Restore original', () => {
|
|
137
|
+
it( 'loads editable original details and discards the cropped attachment edits', async () => {
|
|
138
|
+
const { registry } = await setup();
|
|
139
|
+
expect( screen.getByRole( 'textbox', { name: 'Title' } ) ).toHaveValue(
|
|
140
|
+
'Cropped title'
|
|
141
|
+
);
|
|
142
|
+
await userEvent.fill(
|
|
143
|
+
screen.getByRole( 'textbox', { name: 'Alternative text' } ),
|
|
144
|
+
'Unsaved cropped text'
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
await restoreOriginal();
|
|
148
|
+
|
|
149
|
+
expect( screen.getByRole( 'textbox', { name: 'Title' } ) ).toHaveValue(
|
|
150
|
+
'Original title'
|
|
151
|
+
);
|
|
152
|
+
const alt = screen.getByRole( 'textbox', { name: 'Alternative text' } );
|
|
153
|
+
expect( alt ).toBeEnabled();
|
|
154
|
+
expect( alt ).toHaveValue( 'Original alternative text' );
|
|
155
|
+
expect(
|
|
156
|
+
registry
|
|
157
|
+
.select( coreStore )
|
|
158
|
+
.hasEditsForEntityRecord( 'postType', 'attachment', 11 )
|
|
159
|
+
).toBe( false );
|
|
160
|
+
} );
|
|
161
|
+
|
|
162
|
+
it( 'saves an untouched restore without writing an attachment', async () => {
|
|
163
|
+
const { writes, onSaved } = await setup();
|
|
164
|
+
await restoreOriginal();
|
|
165
|
+
|
|
166
|
+
await userEvent.click( screen.getByRole( 'button', { name: 'Save' } ) );
|
|
167
|
+
|
|
168
|
+
expect( writes ).not.toHaveBeenCalled();
|
|
169
|
+
expect( onSaved ).toHaveBeenCalledWith( {
|
|
170
|
+
id: 10,
|
|
171
|
+
url: original.source_url,
|
|
172
|
+
media: original,
|
|
173
|
+
previous: { id: 11, url: cropped.source_url },
|
|
174
|
+
} );
|
|
175
|
+
} );
|
|
176
|
+
|
|
177
|
+
it.each( [
|
|
178
|
+
{ withTransform: false, target: 'the original attachment' },
|
|
179
|
+
{ withTransform: true, target: 'a new attachment after rotating' },
|
|
180
|
+
] )( 'saves restored details to $target', async ( { withTransform } ) => {
|
|
181
|
+
const { registry, writes, onSaved } = await setup();
|
|
182
|
+
await restoreOriginal();
|
|
183
|
+
await userEvent.fill(
|
|
184
|
+
screen.getByRole( 'textbox', { name: 'Alternative text' } ),
|
|
185
|
+
'Updated original text'
|
|
186
|
+
);
|
|
187
|
+
if ( withTransform ) {
|
|
188
|
+
await userEvent.click(
|
|
189
|
+
screen.getByRole( 'tab', { name: 'Crop' } )
|
|
190
|
+
);
|
|
191
|
+
await userEvent.click(
|
|
192
|
+
screen.getByRole( 'button', {
|
|
193
|
+
name: 'Rotate 90° clockwise',
|
|
194
|
+
} )
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
await userEvent.click( screen.getByRole( 'button', { name: 'Save' } ) );
|
|
199
|
+
|
|
200
|
+
await waitFor( () => expect( onSaved ).toHaveBeenCalledTimes( 1 ) );
|
|
201
|
+
expect( writes ).toHaveBeenCalledExactlyOnceWith(
|
|
202
|
+
withTransform ? '/wp/v2/media/10/edit' : '/wp/v2/media/10',
|
|
203
|
+
expect.objectContaining( { alt_text: 'Updated original text' } )
|
|
204
|
+
);
|
|
205
|
+
expect( onSaved ).toHaveBeenCalledWith(
|
|
206
|
+
expect.objectContaining( {
|
|
207
|
+
id: withTransform ? 12 : 10,
|
|
208
|
+
previous: { id: 11, url: cropped.source_url },
|
|
209
|
+
} )
|
|
210
|
+
);
|
|
211
|
+
expect(
|
|
212
|
+
registry
|
|
213
|
+
.select( coreStore )
|
|
214
|
+
.hasEditsForEntityRecord( 'postType', 'attachment', 10 )
|
|
215
|
+
).toBe( false );
|
|
216
|
+
// Only the transform case writes to `/edit`; expressed as one
|
|
217
|
+
// unconditional assertion so each case asserts its own expectation.
|
|
218
|
+
const editWrite = withTransform
|
|
219
|
+
? writes.mock.calls[ 0 ][ 1 ]
|
|
220
|
+
: undefined;
|
|
221
|
+
expect( { src: editWrite?.src, post: editWrite?.post } ).toEqual(
|
|
222
|
+
withTransform
|
|
223
|
+
? { src: original.source_url, post: original.post }
|
|
224
|
+
: { src: undefined, post: undefined }
|
|
225
|
+
);
|
|
226
|
+
} );
|
|
227
|
+
} );
|
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
2
|
import { fireEvent, render, screen } from '@testing-library/react';
|
|
3
|
+
import { useEffect } from '@wordpress/element';
|
|
3
4
|
import { useCropOptions } from '../use-crop-options';
|
|
4
5
|
import { ORIGINAL_ASPECT_RATIO } from '../../../image-editor/core/constants';
|
|
5
|
-
import { MediaEditorStateProvider } from '../../../state';
|
|
6
|
+
import { MediaEditorStateProvider, useMediaEditor } from '../../../state';
|
|
7
|
+
|
|
8
|
+
const IMAGE = {
|
|
9
|
+
src: 'test.jpg',
|
|
10
|
+
naturalWidth: 1200,
|
|
11
|
+
naturalHeight: 600,
|
|
12
|
+
};
|
|
6
13
|
|
|
7
14
|
function CropOptionsHarness() {
|
|
15
|
+
// Load the image through the session, as the canvas does.
|
|
16
|
+
const { setSourceImage } = useMediaEditor();
|
|
17
|
+
useEffect( () => setSourceImage( IMAGE ), [ setSourceImage ] );
|
|
8
18
|
const cropOptions = useCropOptions( {
|
|
9
19
|
aspectRatioPresets: [
|
|
10
20
|
{ label: 'Square', value: 1 },
|
|
@@ -47,15 +57,7 @@ function CropOptionsHarness() {
|
|
|
47
57
|
|
|
48
58
|
function renderHarness() {
|
|
49
59
|
render(
|
|
50
|
-
<MediaEditorStateProvider
|
|
51
|
-
initialCropperState={ {
|
|
52
|
-
image: {
|
|
53
|
-
src: 'test.jpg',
|
|
54
|
-
naturalWidth: 1200,
|
|
55
|
-
naturalHeight: 600,
|
|
56
|
-
},
|
|
57
|
-
} }
|
|
58
|
-
>
|
|
60
|
+
<MediaEditorStateProvider>
|
|
59
61
|
<CropOptionsHarness />
|
|
60
62
|
</MediaEditorStateProvider>
|
|
61
63
|
);
|
|
@@ -50,7 +50,7 @@ export function useCropOptions( {
|
|
|
50
50
|
}: UseCropOptionsArgs = {} ): UseCropOptionsReturn {
|
|
51
51
|
const controller = useMediaEditor();
|
|
52
52
|
const { aspectRatioValue } = controller.cropOptions;
|
|
53
|
-
const cropperImage = controller.state.image;
|
|
53
|
+
const cropperImage = controller.cropper.state.image;
|
|
54
54
|
|
|
55
55
|
const aspectRatioOptions = useMemo(
|
|
56
56
|
() => getAspectRatioOptions( aspectRatioPresets ),
|