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 ;
0 commit comments