forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCentralControlBar.js
More file actions
1 lines (1 loc) · 12.1 KB
/
Copy pathCentralControlBar.js
File metadata and controls
1 lines (1 loc) · 12.1 KB
1
define(function(require,exports,module){const AppInit=require("utils/AppInit"),CommandManager=require("command/CommandManager"),Commands=require("command/Commands"),DocumentManager=require("document/DocumentManager"),LanguageManager=require("language/LanguageManager"),MainViewManager=require("view/MainViewManager"),Metrics=require("utils/Metrics"),Strings=require("strings"),WorkspaceManager=require("view/WorkspaceManager"),SidebarView=require("project/SidebarView"),BAR_WIDTH=30;let $bar,$sidebar,$content,$fileLabel,$fileName,editorCollapsed=!1,savedToolbarWidth=null,livePreviewWasOpen=!1,savedSidebarMaxSize=null,applyingCollapsedLayout=!1;function _getRenderedSidebarWidth(){return $sidebar&&SidebarView.isVisible()&&$sidebar[0].offsetWidth||0}function _syncLeftPositions(){if(!$sidebar||!$bar||!$content)return;const sidebarWidth=_getRenderedSidebarWidth();if($bar.css("left",sidebarWidth+"px"),editorCollapsed){const mainToolbar=document.getElementById("main-toolbar"),fullToolbarWidth=Math.max(0,window.innerWidth-sidebarWidth-BAR_WIDTH);mainToolbar&&(mainToolbar.style.setProperty("left",sidebarWidth+BAR_WIDTH+"px","important"),mainToolbar.style.setProperty("right","auto","important"),mainToolbar.style.setProperty("width",fullToolbarWidth+"px","important"))}else $content.css("left",sidebarWidth+BAR_WIDTH+"px")}function _updateFileLabel(){if(!$fileLabel)return;const file=MainViewManager.getCurrentlyViewedFile(MainViewManager.ACTIVE_PANE);if(!file)return $fileLabel.removeClass("is-dirty"),$fileName.text(""),void $fileLabel.attr("title","");const name=file.name||"",fullPath=file.fullPath||"",displayPath=fullPath&&Phoenix&&Phoenix.app&&Phoenix.app.getDisplayPath?Phoenix.app.getDisplayPath(fullPath):fullPath||name;$fileName.text(name),$fileLabel.attr("title",displayPath);const doc=DocumentManager.getOpenDocumentForPath(fullPath);$fileLabel.toggleClass("is-dirty",!(!doc||!doc.isDirty))}function _executeCmd(id){CommandManager.execute(id)}function _isLivePreviewOpen(){const panel=WorkspaceManager.getPanelForID&&WorkspaceManager.getPanelForID("live-preview-panel");return!(!panel||!panel.isVisible())}function _applyCollapsedLayout(){if(!applyingCollapsedLayout){applyingCollapsedLayout=!0;try{const mainToolbar=document.getElementById("main-toolbar"),sidebarWidth=_getRenderedSidebarWidth(),fullToolbarWidth=Math.max(0,window.innerWidth-sidebarWidth-BAR_WIDTH);$content.css({width:0,"min-width":0,right:"auto",visibility:"hidden","pointer-events":"none"}),mainToolbar&&(mainToolbar.style.setProperty("left",sidebarWidth+BAR_WIDTH+"px","important"),mainToolbar.style.setProperty("right","auto","important"),mainToolbar.style.setProperty("width",fullToolbarWidth+"px","important")),null===savedSidebarMaxSize&&(savedSidebarMaxSize=$sidebar.data("maxsize")),$sidebar.data("maxsize","1000%"),WorkspaceManager.recomputeLayout&&WorkspaceManager.recomputeLayout(!0)}finally{applyingCollapsedLayout=!1}}}function _restoreExpandedLayout(skipToolbarRestore){const $mainToolbar=$("#main-toolbar"),mainToolbar=$mainToolbar[0];mainToolbar&&(mainToolbar.style.removeProperty("left"),mainToolbar.style.removeProperty("right"),mainToolbar.style.removeProperty("width")),$mainToolbar.css({left:"",right:"",width:""}),$content.css({width:"","min-width":"",right:"",visibility:"","pointer-events":""});const preCollapseToolbarWidth=savedToolbarWidth;if(savedToolbarWidth=null,livePreviewWasOpen=!1,null!==savedSidebarMaxSize&&($sidebar.data("maxsize",savedSidebarMaxSize),savedSidebarMaxSize=null),skipToolbarRestore)return _syncLeftPositions(),void(WorkspaceManager.recomputeLayout&&WorkspaceManager.recomputeLayout(!0));const defaultWidth=Math.floor(window.innerWidth/2.5);let targetWidth=preCollapseToolbarWidth&&preCollapseToolbarWidth>50?preCollapseToolbarWidth:defaultWidth;const MIN_EDITOR_WIDTH=200,livePanel=WorkspaceManager.getPanelForID&&WorkspaceManager.getPanelForID("live-preview-panel"),panelIconsWidth=$("#plugin-icons-bar").outerWidth()||30,minLPToolbarWidth=(livePanel&&livePanel.minWidth||0)+panelIconsWidth,sidebarWidth=_getRenderedSidebarWidth(),availableForLP=window.innerWidth-sidebarWidth-BAR_WIDTH-200;if(targetWidth>availableForLP&&(targetWidth=Math.max(minLPToolbarWidth,availableForLP)),targetWidth<minLPToolbarWidth&&(targetWidth=minLPToolbarWidth),sidebarWidth+BAR_WIDTH+targetWidth+200>window.innerWidth){const trimmedSidebar=Math.max(30,window.innerWidth-BAR_WIDTH-targetWidth-200);$sidebar.width(trimmedSidebar);const resync=$sidebar.data("resyncSizer");"function"==typeof resync&&resync()}$mainToolbar.width(targetWidth),$content.css("right",targetWidth+"px"),_syncLeftPositions(),WorkspaceManager.recomputeLayout&&WorkspaceManager.recomputeLayout(!0)}const DESIGN_TIME_BUCKETS_MIN=[1,5,10,15,20,30,45,60],MS_PER_MIN=6e4,DESIGN_TIME_TICK_MS=5*MS_PER_MIN;function _makeDesignTracker(metricName){return{timeFrom:null,aggregateMs:0,lastEmittedIdx:-1,metricName:metricName}}function _emitCrossedBuckets(tracker){const minutes=Math.floor(tracker.aggregateMs/MS_PER_MIN);let checkIndex=tracker.lastEmittedIdx+1;for(;checkIndex<DESIGN_TIME_BUCKETS_MIN.length&&minutes>=DESIGN_TIME_BUCKETS_MIN[checkIndex];)tracker.lastEmittedIdx=checkIndex,Metrics.countEvent(Metrics.EVENT_TYPE.UI,tracker.metricName,DESIGN_TIME_BUCKETS_MIN[checkIndex]+"M"),checkIndex++}function _rollTracker(tracker){if(null==tracker.timeFrom)return;const now=Date.now();tracker.aggregateMs+=now-tracker.timeFrom,tracker.timeFrom=now,_emitCrossedBuckets(tracker)}function _flushTracker(tracker){null!=tracker.timeFrom&&(tracker.aggregateMs+=Date.now()-tracker.timeFrom,tracker.timeFrom=null,_emitCrossedBuckets(tracker))}const _designTracker=_makeDesignTracker("designTime"),_mdDesignTracker=_makeDesignTracker("designTimeMD");function _activeFileIsMarkdown(){const file=MainViewManager.getCurrentlyViewedFile(MainViewManager.ACTIVE_PANE);if(!file||!file.fullPath)return!1;const lang=LanguageManager.getLanguageForPath(file.fullPath);if(!lang)return!1;const id=lang.getId();return"markdown"===id||"gfm"===id}function _syncMdTrackerToActiveFile(){const isMd=_activeFileIsMarkdown(),wasTracking=null!=_mdDesignTracker.timeFrom;isMd&&!wasTracking?_mdDesignTracker.timeFrom=Date.now():!isMd&&wasTracking&&_flushTracker(_mdDesignTracker)}function _setEditorCollapsed(collapsed,opts){const wantCollapsed=!!collapsed;if(wantCollapsed===editorCollapsed)return;const skipToolbarRestore=!(!opts||!opts.skipToolbarRestore);if(editorCollapsed&&!wantCollapsed&&$sidebar&&$sidebar[0]){const rendered=_getRenderedSidebarWidth();if(rendered>0){$sidebar[0].style.width=rendered+"px";const resync=$sidebar.data("resyncSizer");"function"==typeof resync&&resync()}}(editorCollapsed=wantCollapsed)?(_designTracker.timeFrom=Date.now(),_activeFileIsMarkdown()&&(_mdDesignTracker.timeFrom=Date.now())):(_flushTracker(_designTracker),_flushTracker(_mdDesignTracker)),$("body").toggleClass("ccb-editor-collapsed",editorCollapsed);const $collapseBtn=$("#ccbCollapseEditorBtn");if($collapseBtn.toggleClass("is-active",editorCollapsed).attr("title",editorCollapsed?Strings.CCB_SWITCH_TO_CODE_EDITOR:Strings.CCB_SWITCH_TO_DESIGN_MODE),_toggleDesignModeCommand&&_toggleDesignModeCommand.setChecked(editorCollapsed),WorkspaceManager.setDesignMode&&WorkspaceManager.setDesignMode(editorCollapsed),editorCollapsed){if(livePreviewWasOpen=_isLivePreviewOpen(),savedToolbarWidth=livePreviewWasOpen&&$("#main-toolbar").outerWidth()||null,!livePreviewWasOpen)return void CommandManager.execute(Commands.FILE_LIVE_FILE_PREVIEW).always(function(){editorCollapsed&&_applyCollapsedLayout()});_applyCollapsedLayout()}else{const keepLivePreviewOpen=!(!opts||!opts.keepLivePreviewOpen),shouldCloseLivePreview=!skipToolbarRestore&&!keepLivePreviewOpen&&!livePreviewWasOpen&&_isLivePreviewOpen();_restoreExpandedLayout(!!shouldCloseLivePreview||skipToolbarRestore),shouldCloseLivePreview&&CommandManager.execute(Commands.FILE_LIVE_FILE_PREVIEW)}}function _updateSidebarToggleIcon(){const $btn=$("#ccbSidebarToggleBtn");if(!$btn.length)return;const isVisible=SidebarView.isVisible();$btn.find("i").attr("class",isVisible?"fa-solid fa-angles-left":"fa-solid fa-angles-right")}function _ccbClickMetric(label){Metrics.countEvent(Metrics.EVENT_TYPE.UI,"ccb",label)}function _wireButtons(){$("#ccbUndoBtn").on("click",function(e){e.preventDefault(),_ccbClickMetric("undo"),_executeCmd(Commands.EDIT_UNDO)}),$("#ccbRedoBtn").on("click",function(e){e.preventDefault(),_ccbClickMetric("redo"),_executeCmd(Commands.EDIT_REDO)}),$("#ccbSaveBtn").on("click",function(e){e.preventDefault(),_ccbClickMetric("save"),_executeCmd(Commands.FILE_SAVE)}),$("#ccbCollapseEditorBtn").on("click",function(e){e.preventDefault(),_ccbClickMetric(editorCollapsed?"designOff":"designOn"),CommandManager.execute(Commands.VIEW_TOGGLE_DESIGN_MODE)}),$("#ccbSidebarToggleBtn").on("click",function(e){e.preventDefault(),_ccbClickMetric("sidebar"),_executeCmd(Commands.VIEW_HIDE_SIDEBAR)}),$("#ccbFileLabel").on("click",function(e){e.preventDefault(),_ccbClickMetric("file"),_executeCmd(Commands.NAVIGATE_SHOW_IN_FILE_TREE)})}setInterval(function(){_rollTracker(_designTracker),_rollTracker(_mdDesignTracker)},DESIGN_TIME_TICK_MS);const _toggleDesignModeCommand=CommandManager.register(Strings.CMD_TOGGLE_DESIGN_MODE,Commands.VIEW_TOGGLE_DESIGN_MODE,function(){_setEditorCollapsed(!editorCollapsed)},{supportsDesignMode:!0});AppInit.htmlReady(function(){$bar=$("#centralControlBar"),$sidebar=$("#sidebar"),$content=$(".content"),$fileLabel=$("#ccbFileLabel"),$fileName=$fileLabel.find(".ccb-file-name"),_wireButtons(),$("#ccbCollapseEditorBtn").attr("title",Strings.CCB_SWITCH_TO_DESIGN_MODE),$("#ccbSidebarToggleBtn").attr("title",Strings.CMD_TOGGLE_SIDEBAR),$("#ccbUndoBtn").attr("title",Strings.CMD_UNDO),$("#ccbRedoBtn").attr("title",Strings.CMD_REDO),$("#ccbSaveBtn").attr("title",Strings.CMD_FILE_SAVE);const goLiveBtn=document.getElementById("toolbar-go-live");function _forwardResizeToMainToolbar(type){if(!editorCollapsed)return;const mtWidth=$("#main-toolbar").outerWidth()||0;$("#main-toolbar").trigger(type,[mtWidth])}goLiveBtn&&goLiveBtn.addEventListener("click",function(e){editorCollapsed&&(e.stopImmediatePropagation(),e.preventDefault(),_setEditorCollapsed(!1,{keepLivePreviewOpen:!0}))},!0),_syncLeftPositions(),$sidebar.on("panelResizeStart.ccb",function(){_forwardResizeToMainToolbar("panelResizeStart")}),$sidebar.on("panelResizeUpdate.ccb",function(){_syncLeftPositions(),_forwardResizeToMainToolbar("panelResizeUpdate")}),$sidebar.on("panelResizeEnd.ccb panelCollapsed.ccb panelExpanded.ccb",function(e){_syncLeftPositions(),editorCollapsed&&_applyCollapsedLayout(),_updateSidebarToggleIcon(),"panelResizeEnd"===e.type&&_forwardResizeToMainToolbar("panelResizeEnd")}),$(window).on("resize.ccb",function(){_syncLeftPositions(),editorCollapsed&&_applyCollapsedLayout()}),WorkspaceManager.on(WorkspaceManager.EVENT_WORKSPACE_PANEL_SHOWN+".ccb",function(){editorCollapsed&&_applyCollapsedLayout()}),WorkspaceManager.on(WorkspaceManager.EVENT_WORKSPACE_PANEL_HIDDEN+".ccb",function(e,panelID){editorCollapsed&&"live-preview-panel"===panelID?_setEditorCollapsed(!1,{skipToolbarRestore:!0}):editorCollapsed&&_applyCollapsedLayout()}),WorkspaceManager.on(WorkspaceManager.EVENT_WORKSPACE_UPDATE_LAYOUT+".ccb",function(){editorCollapsed&&_applyCollapsedLayout()});const _origSetPluginPanelWidth=WorkspaceManager.setPluginPanelWidth;"function"==typeof _origSetPluginPanelWidth&&(WorkspaceManager.setPluginPanelWidth=function(width){if(!editorCollapsed)return _origSetPluginPanelWidth.apply(this,arguments);const iconsBarWidth=$("#plugin-icons-bar").outerWidth()||30,requestedToolbar=width+iconsBarWidth;let newSidebar=window.innerWidth-requestedToolbar-BAR_WIDTH;if(newSidebar<0&&(newSidebar=0),$sidebar&&$sidebar[0]){$sidebar[0].style.width=newSidebar+"px";const resync=$sidebar.data("resyncSizer");"function"==typeof resync&&resync()}_syncLeftPositions(),WorkspaceManager.recomputeLayout&&WorkspaceManager.recomputeLayout(!0)}),_updateSidebarToggleIcon(),MainViewManager.on("currentFileChange.ccb",function(){_updateFileLabel(),editorCollapsed&&_syncMdTrackerToActiveFile()}),DocumentManager.on("dirtyFlagChange.ccb",_updateFileLabel),DocumentManager.on("pathDeleted.ccb fileNameChange.ccb",_updateFileLabel),_updateFileLabel()}),exports.isEditorCollapsed=function(){return editorCollapsed},exports.setEditorCollapsed=_setEditorCollapsed});