3333function RemoteFunctions ( experimental ) {
3434 "use strict" ;
3535
36+ var HIGHLIGHT_CLASSNAME = "__brackets-ld-highlight" ;
37+
3638 // determine the color for a type
3739 function _typeColor ( type , highlight ) {
3840 switch ( type ) {
@@ -49,6 +51,10 @@ function RemoteFunctions(experimental) {
4951
5052 // compute the screen offset of an element
5153 function _screenOffset ( element , key ) {
54+ if ( element === document . body ) {
55+ var bounds = element . getBoundingClientRect ( ) ;
56+ return ( key === "offsetLeft" ? bounds . left : bounds . top ) ;
57+ }
5258 var value = 0 ;
5359 while ( element ) {
5460 value += element [ key ] ;
@@ -204,27 +210,102 @@ function RemoteFunctions(experimental) {
204210 return false ;
205211 } ,
206212
207- add : function ( element ) {
213+ _makeHighlightDiv : function ( element , doAnimation ) {
214+ var elementBounds = element . getBoundingClientRect ( ) ,
215+ highlight = window . document . createElement ( "div" ) ,
216+ styles = window . getComputedStyle ( element ) ;
217+
218+ highlight . className = HIGHLIGHT_CLASSNAME ;
219+
220+ var stylesToSet = {
221+ "left" : _screenOffset ( element , "offsetLeft" ) + "px" ,
222+ "top" : _screenOffset ( element , "offsetTop" ) + "px" ,
223+ "width" : elementBounds . width + "px" ,
224+ "height" : elementBounds . height + "px" ,
225+ "z-index" : 2000000 ,
226+ "position" : "absolute" ,
227+ "pointer-events" : "none" ,
228+ "border-top-left-radius" : styles . borderTopLeftRadius ,
229+ "border-top-right-radius" : styles . borderTopRightRadius ,
230+ "border-bottom-left-radius" : styles . borderBottomLeftRadius ,
231+ "border-bottom-right-radius" : styles . borderBottomRightRadius
232+ } ;
233+
234+ var animateStartValues = {
235+ "opacity" : 0 ,
236+ "background" : "rgba(94,167,255, 0.5)" ,
237+ "box-shadow" : "0 0 2px 1px rgba(94,167,255, 1), inset 0 0 4px 1px rgba(255,255,255,1)"
238+ } ;
239+
240+ var animateEndValues = {
241+ "opacity" : 1 ,
242+ "background" : "rgba(94,167,255, 0.1)" ,
243+ "box-shadow" : "0 0 1px 1px rgba(94,167,255, 0.6), inset 0 0 4px 1px rgba(255,255,255,0.8)"
244+ } ;
245+
246+ var transitionValues = {
247+ "-webkit-transition-property" : "opacity, box-shadow, background" ,
248+ "-webkit-transition-duration" : "0.3s, 0.4s, 0.4s" ,
249+ "transition-property" : "opacity, box-shadow, background" ,
250+ "transition-duration" : "0.3s, 0.4s, 0.4s"
251+ } ;
252+
253+ function _setStyleValues ( styleValues , obj ) {
254+ var prop ;
255+
256+ for ( prop in styleValues ) {
257+ obj . setProperty ( prop , styleValues [ prop ] ) ;
258+ }
259+ }
260+
261+ _setStyleValues ( stylesToSet , highlight . style ) ;
262+ _setStyleValues (
263+ doAnimation ? animateStartValues : animateEndValues ,
264+ highlight . style
265+ ) ;
266+
267+
268+ if ( doAnimation ) {
269+ _setStyleValues ( transitionValues , highlight . style ) ;
270+
271+ window . setTimeout ( function ( ) {
272+ _setStyleValues ( animateEndValues , highlight . style ) ;
273+ } , 0 ) ;
274+ }
275+
276+ window . document . body . appendChild ( highlight ) ;
277+ } ,
278+
279+ add : function ( element , doAnimation ) {
208280 if ( this . _elementExists ( element ) || element === document ) {
209281 return ;
210282 }
211283 if ( this . trigger ) {
212284 _trigger ( element , "highlight" , 1 ) ;
213285 }
214286 this . elements . push ( element ) ;
215- this . orgColors . push ( element . style . getPropertyValue ( "background-color" ) ) ;
216- element . style . setProperty ( "background-color" , this . color ) ;
287+
288+ this . _makeHighlightDiv ( element , doAnimation ) ;
217289 } ,
218290
219291 clear : function ( ) {
220- var i ;
221- for ( i in this . elements ) {
222- var e = this . elements [ i ] ;
223- e . style . setProperty ( "background-color" , this . orgColors [ i ] ) ;
224- _trigger ( e , "highlight" , 0 , true ) ;
292+ var i , highlights = window . document . querySelectorAll ( "." + HIGHLIGHT_CLASSNAME ) ,
293+ body = window . document . body ;
294+
295+ for ( i = 0 ; i < highlights . length ; i ++ ) {
296+ body . removeChild ( highlights [ i ] ) ;
225297 }
298+
226299 this . elements = [ ] ;
227- this . orgColors = [ ] ;
300+ } ,
301+
302+ redraw : function ( ) {
303+ var i , highlighted = this . elements . slice ( 0 ) ;
304+
305+ this . clear ( ) ;
306+ for ( i in highlighted ) {
307+ this . add ( highlighted [ i ] , false ) ;
308+ }
228309 }
229310 } ;
230311
@@ -252,7 +333,7 @@ function RemoteFunctions(experimental) {
252333 if ( ! event . metaKey ) {
253334 return ;
254335 }
255- _localHighlight . add ( event . target ) ;
336+ _localHighlight . add ( event . target , true ) ;
256337 }
257338
258339 function onMouseOut ( event ) {
@@ -305,7 +386,6 @@ function RemoteFunctions(experimental) {
305386 }
306387 }
307388
308-
309389 /** Public Commands **********************************************************/
310390
311391 // show goto
@@ -336,7 +416,7 @@ function RemoteFunctions(experimental) {
336416 if ( clear ) {
337417 _remoteHighlight . clear ( ) ;
338418 }
339- _remoteHighlight . add ( node ) ;
419+ _remoteHighlight . add ( node , true ) ;
340420 }
341421
342422 // highlight a rule
@@ -347,16 +427,32 @@ function RemoteFunctions(experimental) {
347427 highlight ( nodes [ i ] ) ;
348428 }
349429 }
430+
431+ // redraw active highlights
432+ function redrawHighlights ( ) {
433+ if ( _remoteHighlight ) {
434+ _remoteHighlight . redraw ( ) ;
435+ }
436+ }
350437
351438 // init
352439 if ( experimental ) {
353440 window . document . addEventListener ( "keydown" , onKeyDown ) ;
354441 }
442+
443+ window . addEventListener ( "resize" , redrawHighlights ) ;
444+
445+ // Scrolling a div can interfere with highlighting.
446+ var i , divs = window . document . getElementsByTagName ( "div" ) ;
447+ for ( i = 0 ; i < divs . length ; i ++ ) {
448+ divs [ i ] . addEventListener ( "scroll" , redrawHighlights ) ;
449+ }
355450
356451 return {
357452 "showGoto" : showGoto ,
358453 "hideHighlight" : hideHighlight ,
359454 "highlight" : highlight ,
360- "highlightRule" : highlightRule
455+ "highlightRule" : highlightRule ,
456+ "redrawHighlights" : redrawHighlights
361457 } ;
362458}
0 commit comments