6
6
* @Description : 锁定文件
7
7
*/
8
8
import { fabric } from 'fabric' ;
9
- import { SelectMode } from '../eventType' ;
9
+ import { SelectEvent , SelectMode } from '../eventType' ;
10
10
import type { IEditor , IPluginTempl } from '@kuaitu/core' ;
11
+ import lockImg from '../assets/lock.svg?url' ;
12
+ // import lockImg from '../assets/rotateicon.svg?url';
13
+ // import unlockImg from '../assets/unlock.svg?url'
11
14
12
15
type IPlugin = Pick < LockPlugin , 'lock' | 'unLock' > ;
13
16
@@ -24,13 +27,100 @@ enum ItypeKey {
24
27
lockScalingY = 'lockScalingY' ,
25
28
}
26
29
30
+ enum IControlKey {
31
+ bl = 'bl' ,
32
+ br = 'br' ,
33
+ mb = 'mb' ,
34
+ ml = 'ml' ,
35
+ mr = 'mr' ,
36
+ mt = 'mt' ,
37
+ tl = 'tl' ,
38
+ tr = 'tr' ,
39
+ mtr = 'mtr' ,
40
+ lock = 'lock' ,
41
+ }
42
+
27
43
export default class LockPlugin implements IPluginTempl {
28
44
static pluginName = 'LockPlugin' ;
29
45
static apis = [ 'lock' , 'unLock' ] ;
30
- constructor ( public canvas : fabric . Canvas , public editor : IEditor ) { }
46
+ constructor ( public canvas : fabric . Canvas , public editor : IEditor ) {
47
+ this . init ( ) ;
48
+ }
49
+
50
+ init ( ) {
51
+ const imgEl = document . createElement ( 'img' ) ;
52
+ imgEl . src = lockImg ;
53
+ const that = this ;
54
+ function renderIcon (
55
+ ctx : CanvasRenderingContext2D ,
56
+ left : number ,
57
+ top : number ,
58
+ styleOverride : any ,
59
+ fabricObject : fabric . Object
60
+ ) {
61
+ const iconWith = 25 ;
62
+ ctx . save ( ) ;
63
+ ctx . translate ( left , top ) ;
64
+ const angle = fabricObject . angle as number ;
65
+ ctx . rotate ( fabric . util . degreesToRadians ( angle ) ) ;
66
+ ctx . drawImage ( imgEl , - iconWith / 2 , - iconWith / 2 , iconWith , iconWith ) ;
67
+ ctx . restore ( ) ;
68
+ }
69
+
70
+ function unLockObject ( eventData : any , transform : any ) : boolean {
71
+ that . unLock ( ) ;
72
+ return true ;
73
+ }
74
+
75
+ fabric . Object . prototype . controls . lock = new fabric . Control ( {
76
+ x : 0.5 ,
77
+ y : 0.5 ,
78
+ offsetY : 0 ,
79
+ cursorStyle : 'pointer' ,
80
+ mouseUpHandler : unLockObject ,
81
+ render : renderIcon ,
82
+ } ) ;
83
+
84
+ fabric . Textbox . prototype . controls . lock = new fabric . Control ( {
85
+ x : 0.5 ,
86
+ y : 0.5 ,
87
+ offsetY : 0 ,
88
+ cursorStyle : 'pointer' ,
89
+ mouseUpHandler : unLockObject ,
90
+ render : renderIcon ,
91
+ } ) ;
92
+ this . canvas . on ( 'selection:created' , ( ) => this . renderCornerByActiveObj ( ) ) ;
93
+ this . canvas . on ( 'selection:updated' , ( ) => this . renderCornerByActiveObj ( ) ) ;
94
+ }
95
+
96
+ controlCornersVisible ( obj : fabric . Object ) {
97
+ const isLocked = obj . lockMovementX ;
98
+ Object . values ( IControlKey ) . forEach ( ( key : IControlKey ) => {
99
+ if ( key === IControlKey . lock ) {
100
+ obj . setControlVisible ( key , isLocked ) ;
101
+ } else {
102
+ obj . setControlVisible ( key , ! isLocked ) ;
103
+ }
104
+ } ) ;
105
+ }
106
+
107
+ renderCornerByActiveObj ( ) {
108
+ const actives = this . canvas
109
+ . getActiveObjects ( )
110
+ . filter ( ( item ) => ! ( item instanceof fabric . GuideLine ) ) ;
111
+ if ( actives && actives . length === 1 ) {
112
+ const active = actives [ 0 ] ;
113
+ this . controlCornersVisible ( active ) ;
114
+ } else if ( actives && actives . length > 1 ) {
115
+ const active = this . canvas . getActiveObject ( ) ;
116
+ if ( active ) {
117
+ this . controlCornersVisible ( active ) ;
118
+ }
119
+ }
120
+ }
31
121
32
122
hookImportAfter ( ) {
33
- this . canvas . forEachObject ( ( obj ) => {
123
+ this . canvas . forEachObject ( ( obj : fabric . Object ) => {
34
124
if ( obj . hasControls === false && obj . selectable === false ) {
35
125
this . canvas . setActiveObject ( obj ) ;
36
126
this . lock ( ) ;
@@ -42,14 +132,13 @@ export default class LockPlugin implements IPluginTempl {
42
132
lock ( ) {
43
133
const activeObject = this . canvas . getActiveObject ( ) as fabric . Object ;
44
134
if ( activeObject ) {
45
- activeObject . hasControls = false ;
46
- activeObject . selectable = false ;
47
- activeObject . evented = false ;
48
135
// 修改默认属性
49
136
Object . values ( ItypeKey ) . forEach ( ( key : ItypeKey ) => {
50
137
activeObject [ key ] = true ;
51
138
} ) ;
52
- this . canvas . discardActiveObject ( ) . renderAll ( ) ;
139
+ this . controlCornersVisible ( activeObject ) ;
140
+ this . canvas . renderAll ( ) ;
141
+ this . editor . emit ( SelectEvent . ONE , [ activeObject ] ) ;
53
142
}
54
143
}
55
144
@@ -63,7 +152,9 @@ export default class LockPlugin implements IPluginTempl {
63
152
Object . values ( ItypeKey ) . forEach ( ( key : ItypeKey ) => {
64
153
activeObject [ key ] = false ;
65
154
} ) ;
66
- this . canvas . discardActiveObject ( ) . renderAll ( ) ;
155
+ this . controlCornersVisible ( activeObject ) ;
156
+ this . canvas . renderAll ( ) ;
157
+ this . editor . emit ( SelectEvent . ONE , [ activeObject ] ) ;
67
158
}
68
159
}
69
160
0 commit comments