Skip to content

Commit a8cd2b6

Browse files
committed
feat: add preview mode option in live preview
1 parent d728342 commit a8cd2b6

File tree

3 files changed

+48
-51
lines changed

3 files changed

+48
-51
lines changed

src/extensionsIntegrated/Phoenix-live-preview/live-preview.css

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@
9797
min-width: fit-content;
9898
display: flex;
9999
align-items: center;
100-
margin-bottom: 4px;
101-
margin-right: 8px;
100+
margin-right: 4px;
102101
max-width: 80%;
103102
text-overflow: ellipsis;
104103
overflow: hidden;
@@ -120,5 +119,4 @@
120119

121120
#livePreviewModeBtn.btn-dropdown::after {
122121
top: 9px !important;
123-
left: 90px !important;
124122
}

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

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ define(function (require, exports, module) {
9696
</iframe>
9797
`;
9898

99-
let isEditModeEnabled = LiveDevelopment.isLPEditFeaturesActive;
100-
let isHighlightModeEnabled = null; // Will be initialized later
101-
10299
if(Phoenix.isTestWindow) {
103100
// for integ tests
104101
window._livePreviewIntegTest = {
@@ -144,42 +141,48 @@ define(function (require, exports, module) {
144141
});
145142

146143

147-
function _toggleLivePreviewEditMode() {
148-
isEditModeEnabled = !isEditModeEnabled;
149-
LiveDevelopment.setLivePreviewEditFeaturesActive(isEditModeEnabled);
144+
// this function is to check if the live highlight feature is enabled or not
145+
function _isLiveHighlightEnabled() {
146+
return CommandManager.get(Commands.FILE_LIVE_HIGHLIGHT).getChecked();
147+
}
150148

151-
// clear any existing markers and highlights when edit mode is disabled
152-
if (!isEditModeEnabled) {
153-
LiveDevelopment.hideHighlight();
154-
LiveDevelopment.dismissLivePreviewBoxes();
149+
/**
150+
* Live Preview 'Preview Mode'. in this mode no live preview highlight or any such features are active
151+
* Just the plain website
152+
*/
153+
function _LPPreviewMode() {
154+
LiveDevelopment.setLivePreviewEditFeaturesActive(false);
155+
if(_isLiveHighlightEnabled()) {
156+
LiveDevelopment.togglePreviewHighlight();
155157
}
156158
}
157159

158-
function _toggleHighlightMode() {
159-
isHighlightModeEnabled = !isHighlightModeEnabled;
160-
161-
// only toggle highlights if the current state doesn't match what we want
162-
const currentHighlightState = _isLiveHighlightEnabled();
163-
if (currentHighlightState !== isHighlightModeEnabled) {
164-
_toggleLiveHighlights();
160+
/**
161+
* Live Preview 'Inspect Mode'. in this mode only the live preview matching with the source code is active
162+
* Meaning that if user clicks on some element that element's source code will be highlighted and vice versa
163+
*/
164+
function _LPInspectMode() {
165+
LiveDevelopment.setLivePreviewEditFeaturesActive(false);
166+
if(!_isLiveHighlightEnabled()) {
167+
LiveDevelopment.togglePreviewHighlight();
165168
}
169+
}
166170

167-
Metrics.countEvent(
168-
Metrics.EVENT_TYPE.LIVE_PREVIEW, "highlightMode", isHighlightModeEnabled ? "enable" : "disable"
169-
);
171+
/**
172+
* Live Preview 'Edit Mode'. this is the most interactive mode, in here the inspect features are available
173+
* along with that we also show element's highlighted boxes and such
174+
*/
175+
function _LPEditMode() {
176+
LiveDevelopment.setLivePreviewEditFeaturesActive(true);
177+
if(!_isLiveHighlightEnabled()) {
178+
LiveDevelopment.togglePreviewHighlight();
179+
}
170180
}
171181

172182
function _showModeSelectionDropdown(event) {
173-
const items = ["Edit Mode", "Highlight Mode"];
183+
const items = ["Preview Mode", "Inspect Mode", "Edit Mode"];
174184

175-
const dropdown = new DropdownButton.DropdownButton("", items, function (item, index) {
176-
// Add checkmark if the mode is enabled
177-
if ((index === 0 && isEditModeEnabled) || (index === 1 && isHighlightModeEnabled)) {
178-
return "✓ " + item;
179-
}
180-
// when disabled we remove the check, add spacing so that content remains aligned
181-
return "&nbsp;".repeat(4) + item;
182-
});
185+
const dropdown = new DropdownButton.DropdownButton("", items);
183186

184187
// Append to document body for absolute positioning
185188
$("body").append(dropdown.$button);
@@ -202,9 +205,18 @@ define(function (require, exports, module) {
202205
// handle the option selection
203206
dropdown.on("select", function (e, item, index) {
204207
if (index === 0) {
205-
_toggleLivePreviewEditMode();
208+
_LPPreviewMode();
206209
} else if (index === 1) {
207-
_toggleHighlightMode();
210+
_LPInspectMode();
211+
} else if (index === 2) {
212+
_LPEditMode();
213+
}
214+
215+
// need to dismiss the previous highlighting and stuff
216+
LiveDevelopment.hideHighlight();
217+
LiveDevelopment.dismissLivePreviewBoxes();
218+
if($modeBtn) {
219+
$modeBtn[0].textContent = item;
208220
}
209221
});
210222

@@ -214,10 +226,6 @@ define(function (require, exports, module) {
214226
});
215227
}
216228

217-
function _isLiveHighlightEnabled() {
218-
return CommandManager.get(Commands.FILE_LIVE_HIGHLIGHT).getChecked();
219-
}
220-
221229
function _getTrustProjectPage() {
222230
const trustProjectMessage = StringUtils.format(Strings.TRUST_PROJECT,
223231
path.basename(ProjectManager.getProjectRoot().fullPath));
@@ -362,11 +370,6 @@ define(function (require, exports, module) {
362370
Metrics.countEvent(Metrics.EVENT_TYPE.LIVE_PREVIEW, "pinURLBtn", "click");
363371
}
364372

365-
function _toggleLiveHighlights() {
366-
LiveDevelopment.togglePreviewHighlight();
367-
Metrics.countEvent(Metrics.EVENT_TYPE.LIVE_PREVIEW, "HighlightBtn", "click");
368-
}
369-
370373
const ALLOWED_BROWSERS_NAMES = [`chrome`, `firefox`, `safari`, `edge`, `browser`, `browserPrivate`];
371374
function _popoutLivePreview(browserName) {
372375
// We cannot use $iframe.src here if panel is hidden
@@ -468,13 +471,6 @@ define(function (require, exports, module) {
468471
$settingsIcon = $panel.find("#livePreviewSettingsBtn");
469472
$modeBtn = $panel.find("#livePreviewModeBtn");
470473

471-
// initialize the value
472-
if (isHighlightModeEnabled === null) {
473-
isHighlightModeEnabled = _isLiveHighlightEnabled();
474-
}
475-
476-
$modeBtn.on("click", _showModeSelectionDropdown);
477-
478474
$panel.find(".live-preview-settings-banner-btn").on("click", ()=>{
479475
CommandManager.execute(Commands.FILE_LIVE_FILE_PREVIEW_SETTINGS);
480476
Metrics.countEvent(Metrics.EVENT_TYPE.LIVE_PREVIEW, "settingsBtnBanner", "click");
@@ -506,6 +502,9 @@ define(function (require, exports, module) {
506502
$firefoxButton.on("click", ()=>{
507503
_popoutLivePreview("firefox");
508504
});
505+
506+
$modeBtn.on("click", _showModeSelectionDropdown);
507+
509508
_showOpenBrowserIcons();
510509
$settingsIcon.click(()=>{
511510
CommandManager.execute(Commands.FILE_LIVE_FILE_PREVIEW_SETTINGS);

src/extensionsIntegrated/Phoenix-live-preview/panel.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
</button>
3636
</div>
3737
<div style="width: 30%;display: flex;align-items: center; justify-content: flex-end">
38-
<button id="livePreviewModeBtn" title="Configure Live Preview Modes" class="btn-alt-quiet toolbar-button btn-dropdown btn">Select Mode</button>
3938
<button id="livePreviewSettingsBtn" title="{{livePreviewSettings}}"
4039
class="btn-alt-quiet toolbar-button lp-settings-icon"><i class="fa-solid fa-gear"></i></button>
40+
<button id="livePreviewModeBtn" title="Configure Live Preview Modes" class="btn-alt-quiet toolbar-button btn-dropdown btn"><!-- Content will come here dynamically --></button>
4141
</div>
4242
<iframe id="live-preview-server-iframe"
4343
title="live preview server"

0 commit comments

Comments
 (0)