@@ -12,6 +12,8 @@ import * as eventUtils from '../events/utils.js';
1212import * as constants from '../constants.js' ;
1313import * as renderManagement from '../render_management.js' ;
1414import * as registry from '../registry.js' ;
15+ import { Renderer as ZelosRenderer } from '../renderers/zelos/renderer.js' ;
16+ import { ConnectionType } from '../connection_type.js' ;
1517
1618/**
1719 * An error message to throw if the block created by createMarkerBlock_ is
@@ -86,52 +88,73 @@ export class InsertionMarkerPreviewer implements IConnectionPreviewer {
8688 eventUtils . disable ( ) ;
8789 try {
8890 this . hidePreview ( ) ;
89- const dragged = draggedConn . getSourceBlock ( ) ;
90- const marker = this . createInsertionMarker ( dragged ) ;
91- const markerConn = this . getMatchingConnection (
92- dragged ,
93- marker ,
94- draggedConn ,
95- ) ;
96- if ( ! markerConn ) {
97- throw Error ( 'Could not create insertion marker to preview connection' ) ;
98- }
99-
100- // Render disconnected from everything else so that we have a valid
101- // connection location.
102- marker . queueRender ( ) ;
103- renderManagement . triggerQueuedRenders ( ) ;
10491
105- // Connect() also renders the insertion marker.
106- markerConn . connect ( staticConn ) ;
107-
108- const originalOffsetToTarget = {
109- x : staticConn . x - markerConn . x ,
110- y : staticConn . y - markerConn . y ,
111- } ;
112- const originalOffsetInBlock = markerConn . getOffsetInBlock ( ) . clone ( ) ;
113- renderManagement . finishQueuedRenders ( ) . then ( ( ) => {
114- // Position so that the existing block doesn't move.
115- marker ?. positionNearConnection (
116- markerConn ,
117- originalOffsetToTarget ,
118- originalOffsetInBlock ,
119- ) ;
120- marker ?. getSvgRoot ( ) . setAttribute ( 'visibility' , 'visible' ) ;
121- } ) ;
92+ // TODO(7898): Instead of special casing, we should change the dragger to
93+ // track the change in distance between the dragged connection and the
94+ // static connection, so that it doesn't disconnect unless that
95+ // (+ a bit) has been exceeded.
96+ if ( this . shouldUseMarkerPreview ( draggedConn , staticConn ) ) {
97+ this . markerConn = this . previewMarker ( draggedConn , staticConn ) ;
98+ }
12299
123100 if ( this . workspace . getRenderer ( ) . shouldHighlightConnection ( staticConn ) ) {
124101 staticConn . highlight ( ) ;
125102 }
126103
127- this . markerConn = markerConn ;
128104 this . draggedConn = draggedConn ;
129105 this . staticConn = staticConn ;
130106 } finally {
131107 eventUtils . enable ( ) ;
132108 }
133109 }
134110
111+ private shouldUseMarkerPreview (
112+ _draggedConn : RenderedConnection ,
113+ staticConn : RenderedConnection ,
114+ ) : boolean {
115+ return (
116+ staticConn . type === ConnectionType . PREVIOUS_STATEMENT ||
117+ staticConn . type === ConnectionType . NEXT_STATEMENT ||
118+ ! ( this . workspace . getRenderer ( ) instanceof ZelosRenderer )
119+ ) ;
120+ }
121+
122+ private previewMarker (
123+ draggedConn : RenderedConnection ,
124+ staticConn : RenderedConnection ,
125+ ) : RenderedConnection {
126+ const dragged = draggedConn . getSourceBlock ( ) ;
127+ const marker = this . createInsertionMarker ( dragged ) ;
128+ const markerConn = this . getMatchingConnection ( dragged , marker , draggedConn ) ;
129+ if ( ! markerConn ) {
130+ throw Error ( 'Could not create insertion marker to preview connection' ) ;
131+ }
132+
133+ // Render disconnected from everything else so that we have a valid
134+ // connection location.
135+ marker . queueRender ( ) ;
136+ renderManagement . triggerQueuedRenders ( ) ;
137+
138+ // Connect() also renders the insertion marker.
139+ markerConn . connect ( staticConn ) ;
140+
141+ const originalOffsetToTarget = {
142+ x : staticConn . x - markerConn . x ,
143+ y : staticConn . y - markerConn . y ,
144+ } ;
145+ const originalOffsetInBlock = markerConn . getOffsetInBlock ( ) . clone ( ) ;
146+ renderManagement . finishQueuedRenders ( ) . then ( ( ) => {
147+ // Position so that the existing block doesn't move.
148+ marker ?. positionNearConnection (
149+ markerConn ,
150+ originalOffsetToTarget ,
151+ originalOffsetInBlock ,
152+ ) ;
153+ marker ?. getSvgRoot ( ) . setAttribute ( 'visibility' , 'visible' ) ;
154+ } ) ;
155+ return markerConn ;
156+ }
157+
135158 private createInsertionMarker ( origBlock : BlockSvg ) {
136159 const result = this . workspace . newBlock ( origBlock . type ) ;
137160 result . setInsertionMarker ( true ) ;
0 commit comments