@@ -13,7 +13,7 @@ type IEditor = Editor;
1313
1414class WorkspacePlugin implements IPluginTempl {
1515 static pluginName = 'WorkspacePlugin' ;
16- static events = [ 'sizeChange' , 'zoomChange' ] ;
16+ static events = [ 'sizeChange' ] ;
1717 static apis = [
1818 'big' ,
1919 'small' ,
@@ -148,69 +148,6 @@ class WorkspacePlugin implements IPluginTempl {
148148 this . auto ( ) ;
149149 }
150150
151- setCoverMask ( hack = false ) {
152- if ( ! this . coverMask || ! this . workspace ) {
153- return ;
154- }
155- const center = this . canvas . getCenter ( ) ;
156- const zoom = this . canvas . getZoom ( ) ;
157- this . canvas . zoomToPoint (
158- new fabric . Point ( center . left , center . top ) ,
159- hack ? zoom - 0.0000001 : zoom // 比较hack的方法,判断为fabric内部的数据更新问题
160- ) ;
161- if ( zoom ) {
162- const { workspaceEl } = this ;
163- const width = workspaceEl . offsetWidth ;
164- const height = workspaceEl . offsetHeight ;
165- const cWidth = width / zoom ;
166- const cHeight = height / zoom ;
167- this . coverMask . width = cWidth ;
168- this . coverMask . height = cHeight ;
169- this . coverMask . left = ( this . workspace . left || 0 ) + ( this . workspace . width ! - cWidth ) / 2 ;
170- this . coverMask . top = ( this . workspace . top || 0 ) + ( this . workspace . height ! - cHeight ) / 2 ;
171- this . workspace . clone ( ( clone : fabric . Rect ) => {
172- clone . left = - clone . width ! / 2 ;
173- clone . top = - clone . height ! / 2 ;
174- clone . inverted = true ;
175- this . coverMask ! . objectCaching = false ;
176- this . coverMask ! . clipPath = clone ;
177- this . canvas . requestRenderAll ( ) ;
178- } ) ;
179- }
180- }
181-
182- clipPath ( ) {
183- if ( this . coverMask ) {
184- return ;
185- }
186- // 超出画布不展示
187- this . workspace ?. clone ( ( cloned : fabric . Rect ) => {
188- this . canvas . clipPath = cloned ;
189- this . canvas . requestRenderAll ( ) ;
190- } ) ;
191- }
192-
193- maskEnable ( needBindLoadJSON = true ) {
194- const coverMask = new fabric . Rect ( {
195- fill : 'rgba(0,0,0,0.7)' ,
196- id : 'coverMask' ,
197- strokeWidth : 0 ,
198- } ) ;
199- coverMask . set ( 'selectable' , false ) ;
200- coverMask . set ( 'hasControls' , false ) ;
201- coverMask . set ( 'evented' , false ) ;
202- coverMask . hoverCursor = 'default' ;
203- this . canvas . on ( 'object:added' , ( ) => {
204- coverMask . bringToFront ( ) ;
205- } ) ;
206- this . canvas . clipPath = undefined ;
207- this . canvas . add ( coverMask ) ;
208- this . coverMask = coverMask ;
209- this . setCoverMask ( ) ;
210- // 适配模板和psd的loadjson,在加载完成后再入mask
211- needBindLoadJSON && this . editor . on ( 'loadJson' , ( ) => this . maskEnable ( false ) ) ;
212- }
213-
214151 setZoomAuto ( scale : number , cb ?: ( left ?: number , top ?: number ) => void ) {
215152 const { workspaceEl } = this ;
216153 const width = workspaceEl . offsetWidth ;
@@ -222,8 +159,7 @@ class WorkspacePlugin implements IPluginTempl {
222159 this . canvas . zoomToPoint ( new fabric . Point ( center . left , center . top ) , scale ) ;
223160 if ( ! this . workspace ) return ;
224161 this . setCenterFromObject ( this . workspace ) ;
225- // console.log(this.editor, this.canvas)
226- // this.editor && this.editor?.workspaceMaskToggle();
162+ this . editor . getPlugin ( 'MaskPlugin' ) && this . editor ?. setCoverMask ( true ) ;
227163
228164 if ( cb ) cb ( this . workspace . left , this . workspace . top ) ;
229165 }
@@ -281,25 +217,13 @@ class WorkspacePlugin implements IPluginTempl {
281217 const center = this . canvas . getCenter ( ) ;
282218 this . canvas . zoomToPoint ( new fabric . Point ( center . left , center . top ) , zoom ) ;
283219
284- // this.editor && this.editor?.workspaceMaskToggle()
220+ this . editor . getPlugin ( 'MaskPlugin' ) && this . editor ?. setCoverMask ( true ) ;
285221
286222 opt . e . preventDefault ( ) ;
287223 opt . e . stopPropagation ( ) ;
288224 } ) ;
289225 }
290226
291- clipPathOrRefreshMask ( ) {
292- if ( this . editor . getPlugin ( 'MaskPlugin' ) ) {
293- this . editor . setCoverMask ( true ) ;
294- } else {
295- // 超出画布不展示
296- this . workspace ?. clone ( ( cloned : fabric . Rect ) => {
297- this . canvas . clipPath = cloned ;
298- this . canvas . requestRenderAll ( ) ;
299- } ) ;
300- }
301- }
302-
303227 destroy ( ) {
304228 this . resizeObserver . disconnect ( ) ;
305229 this . canvas . off ( ) ;
0 commit comments