25
25
</template >
26
26
27
27
<script >
28
- import { isEmpty , debounce } from ' lodash-es'
29
- import select from ' @/mixins/select'
28
+ import { isEmpty , debounce } from ' lodash-es' ;
29
+ import select from ' @/mixins/select' ;
30
30
31
- const canvasDom = document .getElementById (' canvas' ) || null
31
+ const canvasDom = document .getElementById (' canvas' ) || null ;
32
32
export default ({
33
33
name: ' mouseMenu' ,
34
34
inject: [' canvas' , ' fabric' ],
@@ -39,39 +39,39 @@ export default ({
39
39
left: 0 ,
40
40
top: 0 ,
41
41
zIndex: - 100 ,
42
- menu: null
43
- }
42
+ menu: null ,
43
+ };
44
44
},
45
45
computed: {
46
46
// 单选且等于组元素
47
47
isGroup () {
48
- return (this .mSelectMode === ' one' && this .mSelectOneType === ' group' )
48
+ return (this .mSelectMode === ' one' && this .mSelectOneType === ' group' );
49
49
},
50
50
// 是否为多选
51
- isMultiple (){
52
- return (this .mSelectMode === ' multiple' )
51
+ isMultiple () {
52
+ return (this .mSelectMode === ' multiple' );
53
53
},
54
54
},
55
55
mounted () {
56
56
this .$nextTick (() => {
57
- this .menu = this .$refs .mouseMenuRef
58
- this .menu && (this .menu .oncontextmenu = e => e .preventDefault ())
59
- this .init ()
57
+ this .menu = this .$refs .mouseMenuRef ;
58
+ this .menu && (this .menu .oncontextmenu = ( e ) => e .preventDefault ());
59
+ this .init ();
60
60
});
61
61
// 监听点击 隐藏(右键点击外部和fabric右键有冲突,因为点击非canvas只有点击左键才可以隐藏)
62
- window .addEventListener (' click' , debounce (this .clickHide , 200 ))
62
+ window .addEventListener (' click' , debounce (this .clickHide , 200 ));
63
63
},
64
64
65
65
beforeMount () {
66
- window .removeEventListener (' click' , this .clickHide )
66
+ window .removeEventListener (' click' , this .clickHide );
67
67
},
68
68
69
69
methods: {
70
70
init () {
71
71
if (! isEmpty (this .canvas ) && ! isEmpty (this .canvas .c )) {
72
72
this .canvas .c .on (' mouse:down' , this .handleMouseUp );
73
73
} else {
74
- this .hideMenu ()
74
+ this .hideMenu ();
75
75
}
76
76
},
77
77
@@ -80,96 +80,94 @@ export default ({
80
80
if (opt .button === 3 && opt .target && opt .target .id !== ' workspace' ) {
81
81
// 显示菜单,设置右键菜单位置
82
82
// 获取菜单组件的宽高
83
- const menuWidth = this .menu .offsetWidth
84
- const menuHeight = this .menu .offsetHeight
83
+ const menuWidth = this .menu .offsetWidth ;
84
+ const menuHeight = this .menu .offsetHeight ;
85
85
// 当前鼠标位置
86
- let pointX = opt .pointer .x
87
- let pointY = opt .pointer .y
86
+ let pointX = opt .pointer .x ;
87
+ let pointY = opt .pointer .y ;
88
88
89
89
// 计算菜单出现的位置
90
90
// 如果鼠标靠近画布右侧,菜单就出现在鼠标指针左侧
91
91
if (canvas .width - pointX <= menuWidth) {
92
- pointX -= menuWidth
92
+ pointX -= menuWidth;
93
93
}
94
94
// 如果鼠标靠近画布底部,菜单就出现在鼠标指针上方
95
95
if (canvas .height - pointY <= menuHeight) {
96
- pointY -= menuHeight
96
+ pointY -= menuHeight;
97
97
}
98
- this .showMenu (pointX, pointY)
99
-
98
+ this .showMenu (pointX, pointY);
100
99
} else {
101
- this .hideMenu ()
100
+ this .hideMenu ();
102
101
}
103
102
} catch (error) {
104
- console .log (error)
103
+ console .log (error);
105
104
}
106
105
},
107
106
108
107
showMenu (x , y ) {
109
- this .show = ' visible'
110
- this .left = ` ${ x} px`
111
- this .top = ` ${ y} px`
112
- this .zIndex = 100
108
+ this .show = ' visible' ;
109
+ this .left = ` ${ x} px` ;
110
+ this .top = ` ${ y} px` ;
111
+ this .zIndex = 100 ;
113
112
},
114
113
115
114
hideMenu (e ) {
116
- this .show = ' hidden'
117
- this .left = 0
118
- this .top = 0
119
- this .zIndex = - 100
115
+ this .show = ' hidden' ;
116
+ this .left = 0 ;
117
+ this .top = 0 ;
118
+ this .zIndex = - 100 ;
120
119
},
121
120
122
121
clickHide (e ) {
123
122
if ((e .target !== canvasDom) && (this .show === ' visible' )) {
124
- this .hideMenu ()
123
+ this .hideMenu ();
125
124
}
126
125
},
127
126
128
127
handleMenu (e ) {
129
- const active = e .target .dataset .active || e .srcElement .dataset .active
130
- if (! active) return this .hideMenu ()
131
- const canvas = this .canvas .c
128
+ const active = e .target .dataset .active || e .srcElement .dataset .active ;
129
+ if (! active) return this .hideMenu ();
130
+ const canvas = this .canvas .c ;
132
131
const activeObject = canvas .getActiveObjects ();
133
132
switch (active) {
134
133
case ' copy' :
135
- this .canvas .editor .clone ()
134
+ this .canvas .editor .clone ();
136
135
break ;
137
136
case ' delete' :
138
- activeObject && activeObject .map (item => canvas .remove (item))
139
- canvas .requestRenderAll ()
140
- canvas .discardActiveObject ()
137
+ activeObject && activeObject .map (( item ) => canvas .remove (item));
138
+ canvas .requestRenderAll ();
139
+ canvas .discardActiveObject ();
141
140
break ;
142
141
case ' center' :
143
- this .canvas .editor .centerAlign .position (' center' )
142
+ this .canvas .editor .centerAlign .position (' center' );
144
143
break ;
145
144
case ' group' :
146
- this .canvas .editor .group ()
145
+ this .canvas .editor .group ();
147
146
break ;
148
147
case ' unGroup' :
149
- this .canvas .editor .unGroup ()
148
+ this .canvas .editor .unGroup ();
150
149
break ;
151
150
case ' up' :
152
- this .canvas .editor .up ()
151
+ this .canvas .editor .up ();
153
152
break ;
154
153
case ' down' :
155
- this .canvas .editor .down ()
154
+ this .canvas .editor .down ();
156
155
break ;
157
156
case ' upTop' :
158
- this .canvas .editor .upTop ()
157
+ this .canvas .editor .upTop ();
159
158
break ;
160
159
case ' downTop' :
161
- this .canvas .editor .downTop ()
160
+ this .canvas .editor .downTop ();
162
161
break ;
163
162
default :
164
163
break ;
165
164
}
166
- this .hideMenu ()
167
- }
168
- }
169
- })
165
+ this .hideMenu ();
166
+ },
167
+ },
168
+ });
170
169
</script >
171
170
172
-
173
171
<style lang='less' scoped>
174
172
.menu-wrap {
175
173
width : 196px ;
@@ -204,4 +202,4 @@ export default ({
204
202
color : red ;
205
203
}
206
204
}
207
- </style >
205
+ </style >
0 commit comments