Skip to content

Commit e36e8b8

Browse files
committed
feat: add setting to sync the preview and the editor
1 parent d66e457 commit e36e8b8

8 files changed

Lines changed: 62 additions & 7 deletions

File tree

src/LiveDevelopment/BrowserScripts/LiveDevProtocolRemote.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,13 @@
539539
// as a live select.
540540
return;
541541
}
542+
if (window._LD && !window._LD.isSyncEnabled()) {
543+
// When sync is disabled, highlight the element directly in the browser
544+
// without doing a round-trip through the editor (which would move the cursor)
545+
var tagId = element.getAttribute('data-brackets-id');
546+
window._LD.highlightRule("[data-brackets-id='" + tagId + "']");
547+
return;
548+
}
542549
MessageBroker.send({
543550
"tagId": element.getAttribute('data-brackets-id'),
544551
"nodeID": element.id,

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,8 @@ function RemoteFunctions(config = {}) {
618618
}
619619

620620
// send cursor movement message to editor so cursor jumps to clicked element
621-
if (element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR)) {
621+
if (element.hasAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR) &&
622+
config.syncSourceAndPreview !== false) {
622623
MessageBroker.send({
623624
"tagId": element.getAttribute(GLOBALS.DATA_BRACKETS_ID_ATTR),
624625
"nodeID": element.id,
@@ -1314,6 +1315,7 @@ function RemoteFunctions(config = {}) {
13141315
"dismissUIAndCleanupState": dismissUIAndCleanupState,
13151316
"escapeKeyPressInEditor": _handleEscapeKeyPress,
13161317
"getMode": function() { return config.mode; },
1318+
"isSyncEnabled": function() { return config.syncSourceAndPreview !== false; },
13171319
"suppressDOMEditDismissal": suppressDOMEditDismissal,
13181320
"setHotCornerHidden": function(hidden) {
13191321
if (SHARED_STATE._hotCorner && SHARED_STATE._hotCorner.hotCorner) {

src/LiveDevelopment/LivePreviewConstants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ define(function main(require, exports, module) {
4242

4343
exports.PREFERENCE_SHOW_RULER_LINES = "livePreviewShowMeasurements";
4444
exports.PREFERENCE_SHOW_SPACING_HANDLES = "livePreviewShowSpacingHandles";
45+
46+
exports.PREFERENCE_LIVE_PREVIEW_SYNC = "livePreviewSyncSourceAndPreview";
4547
});

src/LiveDevelopment/MultiBrowserImpl/documents/LiveDocument.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ define(function (require, exports, module) {
192192
if (!this.editor) {
193193
return;
194194
}
195-
if(!_disableHighlightOnCursor){
195+
if(!_disableHighlightOnCursor &&
196+
PreferencesManager.get(CONSTANTS.PREFERENCE_LIVE_PREVIEW_SYNC) !== false){
196197
this.updateHighlight();
197198
}
198199
};

src/LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ define(function (require, exports, module) {
240240
// hilights are enabled only in edit and highlight mode
241241
return;
242242
}
243+
if(PreferencesManager.get(CONSTANTS.PREFERENCE_LIVE_PREVIEW_SYNC) === false){
244+
return;
245+
}
243246
const liveDoc = LiveDevMultiBrowser.getCurrentLiveDoc(),
244247
activeEditor = EditorManager.getActiveEditor(), // this can be an inline editor
245248
activeFullEditor = EditorManager.getCurrentFullEditor();

src/LiveDevelopment/main.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ define(function main(require, exports, module) {
7171
elemHighlights: CONSTANTS.HIGHLIGHT_HOVER, // default value, this will get updated when the extension loads
7272
showRulerLines: false, // default value, this will get updated when the extension loads
7373
showSpacingHandles: true, // default value, this will get updated when the extension loads
74+
syncSourceAndPreview: true, // default value, this will get updated when the extension loads
7475
isPaidUser: false, // will be updated when we fetch entitlements
7576
isLoggedIn: false, // will be updated when we fetch entitlements
7677
hasLiveEditCapability: false // handled inside _liveEditCapabilityChanged function
@@ -332,6 +333,13 @@ define(function main(require, exports, module) {
332333
MultiBrowserLiveDev.updateConfig(config);
333334
}
334335

336+
function updateSyncConfig() {
337+
const prefValue = PreferencesManager.get(CONSTANTS.PREFERENCE_LIVE_PREVIEW_SYNC);
338+
const config = MultiBrowserLiveDev.getConfig();
339+
config.syncSourceAndPreview = prefValue !== false;
340+
MultiBrowserLiveDev.updateConfig(config);
341+
}
342+
335343
EventDispatcher.makeEventDispatcher(exports);
336344

337345
// private api
@@ -356,6 +364,7 @@ define(function main(require, exports, module) {
356364
exports.updateElementHighlightConfig = updateElementHighlightConfig;
357365
exports.updateRulerLinesConfig = updateRulerLinesConfig;
358366
exports.updateSpacingHandlesConfig = updateSpacingHandlesConfig;
367+
exports.updateSyncConfig = updateSyncConfig;
359368
exports.getConnectionIds = MultiBrowserLiveDev.getConnectionIds;
360369
exports.getLivePreviewDetails = MultiBrowserLiveDev.getLivePreviewDetails;
361370
exports.hideHighlight = MultiBrowserLiveDev.hideHighlight;

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ define(function (require, exports, module) {
116116
description: Strings.LIVE_DEV_SETTINGS_SHOW_SPACING_HANDLES_PREFERENCE
117117
});
118118

119+
// live preview sync source and preview preference
120+
const PREFERENCE_LIVE_PREVIEW_SYNC = CONSTANTS.PREFERENCE_LIVE_PREVIEW_SYNC;
121+
PreferencesManager.definePreference(PREFERENCE_LIVE_PREVIEW_SYNC, "boolean", true, {
122+
description: Strings.LIVE_DEV_SETTINGS_SYNC_SOURCE_AND_PREVIEW_PREFERENCE
123+
});
124+
119125
const LIVE_PREVIEW_PANEL_ID = "live-preview-panel";
120126
const LIVE_PREVIEW_IFRAME_ID = "panel-live-preview-frame";
121127
const LIVE_PREVIEW_IFRAME_HTML = `
@@ -333,22 +339,30 @@ define(function (require, exports, module) {
333339

334340
function _showModeSelectionDropdown(event) {
335341
const isEditFeaturesActive = isProEditUser;
342+
const currentMode = LiveDevelopment.getCurrentMode();
343+
const isNotPreviewMode = currentMode !== LiveDevelopment.CONSTANTS.LIVE_PREVIEW_MODE;
336344
const items = [
337345
Strings.LIVE_PREVIEW_MODE_PREVIEW,
338346
Strings.LIVE_PREVIEW_MODE_HIGHLIGHT,
339347
Strings.LIVE_PREVIEW_MODE_EDIT
340348
];
341349

350+
// Add sync toggle for highlight and edit modes
351+
if (isNotPreviewMode) {
352+
items.push("---");
353+
items.push(Strings.LIVE_PREVIEW_SYNC_SOURCE_AND_PREVIEW);
354+
}
355+
342356
// Only add edit highlight option if edit features are active
343357
if (isEditFeaturesActive) {
344-
items.push("---");
358+
if (!isNotPreviewMode) {
359+
items.push("---");
360+
}
345361
items.push(Strings.LIVE_PREVIEW_EDIT_HIGHLIGHT_ON);
346362
items.push(Strings.LIVE_PREVIEW_SHOW_RULER_LINES);
347363
items.push(Strings.LIVE_PREVIEW_SHOW_SPACING_HANDLES);
348364
}
349365

350-
const currentMode = LiveDevelopment.getCurrentMode();
351-
352366
$dropdown = new DropdownButton.DropdownButton("", items, function(item, index) {
353367
if (item === Strings.LIVE_PREVIEW_MODE_PREVIEW) {
354368
// using empty spaces to keep content aligned
@@ -366,6 +380,12 @@ define(function (require, exports, module) {
366380
html: `${checkmark}${item}${crownIcon}`,
367381
enabled: true
368382
};
383+
} else if (item === Strings.LIVE_PREVIEW_SYNC_SOURCE_AND_PREVIEW) {
384+
const isEnabled = PreferencesManager.get(PREFERENCE_LIVE_PREVIEW_SYNC) !== false;
385+
if(isEnabled) {
386+
return `✓ ${Strings.LIVE_PREVIEW_SYNC_SOURCE_AND_PREVIEW}`;
387+
}
388+
return `${'\u00A0'.repeat(4)}${Strings.LIVE_PREVIEW_SYNC_SOURCE_AND_PREVIEW}`;
369389
} else if (item === Strings.LIVE_PREVIEW_EDIT_HIGHLIGHT_ON) {
370390
const isHoverMode =
371391
PreferencesManager.get(PREFERENCE_PROJECT_ELEMENT_HIGHLIGHT) === CONSTANTS.HIGHLIGHT_HOVER;
@@ -422,6 +442,11 @@ define(function (require, exports, module) {
422442
Metrics.countEvent(Metrics.EVENT_TYPE.PRO, "proUpsellDlg", "fail");
423443
}
424444
}
445+
} else if (item === Strings.LIVE_PREVIEW_SYNC_SOURCE_AND_PREVIEW) {
446+
// Toggle sync source and preview on/off
447+
const currentValue = PreferencesManager.get(PREFERENCE_LIVE_PREVIEW_SYNC);
448+
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_SYNC, currentValue === false);
449+
return; // Don't dismiss for this option
425450
} else if (item === Strings.LIVE_PREVIEW_EDIT_HIGHLIGHT_ON) {
426451
// Don't allow edit highlight toggle if edit features are not active
427452
if (!isEditFeaturesActive) {
@@ -1252,11 +1277,15 @@ define(function (require, exports, module) {
12521277
PreferencesManager.on("change", PREFERENCE_SHOW_SPACING_HANDLES, function() {
12531278
LiveDevelopment.updateSpacingHandlesConfig();
12541279
});
1280+
PreferencesManager.on("change", PREFERENCE_LIVE_PREVIEW_SYNC, function() {
1281+
LiveDevelopment.updateSyncConfig();
1282+
});
12551283

1256-
// Initialize element highlight, ruler lines, and spacing handles config on startup
1284+
// Initialize element highlight, ruler lines, spacing handles, and sync config on startup
12571285
LiveDevelopment.updateElementHighlightConfig();
12581286
LiveDevelopment.updateRulerLinesConfig();
12591287
LiveDevelopment.updateSpacingHandlesConfig();
1288+
LiveDevelopment.updateSyncConfig();
12601289

12611290
LiveDevelopment.openLivePreview();
12621291
LiveDevelopment.on(LiveDevelopment.EVENT_OPEN_PREVIEW_URL, _openLivePreviewURL);

src/nls/root/strings.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ define({
240240
"LIVE_DEV_STYLES_PANEL_NO_STYLES": "No styles found",
241241
"LIVE_DEV_STYLES_PANEL_PROPERTY_PLACEHOLDER": "property",
242242
"LIVE_DEV_STYLES_PANEL_VALUE_PLACEHOLDER": "value",
243-
"LIVE_DEV_STYLES_TAB_ADVANCED": "Advanced Styles",
243+
"LIVE_DEV_STYLES_TAB_ADVANCED": "Styles",
244244
"LIVE_DEV_STYLES_TAB_COMPUTED": "Computed",
245245
"LIVE_DEV_STYLES_FILTER_ALL": "All",
246246
"LIVE_DEV_STYLES_FILTER_LAYOUT": "Layout",
@@ -280,6 +280,8 @@ define({
280280
"LIVE_PREVIEW_SHOW_RULER_LINES": "Show Measurements",
281281
"LIVE_PREVIEW_SHOW_SPACING_HANDLES": "Show Spacing Handles",
282282
"LIVE_DEV_SETTINGS_SHOW_SPACING_HANDLES_PREFERENCE": "Show spacing handles when elements are selected in live preview edit mode. Defaults to 'true'",
283+
"LIVE_PREVIEW_SYNC_SOURCE_AND_PREVIEW": "Sync Code & Preview",
284+
"LIVE_DEV_SETTINGS_SYNC_SOURCE_AND_PREVIEW_PREFERENCE": "Sync source code cursor with live preview element highlighting. When enabled, moving the cursor in the editor highlights the corresponding element in the live preview, and clicking an element in the live preview jumps the cursor to its source code. Defaults to 'true'",
283285
"LIVE_PREVIEW_MODE_PREFERENCE": "'{0}' shows only the webpage, '{1}' connects the webpage to your code - click on elements to jump to their code and vice versa, '{2}' provides highlighting along with advanced element manipulation",
284286
"LIVE_PREVIEW_CONFIGURE_MODES": "Configure Live Preview Modes",
285287

0 commit comments

Comments
 (0)