@@ -67,8 +67,6 @@ define(function (require, exports, module) {
6767 * @type {Array.<Object> }
6868 */
6969 var jumpForwardStack = [ ] ,
70- activePosNotSynced = false ,
71- currentEditPos = null ,
7270 jumpInProgress = false ,
7371 commandJumpBack ,
7472 commandJumpFwd ;
@@ -151,16 +149,32 @@ define(function (require, exports, module) {
151149 /**
152150 * Prototype to capture a navigation frame and it's various data/functional attributues
153151 */
154- function NavigationFrame ( editor , selectionObj ) {
155- this . cm = editor . _codeMirror ;
156- this . filePath = editor . document . file . _path ;
157- this . inMem = editor . document . file . constructor . name === "InMemoryFile" ;
158- this . paneId = editor . _paneId ;
159- this . _hash = editor . document . file . _hash ;
152+ function NavigationFrame ( editor , selectionObj , fileWithoutEditor , fileWithoutEditorPaneID ) {
153+ if ( editor ) {
154+ this . cm = editor . _codeMirror ;
155+ this . paneId = editor . _paneId ;
156+ }
160157 this . uId = ( new Date ( ) ) . getTime ( ) ;
161- this . selections = selectionObj . ranges || [ ] ;
158+
159+ if ( editor && fileWithoutEditor ) {
160+ console . error ( "Both Editor and fileWithoutEditor set for NavigationFrame, only one is expected!!!" ) ;
161+ }
162+ if ( fileWithoutEditor ) {
163+ this . nonEditorView = true ;
164+ this . paneId = fileWithoutEditorPaneID ;
165+ }
166+ const fileToUse = fileWithoutEditor ? fileWithoutEditor : editor . document . file ;
167+ this . filePath = fileToUse . _path ;
168+ this . inMem = fileToUse . constructor . name === "InMemoryFile" ;
169+ this . _hash = fileToUse . _hash ;
170+
162171 this . bookMarkIds = [ ] ;
163- this . _createMarkers ( selectionObj . ranges ) ;
172+ if ( selectionObj ) {
173+ this . selections = selectionObj . ranges || [ ] ;
174+ this . _createMarkers ( selectionObj . ranges ) ;
175+ } else {
176+ this . selections = [ ] ;
177+ }
164178 }
165179
166180 /**
@@ -205,7 +219,10 @@ define(function (require, exports, module) {
205219 * -> Addition/Updation of characters in the captured selection
206220 */
207221 NavigationFrame . prototype . _createMarkers = function ( ranges ) {
208- var range ,
222+ if ( ! ranges ) {
223+ return ;
224+ }
225+ let range ,
209226 rangeStart ,
210227 rangeEnd ,
211228 index ,
@@ -284,6 +301,9 @@ define(function (require, exports, module) {
284301 * Function to check if we have valid markers in cm for this frame
285302 */
286303 NavigationFrame . prototype . _validateMarkers = function ( ) {
304+ if ( this . nonEditorView ) {
305+ return true ;
306+ }
287307 this . _backupSelectionRanges ( ) ;
288308 return this . selections . length ;
289309 } ;
@@ -292,19 +312,21 @@ define(function (require, exports, module) {
292312 * Function to actually navigate to the position(file,selections) captured in this frame
293313 */
294314 NavigationFrame . prototype . goTo = function ( ) {
295- var self = this ;
315+ const self = this ;
296316 this . _backupSelectionRanges ( ) ;
297317 jumpInProgress = true ;
298318
299319 // To ensure we don't reopen the same doc in the last known pane
300320 // rather bring it to the same pane where user has opened it
301- var thisDoc = DocumentManager . getOpenDocumentForPath ( this . filePath ) ;
321+ let thisDoc = DocumentManager . getOpenDocumentForPath ( this . filePath ) ;
302322 if ( thisDoc && thisDoc . _masterEditor ) {
303323 this . paneId = thisDoc . _masterEditor . _paneId ;
304324 }
305325
306326 CommandManager . execute ( Commands . FILE_OPEN , { fullPath : this . filePath , paneId : this . paneId } ) . done ( function ( ) {
307- EditorManager . getCurrentFullEditor ( ) . setSelections ( self . selections , true ) ;
327+ if ( ! self . nonEditorView ) {
328+ EditorManager . getCurrentFullEditor ( ) . setSelections ( self . selections , true ) ;
329+ }
308330 _validateNavigationCmds ( ) ;
309331 } ) . always ( function ( ) {
310332 jumpInProgress = false ;
@@ -341,22 +363,19 @@ define(function (require, exports, module) {
341363 navFrame . _clearMarkers ( ) ;
342364 }
343365
344- currentEditPos = new NavigationFrame ( event . target , selectionObj ) ;
366+ let currentEditPos = new NavigationFrame ( event . target , selectionObj ) ;
345367 let lastBack = jumpBackwardStack . pop ( ) ;
346368 if ( lastBack && lastBack !== currentEditPos ) {
347369 // make sure that we don't push in duplicates
348370 jumpBackwardStack . push ( lastBack ) ;
349371 }
350372 jumpBackwardStack . push ( currentEditPos ) ;
351373 _validateNavigationCmds ( ) ;
352- activePosNotSynced = false ;
353374 } ;
354375 if ( force || ( event && event . type === 'mousedown' ) || ( event && event . type === "beforeSelectionChange" ) ) {
355376 // We should record nav history immediately is the user changes currently active doc by clicking files
356377 _recordCurrentPos ( ) ;
357378 }
358- } else {
359- activePosNotSynced = true ;
360379 }
361380 }
362381
@@ -403,38 +422,50 @@ define(function (require, exports, module) {
403422 return prev . length === current . length ;
404423 }
405424
425+ function _getCurrentEditNavFrame ( ) {
426+ const currentFullEditor = EditorManager . getCurrentFullEditor ( ) ;
427+ const currentlyViewedFile = MainViewManager . getCurrentlyViewedFile ( ) ;
428+ let currentEditNavFrame ;
429+ if ( currentFullEditor ) {
430+ currentEditNavFrame = new NavigationFrame ( EditorManager . getCurrentFullEditor ( ) ,
431+ { ranges : EditorManager . getCurrentFullEditor ( ) . _codeMirror . listSelections ( ) } ) ;
432+ } else if ( currentlyViewedFile ) {
433+ currentEditNavFrame = new NavigationFrame ( null , null ,
434+ currentlyViewedFile , MainViewManager . getActivePaneId ( ) ) ;
435+ }
436+ return currentEditNavFrame ;
437+ }
438+
406439 /**
407440 * Command handler to navigate backward
408441 */
409442 function _navigateBack ( skipCurrentFile ) {
410443 let navFrame = jumpBackwardStack . pop ( ) ;
411- currentEditPos = new NavigationFrame ( EditorManager . getCurrentFullEditor ( ) ,
412- { ranges : EditorManager . getCurrentFullEditor ( ) . _codeMirror . listSelections ( ) } ) ;
444+ let currentEditNavFrame = _getCurrentEditNavFrame ( ) ;
413445
414446 // Check if the poped frame is the current active frame or doesn't have any valid marker information
415447 // if true, jump again
416- while ( navFrame && navFrame === currentEditPos
417- || ( navFrame && navFrame . filePath === currentEditPos . filePath
418- && _isSimilarSelection ( navFrame . selections , currentEditPos . selections )
419- && _isSimilarBookmarks ( navFrame . bookMarkIds , currentEditPos . bookMarkIds ) )
420- || ( skipCurrentFile && navFrame && navFrame . filePath === currentEditPos . filePath ) ) {
448+ while ( navFrame && navFrame === currentEditNavFrame
449+ || ( navFrame && navFrame . filePath === currentEditNavFrame . filePath
450+ && _isSimilarSelection ( navFrame . selections , currentEditNavFrame . selections )
451+ && _isSimilarBookmarks ( navFrame . bookMarkIds , currentEditNavFrame . bookMarkIds ) )
452+ || ( skipCurrentFile && navFrame && navFrame . filePath === currentEditNavFrame . filePath ) ) {
421453 navFrame = jumpBackwardStack . pop ( ) ;
422454 }
423455
424456 if ( navFrame ) {
425457 // We will check for the file existence now, if it doesn't exist we will jump back again
426458 // but discard the popped frame as invalid.
427459 _validateFrame ( navFrame ) . done ( function ( ) {
428- jumpForwardStack . push ( currentEditPos ) ;
460+ jumpForwardStack . push ( currentEditNavFrame ) ;
429461 navFrame . goTo ( ) ;
430- currentEditPos = navFrame ;
431462 } ) . fail ( function ( ) {
432463 CommandManager . execute ( NAVIGATION_JUMP_BACK ) ;
433464 } ) . always ( function ( ) {
434465 _validateNavigationCmds ( ) ;
435466 } ) ;
436467 } else {
437- jumpBackwardStack . push ( currentEditPos ) ;
468+ jumpBackwardStack . push ( currentEditNavFrame ) ;
438469 }
439470 }
440471
@@ -443,30 +474,28 @@ define(function (require, exports, module) {
443474 */
444475 function _navigateForward ( skipCurrentFile ) {
445476 let navFrame = jumpForwardStack . pop ( ) ;
446- currentEditPos = new NavigationFrame ( EditorManager . getCurrentFullEditor ( ) ,
447- { ranges : EditorManager . getCurrentFullEditor ( ) . _codeMirror . listSelections ( ) } ) ;
477+ let currentEditNavFrame = _getCurrentEditNavFrame ( ) ;
448478
449479 if ( ! navFrame ) {
450480 return ;
451481 }
452482
453483 // Check if the poped frame is the current active frame or doesn't have any valid marker information
454484 // if true, jump again
455- while ( navFrame === currentEditPos
456- || ( navFrame && navFrame . filePath === currentEditPos . filePath
457- && _isSimilarSelection ( navFrame . selections , currentEditPos . selections )
458- && _isSimilarBookmarks ( navFrame . bookMarkIds , currentEditPos . bookMarkIds ) )
459- || ( skipCurrentFile && navFrame && navFrame . filePath === currentEditPos . filePath ) ) {
485+ while ( navFrame === currentEditNavFrame
486+ || ( navFrame && navFrame . filePath === currentEditNavFrame . filePath
487+ && _isSimilarSelection ( navFrame . selections , currentEditNavFrame . selections )
488+ && _isSimilarBookmarks ( navFrame . bookMarkIds , currentEditNavFrame . bookMarkIds ) )
489+ || ( skipCurrentFile && navFrame && navFrame . filePath === currentEditNavFrame . filePath ) ) {
460490 navFrame = jumpForwardStack . pop ( ) ;
461491 }
462492
463493 if ( navFrame ) {
464494 // We will check for the file existence now, if it doesn't exist we will jump back again
465495 // but discard the popped frame as invalid.
466496 _validateFrame ( navFrame ) . done ( function ( ) {
467- jumpBackwardStack . push ( currentEditPos ) ;
497+ jumpBackwardStack . push ( currentEditNavFrame ) ;
468498 navFrame . goTo ( ) ;
469- currentEditPos = navFrame ;
470499 } ) . fail ( function ( ) {
471500 _validateNavigationCmds ( ) ;
472501 CommandManager . execute ( NAVIGATION_JUMP_FWD ) ;
@@ -598,16 +627,33 @@ define(function (require, exports, module) {
598627 }
599628
600629 if ( current && current . _paneId ) { // Handle only full editors
601- activePosNotSynced = true ;
602630 current . off ( "beforeSelectionChange" , _recordJumpDef ) ;
603631 current . on ( "beforeSelectionChange" , _recordJumpDef ) ;
604632 current . off ( "beforeDestroy" , _handleEditorCleanup ) ;
605633 current . on ( "beforeDestroy" , _handleEditorCleanup ) ;
606634 }
607635 }
608636
637+ function _currentFileChanged ( _evt , currentFile , currentPane ) {
638+ // We may not always have an active editor to navigate, For Eg: image/video or other custom views
639+ // have its onw non-editor views. This section is to handle those cases.
640+ const activeFullEditor = EditorManager . getCurrentFullEditor ( ) ;
641+ if ( activeFullEditor ) {
642+ return ;
643+ }
644+ const currentEditNavFrame = _getCurrentEditNavFrame ( ) ;
645+ let lastBack = jumpBackwardStack . pop ( ) ;
646+ if ( lastBack && lastBack !== currentEditNavFrame ) {
647+ // make sure that we don't push in duplicates
648+ jumpBackwardStack . push ( lastBack ) ;
649+ }
650+ jumpBackwardStack . push ( currentEditNavFrame ) ;
651+ _validateNavigationCmds ( ) ;
652+ }
653+
609654 function _initHandlers ( ) {
610655 EditorManager . on ( "activeEditorChange" , _handleActiveEditorChange ) ;
656+ MainViewManager . on ( "currentFileChange" , _currentFileChanged ) ;
611657 ProjectManager . on ( "projectOpen" , _clearStacks ) ;
612658 EditorManager . on ( "_fullEditorCreatedForDocument" , function ( event , document , editor ) {
613659 _reinstateMarkers ( editor , jumpBackwardStack ) ;
0 commit comments