Skip to content

Commit 3a1c0f9

Browse files
committed
feat: login integration for live preview edit
1 parent b47a932 commit 3a1c0f9

File tree

2 files changed

+99
-89
lines changed
  • src
    • LiveDevelopment
    • extensionsIntegrated/Phoenix-live-preview

2 files changed

+99
-89
lines changed

src/LiveDevelopment/main.js

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ define(function main(require, exports, module) {
4848
EditorManager = require("editor/EditorManager");
4949

5050

51-
// this is responsible to make the advanced live preview features active or inactive
52-
// @abose (make the first value true when its a paid user, everything rest is handled automatically)
53-
let isProUser = window.KernalModeTrust ? true : false;
54-
// when isFreeTrialUser is true isProUser should also be true
55-
// when its false, isProUser can be true/false doesn't matter
56-
let isFreeTrialUser = true;
51+
const KernalModeTrust = window.KernalModeTrust;
52+
53+
// this will later be assigned its correct values once entitlementsManager loads
54+
let isProUser = false;
55+
let isFreeTrialUser = false;
5756

5857
const EVENT_LIVE_HIGHLIGHT_PREF_CHANGED = "liveHighlightPrefChange";
58+
const PREFERENCE_LIVE_PREVIEW_MODE = "livePreviewMode";
5959

6060
// state manager key to track image gallery selected state, by default we keep this as selected
6161
// if this is true we show the image gallery when an image element is clicked
@@ -316,6 +316,70 @@ define(function main(require, exports, module) {
316316
return false;
317317
}
318318

319+
// default mode means on first load for pro user we have edit mode
320+
// for free user we have highlight mode
321+
function _getDefaultMode() {
322+
return isProUser ? "edit" : "highlight";
323+
}
324+
325+
// to set that mode in the preferences
326+
function _initializeMode() {
327+
if (isFreeTrialUser) {
328+
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, "edit");
329+
return;
330+
}
331+
332+
const savedMode = PreferencesManager.get(PREFERENCE_LIVE_PREVIEW_MODE) || _getDefaultMode();
333+
334+
if (savedMode === "highlight" && isProUser) {
335+
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, "edit");
336+
} else if (savedMode === "edit" && !isProUser) {
337+
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, "highlight");
338+
}
339+
}
340+
341+
// this is called everytime there is a change in entitlements
342+
async function _updateProUserStatus() {
343+
if (!KernalModeTrust) {
344+
return;
345+
}
346+
347+
try {
348+
const entitlement = await KernalModeTrust.EntitlementsManager.getLiveEditEntitlement();
349+
const isPaidSub = await KernalModeTrust.EntitlementsManager.isPaidSubscriber();
350+
351+
isProUser = entitlement.activated || isPaidSub;
352+
isFreeTrialUser = await KernalModeTrust.EntitlementsManager.isInProTrial();
353+
354+
config.isProUser = isProUser;
355+
exports.isProUser = isProUser;
356+
exports.isFreeTrialUser = isFreeTrialUser;
357+
358+
_initializeMode();
359+
360+
if (MultiBrowserLiveDev.status >= MultiBrowserLiveDev.STATUS_ACTIVE) {
361+
MultiBrowserLiveDev.updateConfig(JSON.stringify(config));
362+
MultiBrowserLiveDev.registerHandlers();
363+
}
364+
} catch (error) {
365+
console.error("Error updating pro user status:", error);
366+
isProUser = false;
367+
isFreeTrialUser = false;
368+
}
369+
}
370+
371+
function setMode(mode) {
372+
if (mode === "edit" && !exports.isProUser) {
373+
return false;
374+
}
375+
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, mode);
376+
return true;
377+
}
378+
379+
function getCurrentMode() {
380+
return PreferencesManager.get(PREFERENCE_LIVE_PREVIEW_MODE) || _getDefaultMode();
381+
}
382+
319383
/** Initialize LiveDevelopment */
320384
AppInit.appReady(function () {
321385
params.parse();
@@ -330,6 +394,15 @@ define(function main(require, exports, module) {
330394
_loadStyles();
331395
_updateHighlightCheckmark();
332396

397+
// init pro user status and listen for changes
398+
if (KernalModeTrust) {
399+
_updateProUserStatus();
400+
KernalModeTrust.EntitlementsManager.on(
401+
KernalModeTrust.EntitlementsManager.EVENT_ENTITLEMENTS_CHANGED,
402+
_updateProUserStatus
403+
);
404+
}
405+
333406
// update styles for UI status
334407
_status = [
335408
{ tooltip: Strings.LIVE_DEV_STATUS_TIP_NOT_CONNECTED, style: "warning" },
@@ -385,6 +458,11 @@ define(function main(require, exports, module) {
385458
}
386459
});
387460

461+
PreferencesManager.definePreference(PREFERENCE_LIVE_PREVIEW_MODE, "string", _getDefaultMode(), {
462+
description: StringUtils.format(Strings.LIVE_PREVIEW_MODE_PREFERENCE, "'preview'", "'highlight'", "'edit'"),
463+
values: ["preview", "highlight", "edit"]
464+
});
465+
388466
config.highlight = PreferencesManager.getViewState("livedevHighlight");
389467

390468
function setLivePreviewEditFeaturesActive(enabled) {
@@ -441,4 +519,6 @@ define(function main(require, exports, module) {
441519
exports.getLivePreviewDetails = MultiBrowserLiveDev.getLivePreviewDetails;
442520
exports.hideHighlight = MultiBrowserLiveDev.hideHighlight;
443521
exports.dismissLivePreviewBoxes = MultiBrowserLiveDev.dismissLivePreviewBoxes;
522+
exports.setMode = setMode;
523+
exports.getCurrentMode = getCurrentMode;
444524
});

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

Lines changed: 13 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,9 @@ define(function (require, exports, module) {
8787
const PREVIEW_TRUSTED_PROJECT_KEY = "preview_trusted";
8888
const PREVIEW_PROJECT_README_KEY = "preview_readme";
8989

90-
// live preview mode pref
91-
const PREFERENCE_LIVE_PREVIEW_MODE = "livePreviewMode";
92-
9390
// holds the dropdown instance
9491
let $dropdown = null;
95-
96-
/**
97-
* Get the appropriate default mode based on whether edit features are active
98-
* @returns {string} "highlight" if edit features inactive, "edit" if active
99-
*/
100-
function _getDefaultMode() {
101-
return LiveDevelopment.isProUser ? "edit" : "highlight";
102-
}
103-
104-
// define the live preview mode preference
105-
PreferencesManager.definePreference(PREFERENCE_LIVE_PREVIEW_MODE, "string", _getDefaultMode(), {
106-
description: StringUtils.format(Strings.LIVE_PREVIEW_MODE_PREFERENCE, "'preview'", "'highlight'", "'edit'"),
107-
values: ["preview", "highlight", "edit"]
108-
});
92+
const PREFERENCE_LIVE_PREVIEW_MODE = "livePreviewMode";
10993

11094
// live preview element highlights preference (whether on hover or click)
11195
const PREFERENCE_PROJECT_ELEMENT_HIGHLIGHT = "livePreviewElementHighlights";
@@ -372,43 +356,21 @@ define(function (require, exports, module) {
372356
}
373357
}
374358

375-
/**
376-
* init live preview mode from saved preferences
377-
*/
378359
function _initializeMode() {
379-
// when user is on free trial we just push the edit mode to them every time they open/reload Phoenix
380-
if(LiveDevelopment.isFreeTrialUser) {
381-
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, "edit");
382-
_LPEditMode();
383-
$previewBtn.removeClass('selected');
384-
_updateModeButton("edit");
385-
return;
386-
}
387-
388-
const savedMode = PreferencesManager.get(PREFERENCE_LIVE_PREVIEW_MODE) || _getDefaultMode();
389-
const isEditFeaturesActive = LiveDevelopment.isProUser;
360+
const currentMode = LiveDevelopment.getCurrentMode();
390361

391-
// If user has edit mode saved but edit features are not active, default to highlight
392-
let effectiveMode = savedMode;
393-
if (savedMode === "edit" && !isEditFeaturesActive) {
394-
effectiveMode = "highlight";
395-
// Update the preference to reflect the actual mode being used
396-
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, "highlight");
397-
}
398-
399-
// apply the effective mode
400-
if (effectiveMode === "highlight") {
362+
if (currentMode === "highlight") {
401363
_LPHighlightMode();
402364
$previewBtn.removeClass('selected');
403-
} else if (effectiveMode === "edit" && isEditFeaturesActive) {
365+
} else if (currentMode === "edit") {
404366
_LPEditMode();
405367
$previewBtn.removeClass('selected');
406368
} else {
407369
_LPPreviewMode();
408370
$previewBtn.addClass('selected');
409371
}
410372

411-
_updateModeButton(effectiveMode);
373+
_updateModeButton(currentMode);
412374
}
413375

414376
function _showModeSelectionDropdown(event) {
@@ -425,9 +387,7 @@ define(function (require, exports, module) {
425387
items.push(Strings.LIVE_PREVIEW_EDIT_HIGHLIGHT_ON);
426388
}
427389

428-
const rawMode = PreferencesManager.get(PREFERENCE_LIVE_PREVIEW_MODE) || _getDefaultMode();
429-
// this is to take care of invalid values in the pref file
430-
const currentMode = ["preview", "highlight", "edit"].includes(rawMode) ? rawMode : _getDefaultMode();
390+
const currentMode = LiveDevelopment.getCurrentMode();
431391

432392
$dropdown = new DropdownButton.DropdownButton("", items, function(item, index) {
433393
if (item === Strings.LIVE_PREVIEW_MODE_PREVIEW) {
@@ -472,28 +432,22 @@ define(function (require, exports, module) {
472432

473433
// handle the option selection
474434
$dropdown.on("select", function (e, item, index) {
475-
// here we just set the preference
476-
// as the preferences listener will automatically handle the required changes
477435
if (index === 0) {
478-
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, "preview");
436+
LiveDevelopment.setMode("preview");
479437
} else if (index === 1) {
480-
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, "highlight");
438+
LiveDevelopment.setMode("highlight");
481439
} else if (index === 2) {
482-
if (!isEditFeaturesActive) {
483-
// when the feature is not active we need to show a dialog to the user asking
484-
// them to subscribe to pro
440+
if (!LiveDevelopment.setMode("edit")) {
485441
_showProFeatureDialog();
486-
} else {
487-
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, "edit");
488442
}
489443
} else if (item === Strings.LIVE_PREVIEW_EDIT_HIGHLIGHT_ON) {
490444
// Don't allow edit highlight toggle if edit features are not active
491445
if (!isEditFeaturesActive) {
492446
return;
493447
}
494448
// Toggle between hover and click
495-
const currentMode = PreferencesManager.get(PREFERENCE_PROJECT_ELEMENT_HIGHLIGHT);
496-
const newMode = currentMode !== "click" ? "click" : "hover";
449+
const currMode = PreferencesManager.get(PREFERENCE_PROJECT_ELEMENT_HIGHLIGHT);
450+
const newMode = currMode !== "click" ? "click" : "hover";
497451
PreferencesManager.set(PREFERENCE_PROJECT_ELEMENT_HIGHLIGHT, newMode);
498452
return; // Don't dismiss highlights for this option
499453
}
@@ -1268,32 +1222,8 @@ define(function (require, exports, module) {
12681222
// init live preview mode from saved preferences
12691223
_initializeMode();
12701224
// listen for pref changes
1271-
PreferencesManager.on("change", PREFERENCE_LIVE_PREVIEW_MODE, function () {
1272-
// Get the current preference value directly
1273-
const newMode = PreferencesManager.get(PREFERENCE_LIVE_PREVIEW_MODE);
1274-
const isEditFeaturesActive = LiveDevelopment.isProUser;
1275-
1276-
// If user tries to set edit mode but edit features are not active, default to highlight
1277-
let effectiveMode = newMode;
1278-
if (newMode === "edit" && !isEditFeaturesActive) {
1279-
effectiveMode = "highlight";
1280-
// Update the preference to reflect the actual mode being used
1281-
PreferencesManager.set(PREFERENCE_LIVE_PREVIEW_MODE, "highlight");
1282-
return; // Return to avoid infinite loop
1283-
}
1284-
1285-
if (effectiveMode === "highlight") {
1286-
_LPHighlightMode();
1287-
$previewBtn.removeClass('selected');
1288-
} else if (effectiveMode === "edit" && isEditFeaturesActive) {
1289-
_LPEditMode();
1290-
$previewBtn.removeClass('selected');
1291-
} else {
1292-
_LPPreviewMode();
1293-
$previewBtn.addClass('selected');
1294-
}
1295-
1296-
_updateModeButton(effectiveMode);
1225+
PreferencesManager.on("change", "livePreviewMode", function () {
1226+
_initializeMode();
12971227
});
12981228

12991229
// Handle element highlight preference changes from this extension

0 commit comments

Comments
 (0)