Skip to content

Commit 2e7c605

Browse files
committed
chore: better live preview file selection when a css/non html file seelcted
If a non previewable file is selected (Eg: css/js) then we show the last previewed html/md/priviewable file. this is more predicatable and will lead to an active preview most of teh time than showing the no preview screen.
1 parent 97c9d61 commit 2e7c605

5 files changed

Lines changed: 66 additions & 61 deletions

File tree

src/LiveDevelopment/LiveDevMultiBrowser.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ define(function (require, exports, module) {
8888
LiveDevProtocol = require("LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol"),
8989
Metrics = require("utils/Metrics"),
9090
WorkspaceManager = require("view/WorkspaceManager"),
91+
FileSystem = require("filesystem/FileSystem"),
9192
PageLoaderWorkerScript = require("text!LiveDevelopment/BrowserScripts/pageLoaderWorker.js");
9293

9394
// Documents
@@ -538,15 +539,34 @@ define(function (require, exports, module) {
538539
}
539540
}
540541

542+
let _lastPreviewedHTMLFilePath;
543+
async function _resolveLivePreviewDocument() {
544+
let activeDocument = DocumentManager.getCurrentDocument();
545+
if(livePreviewUrlPinned){
546+
return jsPromise(DocumentManager.getDocumentForPath(currentPreviewFilePath));
547+
}
548+
if (activeDocument && activeDocument.getLanguage().getId() === "html") {
549+
_lastPreviewedHTMLFilePath = activeDocument.file.fullPath;
550+
return activeDocument;
551+
}
552+
// if a css/js/non html file is active, we fall back to the last previewed HTML file
553+
let fileExists = await FileSystem.existsAsync(_lastPreviewedHTMLFilePath);
554+
if(!fileExists){
555+
// the last previewed file may have been deleted
556+
return activeDocument; // can be null
557+
}
558+
return jsPromise(DocumentManager.getDocumentForPath(_lastPreviewedHTMLFilePath));
559+
}
560+
561+
ProjectManager.on(ProjectManager.EVENT_PROJECT_OPEN, ()=>{
562+
_lastPreviewedHTMLFilePath = null;
563+
});
541564

542565
/**
543566
* Open a live preview on the current docuemnt.
544567
*/
545568
async function open() {
546-
let doc = DocumentManager.getCurrentDocument();
547-
if(livePreviewUrlPinned){
548-
doc = await jsPromise(DocumentManager.getDocumentForPath(currentPreviewFilePath));
549-
}
569+
let doc = await _resolveLivePreviewDocument();
550570

551571
// wait for server (StaticServer, Base URL or file:)
552572
_prepareServer(doc)

src/extensionsIntegrated/Phoenix-live-preview/BrowserStaticServer.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ define(function (require, exports, module) {
128128
return Phoenix.isNativeApp || !(Phoenix.browser.desktop.isSafari || Phoenix.browser.mobile.isIos);
129129
}
130130

131+
let _lastPreviewedFilePath;
132+
131133
/**
132134
* Finds out a {URL,filePath} to live preview from the project. Will return and empty object if the current
133135
* file is not previewable.
@@ -179,33 +181,24 @@ define(function (require, exports, module) {
179181
});
180182
return;
181183
} else if(utils.isPreviewableFile(fullPath)){
182-
const filePath = httpFilePath || path.relative(projectRoot, fullPath);
184+
// this is the case where the user has html/svg/any previewable file as the active document
185+
_lastPreviewedFilePath = fullPath;
186+
}
187+
let fileExists = await FileSystem.existsAsync(_lastPreviewedFilePath);
188+
if(_lastPreviewedFilePath && fileExists){
189+
// user either has active document as a previewable file or this is the case where
190+
// user switched to a css/js/other file that is not previewable, but we have on old previewable
191+
// file we will just take the _lastPreviewedFilePath as active
192+
const filePath = httpFilePath || path.relative(projectRoot, _lastPreviewedFilePath);
183193
let URL = httpFilePath || `${projectRootUrl}${filePath}`;
184194
resolve({
185195
URL,
186196
filePath: filePath,
187-
fullPath: fullPath,
188-
isMarkdownFile: utils.isMarkdownFile(fullPath),
189-
isHTMLFile: utils.isHTMLFile(fullPath)
197+
fullPath: _lastPreviewedFilePath,
198+
isMarkdownFile: utils.isMarkdownFile(_lastPreviewedFilePath),
199+
isHTMLFile: utils.isHTMLFile(_lastPreviewedFilePath)
190200
});
191201
return;
192-
} else {
193-
const currentLivePreviewDetails = LiveDevelopment.getLivePreviewDetails();
194-
if(currentLivePreviewDetails && currentLivePreviewDetails.liveDocument
195-
&& currentLivePreviewDetails.liveDocument.isRelated
196-
&& currentLivePreviewDetails.liveDocument.isRelated(fullPath)){
197-
fullPath = currentLivePreviewDetails.liveDocument.doc.file.fullPath;
198-
const filePath = path.relative(projectRoot, fullPath);
199-
let URL = `${projectRootUrl}${filePath}`;
200-
resolve({
201-
URL,
202-
filePath: filePath,
203-
fullPath: fullPath,
204-
isMarkdownFile: utils.isMarkdownFile(fullPath),
205-
isHTMLFile: utils.isHTMLFile(fullPath)
206-
});
207-
return;
208-
}
209202
}
210203
}
211204
resolve({
@@ -588,14 +581,16 @@ define(function (require, exports, module) {
588581
if(!url.startsWith(_staticServerInstance.getBaseUrl())) {
589582
return Promise.reject("Not serving content as url belongs to another phcode instance: " + url);
590583
}
591-
if(utils.isMarkdownFile(path) && currentFile && currentFile.fullPath === path){
584+
const shouldServeRendered = ((path === _lastPreviewedFilePath)
585+
|| (currentFile && currentFile.fullPath === path));
586+
if(utils.isMarkdownFile(path) && shouldServeRendered){
592587
return _getMarkdown(path);
593588
}
594589
if(_staticServerInstance){
595590
return _staticServerInstance._getInstrumentedContent(path, url);
596591
}
597592
return Promise.reject("Cannot get content");
598-
};
593+
}
599594

600595
/**
601596
* See BaseServer#start. Starts listenting to StaticServerDomain events.
@@ -712,6 +707,7 @@ define(function (require, exports, module) {
712707
}
713708

714709
function _projectOpened(_evt, projectRoot) {
710+
_lastPreviewedFilePath = null;
715711
navigatorChannel.postMessage({
716712
type: 'PROJECT_SWITCH',
717713
projectRoot: projectRoot.fullPath

src/extensionsIntegrated/Phoenix-live-preview/NodeStaticServer.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,9 @@ define(function (require, exports, module) {
431431
return Promise.reject("Not serving content as url invalid: " + url);
432432
}
433433
const filePath = _staticServerInstance.urlToPath(url);
434-
if(utils.isMarkdownFile(filePath) && currentFile && currentFile.fullPath === filePath){
434+
const shouldServeRendered = ((filePath === _lastPreviewedFilePath)
435+
|| (currentFile && currentFile.fullPath === path));
436+
if(utils.isMarkdownFile(filePath) && shouldServeRendered){
435437
return _getMarkdown(filePath);
436438
}
437439
if(_staticServerInstance){
@@ -580,6 +582,7 @@ define(function (require, exports, module) {
580582
}
581583

582584
function _projectOpened(_evt, projectRoot) {
585+
_lastPreviewedFilePath = null;
583586
liveServerConnector.execPeer('navMessageProjectOpened', {
584587
type: 'PROJECT_SWITCH',
585588
projectRoot: projectRoot.fullPath
@@ -635,6 +638,8 @@ define(function (require, exports, module) {
635638
return livePreviewTabs.size > 0;
636639
}
637640

641+
let _lastPreviewedFilePath;
642+
638643
/**
639644
* Finds out a {URL,filePath} to live preview from the project. Will return and empty object if the current
640645
* file is not previewable.
@@ -703,33 +708,24 @@ define(function (require, exports, module) {
703708
});
704709
return;
705710
} else if(utils.isPreviewableFile(fullPath)){
706-
const relativeFilePath = httpFilePath || path.relative(projectRoot, fullPath);
707-
let URL = httpFilePath || decodeURI(_staticServerInstance.pathToUrl(fullPath));
711+
// this is the case where the user has html/svg/any previewable file as the active document
712+
_lastPreviewedFilePath = fullPath;
713+
}
714+
let fileExists = await FileSystem.existsAsync(_lastPreviewedFilePath);
715+
if(_lastPreviewedFilePath && fileExists){
716+
// user either has active document as a previewable file or this is the case where
717+
// user switched to a css/js/other file that is not previewable, but we have on old previewable
718+
// file we will just take the _lastPreviewedFilePath as active
719+
const relativeFilePath = httpFilePath || path.relative(projectRoot, _lastPreviewedFilePath);
720+
let URL = httpFilePath || decodeURI(_staticServerInstance.pathToUrl(_lastPreviewedFilePath));
708721
resolve({
709722
URL,
710723
filePath: relativeFilePath,
711-
fullPath: fullPath,
712-
isMarkdownFile: utils.isMarkdownFile(fullPath),
713-
isHTMLFile: utils.isHTMLFile(fullPath)
724+
fullPath: _lastPreviewedFilePath,
725+
isMarkdownFile: utils.isMarkdownFile(_lastPreviewedFilePath),
726+
isHTMLFile: utils.isHTMLFile(_lastPreviewedFilePath)
714727
});
715728
return;
716-
} else {
717-
const currentLivePreviewDetails = LiveDevelopment.getLivePreviewDetails();
718-
if(currentLivePreviewDetails && currentLivePreviewDetails.liveDocument
719-
&& currentLivePreviewDetails.liveDocument.isRelated
720-
&& currentLivePreviewDetails.liveDocument.isRelated(fullPath)){
721-
fullPath = currentLivePreviewDetails.liveDocument.doc.file.fullPath;
722-
const relativeFilePath = httpFilePath || path.relative(projectRoot, fullPath);
723-
let URL = httpFilePath || decodeURI(_staticServerInstance.pathToUrl(fullPath));
724-
resolve({
725-
URL,
726-
filePath: relativeFilePath,
727-
fullPath: fullPath,
728-
isMarkdownFile: utils.isMarkdownFile(fullPath),
729-
isHTMLFile: utils.isHTMLFile(fullPath)
730-
});
731-
return;
732-
}
733729
}
734730
resolve({
735731
URL: getNoPreviewURL(),

src/extensionsIntegrated/Phoenix-live-preview/main.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -823,13 +823,7 @@ define(function (require, exports, module) {
823823
currentLivePreviewURL = newSrc;
824824
currentPreviewFile = previewDetails.fullPath;
825825
}
826-
const existingPreviewFile = $iframe && $iframe.attr('data-original-path');
827-
const existingPreviewURL = $iframe && $iframe.attr('data-original-src');
828-
if(isReload && previewDetails.isNoPreview && existingPreviewURL &&
829-
existingPreviewFile && ProjectManager.isWithinProject(existingPreviewFile)) {
830-
currentLivePreviewURL = existingPreviewURL;
831-
currentPreviewFile = existingPreviewFile;
832-
} else if(isReload){
826+
if(isReload && previewDetails.isHTMLFile){
833827
LiveDevelopment.openLivePreview();
834828
}
835829
let relativeOrFullPath= ProjectManager.makeProjectRelativeIfPossible(currentPreviewFile);
@@ -852,10 +846,6 @@ define(function (require, exports, module) {
852846
$iframe = newIframe;
853847
if(_isProjectPreviewTrusted()){
854848
$iframe.attr('src', currentLivePreviewURL);
855-
// we have to save src as the iframe src attribute may have redirected, and we cannot read it as its
856-
// a third party domain once its redirected.
857-
$iframe.attr('data-original-src', currentLivePreviewURL);
858-
$iframe.attr('data-original-path', currentPreviewFile);
859849
if(Phoenix.isTestWindow) {
860850
window._livePreviewIntegTest.currentLivePreviewURL = currentLivePreviewURL;
861851
window._livePreviewIntegTest.urlLoadCount++;

src/filesystem/FileSystem.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,9 @@ define(function (require, exports, module) {
755755
* @param {function(?string, boolean)} callback
756756
*/
757757
FileSystem.prototype.existsAsync = function (path) {
758+
if(!path) {
759+
return Promise.resolve(false);
760+
}
758761
return this._impl.existsAsync(path);
759762
};
760763

0 commit comments

Comments
 (0)