@@ -113,7 +113,7 @@ export const mergeOutlines = (outlines: PendingOutline[]) => {
113113} ;
114114
115115export const recalcOutlines = throttle ( ( ) => {
116- const { scheduledOutlines, activeOutlines } = ReactScanInternals ;
116+ const { scheduledOutlines, activeOutlinesMap } = ReactScanInternals ;
117117 for ( let i = scheduledOutlines . length - 1 ; i >= 0 ; i -- ) {
118118 const outline = scheduledOutlines [ i ] ;
119119 const rect = getRect ( outline . domNode ) ;
@@ -123,11 +123,16 @@ export const recalcOutlines = throttle(() => {
123123 }
124124 outline . rect = rect ;
125125 }
126- for ( let i = activeOutlines . length - 1 ; i >= 0 ; i -- ) {
127- const { outline } = activeOutlines [ i ] ;
126+
127+ const activeOutlines = Array . from ( activeOutlinesMap . values ( ) ) ;
128+
129+ for ( let i = 0 , len = activeOutlines . length ; i < len ; i ++ ) {
130+ const activeOutline = activeOutlines [ i ] ;
131+ const { outline } = activeOutline ;
128132 const rect = getRect ( outline . domNode ) ;
129133 if ( ! rect ) {
130134 activeOutlines . splice ( i , 1 ) ;
135+ activeOutlinesMap . delete ( getOutlineKey ( outline ) ) ;
131136 continue ;
132137 }
133138 outline . rect = rect ;
@@ -217,17 +222,35 @@ export const paintOutline = (
217222 log ( outline . renders ) ;
218223 }
219224
220- ReactScanInternals . activeOutlines . push ( {
221- outline,
222- alpha,
223- frame,
224- totalFrames,
225- resolve : ( ) => {
225+ const outlineKey = getOutlineKey ( outline ) ;
226+ const existingActiveOutline =
227+ ReactScanInternals . activeOutlinesMap . get ( outlineKey ) ;
228+
229+ if ( existingActiveOutline ) {
230+ existingActiveOutline . outline . renders . push ( ...outline . renders ) ;
231+ existingActiveOutline . frame = frame ;
232+ existingActiveOutline . totalFrames = totalFrames ;
233+ existingActiveOutline . alpha = alpha ;
234+ existingActiveOutline . text = getLabelText (
235+ existingActiveOutline . outline . renders ,
236+ ) ;
237+ existingActiveOutline . resolve = ( ) => {
226238 resolve ( ) ;
227- options . onPaintFinish ?.( outline ) ;
228- } ,
229- text,
230- } ) ;
239+ options . onPaintFinish ?.( existingActiveOutline . outline ) ;
240+ } ;
241+ } else {
242+ ReactScanInternals . activeOutlinesMap . set ( outlineKey , {
243+ outline,
244+ alpha,
245+ frame,
246+ totalFrames,
247+ resolve : ( ) => {
248+ resolve ( ) ;
249+ options . onPaintFinish ?.( outline ) ;
250+ } ,
251+ text,
252+ } ) ;
253+ }
231254
232255 if ( ! animationFrameId ) {
233256 fadeOutOutline ( ctx ) ;
@@ -236,7 +259,7 @@ export const paintOutline = (
236259} ;
237260
238261export const fadeOutOutline = ( ctx : CanvasRenderingContext2D ) => {
239- const { activeOutlines } = ReactScanInternals ;
262+ const { activeOutlinesMap } = ReactScanInternals ;
240263
241264 ctx . clearRect ( 0 , 0 , ctx . canvas . width , ctx . canvas . height ) ;
242265
@@ -247,34 +270,36 @@ export const fadeOutOutline = (ctx: CanvasRenderingContext2D) => {
247270
248271 const pendingLabeledOutlines : PaintedOutline [ ] = [ ] ;
249272
250- for ( let i = ReactScanInternals . activeOutlines . length - 1 ; i >= 0 ; i -- ) {
251- const animation = ReactScanInternals . activeOutlines [ i ] ;
252- const { outline, frame, totalFrames } = animation ;
273+ const activeOutlines = Array . from ( activeOutlinesMap . values ( ) ) ;
274+
275+ for ( let i = 0 , len = activeOutlines . length ; i < len ; i ++ ) {
276+ const activeOutline = activeOutlines [ i ] ;
277+ const { outline, frame, totalFrames } = activeOutline ;
253278 const { rect } = outline ;
254279 const unstable = isOutlineUnstable ( outline ) ;
255280
256281 const alphaScalar = unstable ? 0.8 : 0.2 ;
257282
258- animation . alpha = alphaScalar * ( 1 - frame / totalFrames ) ;
283+ activeOutline . alpha = alphaScalar * ( 1 - frame / totalFrames ) ;
259284
260- maxStrokeAlpha = Math . max ( maxStrokeAlpha , animation . alpha ) ;
261- maxFillAlpha = Math . max ( maxFillAlpha , animation . alpha * 0.1 ) ;
285+ maxStrokeAlpha = Math . max ( maxStrokeAlpha , activeOutline . alpha ) ;
286+ maxFillAlpha = Math . max ( maxFillAlpha , activeOutline . alpha * 0.1 ) ;
262287
263288 combinedPath . rect ( rect . x , rect . y , rect . width , rect . height ) ;
264289
265290 if ( unstable ) {
266291 pendingLabeledOutlines . push ( {
267- alpha : animation . alpha ,
292+ alpha : activeOutline . alpha ,
268293 outline,
269- text : animation . text ,
294+ text : activeOutline . text ,
270295 } ) ;
271296 }
272297
273- animation . frame ++ ;
298+ activeOutline . frame ++ ;
274299
275- if ( animation . frame > animation . totalFrames ) {
276- activeOutlines . splice ( i , 1 ) ;
277- animation . resolve ( ) ;
300+ if ( activeOutline . frame > activeOutline . totalFrames ) {
301+ activeOutlinesMap . delete ( getOutlineKey ( outline ) ) ;
302+ activeOutline . resolve ( ) ;
278303 }
279304 }
280305
0 commit comments