Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 14370ae

Browse files
committed
Merge pull request #10285 from adobe/kai/fix-10206-ld-mb-unittests-part1
Live Preview MultiBrowser improvements (#10206, #10239)
2 parents 93be1de + eef9c68 commit 14370ae

File tree

7 files changed

+82
-33
lines changed

7 files changed

+82
-33
lines changed

src/LiveDevelopment/LiveDevMultiBrowser.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,13 @@ define(function (require, exports, module) {
467467
}
468468

469469
/**
470-
* Close all active connections
470+
* Closes all active connections.
471+
* Returns a resolved promise for API compatibility.
472+
* @return {$.Promise} A resolved promise
471473
*/
472474
function close() {
473-
return _close(true);
475+
_close(true);
476+
return new $.Deferred().resolve().promise();
474477
}
475478

476479
/**

src/LiveDevelopment/LiveDevelopment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
* 2: Loading agents
5353
* 3: Active
5454
* 4: Out of sync
55+
* 5: Sync error
5556
*
5657
* The reason codes are:
5758
* - null (Unknown reason)

src/LiveDevelopment/main.js

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ define(function main(require, exports, module) {
4040
Commands = require("command/Commands"),
4141
AppInit = require("utils/AppInit"),
4242
LiveDevelopment = require("LiveDevelopment/LiveDevelopment"),
43+
MultiBrowserLiveDev = require("LiveDevelopment/LiveDevMultiBrowser"),
4344
Inspector = require("LiveDevelopment/Inspector/Inspector"),
4445
CommandManager = require("command/CommandManager"),
4546
PreferencesManager = require("preferences/PreferencesManager"),
@@ -50,9 +51,6 @@ define(function main(require, exports, module) {
5051
ExtensionUtils = require("utils/ExtensionUtils"),
5152
StringUtils = require("utils/StringUtils");
5253

53-
// expermiental multi-browser implementation
54-
var MultiBrowserLiveDev = require("LiveDevelopment/LiveDevMultiBrowser");
55-
5654
var params = new UrlParams();
5755
var config = {
5856
experimental: false, // enable experimental features
@@ -75,7 +73,41 @@ define(function main(require, exports, module) {
7573

7674
// current selected implementation (LiveDevelopment | LiveDevMultiBrowser)
7775
var LiveDevImpl;
76+
77+
// "livedev.multibrowser" preference
78+
var PREF_MULTIBROWSER = "multibrowser";
79+
var prefs = PreferencesManager.getExtensionPrefs("livedev");
80+
var multiBrowserPref = prefs.definePreference(PREF_MULTIBROWSER, "boolean", false);
81+
82+
/** Toggles or sets the preference **/
83+
function _togglePref(key, value) {
84+
var val,
85+
oldPref = !!prefs.get(key);
7886

87+
if (value === undefined) {
88+
val = !oldPref;
89+
} else {
90+
val = !!value;
91+
}
92+
93+
// update menu
94+
if (val !== oldPref) {
95+
prefs.set(key, val);
96+
}
97+
98+
return val;
99+
}
100+
101+
/* Toggles or sets the "livedev.multibrowser" preference */
102+
function _toggleLivePreviewMultiBrowser(value) {
103+
var val = _togglePref(PREF_MULTIBROWSER, value);
104+
105+
CommandManager.get(Commands.TOGGLE_LIVE_PREVIEW_MB_MODE).setChecked(val);
106+
// Issue #10217: multi-browser does not support user server, so disable
107+
// the setting if it is enabled.
108+
CommandManager.get(Commands.FILE_PROJECT_SETTINGS).setEnabled(!val);
109+
}
110+
79111
/** Load Live Development LESS Style */
80112
function _loadStyles() {
81113
var lessText = require("text!LiveDevelopment/main.less"),
@@ -215,10 +247,11 @@ define(function main(require, exports, module) {
215247
PreferencesManager.setViewState("livedev.highlight", config.highlight);
216248
}
217249

218-
// sets the MultiBrowserLiveDev implementation if multibrowser = true,
219-
// keeps default LiveDevelopment implementation based on CDT in other case.
220-
// since UI status are slightly different btw implementations, it also set
221-
// the corresponding style values.
250+
/**
251+
* Sets the MultiBrowserLiveDev implementation if multibrowser is truthy,
252+
* keeps default LiveDevelopment implementation based on CDT otherwise.
253+
* It also resets the listeners and UI elements.
254+
*/
222255
function _setImplementation(multibrowser) {
223256
if (multibrowser) {
224257
// set implemenation
@@ -246,6 +279,11 @@ define(function main(require, exports, module) {
246279
{ tooltip: Strings.LIVE_DEV_STATUS_TIP_SYNC_ERROR, style: "sync-error" }
247280
];
248281
}
282+
// setup status changes listeners for new implementation
283+
_setupGoLiveButton();
284+
_setupGoLiveMenu();
285+
// toggle the menu
286+
_toggleLivePreviewMultiBrowser(multibrowser);
249287
}
250288

251289
/** Setup window references to useful LiveDevelopment modules */
@@ -261,7 +299,7 @@ define(function main(require, exports, module) {
261299
LiveDevelopment.reload();
262300
}
263301
}
264-
302+
265303
/** Initialize LiveDevelopment */
266304
AppInit.appReady(function () {
267305
params.parse();
@@ -274,13 +312,12 @@ define(function main(require, exports, module) {
274312
// It has to be initiated at this point in case of dynamically switching
275313
// by changing the preference value.
276314
MultiBrowserLiveDev.init(config);
277-
278-
_loadStyles();
279-
_setupGoLiveButton();
280-
_setupGoLiveMenu();
281315

316+
_loadStyles();
282317
_updateHighlightCheckmark();
283318

319+
_setImplementation(prefs.get(PREF_MULTIBROWSER));
320+
284321
if (config.debug) {
285322
_setupDebugHelpers();
286323
}
@@ -299,6 +336,25 @@ define(function main(require, exports, module) {
299336
LiveDevelopment.redrawHighlight();
300337
}
301338
});
339+
340+
multiBrowserPref
341+
.on("change", function () {
342+
// Stop the current session if it is open and set implementation based on
343+
// the pref value. We could start the new implementation immediately, but
344+
// since the current document is potentially a user preferences file, Live
345+
// Preview will not locate the html file to serve.
346+
if (LiveDevImpl && LiveDevImpl.status >= LiveDevImpl.STATUS_ACTIVE) {
347+
LiveDevImpl.close()
348+
.done(function () {
349+
// status changes will now be listened by the new implementation
350+
LiveDevImpl.off("statusChange");
351+
_setImplementation(prefs.get(PREF_MULTIBROWSER));
352+
});
353+
} else {
354+
_setImplementation(prefs.get(PREF_MULTIBROWSER));
355+
}
356+
});
357+
302358
});
303359

304360
// init prefs
@@ -312,29 +368,15 @@ define(function main(require, exports, module) {
312368
"highlight": "user livedev.highlight",
313369
"afterFirstLaunch": "user livedev.afterFirstLaunch"
314370
}, true);
315-
316-
PreferencesManager.definePreference("livedev.multibrowser", "boolean", false)
317-
.on("change", function () {
318-
// stop the current session if it is open and set implementation based on
319-
// the pref value. It could be automaticallty restarted but, since the preference file,
320-
// is the document open in the editor, it will no launch a live document.
321-
if (LiveDevImpl && LiveDevImpl.status >= LiveDevImpl.STATUS_ACTIVE) {
322-
LiveDevImpl.close();
323-
// status changes will now be listen from the new implementation
324-
LiveDevImpl.off('statusChange');
325-
}
326-
_setImplementation(PreferencesManager.get('livedev.multibrowser'));
327-
// setup status changes listeners for new implementation
328-
_setupGoLiveButton();
329-
_setupGoLiveMenu();
330-
});
331-
371+
332372
config.highlight = PreferencesManager.getViewState("livedev.highlight");
333373

334374
// init commands
335375
CommandManager.register(Strings.CMD_LIVE_FILE_PREVIEW, Commands.FILE_LIVE_FILE_PREVIEW, _handleGoLiveCommand);
336376
CommandManager.register(Strings.CMD_LIVE_HIGHLIGHT, Commands.FILE_LIVE_HIGHLIGHT, _handlePreviewHighlightCommand);
337377
CommandManager.register(Strings.CMD_RELOAD_LIVE_PREVIEW, Commands.CMD_RELOAD_LIVE_PREVIEW, _handleReloadLivePreviewCommand);
378+
CommandManager.register(Strings.CMD_TOGGLE_LIVE_PREVIEW_MB_MODE, Commands.TOGGLE_LIVE_PREVIEW_MB_MODE, _toggleLivePreviewMultiBrowser);
379+
338380
CommandManager.get(Commands.FILE_LIVE_HIGHLIGHT).setEnabled(false);
339381

340382
// Export public functions

src/command/Commands.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ define(function (require, exports, module) {
4848
exports.FILE_CLOSE_LIST = "file.close_list"; // DocumentCommandHandlers.js handleFileCloseList()
4949
exports.FILE_OPEN_DROPPED_FILES = "file.openDroppedFiles"; // DragAndDrop.js openDroppedFiles()
5050
exports.FILE_LIVE_FILE_PREVIEW = "file.liveFilePreview"; // LiveDevelopment/main.js _handleGoLiveCommand()
51+
exports.TOGGLE_LIVE_PREVIEW_MB_MODE = "file.toggleLivePreviewMB"; // LiveDevelopment/main.js _toggleLivePreviewMultiBrowser()
5152
exports.CMD_RELOAD_LIVE_PREVIEW = "file.reloadLivePreview"; // LiveDevelopment/main.js _handleReloadLivePreviewCommand()
5253
exports.FILE_LIVE_HIGHLIGHT = "file.previewHighlight"; // LiveDevelopment/main.js _handlePreviewHighlightCommand()
5354
exports.FILE_PROJECT_SETTINGS = "file.projectSettings"; // ProjectManager.js _projectSettings()

src/command/DefaultMenus.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ define(function (require, exports, module) {
5353
menu.addMenuItem(Commands.FILE_SAVE_AS);
5454
menu.addMenuDivider();
5555
menu.addMenuItem(Commands.FILE_LIVE_FILE_PREVIEW);
56+
menu.addMenuItem(Commands.TOGGLE_LIVE_PREVIEW_MB_MODE);
5657
menu.addMenuItem(Commands.FILE_PROJECT_SETTINGS);
5758
menu.addMenuDivider();
5859
menu.addMenuItem(Commands.FILE_EXTENSION_MANAGER);

src/nls/root/strings.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ define({
317317
"CMD_FILE_SAVE_ALL" : "Save All",
318318
"CMD_FILE_SAVE_AS" : "Save As\u2026",
319319
"CMD_LIVE_FILE_PREVIEW" : "Live Preview",
320+
"CMD_TOGGLE_LIVE_PREVIEW_MB_MODE" : "Enable Experimental Live Preview",
320321
"CMD_RELOAD_LIVE_PREVIEW" : "Force Reload Live Preview",
321322
"CMD_PROJECT_SETTINGS" : "Project Settings\u2026",
322323
"CMD_FILE_RENAME" : "Rename",

test/spec/LiveDevelopmentMultiBrowser-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ define(function (require, exports, module) {
2828

2929
var SpecRunnerUtils = require("spec/SpecRunnerUtils");
3030

31-
describe("MultiBrowser (experimental) - LiveDevelopment", function () {
31+
describe("MultiBrowser (experimental)", function () {
3232

3333
this.category = "livepreview";
3434

@@ -66,6 +66,7 @@ define(function (require, exports, module) {
6666
});
6767

6868
afterEach(function () {
69+
LiveDevelopment.close();
6970
SpecRunnerUtils.closeTestWindow();
7071
testWindow = null;
7172
brackets = null;
@@ -88,7 +89,6 @@ define(function (require, exports, module) {
8889

8990
describe("Init Session", function () {
9091

91-
9292
it("should establish a browser connection for an opened html file", function () {
9393
//open a file
9494
runs(function () {

0 commit comments

Comments
 (0)