@@ -51,7 +51,7 @@ export class NewBlockLayer extends Layer<
5151 LayerContext & { canvas : HTMLCanvasElement ; ctx : CanvasRenderingContext2D }
5252> {
5353 private copyBlocks : BlockState [ ] = [ ] ;
54- private initialPoint : TPoint ;
54+ private initialPoint ! : TPoint ;
5555 private blockStates : Array < {
5656 x : number ;
5757 y : number ;
@@ -74,7 +74,7 @@ export class NewBlockLayer extends Layer<
7474 this . setContext ( {
7575 canvas : this . getCanvas ( ) ,
7676 graphCanvas : props . graph . getGraphCanvas ( ) ,
77- ctx : this . getCanvas ( ) . getContext ( "2d" ) ,
77+ ctx : this . getCanvas ( ) . getContext ( "2d" ) ?? undefined ,
7878 camera : props . camera ,
7979 constants : this . props . graph . graphConstants ,
8080 colors : this . props . graph . graphColors ,
@@ -115,13 +115,18 @@ export class NewBlockLayer extends Layer<
115115 dragCursor : "copy" ,
116116 autopanning : true ,
117117 } )
118- . on ( EVENTS . DRAG_START , ( event : MouseEvent ) => {
118+ . on ( EVENTS . DRAG_START , ( ( ...args : unknown [ ] ) => {
119+ const event = args [ 0 ] as MouseEvent ;
119120 this . onStartNewBlock ( event , target ) ;
120- } )
121- . on ( EVENTS . DRAG_UPDATE , ( event : MouseEvent ) => this . onMoveNewBlock ( event ) )
122- . on ( EVENTS . DRAG_END , ( event : MouseEvent ) => {
121+ } ) as ( ...args : unknown [ ] ) => void )
122+ . on ( EVENTS . DRAG_UPDATE , ( ( ...args : unknown [ ] ) => {
123+ const event = args [ 0 ] as MouseEvent ;
124+ this . onMoveNewBlock ( event ) ;
125+ } ) as ( ...args : unknown [ ] ) => void )
126+ . on ( EVENTS . DRAG_END , ( ( ...args : unknown [ ] ) => {
127+ const event = args [ 0 ] as MouseEvent ;
123128 this . onEndNewBlock ( event , this . context . graph . getPointInCameraSpace ( event ) ) ;
124- } ) ;
129+ } ) as ( ... args : unknown [ ] ) => void ) ;
125130 }
126131 } ;
127132
@@ -132,9 +137,10 @@ export class NewBlockLayer extends Layer<
132137 return ;
133138 }
134139
140+ const colors = this . context . colors ;
135141 render ( this . context . ctx , ( ctx ) => {
136142 ctx . beginPath ( ) ;
137- ctx . fillStyle = this . props . ghostBackground || this . context . colors . block . border ;
143+ ctx . fillStyle = this . props . ghostBackground || colors ? .block . border || "#ccc" ;
138144
139145 // Draw each block ghost
140146 for ( const blockState of this . blockStates ) {
@@ -158,9 +164,10 @@ export class NewBlockLayer extends Layer<
158164
159165 // If we have a validation function, filter out blocks that can't be duplicated
160166 if ( this . props . isDuplicateAllowed ) {
161- blockStates = selectedBlockStates . filter ( ( blockState ) =>
162- this . props . isDuplicateAllowed ( blockState . getViewComponent ( ) )
163- ) ;
167+ blockStates = selectedBlockStates . filter ( ( blockState ) => {
168+ const viewComponent = blockState . getViewComponent ( ) ;
169+ return viewComponent && this . props . isDuplicateAllowed ?.( viewComponent ) ;
170+ } ) ;
164171
165172 // If no blocks can be duplicated, exit
166173 if ( blockStates . length === 0 ) return ;
@@ -219,8 +226,8 @@ export class NewBlockLayer extends Layer<
219226 } ) ;
220227 }
221228
222- private lastMouseX : number ;
223- private lastMouseY : number ;
229+ private lastMouseX ? : number ;
230+ private lastMouseY ? : number ;
224231
225232 private onMoveNewBlock ( event : MouseEvent ) {
226233 if ( ! this . copyBlocks . length ) {
@@ -258,7 +265,7 @@ export class NewBlockLayer extends Layer<
258265 this . performRender ( ) ;
259266 }
260267
261- private onEndNewBlock ( event : MouseEvent , point : TPoint ) {
268+ private onEndNewBlock ( event : MouseEvent , point : TPoint | null ) {
262269 if ( ! this . copyBlocks . length ) {
263270 return ;
264271 }
@@ -269,6 +276,10 @@ export class NewBlockLayer extends Layer<
269276 this . lastMouseY = undefined ;
270277 this . performRender ( ) ;
271278
279+ if ( ! point ) {
280+ return ;
281+ }
282+
272283 // Calculate the offset from the initial point to the final point
273284 const offsetX = point . x - this . initialPoint . x ;
274285 const offsetY = point . y - this . initialPoint . y ;
@@ -343,7 +354,7 @@ export class NewBlockLayer extends Layer<
343354 const targetId = connection . targetBlockId ;
344355
345356 // If both source and target blocks were duplicated, create a new connection
346- if ( blockIdMap . has ( sourceId . toString ( ) ) && blockIdMap . has ( targetId . toString ( ) ) ) {
357+ if ( sourceId && targetId && blockIdMap . has ( sourceId . toString ( ) ) && blockIdMap . has ( targetId . toString ( ) ) ) {
347358 const newSourceId = blockIdMap . get ( sourceId . toString ( ) ) ;
348359 const newTargetId = blockIdMap . get ( targetId . toString ( ) ) ;
349360
0 commit comments