@@ -3,53 +3,36 @@ import { toolbarButtonProps } from "@html_editor/main/toolbar/toolbar";
3
3
import { registry } from "@web/core/registry" ;
4
4
import { ImageTransformation } from "./image_transformation" ;
5
5
6
- export class ImageTransformButton extends Component {
7
- static template = "html_editor.ImageTransformButton" ;
8
- static props = {
9
- id : String ,
10
- icon : String ,
11
- title : String ,
12
- getSelectedImage : Function ,
13
- resetImageTransformation : Function ,
14
- addStep : Function ,
15
- document : { validate : ( p ) => p . nodeType === Node . DOCUMENT_NODE } ,
16
- editable : { validate : ( p ) => p . nodeType === Node . ELEMENT_NODE } ,
17
- ...toolbarButtonProps ,
18
- activeTitle : String ,
19
- } ;
20
-
21
- setup ( ) {
22
- this . state = useState ( { active : false } ) ;
23
- this . pointerDownInsideTransform = false ;
24
- // We close the image transform when we click outside any element not
25
- // related to it. When the pointerdown of the click is inside the image
26
- // transform and pointerup is outside while resizing or rotating the
27
- // image it will consider the click as being done outside image
28
- // transform. So we need to keep track if the pointerdown is inside or
29
- // outside to know if we want to close the image transform component or
30
- // not.
31
- useExternalListener ( this . props . document , "pointerdown" , ( ev ) => {
32
- if ( this . isNodeInsideTransform ( ev . target ) ) {
33
- this . pointerDownInsideTransform = true ;
34
- } else {
35
- this . closeImageTransformation ( ) ;
36
- this . pointerDownInsideTransform = false ;
37
- }
38
- } ) ;
39
- useExternalListener ( this . props . document , "click" , ( ev ) => {
40
- if ( ! this . isNodeInsideTransform ( ev . target ) && ! this . pointerDownInsideTransform ) {
41
- this . closeImageTransformation ( ) ;
42
- }
43
- this . pointerDownInsideTransform = false ;
44
- } ) ;
45
- // When we click on any character the image is deleted and we need to close the image transform
46
- // We handle this by selectionchange
47
- useExternalListener ( this . props . document , "selectionchange" , ( ev ) => {
48
- this . closeImageTransformation ( ) ;
49
- } ) ;
50
- }
6
+ export function useTransformOperations ( state , document , editable , addStep ) {
7
+ let pointerDownInsideTransform = false ;
8
+ // We close the image transform when we click outside any element not
9
+ // related to it. When the pointerdown of the click is inside the image
10
+ // transform and pointerup is outside while resizing or rotating the
11
+ // image it will consider the click as being done outside image
12
+ // transform. So we need to keep track if the pointerdown is inside or
13
+ // outside to know if we want to close the image transform component or
14
+ // not.
15
+ useExternalListener ( document , "pointerdown" , ( ev ) => {
16
+ if ( isNodeInsideTransform ( ev . target ) ) {
17
+ pointerDownInsideTransform = true ;
18
+ } else {
19
+ closeImageTransformation ( ) ;
20
+ pointerDownInsideTransform = false ;
21
+ }
22
+ } ) ;
23
+ useExternalListener ( document , "click" , ( ev ) => {
24
+ if ( ! isNodeInsideTransform ( ev . target ) && ! pointerDownInsideTransform ) {
25
+ closeImageTransformation ( ) ;
26
+ }
27
+ pointerDownInsideTransform = false ;
28
+ } ) ;
29
+ // When we click on any character the image is deleted and we need to close the image transform
30
+ // We handle this by selectionchange
31
+ useExternalListener ( document , "selectionchange" , ( ev ) => {
32
+ closeImageTransformation ( ) ;
33
+ } ) ;
51
34
52
- isNodeInsideTransform ( node ) {
35
+ function isNodeInsideTransform ( node ) {
53
36
if ( ! node ) {
54
37
return false ;
55
38
}
@@ -60,7 +43,7 @@ export class ImageTransformButton extends Component {
60
43
return true ;
61
44
}
62
45
if (
63
- this . isImageTransformationOpen ( ) &&
46
+ isImageTransformationOpen ( ) &&
64
47
node . matches (
65
48
".transfo-container, .transfo-container div, .transfo-container i, .transfo-container span"
66
49
)
@@ -70,41 +53,74 @@ export class ImageTransformButton extends Component {
70
53
return false ;
71
54
}
72
55
73
- onButtonClick ( ) {
74
- this . handleImageTransformation ( this . props . getSelectedImage ( ) ) ;
75
- }
76
-
77
- handleImageTransformation ( image ) {
78
- if ( this . isImageTransformationOpen ( ) ) {
79
- this . props . resetImageTransformation ( image ) ;
80
- this . closeImageTransformation ( ) ;
81
- } else {
82
- this . openImageTransformation ( image ) ;
83
- }
84
- }
85
-
86
- openImageTransformation ( image ) {
87
- this . state . active = true ;
56
+ function openImageTransformation ( image ) {
57
+ state . active = true ;
88
58
registry . category ( "main_components" ) . add ( "ImageTransformation" , {
89
59
Component : ImageTransformation ,
90
60
props : {
91
61
image,
92
- document : this . props . document ,
93
- editable : this . props . editable ,
94
- destroy : ( ) => this . closeImageTransformation ( ) ,
95
- onChange : ( ) => this . props . addStep ( ) ,
62
+ document : document ,
63
+ editable : editable ,
64
+ destroy : ( ) => closeImageTransformation ( ) ,
65
+ onChange : ( ) => addStep ( ) ,
96
66
} ,
97
67
} ) ;
98
68
}
99
69
100
- isImageTransformationOpen ( ) {
70
+ function isImageTransformationOpen ( ) {
101
71
return registry . category ( "main_components" ) . contains ( "ImageTransformation" ) ;
102
72
}
103
73
104
- closeImageTransformation ( ) {
105
- this . state . active = false ;
106
- if ( this . isImageTransformationOpen ( ) ) {
74
+ function closeImageTransformation ( ) {
75
+ state . active = false ;
76
+ if ( isImageTransformationOpen ( ) ) {
107
77
registry . category ( "main_components" ) . remove ( "ImageTransformation" ) ;
108
78
}
109
79
}
80
+
81
+ return {
82
+ pointerDownInsideTransform : pointerDownInsideTransform ,
83
+ isNodeInsideTransform : isNodeInsideTransform ,
84
+ openImageTransformation : openImageTransformation ,
85
+ isImageTransformationOpen : isImageTransformationOpen ,
86
+ closeImageTransformation : closeImageTransformation ,
87
+ }
88
+ }
89
+ export class ImageTransformButton extends Component {
90
+ static template = "html_editor.ImageTransformButton" ;
91
+ static props = {
92
+ id : String ,
93
+ icon : String ,
94
+ title : String ,
95
+ getSelectedImage : Function ,
96
+ resetImageTransformation : Function ,
97
+ addStep : Function ,
98
+ document : { validate : ( p ) => p . nodeType === Node . DOCUMENT_NODE } ,
99
+ editable : { validate : ( p ) => p . nodeType === Node . ELEMENT_NODE } ,
100
+ ...toolbarButtonProps ,
101
+ activeTitle : String ,
102
+ } ;
103
+
104
+ setup ( ) {
105
+ this . state = useState ( { active : false } ) ;
106
+ Object . assign ( this . props , useTransformOperations (
107
+ this . state ,
108
+ this . props . document ,
109
+ this . props . editable ,
110
+ this . props . addStep
111
+ ) ) ;
112
+ }
113
+
114
+ onButtonClick ( ) {
115
+ this . handleImageTransformation ( this . props . getSelectedImage ( ) ) ;
116
+ }
117
+
118
+ handleImageTransformation ( image ) {
119
+ if ( this . props . isImageTransformationOpen ( ) ) {
120
+ this . props . resetImageTransformation ( image ) ;
121
+ this . props . closeImageTransformation ( ) ;
122
+ } else {
123
+ this . props . openImageTransformation ( image ) ;
124
+ }
125
+ }
110
126
}
0 commit comments