1
- import { cropperDataFieldsWithAspectRatio , isGif , loadImage } from "@html_editor/utils/image_processing" ;
1
+ import {
2
+ cropperDataFieldsWithAspectRatio ,
3
+ isGif ,
4
+ loadImage ,
5
+ } from "@html_editor/utils/image_processing" ;
2
6
import { registry } from "@web/core/registry" ;
3
7
import { Plugin } from "@html_editor/plugin" ;
4
8
import { ImageToolOption } from "./image_tool_option" ;
@@ -11,6 +15,7 @@ import {
11
15
} from "@html_builder/utils/option_sequence" ;
12
16
import { ReplaceMediaOption , searchSupportedParentLinkEl } from "./replace_media_option" ;
13
17
import { computeMaxDisplayWidth } from "./image_format_option" ;
18
+ import { ImageSizeTag } from "./image_size_tag" ;
14
19
15
20
export const REPLACE_MEDIA_SELECTOR = "img, .media_iframe_video, span.fa, i.fa" ;
16
21
export const REPLACE_MEDIA_EXCLUDE =
@@ -28,6 +33,10 @@ class ImageToolOptionPlugin extends Plugin {
28
33
] ;
29
34
static shared = [ "canHaveHoverEffect" ] ;
30
35
resources = {
36
+ builder_header_middle_buttons : {
37
+ Component : ImageSizeTag ,
38
+ selector : "img" ,
39
+ } ,
31
40
builder_options : [
32
41
withSequence ( REPLACE_MEDIA , {
33
42
OptionComponent : ReplaceMediaOption ,
@@ -49,32 +58,45 @@ class ImageToolOptionPlugin extends Plugin {
49
58
on_media_dialog_saved_handlers : async ( elements , { node } ) => {
50
59
for ( const image of elements ) {
51
60
if ( image && image . tagName === "IMG" ) {
52
- const updateImageAttributes = await this . dependencies . imagePostProcess . processImage ( {
53
- img : image ,
54
- newDataset : {
55
- formatMimetype : "image/webp" ,
56
- } ,
57
- // TODO Using a callback is currently needed to avoid
58
- // the extra RPC that would occur if loadImageInfo was
59
- // called before processImage as well. This flow can be
60
- // simplified if image infos are somehow cached.
61
- onImageInfoLoaded : async ( dataset ) => {
62
- if ( ! dataset . originalSrc || ! dataset . originalId ) {
63
- return true ;
64
- }
65
- const original = await loadImage ( dataset . originalSrc ) ;
66
- const maxWidth = dataset . width ? image . naturalWidth : original . naturalWidth ;
67
- const optimizedWidth = Math . min ( maxWidth , computeMaxDisplayWidth ( node || this . editable ) ) ;
68
- if ( ! [ "image/gif" , "image/svg+xml" ] . includes ( dataset . mimetypeBeforeConversion ) ) {
69
- // Convert to recommended format and width.
70
- dataset . resizeWidth = optimizedWidth ;
71
- } else if ( dataset . shape && dataset . mimetypeBeforeConversion !== "image/gif" ) {
72
- dataset . resizeWidth = optimizedWidth ;
73
- } else {
74
- return true ;
75
- }
76
- } ,
77
- } ) ;
61
+ const updateImageAttributes =
62
+ await this . dependencies . imagePostProcess . processImage ( {
63
+ img : image ,
64
+ newDataset : {
65
+ formatMimetype : "image/webp" ,
66
+ } ,
67
+ // TODO Using a callback is currently needed to avoid
68
+ // the extra RPC that would occur if loadImageInfo was
69
+ // called before processImage as well. This flow can be
70
+ // simplified if image infos are somehow cached.
71
+ onImageInfoLoaded : async ( dataset ) => {
72
+ if ( ! dataset . originalSrc || ! dataset . originalId ) {
73
+ return true ;
74
+ }
75
+ const original = await loadImage ( dataset . originalSrc ) ;
76
+ const maxWidth = dataset . width
77
+ ? image . naturalWidth
78
+ : original . naturalWidth ;
79
+ const optimizedWidth = Math . min (
80
+ maxWidth ,
81
+ computeMaxDisplayWidth ( node || this . editable )
82
+ ) ;
83
+ if (
84
+ ! [ "image/gif" , "image/svg+xml" ] . includes (
85
+ dataset . mimetypeBeforeConversion
86
+ )
87
+ ) {
88
+ // Convert to recommended format and width.
89
+ dataset . resizeWidth = optimizedWidth ;
90
+ } else if (
91
+ dataset . shape &&
92
+ dataset . mimetypeBeforeConversion !== "image/gif"
93
+ ) {
94
+ dataset . resizeWidth = optimizedWidth ;
95
+ } else {
96
+ return true ;
97
+ }
98
+ } ,
99
+ } ) ;
78
100
updateImageAttributes ( ) ;
79
101
}
80
102
}
@@ -91,7 +113,10 @@ class ImageToolOptionPlugin extends Plugin {
91
113
onClose : resolve ,
92
114
onSave : async ( newDataset ) => {
93
115
resolve (
94
- this . dependencies . imagePostProcess . processImage ( { img, newDataset } )
116
+ this . dependencies . imagePostProcess . processImage ( {
117
+ img,
118
+ newDataset,
119
+ } )
95
120
) ;
96
121
} ,
97
122
} ) ;
0 commit comments