@@ -43,12 +43,16 @@ define(function main(require, exports, module) {
4343 ExtensionUtils = require ( "utils/ExtensionUtils" ) ,
4444 StringUtils = require ( "utils/StringUtils" ) ,
4545 EventDispatcher = require ( "utils/EventDispatcher" ) ,
46- WorkspaceManager = require ( "view/WorkspaceManager" ) ;
46+ WorkspaceManager = require ( "view/WorkspaceManager" ) ,
47+ EditorManager = require ( "editor/EditorManager" ) ;
4748
4849
4950 // this is responsible to make the advanced live preview features active or inactive
50- // @abose (make this variable false when not a paid user, everything rest is handled automatically)
51- let isLPEditFeaturesActive = false ;
51+ // @abose (make the first value true when its a paid user, everything rest is handled automatically)
52+ let isProUser = window . KernalModeTrust ? true : false ;
53+ // when isFreeTrialUser is true isProUser should also be true
54+ // when its false, isProUser can be true/false doesn't matter
55+ let isFreeTrialUser = true ;
5256
5357 const EVENT_LIVE_HIGHLIGHT_PREF_CHANGED = "liveHighlightPrefChange" ;
5458
@@ -64,7 +68,7 @@ define(function main(require, exports, module) {
6468 paddingColor : { r : 147 , g : 196 , b : 125 , a : 0.66 } ,
6569 showInfo : true
6670 } ,
67- isLPEditFeaturesActive : isLPEditFeaturesActive ,
71+ isProUser : isProUser ,
6872 elemHighlights : "hover" , // default value, this will get updated when the extension loads
6973 // this strings are used in RemoteFunctions.js
7074 // we need to pass this through config as remoteFunctions runs in browser context and cannot
@@ -269,6 +273,122 @@ define(function main(require, exports, module) {
269273 return false ;
270274 }
271275
276+ let $livePreviewPanel = null ; // stores the live preview panel, need this as overlay is appended inside this
277+ let $overlayContainer = null ; // the overlay container
278+ let shouldShowSyncErrorOverlay = true ; // once user closes the overlay we don't show them again
279+ let shouldShowConnectingOverlay = true ;
280+ let connectingOverlayTimer = null ; // this is needed as we show the connecting overlay after 3s
281+ let connectingOverlayTimeDuration = 3000 ;
282+
283+ /**
284+ * this function is responsible to check whether to show the overlay or not and how it should be shown
285+ * because if user has closed the overlay manually, we don't show it again
286+ * secondly, for connecting overlay we show that after a 3s timer, but sync error overlay is shown immediately
287+ * @param {String } textMessage - the text that is written inside the overlay
288+ * @param {Number } status - 1 for connect, 4 for sync error but we match it using MultiBrowserLiveDev
289+ */
290+ function _handleOverlay ( textMessage , status ) {
291+ if ( ! $livePreviewPanel ) {
292+ $livePreviewPanel = $ ( "#panel-live-preview" ) ;
293+ }
294+
295+ // remove any existing overlay & timer
296+ _hideOverlay ( ) ;
297+
298+ // to not show the overlays if user has already closed it before
299+ if ( status === MultiBrowserLiveDev . STATUS_CONNECTING && ! shouldShowConnectingOverlay ) { return ; }
300+ if ( status === MultiBrowserLiveDev . STATUS_SYNC_ERROR && ! shouldShowSyncErrorOverlay ) { return ; }
301+
302+ // for connecting status, we delay showing the overlay by 3 seconds
303+ if ( status === MultiBrowserLiveDev . STATUS_CONNECTING ) {
304+ connectingOverlayTimer = setTimeout ( ( ) => {
305+ _createAndShowOverlay ( textMessage , status ) ;
306+ connectingOverlayTimer = null ;
307+ } , connectingOverlayTimeDuration ) ;
308+ return ;
309+ }
310+
311+ // for sync error status, show immediately
312+ _createAndShowOverlay ( textMessage , status ) ;
313+ }
314+
315+ /**
316+ * this function is responsible to create & show the overlay.
317+ * so overlay is shown when the live preview is connecting or live preview stopped because of some syntax error
318+ * @param {String } textMessage - the text that is written inside the overlay
319+ * @param {Number } status - 1 for connect, 4 for sync error but we match it using MultiBrowserLiveDev
320+ */
321+ function _createAndShowOverlay ( textMessage , status ) {
322+ if ( ! $livePreviewPanel ) {
323+ $livePreviewPanel = $ ( "#panel-live-preview" ) ;
324+ }
325+
326+ // create the overlay element
327+ // styled inside the 'src/extensionsIntegrated/Phoenix-live-preview/live-preview.css'
328+ $overlayContainer = $ ( "<div>" ) . addClass ( "live-preview-status-overlay" ) ; // the wrapper for overlay element
329+ const $message = $ ( "<div>" ) . addClass ( "live-preview-overlay-message" ) . text ( textMessage ) ;
330+
331+ // the close button at the right end of the overlay
332+ const $close = $ ( "<div>" ) . addClass ( "live-preview-overlay-close" )
333+ . attr ( "title" , Strings . LIVE_PREVIEW_HIDE_OVERLAY )
334+ . on ( 'click' , ( ) => {
335+ if ( status === MultiBrowserLiveDev . STATUS_CONNECTING ) {
336+ shouldShowConnectingOverlay = false ;
337+ } else if ( status === MultiBrowserLiveDev . STATUS_SYNC_ERROR ) {
338+ shouldShowSyncErrorOverlay = false ;
339+ }
340+ _hideOverlay ( ) ;
341+ } ) ;
342+ const $closeIcon = $ ( "<i>" ) . addClass ( "fas fa-times" ) ;
343+
344+ $close . append ( $closeIcon ) ;
345+ $overlayContainer . append ( $message ) ;
346+ $overlayContainer . append ( $close ) ;
347+ $livePreviewPanel . append ( $overlayContainer ) ;
348+ }
349+
350+ /**
351+ * responsible to hide the overlay
352+ */
353+ function _hideOverlay ( ) {
354+ _clearConnectingOverlayTimer ( ) ;
355+ if ( $overlayContainer ) {
356+ $overlayContainer . remove ( ) ;
357+ $overlayContainer = null ;
358+ }
359+ }
360+
361+ /**
362+ * This is a helper function that just checks that if connectingOverlayTimer exists, we clear it
363+ */
364+ function _clearConnectingOverlayTimer ( ) {
365+ if ( connectingOverlayTimer ) {
366+ clearTimeout ( connectingOverlayTimer ) ;
367+ connectingOverlayTimer = null ;
368+ }
369+ }
370+
371+ /**
372+ * this function adds/remove the full-width class from the overlay container
373+ * styled inside 'src/extensionsIntegrated/Phoenix-live-preview/live-preview.css'
374+ *
375+ * we need this because
376+ * normally when live preview has a good width (more than 305px) then a 3px divider is shown at the left end
377+ * so in that case we give the overlay a width of (100% - 3px),
378+ * but when the live preview width is reduced
379+ * then that divider line gets cut off, so in that case we make the width 100% for this overlay
380+ *
381+ * without this handling, a white gap appears on the left side, which is distracting
382+ */
383+ function _setOverlayWidth ( ) {
384+ if ( ! $overlayContainer || ! $livePreviewPanel . length ) { return ; }
385+ if ( $livePreviewPanel . width ( ) <= 305 ) {
386+ $overlayContainer . addClass ( "full-width" ) ;
387+ } else {
388+ $overlayContainer . removeClass ( "full-width" ) ;
389+ }
390+ }
391+
272392 /** Initialize LiveDevelopment */
273393 AppInit . appReady ( function ( ) {
274394 params . parse ( ) ;
@@ -323,6 +443,19 @@ define(function main(require, exports, module) {
323443 exports . trigger ( exports . EVENT_LIVE_PREVIEW_RELOAD , clientDetails ) ;
324444 } ) ;
325445
446+ MultiBrowserLiveDev . on ( MultiBrowserLiveDev . EVENT_STATUS_CHANGE , function ( event , status ) {
447+ if ( status === MultiBrowserLiveDev . STATUS_CONNECTING ) {
448+ _handleOverlay ( Strings . LIVE_DEV_STATUS_TIP_PROGRESS1 , status ) ;
449+ } else if ( status === MultiBrowserLiveDev . STATUS_SYNC_ERROR ) {
450+ _handleOverlay ( Strings . LIVE_DEV_STATUS_TIP_SYNC_ERROR , status ) ;
451+ } else {
452+ _hideOverlay ( ) ;
453+ }
454+ } ) ;
455+ // to understand why we need this, pls read the _setOverlayWidth function
456+ new ResizeObserver ( _setOverlayWidth ) . observe ( $ ( "#main-plugin-panel" ) [ 0 ] ) ;
457+ EditorManager . on ( "activeEditorChange" , _hideOverlay ) ;
458+
326459 // allow live preview to handle escape key event
327460 // Escape is mainly to hide boxes if they are visible
328461 WorkspaceManager . addEscapeKeyEventHandler ( "livePreview" , _handleLivePreviewEscapeKey ) ;
@@ -341,8 +474,9 @@ define(function main(require, exports, module) {
341474 config . highlight = PreferencesManager . getViewState ( "livedevHighlight" ) ;
342475
343476 function setLivePreviewEditFeaturesActive ( enabled ) {
344- isLPEditFeaturesActive = enabled ;
345- config . isLPEditFeaturesActive = enabled ;
477+ // TODO: @abose here add kernal mode trust check
478+ isProUser = enabled ;
479+ config . isProUser = enabled ;
346480 if ( MultiBrowserLiveDev && MultiBrowserLiveDev . status >= MultiBrowserLiveDev . STATUS_ACTIVE ) {
347481 MultiBrowserLiveDev . updateConfig ( JSON . stringify ( config ) ) ;
348482 MultiBrowserLiveDev . registerHandlers ( ) ;
@@ -368,7 +502,8 @@ define(function main(require, exports, module) {
368502
369503 EventDispatcher . makeEventDispatcher ( exports ) ;
370504
371- exports . isLPEditFeaturesActive = isLPEditFeaturesActive ;
505+ exports . isProUser = isProUser ;
506+ exports . isFreeTrialUser = isFreeTrialUser ;
372507
373508 // public events
374509 exports . EVENT_OPEN_PREVIEW_URL = MultiBrowserLiveDev . EVENT_OPEN_PREVIEW_URL ;
0 commit comments