Skip to content

Commit 0fa3b5e

Browse files
committed
fix: opening text files using open with from os opens live preview panel
1 parent 847d1a3 commit 0fa3b5e

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

src/document/DocumentCommandHandlers.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
// jshint ignore: start
2323
/*jslint regexp: true */
24-
/*globals logger, Phoenix, path*/
24+
/*globals logger, jsPromise, path*/
2525

2626
define(function (require, exports, module) {
2727

@@ -1661,15 +1661,15 @@ define(function (require, exports, module) {
16611661

16621662
async function _tryToOpenFile(absOrRelativePath, cwdIfRelativePath) {
16631663
try{
1664-
let fileToOpen = absOrRelativePath;
1664+
let fileToOpen;
16651665
if(cwdIfRelativePath){
16661666
fileToOpen = window.path.join(Phoenix.VFS.getTauriVirtualPath(cwdIfRelativePath), absOrRelativePath);
16671667
} else {
16681668
fileToOpen = Phoenix.VFS.getTauriVirtualPath(absOrRelativePath);
16691669
}
16701670
let isFile = await _fileExists(fileToOpen);
16711671
if(isFile){
1672-
FileViewController.openFileAndAddToWorkingSet(fileToOpen);
1672+
await jsPromise(FileViewController.openFileAndAddToWorkingSet(fileToOpen));
16731673
return true;
16741674
}
16751675
} catch (e) {
@@ -2187,7 +2187,10 @@ define(function (require, exports, module) {
21872187
}
21882188
firstProjectOpenHandled = true;
21892189
Phoenix.app.setSingleInstanceCLIArgsHandler(_singleInstanceHandler);
2190-
_openFilesPassedInFromCLI();
2190+
_openFilesPassedInFromCLI()
2191+
.finally(()=>{
2192+
ProjectManager.trigger(ProjectManager.EVENT_AFTER_STARTUP_FILES_LOADED);
2193+
});
21912194
});
21922195

21932196
// Exported for unit testing only

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ define(function (require, exports, module) {
575575
}
576576
}
577577

578+
let startupFilesLoadHandled = false;
578579
async function _projectOpened(_evt) {
579580
customLivePreviewBannerShown = false;
580581
$panel.find(".live-preview-custom-banner").addClass("forced-hidden");
@@ -589,6 +590,20 @@ define(function (require, exports, module) {
589590
_togglePinUrl();
590591
}
591592
$iframe.attr('src', StaticServer.getNoPreviewURL());
593+
if(ProjectManager.isStartupFilesLoaded()){
594+
startupFilesLoadHandled = true;
595+
}
596+
if(!panelShownAtStartup && !isBrowser && ProjectManager.isStartupFilesLoaded()){
597+
// we dont do this in browser as the virtual server may not yet be started on app start
598+
// project open and a 404 page will briefly flash in the browser!
599+
// this mainly applies when phoenix is started with a preview file already open in previous exit
600+
const currentDocument = DocumentManager.getCurrentDocument();
601+
const currentFile = currentDocument? currentDocument.file : ProjectManager.getSelectedItem();
602+
const isPreviewable = currentFile ? utils.isPreviewableFile(currentFile.fullPath) : false;
603+
if(isPreviewable){
604+
_setPanelVisibility(true);
605+
}
606+
}
592607
if(!panel.isVisible()){
593608
return;
594609
}
@@ -637,7 +652,7 @@ define(function (require, exports, module) {
637652
}
638653

639654
async function _currentFileChanged(_event, changedFile) {
640-
if(!changedFile || !changedFile.fullPath){
655+
if(!changedFile || !changedFile.fullPath || !ProjectManager.isStartupFilesLoaded()){
641656
return;
642657
}
643658
const fullPath = changedFile.fullPath;
@@ -752,6 +767,14 @@ define(function (require, exports, module) {
752767
ProjectManager.on(ProjectManager.EVENT_PROJECT_OPEN, _projectOpened);
753768
ProjectManager.on(ProjectManager.EVENT_PROJECT_CLOSE, _projectClosed);
754769
EditorManager.on("activeEditorChange", _activeDocChanged);
770+
ProjectManager.on(ProjectManager.EVENT_AFTER_STARTUP_FILES_LOADED, ()=>{
771+
if(!startupFilesLoadHandled){
772+
// we have to use this handled flag as there is no ordering of EVENT_AFTER_STARTUP_FILES_LOADED
773+
// and EVENT_PROJECT_OPEN. if _projectOpened has already shown the live preview panel when it saw that
774+
// ProjectManager.isStartupFilesLoaded() is true, we should not call project opened again at boot.
775+
_projectOpened();
776+
}
777+
});
755778
CommandManager.register(Strings.CMD_LIVE_FILE_PREVIEW, Commands.FILE_LIVE_FILE_PREVIEW, function () {
756779
_toggleVisibilityOnClick();
757780
});

src/project/ProjectManager.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ define(function (require, exports, module) {
8585
EVENT_PROJECT_OPEN_FAILED = "projectFileOpenFailed",
8686
EVENT_PROJECT_OPEN = "projectOpen",
8787
EVENT_AFTER_PROJECT_OPEN = "afterProjectOpen",
88+
// on boot, we load files that have been passed in from os either with `open with` from os file explorer or
89+
// as cli from terminal. EVENT_AFTER_STARTUP_FILES_LOADED is trigerred after those files have been loaded.
90+
// Note that this may be trigerred before any extensions get loaded, so always a good idea to check for
91+
// isStartupFilesLoaded()
92+
EVENT_AFTER_STARTUP_FILES_LOADED = "startupFilesLoaded",
8893
EVENT_PROJECT_REFRESH = "projectRefresh",
8994
EVENT_CONTENT_CHANGED = "contentChanged",
9095
EVENT_PROJECT_FILE_CHANGED = "projectFileChanged",
@@ -2018,6 +2023,14 @@ define(function (require, exports, module) {
20182023
_flagProjectExitedSafely(getProjectRoot().fullPath);
20192024
_unwatchProjectRoot();
20202025
});
2026+
let startupFilesLoaded = false;
2027+
exports.on(EVENT_AFTER_STARTUP_FILES_LOADED, ()=>{
2028+
startupFilesLoaded = true;
2029+
});
2030+
2031+
function isStartupFilesLoaded() {
2032+
return startupFilesLoaded;
2033+
}
20212034

20222035
// Due to circular dependencies, not safe to call on() directly for other modules' events
20232036
EventDispatcher.on_duringInit(FileViewController, "documentSelectionFocusChange", _documentSelectionFocusChange);
@@ -2246,12 +2259,14 @@ define(function (require, exports, module) {
22462259
exports.addClassesProvider = addClassesProvider;
22472260
exports.rerenderTree = rerenderTree;
22482261
exports.setProjectBusy = setProjectBusy;
2262+
exports.isStartupFilesLoaded = isStartupFilesLoaded;
22492263

22502264
// public events
22512265
exports.EVENT_PROJECT_BEFORE_CLOSE = EVENT_PROJECT_BEFORE_CLOSE;
22522266
exports.EVENT_PROJECT_CLOSE = EVENT_PROJECT_CLOSE;
22532267
exports.EVENT_PROJECT_OPEN = EVENT_PROJECT_OPEN;
22542268
exports.EVENT_AFTER_PROJECT_OPEN = EVENT_AFTER_PROJECT_OPEN;
2269+
exports.EVENT_AFTER_STARTUP_FILES_LOADED = EVENT_AFTER_STARTUP_FILES_LOADED;
22552270
exports.EVENT_PROJECT_REFRESH = EVENT_PROJECT_REFRESH;
22562271
exports.EVENT_CONTENT_CHANGED = EVENT_CONTENT_CHANGED;
22572272
exports.EVENT_PROJECT_FILE_CHANGED = EVENT_PROJECT_FILE_CHANGED;

0 commit comments

Comments
 (0)