Skip to content

Commit db0159c

Browse files
committed
feat: add select mode button in the live preview toolbar
1 parent ef6b544 commit db0159c

File tree

4 files changed

+115
-2
lines changed

4 files changed

+115
-2
lines changed

src/LiveDevelopment/main.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,14 @@ define(function main(require, exports, module) {
354354

355355
config.highlight = PreferencesManager.getViewState("livedevHighlight");
356356

357+
function setLivePreviewEditFeaturesActive(enabled) {
358+
isLPEditFeaturesActive = enabled;
359+
config.isLPEditFeaturesActive = enabled;
360+
if (MultiBrowserLiveDev && MultiBrowserLiveDev.status >= MultiBrowserLiveDev.STATUS_ACTIVE) {
361+
MultiBrowserLiveDev.updateConfig(JSON.stringify(config));
362+
}
363+
}
364+
357365
// init commands
358366
CommandManager.register(Strings.CMD_LIVE_HIGHLIGHT, Commands.FILE_LIVE_HIGHLIGHT, togglePreviewHighlight);
359367
CommandManager.register(Strings.CMD_RELOAD_LIVE_PREVIEW, Commands.CMD_RELOAD_LIVE_PREVIEW, _handleReloadLivePreviewCommand);
@@ -380,6 +388,7 @@ define(function main(require, exports, module) {
380388
exports.setLivePreviewPinned = setLivePreviewPinned;
381389
exports.setLivePreviewTransportBridge = setLivePreviewTransportBridge;
382390
exports.togglePreviewHighlight = togglePreviewHighlight;
391+
exports.setLivePreviewEditFeaturesActive = setLivePreviewEditFeaturesActive;
383392
exports.getConnectionIds = MultiBrowserLiveDev.getConnectionIds;
384393
exports.getLivePreviewDetails = MultiBrowserLiveDev.getLivePreviewDetails;
385394
});

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,33 @@
100100
.pointer-icon {
101101
background: url("./images/sprites.svg#pointer-icon") center no-repeat;
102102
}
103+
104+
#livePreviewModeBtn {
105+
min-width: fit-content;
106+
display: flex;
107+
align-items: center;
108+
margin-bottom: 4px;
109+
margin-right: 8px;
110+
max-width: 80%;
111+
text-overflow: ellipsis;
112+
overflow: hidden;
113+
white-space: nowrap;
114+
cursor: pointer;
115+
background: #3C3F41;
116+
box-shadow: none;
117+
border: 1px solid #3C3F41;
118+
box-sizing: border-box;
119+
color: #a0a0a0;
120+
font-size: 14px;
121+
font-weight: normal;
122+
}
123+
124+
#livePreviewModeBtn:hover {
125+
border: 1px solid rgba(0, 0, 0, 0.24) !important;
126+
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12) !important;
127+
}
128+
129+
#livePreviewModeBtn.btn-dropdown::after {
130+
top: 9px !important;
131+
left: 90px !important;
132+
}

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

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ define(function (require, exports, module) {
6262
NativeApp = require("utils/NativeApp"),
6363
StringUtils = require("utils/StringUtils"),
6464
FileSystem = require("filesystem/FileSystem"),
65+
DropdownButton = require("widgets/DropdownButton"),
6566
BrowserStaticServer = require("./BrowserStaticServer"),
6667
NodeStaticServer = require("./NodeStaticServer"),
6768
LivePreviewSettings = require("./LivePreviewSettings"),
@@ -95,6 +96,9 @@ define(function (require, exports, module) {
9596
</iframe>
9697
`;
9798

99+
let isEditModeEnabled = true;
100+
let isHighlightModeEnabled = true;
101+
98102
if(Phoenix.isTestWindow) {
99103
// for integ tests
100104
window._livePreviewIntegTest = {
@@ -120,7 +124,8 @@ define(function (require, exports, module) {
120124
$safariButtonBallast,
121125
$edgeButtonBallast,
122126
$firefoxButtonBallast,
123-
$panelTitle;
127+
$panelTitle,
128+
$modeBtn;
124129

125130
let customLivePreviewBannerShown = false;
126131

@@ -139,6 +144,71 @@ define(function (require, exports, module) {
139144
editor.focus();
140145
});
141146

147+
148+
function _toggleLivePreviewEditMode() {
149+
isEditModeEnabled = !isEditModeEnabled;
150+
LiveDevelopment.setLivePreviewEditFeaturesActive(isEditModeEnabled);
151+
}
152+
153+
function _toggleHighlightMode() {
154+
isHighlightModeEnabled = !isHighlightModeEnabled;
155+
156+
// only toggle highlights if the current state doesn't match what we want
157+
const currentHighlightState = _isLiveHighlightEnabled();
158+
if (currentHighlightState !== isHighlightModeEnabled) {
159+
_toggleLiveHighlights();
160+
}
161+
162+
Metrics.countEvent(
163+
Metrics.EVENT_TYPE.LIVE_PREVIEW, "highlightMode", isHighlightModeEnabled ? "enable" : "disable"
164+
);
165+
}
166+
167+
function _showModeSelectionDropdown(event) {
168+
const items = ["Edit Mode", "Highlight Mode"];
169+
170+
const dropdown = new DropdownButton.DropdownButton("", items, function (item, index) {
171+
// Add checkmark if the mode is enabled
172+
if ((index === 0 && isEditModeEnabled) || (index === 1 && isHighlightModeEnabled)) {
173+
return "✓ " + item;
174+
}
175+
// when disabled we remove the check, add spacing so that content remains aligned
176+
return "&nbsp;".repeat(4) + item;
177+
});
178+
179+
// Append to document body for absolute positioning
180+
$("body").append(dropdown.$button);
181+
182+
// Position the dropdown at the mouse coordinates
183+
dropdown.$button.css({
184+
position: "absolute",
185+
left: event.pageX + "px",
186+
top: event.pageY + "px",
187+
zIndex: 1000
188+
});
189+
190+
// Add a custom class to override the max-height
191+
dropdown.dropdownExtraClasses = "mode-context-menu";
192+
193+
dropdown.showDropdown();
194+
195+
$(".mode-context-menu").css("max-height", "300px");
196+
197+
// handle the option selection
198+
dropdown.on("select", function (e, item, index) {
199+
if (index === 0) {
200+
_toggleLivePreviewEditMode();
201+
} else if (index === 1) {
202+
_toggleHighlightMode();
203+
}
204+
});
205+
206+
// Remove the button after the dropdown is hidden
207+
dropdown.$button.css({
208+
display: "none"
209+
});
210+
}
211+
142212
function _isLiveHighlightEnabled() {
143213
return CommandManager.get(Commands.FILE_LIVE_HIGHLIGHT).getChecked();
144214
}
@@ -402,6 +472,9 @@ define(function (require, exports, module) {
402472
$firefoxButtonBallast = $panel.find("#firefoxButtonBallast");
403473
$panelTitle = $panel.find("#panel-live-preview-title");
404474
$settingsIcon = $panel.find("#livePreviewSettingsBtn");
475+
$modeBtn = $panel.find("#livePreviewModeBtn");
476+
477+
$modeBtn.on("click", _showModeSelectionDropdown);
405478

406479
$panel.find(".live-preview-settings-banner-btn").on("click", ()=>{
407480
CommandManager.execute(Commands.FILE_LIVE_FILE_PREVIEW_SETTINGS);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
<img src="thirdparty/devicon/icons/firefox/firefox-original.svg" />
3636
</button>
3737
</div>
38-
<div style="width: 20%;display: flex;align-items: center; justify-content: flex-end">
38+
<div style="width: 30%;display: flex;align-items: center; justify-content: flex-end">
39+
<button id="livePreviewModeBtn" title="Configure Live Preview Modes" class="btn-alt-quiet toolbar-button btn-dropdown btn">Select Mode</button>
3940
<button id="livePreviewSettingsBtn" title="{{livePreviewSettings}}"
4041
class="btn-alt-quiet toolbar-button lp-settings-icon"><i class="fa-solid fa-gear"></i></button>
4142
</div>

0 commit comments

Comments
 (0)