@@ -76,12 +76,9 @@ define(function (require, exports, module) {
7676 DocumentManager = require ( "document/DocumentManager" ) ,
7777 EditorManager = require ( "editor/EditorManager" ) ,
7878 EventDispatcher = require ( "utils/EventDispatcher" ) ,
79- FileUtils = require ( "file/FileUtils" ) ,
8079 MainViewManager = require ( "view/MainViewManager" ) ,
81- PreferencesDialogs = require ( "preferences/PreferencesDialogs" ) ,
8280 ProjectManager = require ( "project/ProjectManager" ) ,
8381 Strings = require ( "strings" ) ,
84- _ = require ( "thirdparty/lodash" ) ,
8582 LiveDevelopmentUtils = require ( "LiveDevelopment/LiveDevelopmentUtils" ) ,
8683 LiveDevServerManager = require ( "LiveDevelopment/LiveDevServerManager" ) ,
8784 LivePreviewTransport = require ( "LiveDevelopment/MultiBrowserImpl/transports/LivePreviewTransport" ) ,
@@ -324,118 +321,6 @@ define(function (require, exports, module) {
324321 } ) ;
325322 }
326323
327- /**
328- * @private
329- * Determine an index file that can be used to start Live Development.
330- * This function will inspect all files in a project to find the closest index file
331- * available for currently opened document. We are searching for these files:
332- * - index.html
333- * - index.htm
334- *
335- * If the project is configured with a custom base url for live development, then
336- * the list of possible index files is extended to contain these index files too:
337- * - index.php
338- * - index.php3
339- * - index.php4
340- * - index.php5
341- * - index.phtm
342- * - index.phtml
343- * - index.cfm
344- * - index.cfml
345- * - index.asp
346- * - index.aspx
347- * - index.jsp
348- * - index.jspx
349- * - index.shm
350- * - index.shml
351- *
352- * If a file was found, the promise will be resolved with the full path to this file. If no file
353- * was found in the whole project tree, the promise will be resolved with null.
354- *
355- * @return {jQuery.Promise } A promise that is resolved with a full path
356- * to a file if one could been determined, or null if there was no suitable index
357- * file.
358- */
359- function _getInitialDocFromCurrent ( ) {
360- var doc = DocumentManager . getCurrentDocument ( ) ,
361- refPath ,
362- i ;
363-
364- // Is the currently opened document already a file we can use for Live Development?
365- if ( doc ) {
366- refPath = doc . file . fullPath ;
367- if ( LiveDevelopmentUtils . isStaticHtmlFileExt ( refPath ) || LiveDevelopmentUtils . isServerHtmlFileExt ( refPath ) ) {
368- return new $ . Deferred ( ) . resolve ( doc ) ;
369- }
370- }
371-
372- var result = new $ . Deferred ( ) ;
373-
374- var baseUrl = ProjectManager . getBaseUrl ( ) ,
375- hasOwnServerForLiveDevelopment = ( baseUrl && baseUrl . length ) ;
376-
377- ProjectManager . getAllFiles ( ) . done ( function ( allFiles ) {
378- var projectRoot = ProjectManager . getProjectRoot ( ) . fullPath ,
379- containingFolder ,
380- indexFileFound = false ,
381- stillInProjectTree = true ;
382-
383- if ( refPath ) {
384- containingFolder = FileUtils . getDirectoryPath ( refPath ) ;
385- } else {
386- containingFolder = projectRoot ;
387- }
388-
389- var filteredFiltered = allFiles . filter ( function ( item ) {
390- var parent = FileUtils . getParentPath ( item . fullPath ) ;
391-
392- return ( containingFolder . indexOf ( parent ) === 0 ) ;
393- } ) ;
394-
395- var filterIndexFile = function ( fileInfo ) {
396- if ( fileInfo . fullPath . indexOf ( containingFolder ) === 0 ) {
397- if ( FileUtils . getFilenameWithoutExtension ( fileInfo . name ) === "index" ) {
398- if ( hasOwnServerForLiveDevelopment ) {
399- if ( ( LiveDevelopmentUtils . isServerHtmlFileExt ( fileInfo . name ) ) ||
400- ( LiveDevelopmentUtils . isStaticHtmlFileExt ( fileInfo . name ) ) ) {
401- return true ;
402- }
403- } else if ( LiveDevelopmentUtils . isStaticHtmlFileExt ( fileInfo . name ) ) {
404- return true ;
405- }
406- } else {
407- return false ;
408- }
409- }
410- } ;
411-
412- while ( ! indexFileFound && stillInProjectTree ) {
413- i = _ . findIndex ( filteredFiltered , filterIndexFile ) ;
414-
415- // We found no good match
416- if ( i === - 1 ) {
417- // traverse the directory tree up one level
418- containingFolder = FileUtils . getParentPath ( containingFolder ) ;
419- // Are we still inside the project?
420- if ( containingFolder . indexOf ( projectRoot ) === - 1 ) {
421- stillInProjectTree = false ;
422- }
423- } else {
424- indexFileFound = true ;
425- }
426- }
427-
428- if ( i !== - 1 ) {
429- DocumentManager . getDocumentForPath ( filteredFiltered [ i ] . fullPath ) . then ( result . resolve , result . resolve ) ;
430- return ;
431- }
432-
433- result . resolve ( null ) ;
434- } ) ;
435-
436- return result . promise ( ) ;
437- }
438-
439324 /**
440325 * @private
441326 * Close the connection and the associated window
@@ -496,6 +381,9 @@ define(function (require, exports, module) {
496381 // create live document
497382 doc . _ensureMasterEditor ( ) ;
498383 _liveDocument = _createLiveDocument ( doc , doc . _masterEditor ) ;
384+ if ( ! _liveDocument ) {
385+ return ;
386+ }
499387 _server . add ( _liveDocument ) ;
500388 _server . addVirtualContentAtPath (
501389 `${ _liveDocument . doc . file . parentPath } ${ LiveDevProtocol . LIVE_DEV_REMOTE_SCRIPTS_FILE_NAME } ` ,
@@ -584,36 +472,11 @@ define(function (require, exports, module) {
584472 console . error ( "LiveDevelopment._open(): No server active" ) ;
585473 }
586474 } else {
587- // Unlikely that we would get to this state where
588475 // a connection is in process but there is no current
589- // document
590- close ( ) ;
476+ // document, Eg. A project that has only markdown or images that doesnt have a live html/css document
591477 }
592478 }
593479
594- /**
595- * @private
596- * Creates the live document in preparation for launching the
597- * preview of the given document, then launches it. (The live document
598- * must already exist before we launch it so that the server can
599- * ask it for the instrumented version of the document when the browser
600- * requests it.)
601- * TODO: could probably just consolidate this with _open()
602- * @param {Document } doc
603- */
604- function _doLaunchAfterServerReady ( initialDoc ) {
605-
606- _createLiveDocumentForFrame ( initialDoc ) ;
607-
608- // start listening for requests
609- _server . start ( )
610- . then ( ( ) => {
611- // open browser to the url
612- _open ( initialDoc ) ;
613- } ) ;
614-
615- }
616-
617480 /**
618481 * @private
619482 * Create the server in preparation for opening a live preview.
@@ -622,41 +485,24 @@ define(function (require, exports, module) {
622485 * vs. a user server when there is an app server set in File > Project Settings).
623486 */
624487 function _prepareServer ( doc ) {
625- var deferred = new $ . Deferred ( ) ,
626- showBaseUrlPrompt = false ;
627-
628- _server = LiveDevServerManager . getServer ( doc . file . fullPath ) ;
629-
630- // Optionally prompt for a base URL if no server was found but the
631- // file is a known server file extension
632- showBaseUrlPrompt = ! _server && LiveDevelopmentUtils . isServerHtmlFileExt ( doc . file . fullPath ) ;
633-
634- if ( showBaseUrlPrompt ) {
635- // Prompt for a base URL
636- PreferencesDialogs . showProjectPreferencesDialog ( "" , Strings . LIVE_DEV_NEED_BASEURL_MESSAGE )
637- . done ( function ( id ) {
638- if ( id === Dialogs . DIALOG_BTN_OK && ProjectManager . getBaseUrl ( ) ) {
639- // If base url is specifed, then re-invoke _prepareServer() to continue
640- _prepareServer ( doc ) . then ( deferred . resolve , deferred . reject ) ;
641- } else {
642- deferred . reject ( ) ;
643- }
644- } ) ;
645- } else if ( _server ) {
646- // Startup the server
647- var readyPromise = _server . readyToServe ( ) ;
648- if ( ! readyPromise ) {
488+ const deferred = new $ . Deferred ( ) ;
489+ let initialServePath = doc && doc . file . fullPath ;
490+ if ( ! initialServePath ) {
491+ initialServePath = `${ ProjectManager . getProjectRoot ( ) . fullPath } index.html` ;
492+ }
493+
494+ _server = LiveDevServerManager . getServer ( initialServePath ) ;
495+
496+ // Startup the server
497+ const readyPromise = _server . readyToServe ( ) ;
498+ if ( ! readyPromise ) {
499+ _showLiveDevServerNotReadyError ( ) ;
500+ deferred . reject ( ) ;
501+ } else {
502+ readyPromise . then ( deferred . resolve , function ( ) {
649503 _showLiveDevServerNotReadyError ( ) ;
650504 deferred . reject ( ) ;
651- } else {
652- readyPromise . then ( deferred . resolve , function ( ) {
653- _showLiveDevServerNotReadyError ( ) ;
654- deferred . reject ( ) ;
655- } ) ;
656- }
657- } else {
658- // No server found
659- deferred . reject ( ) ;
505+ } ) ;
660506 }
661507
662508 return deferred . promise ( ) ;
@@ -677,7 +523,7 @@ define(function (require, exports, module) {
677523 let docUrl = _resolveUrl ( doc . file . fullPath ) ,
678524 isViewable = _server && _server . canServe ( doc . file . fullPath ) ;
679525
680- if ( _liveDocument . doc . url !== docUrl && isViewable ) {
526+ if ( _liveDocument && _liveDocument . doc . url !== docUrl && isViewable ) {
681527 // clear live doc and related docs
682528 _closeDocuments ( ) ;
683529 // create new live doc
@@ -695,19 +541,31 @@ define(function (require, exports, module) {
695541 function open ( ) {
696542 // TODO: need to run _onDocumentChange() after load if doc != currentDocument here? Maybe not, since activeEditorChange
697543 // doesn't trigger it, while inline editors can still cause edits in doc other than currentDoc...
698- _getInitialDocFromCurrent ( ) . done ( function ( doc ) {
699- var prepareServerPromise = ( doc && _prepareServer ( doc ) ) || new $ . Deferred ( ) . reject ( ) ;
700-
701- // wait for server (StaticServer, Base URL or file:)
702- prepareServerPromise
703- . done ( function ( ) {
704- _setStatus ( STATUS_CONNECTING ) ;
705- _doLaunchAfterServerReady ( doc ) ;
706- } )
707- . fail ( function ( ) {
708- console . log ( "Live preview: no document to preview." ) ;
709- } ) ;
710- } ) ;
544+ const doc = DocumentManager . getCurrentDocument ( ) ;
545+
546+ // wait for server (StaticServer, Base URL or file:)
547+ _prepareServer ( doc )
548+ . done ( function ( ) {
549+ if ( ! _server ) {
550+ return ;
551+ }
552+ _setStatus ( STATUS_CONNECTING ) ;
553+ doc && _createLiveDocumentForFrame ( doc ) ;
554+ if ( _server . isActive ( ) ) {
555+ doc && _open ( doc ) ;
556+ return ;
557+ }
558+
559+ // start server and listen for requests
560+ _server . start ( )
561+ . then ( ( ) => {
562+ // open browser to the url
563+ doc && _open ( doc ) ;
564+ } ) ;
565+ } )
566+ . fail ( function ( ) {
567+ console . log ( "Live preview: no document to preview." ) ;
568+ } ) ;
711569 }
712570
713571 /**
@@ -799,8 +657,6 @@ define(function (require, exports, module) {
799657 DocumentManager
800658 . on ( "documentSaved" , _onDocumentSaved )
801659 . on ( "dirtyFlagChange" , _onDirtyFlagChange ) ;
802- ProjectManager
803- . on ( "beforeProjectClose beforeAppClose" , close ) ;
804660
805661 // Default transport for live connection messages - can be changed
806662 setTransport ( LivePreviewTransport ) ;
@@ -911,14 +767,13 @@ define(function (require, exports, module) {
911767 }
912768
913769 function getLivePreviewBaseURL ( ) {
914- return LiveDevServerManager . getStaticServerBaseURLs ( ) . projectBaseURL ;
770+ return LiveDevServerManager . getStaticServerBaseURLs ( ) . previewBaseURL ;
915771 }
916772
917773 EventDispatcher . makeEventDispatcher ( exports ) ;
918774
919775 // For unit testing
920776 exports . _server = _server ;
921- exports . _getInitialDocFromCurrent = _getInitialDocFromCurrent ;
922777
923778 // Events
924779 exports . EVENT_OPEN_PREVIEW_URL = EVENT_OPEN_PREVIEW_URL ;
0 commit comments