-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathMediaLibraryImageEditor.tsx
More file actions
774 lines (729 loc) · 21 KB
/
MediaLibraryImageEditor.tsx
File metadata and controls
774 lines (729 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/**
* AI editing panel for the WordPress Media Library image editor.
*
* Renders preset action buttons and a Refine Image option directly
* below the native image editor toolbar. Applies AI edits to the
* existing attachment and saves the result as a new attachment.
*/
/**
* WordPress dependencies
*/
import { useState, useRef, useEffect, useCallback } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import {
Button,
TextareaControl,
RangeControl,
Spinner,
Notice,
Icon,
} from '@wordpress/components';
import { chevronLeft, chevronRight } from '@wordpress/icons';
/**
* Internal dependencies
*/
import { runAbility } from '../../../utils/run-ability';
import {
urlToBase64,
prepareExpandCanvas,
compositeDrawing,
} from '../../../utils/image';
import { uploadImage } from '../functions/upload-image';
import { useImageHistory } from '../hooks/useImageHistory';
import type {
GeneratedImageData,
ImageGenerationAbilityInput,
UploadedImage,
} from '../types';
import { MaskCanvas } from './MaskCanvas';
import type { MaskCanvasHandle } from './MaskCanvas';
type EditorState =
| 'idle'
| 'masking'
| 'generating'
| 'preview'
| 'refining'
| 'saving';
type MaskMode = 'remove' | 'replace';
interface MaskingSource {
src: string;
fromState: 'idle' | 'preview' | 'refining';
historyIndex?: number | undefined;
}
const REMOVE_ITEM_PROMPT = __(
'Remove the item circled/marked in red from the image. Replace the marked area naturally by extending the surrounding background, textures, and patterns. Seamlessly match the lighting, colors, perspective, and style. Do not introduce any new objects. The red marking is only an annotation and should not appear in the result.',
'ai'
);
const REPLACE_PROMPT_PREFIX = __(
'Replace the item circled/marked in red in the image with:',
'ai'
);
const REPLACE_PROMPT_SUFFIX = __(
'. Blend the replacement naturally with the surrounding image, matching lighting, perspective, and style. The red marking is only an annotation and should not appear in the result.',
'ai'
);
interface Preset {
label: string;
prompt: string;
icon: JSX.Element;
prepare?: ( url: string ) => Promise< string >;
requiresMask?: MaskMode;
}
const PRESETS: Preset[] = [
{
label: __( 'Expand Background', 'ai' ),
prompt: __(
'Outpaint the image to create a wider panoramic view. Expand the scene outward in all directions to fill the empty transparent border while preserving the original style, lighting, colors, and perspective. Continue textures, structures, and environmental elements naturally so the extension blends seamlessly with the original image. Preserve the original image exactly and only generate content in the empty area.',
'ai'
),
icon: <Icon icon="editor-expand" />,
prepare: ( url: string ) => prepareExpandCanvas( url ),
},
{
label: __( 'Remove Background', 'ai' ),
prompt: __(
'Remove the entire background and isolate the main subject. Replace the background with a pure solid white (#FFFFFF) background. Preserve all details of the subject and maintain natural, clean edges around the silhouette. Ensure there are no remaining environmental elements, textures, gradients, or shadows from the original background. The final result should look like a professional studio product photo with a perfectly clean white backdrop.',
'ai'
),
icon: <Icon icon="remove" />,
},
{
label: __( 'Remove Item', 'ai' ),
prompt: REMOVE_ITEM_PROMPT,
icon: <Icon icon="editor-removeformatting" />,
requiresMask: 'remove',
},
{
label: __( 'Replace Item', 'ai' ),
prompt: '',
icon: <Icon icon="migrate" />,
requiresMask: 'replace',
},
];
interface Props {
postId: number;
attachmentUrl: string;
imagePanel?: HTMLElement;
}
/**
* AI editing panel for the WordPress Media Library image editor.
*
* Shows preset action buttons and a Refine Image option directly in the panel
* below the native image editor toolbar.
*
* @param {Props} props Component props.
*/
export function MediaLibraryImageEditor( {
attachmentUrl,
imagePanel,
}: Props ) {
const [ state, setState ] = useState< EditorState >( 'idle' );
const [ prompt, setPrompt ] = useState( '' );
const [ refinePrompt, setRefinePrompt ] = useState( '' );
const [ savedUpload, setSavedUpload ] = useState< UploadedImage | null >(
null
);
const [ error, setError ] = useState< string | null >( null );
// Mask editing state.
const [ maskMode, setMaskMode ] = useState< MaskMode | null >( null );
const [ brushSize, setBrushSize ] = useState( 15 );
const [ replacePrompt, setReplacePrompt ] = useState( '' );
const [ hasMask, setHasMask ] = useState( false );
const [ maskingSource, setMaskingSource ] =
useState< MaskingSource | null >( null );
const maskCanvasRef = useRef< MaskCanvasHandle >( null );
const {
history,
historyIndex,
activeEntry,
canGoBack,
canGoForward,
addToHistory,
goBack,
goForward,
resetHistory,
} = useImageHistory();
// Hide the native image canvas once we have a generated image.
useEffect( () => {
if ( ! imagePanel ) {
return;
}
const hasGeneratedImage = historyIndex >= 0;
imagePanel.style.display = hasGeneratedImage ? 'none' : '';
return () => {
imagePanel.style.display = '';
};
}, [ historyIndex, imagePanel ] );
/**
* Generates an AI-refined version of the image.
*
* When `referenceOverride` is provided it is used directly as the
* reference image. Otherwise the attachment URL is fetched and
* converted to a data URI.
*
* @param {string} activePrompt Prompt to use for generation.
* @param {string|undefined} referenceOverride Data URI to use as reference; omit for fresh edits.
* @param {boolean} isRefinement True when refining a previously generated image.
* @param {number|undefined} refHistoryIndex History index of the entry whose image is the reference.
* @param {string|undefined} displayReference Unmodified image to show in comparison; defaults to referenceOverride.
*/
async function handleGenerate(
activePrompt: string = prompt.trim(),
referenceOverride?: string,
isRefinement: boolean = false,
refHistoryIndex?: number,
displayReference?: string
): Promise< void > {
setError( null );
setState( 'generating' );
try {
const reference =
referenceOverride ?? ( await urlToBase64( attachmentUrl ) );
const input: ImageGenerationAbilityInput = {
prompt: activePrompt,
reference,
};
const response = ( await runAbility(
'ai/image-generation',
input
) ) as GeneratedImageData;
if ( ! response?.image ) {
throw new Error(
__( 'Invalid response from image generation.', 'ai' )
);
}
const historyReference = displayReference ?? referenceOverride;
const prevData = activeEntry?.generatedData;
const previousPrompts = historyReference
? prevData?.prompts ??
( prevData?.prompt ? [ prevData.prompt ] : [] )
: [];
const promptHistory = previousPrompts.filter( Boolean );
const lastPrompt = promptHistory[ promptHistory.length - 1 ];
const prompts =
lastPrompt === activePrompt
? promptHistory
: [ ...promptHistory, activePrompt ];
addToHistory(
{ ...response, prompt: activePrompt, prompts },
historyReference,
isRefinement,
refHistoryIndex
);
setSavedUpload( null );
setState( 'preview' );
} catch ( err: any ) {
setError(
err?.message ||
__( 'An error occurred during image generation.', 'ai' )
);
// Return to whichever state triggered the generation.
setState( isRefinement ? 'refining' : 'idle' );
}
}
/**
* Saves the active generated image to the Media Library.
*/
async function handleSave(): Promise< void > {
if ( ! activeEntry ) {
return;
}
setError( null );
setState( 'saving' );
try {
const uploaded = await uploadImage( activeEntry.generatedData );
setSavedUpload( uploaded );
setState( 'preview' );
} catch ( err: any ) {
setError( err?.message || __( 'Failed to save image.', 'ai' ) );
setState( 'preview' );
}
}
/**
* Resets the panel back to the idle state.
*/
function handleReset(): void {
resetHistory();
setSavedUpload( null );
setPrompt( '' );
setRefinePrompt( '' );
setReplacePrompt( '' );
setMaskMode( null );
setMaskingSource( null );
setHasMask( false );
setError( null );
setState( 'idle' );
}
/**
* Enters masking mode for a mask-based preset.
*
* @param {MaskMode} mode Whether this is a remove or replace operation.
* @param {'idle'|'preview'|'refining'} fromState The editor state to return to on cancel.
* @param {string} src Image source to draw the mask on.
* @param {number|undefined} hIndex History index when entering from refining.
*/
function enterMasking(
mode: MaskMode,
fromState: 'idle' | 'preview' | 'refining',
src: string,
hIndex?: number
): void {
setMaskMode( mode );
setMaskingSource( { src, fromState, historyIndex: hIndex } );
setReplacePrompt( '' );
setHasMask( false );
setError( null );
setState( 'masking' );
}
/**
* Handles the "Apply" action in masking mode.
*
* Composites the user's red drawing onto the source image and sends
* the annotated image to the AI with a prompt describing the intent.
*/
const handleMaskApply = useCallback( async () => {
if ( ! maskingSource || ! maskCanvasRef.current ) {
return;
}
const canvas = maskCanvasRef.current.getCanvas();
if ( ! canvas ) {
return;
}
try {
const annotatedImage = await compositeDrawing(
maskingSource.src,
canvas
);
const activePrompt =
maskMode === 'remove'
? REMOVE_ITEM_PROMPT
: `${ REPLACE_PROMPT_PREFIX } ${ replacePrompt.trim() }${ REPLACE_PROMPT_SUFFIX }`;
const isRefinement = maskingSource.fromState === 'refining';
handleGenerate(
activePrompt,
annotatedImage,
isRefinement,
maskingSource.historyIndex,
maskingSource.src
);
} catch ( err: any ) {
setError(
err?.message ?? __( 'Failed to apply drawing to image.', 'ai' )
);
}
}, [ maskingSource, maskMode, replacePrompt ] ); // eslint-disable-line react-hooks/exhaustive-deps
const handleMaskChange = useCallback( ( value: boolean ) => {
setHasMask( value );
}, [] );
const previewSrc = activeEntry?.generatedData?.image?.data
? `data:image/png;base64,${ activeEntry.generatedData.image.data }`
: null;
// Left comparison image = the reference used to generate the active entry,
// falling back to the original attachment URL.
const comparisonLeftSrc = activeEntry?.referenceSrc ?? attachmentUrl;
const comparisonLeftLabel =
activeEntry?.referenceHistoryIndex === undefined
? __( 'Original image', 'ai' )
: sprintf(
/* translators: %d: version number */
__( 'Version %d', 'ai' ),
activeEntry.referenceHistoryIndex + 1
);
const comparisonRightLabel = sprintf(
/* translators: %d: version number */
__( 'Version %d', 'ai' ),
historyIndex + 1
);
const [ showPrompt, setShowPrompt ] = useState( false );
return (
<div className="imgedit-panel-content ai-media-library-editor">
{ state === 'idle' && (
<div className="ai-media-library-editor__idle">
<div className="ai-media-library-editor__presets">
<Button
variant="secondary"
icon={ <Icon icon="format-image" /> }
onClick={ () =>
setShowPrompt( ( show ) => ! show )
}
>
{ __( 'Refine Image', 'ai' ) }
</Button>
{ PRESETS.map( ( preset ) => (
<Button
key={ preset.label }
variant="secondary"
icon={ preset.icon }
onClick={ async () => {
if ( preset.requiresMask ) {
enterMasking(
preset.requiresMask,
'idle',
attachmentUrl
);
return;
}
const reference = preset.prepare
? await preset.prepare( attachmentUrl )
: undefined;
handleGenerate( preset.prompt, reference );
} }
>
{ preset.label }
</Button>
) ) }
</div>
{ showPrompt && (
<>
<TextareaControl
label={ __(
'Describe the refinements you want to make to the image',
'ai'
) }
value={ prompt }
onChange={ setPrompt }
rows={ 3 }
__nextHasNoMarginBottom
/>
<div className="ai-media-library-editor__actions">
<Button
variant="primary"
disabled={ ! prompt.trim() }
onClick={ () => handleGenerate() }
>
{ __( 'Generate', 'ai' ) }
</Button>
</div>
</>
) }
{ error && (
<Notice status="error" isDismissible={ false }>
{ error }
</Notice>
) }
</div>
) }
{ state === 'masking' && maskingSource && (
<div className="ai-media-library-editor__masking">
<MaskCanvas
ref={ maskCanvasRef }
imageSrc={ maskingSource.src }
brushSize={ brushSize }
onMaskChange={ handleMaskChange }
/>
<div className="ai-media-library-editor__masking-sidebar">
<RangeControl
__nextHasNoMarginBottom
label={ __( 'Brush size', 'ai' ) }
value={ brushSize }
onChange={ ( value ) =>
setBrushSize( value ?? 15 )
}
min={ 5 }
max={ 100 }
__next40pxDefaultSize
/>
<div className="ai-media-library-editor__masking-sidebar-buttons">
<Button
variant="secondary"
onClick={ () => maskCanvasRef.current?.undo() }
>
{ __( 'Undo', 'ai' ) }
</Button>
<Button
variant="secondary"
onClick={ () => maskCanvasRef.current?.clear() }
>
{ __( 'Clear', 'ai' ) }
</Button>
</div>
{ maskMode === 'replace' && (
<TextareaControl
label={ __(
'Describe what to replace with',
'ai'
) }
value={ replacePrompt }
onChange={ setReplacePrompt }
rows={ 2 }
__nextHasNoMarginBottom
/>
) }
<div className="ai-media-library-editor__masking-sidebar-actions">
<Button
variant="primary"
disabled={
! hasMask ||
( maskMode === 'replace' &&
! replacePrompt.trim() )
}
onClick={ handleMaskApply }
>
{ maskMode === 'remove'
? __( 'Remove', 'ai' )
: __( 'Replace', 'ai' ) }
</Button>
<Button
variant="tertiary"
onClick={ () => {
setError( null );
setState( maskingSource.fromState );
setMaskMode( null );
setMaskingSource( null );
} }
>
{ __( 'Cancel', 'ai' ) }
</Button>
</div>
{ error && (
<Notice status="error" isDismissible={ false }>
{ error }
</Notice>
) }
</div>
</div>
) }
{ state === 'generating' && (
<div className="ai-media-library-editor__generating">
{ previewSrc && (
<img
src={ previewSrc }
alt={ activeEntry?.generatedData?.prompt ?? '' }
className="ai-media-library-editor__preview-image"
/>
) }
<div className="ai-media-library-editor__spinner-row">
<Spinner />
<span>{ __( 'Generating image…', 'ai' ) }</span>
</div>
</div>
) }
{ state === 'preview' && previewSrc && (
<div className="ai-media-library-editor__preview">
<div className="ai-media-library-editor__presets">
{ PRESETS.map( ( preset ) => (
<Button
key={ preset.label }
variant="secondary"
icon={ preset.icon }
onClick={ async () => {
if ( preset.requiresMask ) {
enterMasking(
preset.requiresMask,
'preview',
previewSrc,
historyIndex
);
return;
}
const reference = preset.prepare
? await preset.prepare(
previewSrc as string
)
: previewSrc ?? undefined;
handleGenerate(
preset.prompt,
reference,
true,
historyIndex
);
} }
>
{ preset.label }
</Button>
) ) }
</div>
{ savedUpload && (
<Notice
status="success"
onDismiss={ () => setSavedUpload( null ) }
>
{ __( 'Image saved!', 'ai' ) }{ ' ' }
<a href={ `upload.php?item=${ savedUpload.id }` }>
{ __( 'View new image', 'ai' ) }
</a>
</Notice>
) }
<div className="ai-image-history-nav">
<Button
className="ai-image-history-nav__arrow"
icon={ chevronLeft }
disabled={ ! canGoBack }
onClick={ goBack }
label={ __( 'Previous version', 'ai' ) }
/>
<div className="ai-image-history-nav__content">
<div className="ai-media-library-editor__comparison">
<div className="ai-media-library-editor__comparison-item">
<p className="ai-media-library-editor__comparison-label">
{ comparisonLeftLabel }
</p>
<img
src={ comparisonLeftSrc }
alt={ comparisonLeftLabel }
className="ai-media-library-editor__preview-image"
/>
</div>
<div className="ai-media-library-editor__comparison-item">
<p className="ai-media-library-editor__comparison-label">
{ comparisonRightLabel }
</p>
<img
src={ previewSrc }
alt={
activeEntry?.generatedData
?.prompt ?? ''
}
className="ai-media-library-editor__preview-image is-active"
/>
</div>
</div>
</div>
<Button
className="ai-image-history-nav__arrow"
icon={ chevronRight }
disabled={ ! canGoForward }
onClick={ goForward }
label={ __( 'Next version', 'ai' ) }
/>
</div>
{ history.length > 1 && (
<p className="ai-image-history-nav__counter">
{ sprintf(
/* translators: 1: current position, 2: total count */
__( '%1$d / %2$d', 'ai' ),
historyIndex + 1,
history.length
) }
</p>
) }
<div className="ai-media-library-editor__actions">
<Button variant="primary" onClick={ handleSave }>
{ __( 'Save to Media Library', 'ai' ) }
</Button>
<Button
variant="secondary"
onClick={ () => {
setRefinePrompt( '' );
setError( null );
setState( 'refining' );
} }
>
{ __( 'Refine Image', 'ai' ) }
</Button>
<Button
variant="secondary"
onClick={ () =>
handleGenerate(
activeEntry?.generatedData.prompt ?? '',
activeEntry?.referenceSrc,
activeEntry?.isRefinement ?? false,
activeEntry?.referenceHistoryIndex
)
}
>
{ __( 'Generate Another Image', 'ai' ) }
</Button>
<Button
variant="tertiary"
isDestructive
onClick={ handleReset }
>
{ __( 'Start over', 'ai' ) }
</Button>
</div>
{ error && (
<Notice status="error" isDismissible={ false }>
{ error }
</Notice>
) }
</div>
) }
{ state === 'refining' && previewSrc && (
<div className="ai-media-library-editor__refining">
<img
src={ previewSrc }
alt={ activeEntry?.generatedData?.prompt ?? '' }
className="ai-media-library-editor__preview-image"
/>
<div className="ai-media-library-editor__presets">
{ PRESETS.map( ( preset ) => (
<Button
key={ preset.label }
variant="secondary"
icon={ preset.icon }
onClick={ () => {
if ( preset.requiresMask && previewSrc ) {
enterMasking(
preset.requiresMask,
'refining',
previewSrc,
historyIndex
);
return;
}
handleGenerate(
preset.prompt,
previewSrc,
true,
historyIndex
);
} }
>
{ preset.label }
</Button>
) ) }
</div>
<TextareaControl
label={ __(
'Describe the refinements you want to make to the image',
'ai'
) }
value={ refinePrompt }
onChange={ setRefinePrompt }
rows={ 3 }
__nextHasNoMarginBottom
/>
<div className="ai-media-library-editor__actions">
<Button
variant="primary"
disabled={ ! refinePrompt.trim() }
onClick={ () =>
handleGenerate(
refinePrompt.trim(),
previewSrc,
true,
historyIndex
)
}
>
{ __( 'Apply', 'ai' ) }
</Button>
<Button
variant="tertiary"
onClick={ () => {
setError( null );
setState( 'preview' );
} }
>
{ __( 'Cancel', 'ai' ) }
</Button>
</div>
{ error && (
<Notice status="error" isDismissible={ false }>
{ error }
</Notice>
) }
</div>
) }
{ state === 'saving' && (
<div className="ai-media-library-editor__saving">
<div className="ai-media-library-editor__spinner-row">
<Spinner />
<span>{ __( 'Saving to Media Library…', 'ai' ) }</span>
</div>
</div>
) }
</div>
);
}