Skip to content

Commit 4750b07

Browse files
committed
chore: pro extension decides what mode to use from entitlements
This refactoring is needed as the core phcode is not aware of live preview edit capabilities. also now at appstart we always switch to edit mode to prevent confusion if its edit capable
1 parent 9bc8702 commit 4750b07

File tree

4 files changed

+80
-87
lines changed

4 files changed

+80
-87
lines changed

src/LiveDevelopment/main.js

Lines changed: 42 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
*/
2121

22-
/*global less, Phoenix */
22+
/*global less */
2323

2424
/**
2525
* main integrates LiveDevelopment into Brackets
@@ -46,25 +46,28 @@ define(function main(require, exports, module) {
4646
StringUtils = require("utils/StringUtils"),
4747
EventDispatcher = require("utils/EventDispatcher");
4848

49-
50-
const KernalModeTrust = window.KernalModeTrust;
51-
if(!KernalModeTrust){
52-
throw new Error("KernalModeTrust is not defined. Cannot boot without trust ring");
53-
}
5449
const LIVE_PREVIEW_MODE = CONSTANTS.LIVE_PREVIEW_MODE,
5550
LIVE_HIGHLIGHT_MODE = CONSTANTS.LIVE_HIGHLIGHT_MODE,
5651
LIVE_EDIT_MODE = CONSTANTS.LIVE_EDIT_MODE;
5752

5853
// this will later be assigned its correct values once entitlementsManager loads
59-
let isProEditUser = false;
60-
let isFreeTrialUser = false;
54+
let hasLiveEditCapability = false;
6155

6256
const PREFERENCE_LIVE_PREVIEW_MODE = CONSTANTS.PREFERENCE_LIVE_PREVIEW_MODE;
6357

6458
// state manager key to track image gallery selected state, by default we keep this as selected
6559
// if this is true we show the image gallery when an image element is clicked
6660
const IMAGE_GALLERY_STATE = "livePreview.imageGallery.state";
6761

62+
PreferencesManager.definePreference(PREFERENCE_LIVE_PREVIEW_MODE, "string", LIVE_HIGHLIGHT_MODE, {
63+
description: StringUtils.format(
64+
Strings.LIVE_PREVIEW_MODE_PREFERENCE, LIVE_PREVIEW_MODE, LIVE_HIGHLIGHT_MODE, LIVE_EDIT_MODE),
65+
values: [LIVE_PREVIEW_MODE, LIVE_HIGHLIGHT_MODE, LIVE_EDIT_MODE]
66+
}).on("change", function () {
67+
// when mode changes we update the config mode and notify remoteFunctions so that it can get updated
68+
_previewModeUpdated();
69+
});
70+
6871
/**
6972
* get the image gallery state from StateManager
7073
* @returns {boolean} true (default)
@@ -88,9 +91,9 @@ define(function main(require, exports, module) {
8891
}
8992
}
9093

91-
var params = new UrlParams();
92-
var config = {
93-
mode: _getDefaultMode(), // 'edit', 'highlight', or 'preview' - will be updated when preference loads
94+
let params = new UrlParams();
95+
const config = {
96+
mode: LIVE_HIGHLIGHT_MODE, // will be updated when we fetch entitlements
9497
elemHighlights: "hover", // default value, this will get updated when the extension loads
9598
showRulerLines: false, // default value, this will get updated when the extension loads
9699
imageGalleryState: _getImageGalleryState() // image gallery selected state
@@ -226,63 +229,36 @@ define(function main(require, exports, module) {
226229
}
227230
}
228231

229-
// default mode means on first load for pro user we have edit mode
230-
// for free user we have highlight mode
231-
function _getDefaultMode() {
232-
return isProEditUser ? LIVE_EDIT_MODE : LIVE_HIGHLIGHT_MODE;
233-
}
234-
235-
// to set that mode in the preferences
236-
function _initializeMode() {
237-
if (isFreeTrialUser) {
238-
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, LIVE_EDIT_MODE);
239-
return;
240-
}
241-
242-
const savedMode = PreferencesManager.get(PREFERENCE_LIVE_PREVIEW_MODE) || _getDefaultMode();
243-
244-
if (savedMode === LIVE_HIGHLIGHT_MODE && isProEditUser) {
245-
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, LIVE_EDIT_MODE);
246-
} else if (savedMode === LIVE_EDIT_MODE && !isProEditUser) {
247-
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, LIVE_HIGHLIGHT_MODE);
248-
}
249-
}
250-
251-
// this is called everytime there is a change in entitlements
252-
async function _updateProUserStatus() {
253-
if (!KernalModeTrust) {
254-
return;
255-
}
256-
257-
try {
258-
const entitlement = await KernalModeTrust.EntitlementsManager.getLiveEditEntitlement();
259-
260-
isProEditUser = entitlement.activated;
261-
isFreeTrialUser = await KernalModeTrust.EntitlementsManager.isInProTrial();
262-
263-
_initializeMode(); // validates mode based on new entitlement
264-
config.mode = getCurrentMode(); // update config.mode after validation
265-
266-
if (MultiBrowserLiveDev.status >= MultiBrowserLiveDev.STATUS_ACTIVE) {
267-
MultiBrowserLiveDev.updateConfig(JSON.stringify(config));
232+
/**
233+
* Internal api used to update live edit capability status as entitlements changes. calling this will update the UI
234+
* but will not functionally enable live editing capabilities as that are dependent on entitlements framework.
235+
* @param newCapability
236+
* @private
237+
*/
238+
function _liveEditCapabilityChanged(newCapability) {
239+
if(newCapability !== hasLiveEditCapability){
240+
hasLiveEditCapability = newCapability;
241+
if(!hasLiveEditCapability && getCurrentMode() === LIVE_EDIT_MODE){
242+
// downgraded, so we need to disable live edit mode
243+
setMode(LIVE_HIGHLIGHT_MODE);
244+
} else if(hasLiveEditCapability) {
245+
// this means that the user has switched to pro-account and we need to enable live edit mode
246+
// as user may have just logged in with a pro-capable account/upgraded to pro.
247+
setMode(LIVE_EDIT_MODE);
268248
}
269-
} catch (error) {
270-
console.error("Error updating pro user status:", error);
271-
isProEditUser = false;
272-
isFreeTrialUser = false;
273249
}
274250
}
275251

276252
function setMode(mode) {
277-
if (mode === LIVE_EDIT_MODE && !isProEditUser) {
253+
if (mode === LIVE_EDIT_MODE && !hasLiveEditCapability) {
278254
return false;
279255
}
280256
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, mode);
281257
return true;
282258
}
283259

284260
function getCurrentMode() {
285-
return PreferencesManager.get(PREFERENCE_LIVE_PREVIEW_MODE) || _getDefaultMode();
261+
return PreferencesManager.get(PREFERENCE_LIVE_PREVIEW_MODE);
286262
}
287263

288264
function isInPreviewMode() {
@@ -302,15 +278,6 @@ define(function main(require, exports, module) {
302278

303279
_loadStyles();
304280

305-
// init pro user status and listen for changes
306-
if (KernalModeTrust) {
307-
_updateProUserStatus();
308-
KernalModeTrust.EntitlementsManager.on(
309-
KernalModeTrust.EntitlementsManager.EVENT_ENTITLEMENTS_CHANGED,
310-
_updateProUserStatus
311-
);
312-
}
313-
314281
// update styles for UI status
315282
_status = [
316283
{ tooltip: Strings.LIVE_DEV_STATUS_TIP_NOT_CONNECTED, style: "warning" },
@@ -340,24 +307,20 @@ define(function main(require, exports, module) {
340307
});
341308
});
342309

343-
function _updateConfigMode() {
310+
function _previewModeUpdated() {
344311
const currentMode = getCurrentMode();
312+
if (currentMode === LIVE_EDIT_MODE && !hasLiveEditCapability) {
313+
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, LIVE_HIGHLIGHT_MODE);
314+
// we will get another update event for this immediately, so just return.
315+
return;
316+
}
345317
config.mode = currentMode;
346318

347319
if (MultiBrowserLiveDev && MultiBrowserLiveDev.status >= MultiBrowserLiveDev.STATUS_ACTIVE) {
348320
MultiBrowserLiveDev.updateConfig(JSON.stringify(config));
349321
}
350322
}
351323

352-
PreferencesManager.definePreference(PREFERENCE_LIVE_PREVIEW_MODE, "string", _getDefaultMode(), {
353-
description: StringUtils.format(
354-
Strings.LIVE_PREVIEW_MODE_PREFERENCE, LIVE_PREVIEW_MODE, LIVE_HIGHLIGHT_MODE, LIVE_EDIT_MODE),
355-
values: [LIVE_PREVIEW_MODE, LIVE_HIGHLIGHT_MODE, LIVE_EDIT_MODE]
356-
}).on("change", function () {
357-
// when mode changes we update the config mode and notify remoteFunctions so that it can get updated
358-
_updateConfigMode();
359-
});
360-
361324
// this function is responsible to update element highlight config
362325
// called from live preview extension when preference changes
363326
function updateElementHighlightConfig() {
@@ -381,13 +344,17 @@ define(function main(require, exports, module) {
381344

382345
EventDispatcher.makeEventDispatcher(exports);
383346

347+
// private api
348+
exports._liveEditCapabilityChanged = _liveEditCapabilityChanged;
349+
384350
// public events
385351
exports.EVENT_OPEN_PREVIEW_URL = MultiBrowserLiveDev.EVENT_OPEN_PREVIEW_URL;
386352
exports.EVENT_CONNECTION_CLOSE = MultiBrowserLiveDev.EVENT_CONNECTION_CLOSE;
387353
exports.EVENT_LIVE_PREVIEW_CLICKED = MultiBrowserLiveDev.EVENT_LIVE_PREVIEW_CLICKED;
388354
exports.EVENT_LIVE_PREVIEW_RELOAD = MultiBrowserLiveDev.EVENT_LIVE_PREVIEW_RELOAD;
389355

390356
// Export public functions
357+
exports.CONSTANTS = CONSTANTS;
391358
exports.openLivePreview = openLivePreview;
392359
exports.closeLivePreview = closeLivePreview;
393360
exports.isInactive = isInactive;

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*/
3737

3838
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
39-
/*global path, jsPromise*/
39+
/*global path*/
4040
//jshint-ignore:no-start
4141

4242
define(function (require, exports, module) {
@@ -324,7 +324,7 @@ define(function (require, exports, module) {
324324
const currentMode = LiveDevelopment.getCurrentMode();
325325

326326
// when in preview mode, we need to give the play button a selected state
327-
if (currentMode === "preview") {
327+
if (currentMode === LiveDevelopment.CONSTANTS.LIVE_PREVIEW_MODE) {
328328
$previewBtn.addClass('selected');
329329
} else {
330330
$previewBtn.removeClass('selected');
@@ -353,12 +353,16 @@ define(function (require, exports, module) {
353353
$dropdown = new DropdownButton.DropdownButton("", items, function(item, index) {
354354
if (item === Strings.LIVE_PREVIEW_MODE_PREVIEW) {
355355
// using empty spaces to keep content aligned
356-
return currentMode === "preview" ? `✓ ${item}` : `${'\u00A0'.repeat(4)}${item}`;
356+
return currentMode === LiveDevelopment.CONSTANTS.LIVE_PREVIEW_MODE ?
357+
`✓ ${item}` : `${'\u00A0'.repeat(4)}${item}`;
357358
} else if (item === Strings.LIVE_PREVIEW_MODE_HIGHLIGHT) {
358-
return currentMode === "highlight" ? `✓ ${item}` : `${'\u00A0'.repeat(4)}${item}`;
359+
return currentMode === LiveDevelopment.CONSTANTS.LIVE_HIGHLIGHT_MODE ?
360+
`✓ ${item}` : `${'\u00A0'.repeat(4)}${item}`;
359361
} else if (item === Strings.LIVE_PREVIEW_MODE_EDIT) {
360-
const checkmark = currentMode === "edit" ? "✓ " : `${'\u00A0'.repeat(4)}`;
361-
const crownIcon = !isEditFeaturesActive ? ' <span style="color: #FBB03B; border: 1px solid #FBB03B; padding: 2px 4px; border-radius: 10px; font-size: 9px; margin-left: 12px;"><i class="fas fa-crown"></i> Pro</span>' : '';
362+
const checkmark = currentMode === LiveDevelopment.CONSTANTS.LIVE_EDIT_MODE ?
363+
"✓ " : `${'\u00A0'.repeat(4)}`;
364+
const crownIcon = !isEditFeaturesActive ?
365+
' <span style="color: #FBB03B; border: 1px solid #FBB03B; padding: 2px 4px; border-radius: 10px; font-size: 9px; margin-left: 12px;"><i class="fas fa-crown"></i> Pro</span>' : '';
362366
return {
363367
html: `${checkmark}${item}${crownIcon}`,
364368
enabled: true
@@ -400,11 +404,11 @@ define(function (require, exports, module) {
400404
// handle the option selection
401405
$dropdown.on("select", function (e, item, index) {
402406
if (index === 0) {
403-
LiveDevelopment.setMode("preview");
407+
LiveDevelopment.setMode(LiveDevelopment.CONSTANTS.LIVE_PREVIEW_MODE);
404408
} else if (index === 1) {
405-
LiveDevelopment.setMode("highlight");
409+
LiveDevelopment.setMode(LiveDevelopment.CONSTANTS.LIVE_HIGHLIGHT_MODE);
406410
} else if (index === 2) {
407-
if (!LiveDevelopment.setMode("edit")) {
411+
if (!LiveDevelopment.setMode(LiveDevelopment.CONSTANTS.LIVE_EDIT_MODE)) {
408412
ProDialogs.showProUpsellDialog(ProDialogs.UPSELL_TYPE_LIVE_EDIT);
409413
}
410414
} else if (item === Strings.LIVE_PREVIEW_EDIT_HIGHLIGHT_ON) {

src/extensionsIntegrated/phoenix-pro/main.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,35 @@ define(function (require, exports, module) {
99
require("./remote-icons");
1010
require("./remote-styles");
1111

12+
const KernalModeTrust = window.KernalModeTrust;
13+
if(!KernalModeTrust){
14+
throw new Error("KernalModeTrust is not defined. Cannot boot without trust ring");
15+
}
16+
1217
const AppInit = require("utils/AppInit"),
13-
WorkspaceManager = require("view/WorkspaceManager"),
18+
WorkspaceManager = require("view/WorkspaceManager"),
19+
LiveDevelopment = require("LiveDevelopment/main"),
1420
LiveDevMultiBrowser = require("LiveDevelopment/LiveDevMultiBrowser"),
1521
Strings = require("strings");
1622

1723
const remoteUtilsCode = require("text!./browser-context/remote-utils.js");
1824
const LiveDevProtocol = require("LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol");
1925
LiveDevProtocol.addRemoteFunctionScript("remoteUtilsCode", remoteUtilsCode);
2026

27+
// this will later be assigned its correct values once entitlementsManager loads
28+
let isProEditActivated = false;
29+
// called everytime there is a change in entitlements (app start/every 15 mis, explicit check/ anytime really)
30+
// called everytime there is a change in entitlements (app start/every 15 mis, explicit check/ anytime really)
31+
async function _entitlementsChanged() {
32+
try {
33+
const entitlement = await KernalModeTrust.EntitlementsManager.getLiveEditEntitlement();
34+
isProEditActivated = entitlement.activated;
35+
} catch (error) {
36+
console.error("Error updating pro user status:", error);
37+
isProEditActivated = false;
38+
}
39+
LiveDevelopment._liveEditCapabilityChanged(isProEditActivated);
40+
}
2141

2242
/**
2343
* this function handles escape key for live preview to hide boxes if they are visible
@@ -36,6 +56,8 @@ define(function (require, exports, module) {
3656
WorkspaceManager.addEscapeKeyEventHandler("livePreview", _handleLivePreviewEscapeKey);
3757

3858
AppInit.appReady(function () {
39-
59+
_entitlementsChanged();
60+
KernalModeTrust.EntitlementsManager.on(
61+
KernalModeTrust.EntitlementsManager.EVENT_ENTITLEMENTS_CHANGED, _entitlementsChanged);
4062
});
4163
});

src/extensionsIntegrated/phoenix-pro/unittests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
*/
55

66
define(function (require, exports, module) {
7-
// require("./unit-tests/LivePreviewEdit-test"); todo renable once this is working
7+
// require("./unit-tests/LivePreviewEdit-test");
88
});

0 commit comments

Comments
 (0)