Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live Preview Desktop first draft #1261

Merged
merged 13 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/LiveDevelopment/LiveDevMultiBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,10 +606,12 @@ define(function (require, exports, module) {
_createLiveDocumentForFrame(initialDoc);

// start listening for requests
_server.start();
_server.start()
.then(()=>{
// open browser to the url
_open(initialDoc);
});

// open browser to the url
_open(initialDoc);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/document/DocumentManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ define(function (require, exports, module) {
* If all you need is the Document's getText() value, use the faster getDocumentText() instead.
*
* @param {!string} fullPath
* @param {!object} fileObj actual File|RemoteFile or some other protocol adapter handle
* @param {object?} fileObj actual File|RemoteFile or some other protocol adapter handle
* @return {$.Promise} A promise object that will be resolved with the Document, or rejected
* with a FileSystemError if the file is not yet open and can't be read from disk.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/extensions/default/HealthData/SendToAnalytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ define(function (require, exports, module) {
Number(healthReport["AppStartupTime"]));
Metrics.valueEvent(PERFORMANCE, "startup", "ModuleDepsResolved",
Number(healthReport["ModuleDepsResolved"]));
Metrics.valueEvent(PERFORMANCE, "startup", "PhStore", PhStore._storageBootstrapTime);
if(Phoenix.browser.isTauri) {
Metrics.valueEvent(PERFORMANCE, "startup", "tauriBoot", window._tauriBootVars.bootstrapTime);
}
if(window.nodeSetupDonePromise) {
window.nodeSetupDonePromise
.then(()=>{
window.PhNodeEngine && window.PhNodeEngine._nodeLoadTime
&& Metrics.valueEvent(PERFORMANCE, "startup", "nodeBoot", window.PhNodeEngine._nodeLoadTime);
});
}
}

// Themes
Expand Down
295 changes: 178 additions & 117 deletions src/extensions/default/Phoenix-live-preview/StaticServer.js

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions src/extensions/default/Phoenix-live-preview/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ define(function (require, exports, module) {
utils = require('utils');

const LIVE_PREVIEW_PANEL_ID = "live-preview-panel",
NAVIGATOR_REDIRECT_PAGE = "REDIRECT_PAGE",
IFRAME_EVENT_SERVER_READY = 'SERVER_READY';
let serverReady = false;
const LIVE_PREVIEW_IFRAME_HTML = `
<iframe id="panel-live-preview-frame" title="Live Preview" style="border: none"
width="100%" height="100%" seamless="true"
src='about:blank'
sandbox="allow-same-origin allow-scripts allow-popups allow-forms allow-modals allow-pointer-lock">
sandbox="allow-same-origin allow-scripts allow-forms allow-modals allow-pointer-lock">
</iframe>
`;

const LOADER_BROADCAST_ID = `live-preview-loader-${Phoenix.PHOENIX_INSTANCE_ID}`;
const navigatorChannel = new BroadcastChannel(LOADER_BROADCAST_ID);

ExtensionInterface.registerExtensionInterface(
ExtensionInterface._DEFAULT_EXTENSIONS_INTERFACE_NAMES.PHOENIX_LIVE_PREVIEW, exports);

Expand Down Expand Up @@ -171,23 +173,17 @@ define(function (require, exports, module) {
}

function _getTabNavigationURL(url) {
let details = LiveDevelopment.getLivePreviewDetails(),
openURL = new URL(url);
let openURL = new URL(url);
// we tag all externally opened urls with query string parameter phcodeLivePreview="true" to address
// #LIVE_PREVIEW_TAB_NAVIGATION_RACE_FIX
openURL.searchParams.set(StaticServer.PHCODE_LIVE_PREVIEW_QUERY_PARAM, "true");
openURL = openURL.href;
if(details.URL !== url) {
openURL = utils.getPageLoaderURL(url);
}
return openURL;
return utils.getPageLoaderURL(openURL.href);
}

function _redirectAllTabs(newURL) {
const openURL = _getTabNavigationURL(newURL);
StaticServer.messageToLivePreviewTabs({
type: NAVIGATOR_REDIRECT_PAGE,
URL: openURL
navigatorChannel.postMessage({
type: 'REDIRECT_PAGE',
url: newURL
});
}

Expand Down Expand Up @@ -234,7 +230,8 @@ define(function (require, exports, module) {
$iframe.attr('srcdoc', null);
};

const popoutSupported = Phoenix.browser.isTauri || Phoenix.browser.desktop.isChromeBased;
const popoutSupported = Phoenix.browser.isTauri
|| Phoenix.browser.desktop.isChromeBased || Phoenix.browser.desktop.isFirefox;
if(!popoutSupported){
// live preview can be popped out currently in only chrome based browsers. The cross domain iframe
// that serves the live preview(phcode.live) is sandboxed to the tab in which phcode.dev resides.
Expand Down Expand Up @@ -284,8 +281,7 @@ define(function (require, exports, module) {
newIframe.insertAfter($iframe);
$iframe.remove();
$iframe = newIframe;
const iframeURL = utils.isImage(previewDetails.fullPath) ? _getTabNavigationURL(newSrc) : newSrc;
$iframe.attr('src', iframeURL);
$iframe.attr('src', newSrc);
}
Metrics.countEvent(Metrics.EVENT_TYPE.LIVE_PREVIEW, "render",
utils.getExtension(previewDetails.fullPath));
Expand All @@ -306,7 +302,11 @@ define(function (require, exports, module) {
}

let livePreviewEnabledOnProjectSwitch = false;
async function _projectOpened() {
async function _projectOpened(_evt, projectRoot) {
navigatorChannel.postMessage({
type: 'PROJECT_SWITCH',
projectRoot: projectRoot.fullPath
});
if(urlPinned){
_togglePinUrl();
}
Expand Down
16 changes: 12 additions & 4 deletions src/extensions/default/Phoenix-live-preview/markdown.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
<title>Markdown- Phoenix</title>


<link rel="stylesheet" href="{{{BOOTSTRAP_LIB_CSS}}}">
<link href="{{{HIGHLIGHT_JS_CSS}}}" rel="stylesheet">
<script src="{{{HIGHLIGHT_JS}}}"></script>
<link rel="stylesheet" href="{{{GFM_CSS}}}" />
<style>
{{{BOOTSTRAP_LIB_CSS}}}
</style>
<style>
{{{HIGHLIGHT_JS_CSS}}}
</style>
<script>
{{{HIGHLIGHT_JS}}}
</script>
<style>
{{{GFM_CSS}}}
</style>
<script type="text/javascript">
function inIframe () {
try {
Expand Down
3 changes: 2 additions & 1 deletion src/extensions/default/Phoenix-live-preview/panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
title="live preview server"
src="about:blank"
style="width:100%;"
sandbox="allow-same-origin allow-scripts"
hidden>
</iframe>
</div>
<div class="frame-container">
<div style="width: 3px;"></div>
<iframe id="panel-live-preview-frame" title="Live Preview" style="border: none"
width="100%" height="100%" seamless="true" srcdoc='<img width="50px" src="styles/images/Spinner-1s-200px.svg">'
sandbox="allow-same-origin allow-scripts allow-popups allow-forms allow-modals allow-pointer-lock">
sandbox="allow-same-origin allow-scripts allow-forms allow-modals allow-pointer-lock">
</iframe>
</div>
</div>
11 changes: 7 additions & 4 deletions src/extensions/default/Phoenix-live-preview/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ define(function (require, exports, module) {
Strings = brackets.getModule("strings"),
DocumentManager = brackets.getModule("document/DocumentManager"),
LiveDevelopment = brackets.getModule("LiveDevelopment/main"),
LiveDevServerManager = brackets.getModule("LiveDevelopment/LiveDevServerManager"),
LivePreviewTransport = brackets.getModule("LiveDevelopment/MultiBrowserImpl/transports/LivePreviewTransport");
LiveDevServerManager = brackets.getModule("LiveDevelopment/LiveDevServerManager");

function getExtension(filePath) {
filePath = filePath || '';
Expand Down Expand Up @@ -88,8 +87,12 @@ define(function (require, exports, module) {
}

function getPageLoaderURL(url) {
return `${LiveDevServerManager.getStaticServerBaseURLs().baseURL}pageLoader.html?`
+`broadcastChannel=${LivePreviewTransport.BROADCAST_CHANNEL_ID}&URL=${encodeURIComponent(url)}`;
return `${Phoenix.baseURL}live-preview-loader.html?`
+`virtualServerURL=${encodeURIComponent(LiveDevServerManager.getStaticServerBaseURLs().baseURL)}`
+`&phoenixInstanceID=${Phoenix.PHOENIX_INSTANCE_ID}&initialURL=${encodeURIComponent(url)}`
+`&localMessage=${encodeURIComponent(Strings.DESCRIPTION_LIVEDEV_SECURITY_POPOUT_MESSAGE)}`
+`&initialProjectRoot=${encodeURIComponent(ProjectManager.getProjectRoot().fullPath)}`
+`&okMessage=${encodeURIComponent(Strings.TRUST_PROJECT)}`;
}

function _isLivePreviewSupported() {
Expand Down
2 changes: 2 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
const appLocalDirPromise = window.__TAURI__.path.appLocalDataDir();
const tempDirPromise = window.__TAURI__.os.tempdir();
window._tauriBootVars = {};
const tauriBootStartTime = Date.now();
window._tauriBootVarsPromise = Promise.all([appNamePromise, documentDirPromise,
appLocalDirPromise, tempDirPromise])
.then((results) => {
Expand All @@ -164,6 +165,7 @@

window._tauriBootVars.appLocalDir = results[2];
window._tauriBootVars.tempDir = results[3];
window._tauriBootVars.bootstrapTime = Date.now() - tauriBootStartTime;
localStorage.setItem(TAURI_BOOT_VARS_LOCALSTORAGE_KEY, JSON.stringify(window._tauriBootVars));
});
}
Expand Down
Loading
Loading