forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
1 lines (1 loc) · 15.4 KB
/
Copy pathmain.js
File metadata and controls
1 lines (1 loc) · 15.4 KB
1
define(function(require,exports,module){const _=require("thirdparty/lodash"),AppInit=require("utils/AppInit"),MainViewManager=require("view/MainViewManager"),FileSystem=require("filesystem/FileSystem"),PreferencesManager=require("preferences/PreferencesManager"),FileUtils=require("file/FileUtils"),CommandManager=require("command/CommandManager"),Commands=require("command/Commands"),KeyBindingManager=require("command/KeyBindingManager"),DocumentManager=require("document/DocumentManager"),EditorManager=require("editor/EditorManager"),WorkspaceManager=require("view/WorkspaceManager"),Menus=require("command/Menus"),Strings=require("strings"),Global=require("./global"),Helper=require("./helper"),Preference=require("./preference"),MoreOptions=require("./more-options"),Overflow=require("./overflow"),DragDrop=require("./drag-drop"),TabBarHTML=require("text!./html/tabbar-pane.html"),TabBarHTML2=require("text!./html/tabbar-second-pane.html");let $tabBar=null,$tabBar2=null;function isTabBarActive(){return Preference.tabBarEnabled&&0!==Preference.tabBarNumberOfTabs}function getAllFilesFromWorkingSet(){if(!isTabBarActive())return;Global.firstPaneWorkingSet=[],Global.secondPaneWorkingSet=[];const paneList=MainViewManager.getPaneIdList();if(paneList&&paneList.length>0){const currFirstPaneWorkingSet=MainViewManager.getWorkingSet(paneList[0]);for(let i=0;i<currFirstPaneWorkingSet.length;i++)Global.firstPaneWorkingSet.push(Helper._getRequiredDataFromEntry(currFirstPaneWorkingSet[i]));if(Helper._handleDuplicateFileNames(Global.firstPaneWorkingSet),paneList.length>1){const currSecondPaneWorkingSet=MainViewManager.getWorkingSet(paneList[1]);for(let i=0;i<currSecondPaneWorkingSet.length;i++)Global.secondPaneWorkingSet.push(Helper._getRequiredDataFromEntry(currSecondPaneWorkingSet[i]));Helper._handleDuplicateFileNames(Global.secondPaneWorkingSet)}Global.firstPaneWorkingSet.forEach(function(entry){const doc=DocumentManager.getOpenDocumentForPath(entry.path);doc&&(entry.isDirty=doc.isDirty)}),Global.secondPaneWorkingSet.forEach(function(entry){const doc=DocumentManager.getOpenDocumentForPath(entry.path);doc&&(entry.isDirty=doc.isDirty)})}}function createTab(entry,paneId){if(!$tabBar||!paneId)return;const activeFileInPane=MainViewManager.getCurrentlyViewedFile(paneId),activePathInPane=activeFileInPane?activeFileInPane.fullPath:null,isActive=entry.path===activePathInPane,currentActivePane=MainViewManager.getActivePaneId(),isPaneActive=paneId===currentActivePane,isDirty=Helper._isFileModified(FileSystem.getFileForPath(entry.path)),isPlaceholder=!0===entry.isPlaceholder;let gitStatus="",gitStatusClass="";if(window.phoenixGitEvents&&window.phoenixGitEvents.TabBarIntegration){const TabBarIntegration=window.phoenixGitEvents.TabBarIntegration;TabBarIntegration.isIgnored(entry.path)?(gitStatus="Ignored",gitStatusClass="git-ignored"):TabBarIntegration.isUntracked(entry.path)?(gitStatus="Untracked",gitStatusClass="git-new"):TabBarIntegration.isModified(entry.path)&&(gitStatus="Modified",gitStatusClass="git-modified")}const $tab=$(`<div class="tab \n ${isActive?"active":""}\n ${isDirty?"dirty":""}\n ${isPlaceholder?"placeholder":""}\n ${gitStatusClass}"\n data-path="${entry.path}" \n title="${Phoenix.app.getDisplayPath(entry.path)}${gitStatus?" ("+gitStatus+")":""}">\n <div class="tab-icon"></div>\n <div class="tab-name"></div>\n <div class="tab-close"><i class="fa-solid fa-times"></i></div>\n </div>`),$icon=Helper._getFileIcon(entry);$tab.find(".tab-icon").append($icon);const $tabName=$tab.find(".tab-name");if(entry.displayName&&entry.displayName!==entry.name){const parts=entry.displayName.split("/"),dirName=parts[0],fileName=parts[1];$tabName.html(`<span class="tab-dirname">${dirName}/</span>${fileName}`)}else $tabName.text(entry.name);return isActive&&!isPaneActive&&$tab.addClass("active-in-inactive-pane"),isPlaceholder&&isActive&&!isPaneActive&&($tab.removeClass("active"),$tab.addClass("active-in-inactive-pane")),$tab}function createTabBar(){if(!isTabBarActive())return void cleanupTabBar();cleanupTabBar();const $paneHeader=$(".pane-header");1===$paneHeader.length?($tabBar=$(TabBarHTML),$(".pane-header").after($tabBar),$("#overflow-button").attr("title",Strings.TABBAR_SHOW_HIDDEN_TABS),WorkspaceManager.recomputeLayout(!0),updateTabs()):2===$paneHeader.length&&($tabBar=$(TabBarHTML),$tabBar2=$(TabBarHTML2),$paneHeader.eq(0).after($tabBar),$paneHeader.eq(1).after($tabBar2),$("#overflow-button").attr("title",Strings.TABBAR_SHOW_HIDDEN_TABS),$("#overflow-button-2").attr("title",Strings.TABBAR_SHOW_HIDDEN_TABS),WorkspaceManager.recomputeLayout(!0),updateTabs())}function updateTabs(){if(!isTabBarActive())return;if(getAllFilesFromWorkingSet(),0===Preference.tabBarNumberOfTabs)return void cleanupTabBar();const activePane=MainViewManager.getActivePaneId(),firstPaneFile=MainViewManager.getCurrentlyViewedFile("first-pane"),secondPaneFile=MainViewManager.getCurrentlyViewedFile("second-pane");let firstPanePlaceholder=null,secondPanePlaceholder=null;if(firstPaneFile&&!Global.firstPaneWorkingSet.some(entry=>entry.path===firstPaneFile.fullPath)&&(firstPanePlaceholder={path:firstPaneFile.fullPath,name:firstPaneFile.name,isPlaceholder:!0,displayName:firstPaneFile.name}),secondPaneFile&&!Global.secondPaneWorkingSet.some(entry=>entry.path===secondPaneFile.fullPath)&&(secondPanePlaceholder={path:secondPaneFile.fullPath,name:secondPaneFile.name,isPlaceholder:!0,displayName:secondPaneFile.name}),firstPanePlaceholder){const hasDuplicate=Global.firstPaneWorkingSet.some(entry=>entry.name===firstPanePlaceholder.name);if(hasDuplicate){const path=firstPanePlaceholder.path,parentDir=FileUtils.getDirectoryPath(path),dirParts=parentDir.split("/"),parentDirName=dirParts[dirParts.length-2]||"";firstPanePlaceholder.displayName=parentDirName+"/"+firstPanePlaceholder.name}}if(secondPanePlaceholder){const hasDuplicate=Global.secondPaneWorkingSet.some(entry=>entry.name===secondPanePlaceholder.name);if(hasDuplicate){const path=secondPanePlaceholder.path,parentDir=FileUtils.getDirectoryPath(path),dirParts=parentDir.split("/"),parentDirName=dirParts[dirParts.length-2]||"";secondPanePlaceholder.displayName=parentDirName+"/"+secondPanePlaceholder.name}}!(Global.firstPaneWorkingSet.length>0||firstPanePlaceholder)||$("#phoenix-tab-bar").length&&!$("#phoenix-tab-bar").is(":hidden")||createTabBar(),!(Global.secondPaneWorkingSet.length>0||secondPanePlaceholder)||$("#phoenix-tab-bar-2").length&&!$("#phoenix-tab-bar-2").is(":hidden")||createTabBar();const $firstTabBar=$("#phoenix-tab-bar");if($firstTabBar.length&&($firstTabBar.empty(),Global.firstPaneWorkingSet.length>0||firstPanePlaceholder)){let tabsCountP1=Math.min(Global.firstPaneWorkingSet.length,Preference.tabBarNumberOfTabs);Preference.tabBarNumberOfTabs<0&&(tabsCountP1=Global.firstPaneWorkingSet.length);let displayedEntries=[];if(firstPaneFile&&Preference.tabBarNumberOfTabs>0){const activeFileIndex=Global.firstPaneWorkingSet.findIndex(entry=>entry.path===firstPaneFile.fullPath);displayedEntries=activeFileIndex>=0&&activeFileIndex>=Preference.tabBarNumberOfTabs?[...Global.firstPaneWorkingSet.slice(0,Preference.tabBarNumberOfTabs-1),Global.firstPaneWorkingSet[activeFileIndex]]:Global.firstPaneWorkingSet.slice(0,tabsCountP1)}else displayedEntries=Global.firstPaneWorkingSet.slice(0,tabsCountP1);displayedEntries.forEach(function(entry){$firstTabBar.append(createTab(entry,"first-pane"))}),firstPanePlaceholder&&$firstTabBar.append(createTab(firstPanePlaceholder,"first-pane"))}const $secondTabBar=$("#phoenix-tab-bar-2");if($secondTabBar.length&&($secondTabBar.empty(),Global.secondPaneWorkingSet.length>0||secondPanePlaceholder)){let tabsCountP2=Math.min(Global.secondPaneWorkingSet.length,Preference.tabBarNumberOfTabs);Preference.tabBarNumberOfTabs<0&&(tabsCountP2=Global.secondPaneWorkingSet.length);let displayedEntries2=[];if(secondPaneFile&&Preference.tabBarNumberOfTabs>0){const activeFileIndex=Global.secondPaneWorkingSet.findIndex(entry=>entry.path===secondPaneFile.fullPath);displayedEntries2=activeFileIndex>=0&&activeFileIndex>=Preference.tabBarNumberOfTabs?[...Global.secondPaneWorkingSet.slice(0,Preference.tabBarNumberOfTabs-1),Global.secondPaneWorkingSet[activeFileIndex]]:Global.secondPaneWorkingSet.slice(0,tabsCountP2)}else displayedEntries2=Global.secondPaneWorkingSet.slice(0,tabsCountP2);displayedEntries2.forEach(function(entry){$secondTabBar.append(createTab(entry,"second-pane"))}),secondPanePlaceholder&&$secondTabBar.append(createTab(secondPanePlaceholder,"second-pane"))}0===Global.firstPaneWorkingSet.length&&!firstPanePlaceholder&&$("#phoenix-tab-bar")&&Helper._hideTabBar($("#phoenix-tab-bar"),$("#overflow-button")),0===Global.secondPaneWorkingSet.length&&!secondPanePlaceholder&&$("#phoenix-tab-bar-2")&&Helper._hideTabBar($("#phoenix-tab-bar-2"),$("#overflow-button-2")),$firstTabBar.length&&(Overflow.toggleOverflowVisibility("first-pane"),"first-pane"===activePane&&setTimeout(function(){Overflow.scrollToActiveTab($firstTabBar)},0)),$secondTabBar.length&&(Overflow.toggleOverflowVisibility("second-pane"),"second-pane"===activePane&&setTimeout(function(){Overflow.scrollToActiveTab($secondTabBar)},0)),DragDrop.init($("#phoenix-tab-bar"),$("#phoenix-tab-bar-2"))}function cleanupTabBar(){$tabBar&&($tabBar.remove(),$tabBar=null),$tabBar2&&($tabBar2.remove(),$tabBar2=null),$(".tab-bar-container").remove(),WorkspaceManager.recomputeLayout(!0)}function handleTabClick(){$(document).on("click",".phoenix-tab-bar .tab",function(event){const filePath=$(this).attr("data-path");if(!filePath)return;const isSecondPane=$(this).closest("#phoenix-tab-bar-2").length>0,paneId=isSecondPane?"second-pane":"first-pane",fileObj=FileSystem.getFileForPath(filePath);($(event.target).hasClass("fa-times")||$(event.target).closest(".tab-close").length)&&(event.preventDefault(),event.stopPropagation(),CommandManager.execute(Commands.FILE_CLOSE,{file:fileObj,paneId:paneId}))}),$(document).on("mouseenter",".phoenix-tab-bar .tab-close",function(){const closeBindings=KeyBindingManager.getKeyBindings(Commands.FILE_CLOSE),closeShortcut=closeBindings.length>0?KeyBindingManager.formatKeyDescriptor(closeBindings[closeBindings.length-1].displayKey):"",closeTabTooltip=closeShortcut?`${Strings.CLOSE_TAB_TOOLTIP} (${closeShortcut})`:Strings.CLOSE_TAB_TOOLTIP;$(this).attr("title",closeTabTooltip)}),$(document).on("mousedown",".phoenix-tab-bar .tab",function(event){if($(event.target).hasClass("fa-times")||$(event.target).closest(".tab-close").length)return;const filePath=$(this).attr("data-path");if(!filePath)return;const isSecondPane=$(this).closest("#phoenix-tab-bar-2").length>0,paneId=isSecondPane?"second-pane":"first-pane",fileObj=FileSystem.getFileForPath(filePath),currentActivePane=MainViewManager.getActivePaneId(),isPaneActive=paneId===currentActivePane,currentFile=MainViewManager.getCurrentlyViewedFile(currentActivePane);$(this).hasClass("placeholder")&&MainViewManager.addToWorkingSet(paneId,fileObj),isPaneActive&¤tFile&¤tFile.fullPath===filePath?setTimeout(function(){const editor=EditorManager.getActiveEditor();editor&&editor.focus()},0):CommandManager.execute(Commands.FILE_OPEN,{fullPath:filePath,paneId:paneId}).always(function(){setTimeout(function(){const editor=EditorManager.getActiveEditor();editor&&editor.focus()},0)})}),$(document).on("contextmenu",".phoenix-tab-bar .tab",function(event){event.preventDefault(),event.stopPropagation();const filePath=$(this).attr("data-path"),isSecondPane=$(this).closest("#phoenix-tab-bar-2").length>0,paneId=isSecondPane?"second-pane":"first-pane";MoreOptions.showMoreOptionsContextMenu(paneId,filePath,event.pageX,event.pageY)})}const debounceUpdateTabs=_.debounce(updateTabs,2);function onFileSave(event,commandId){if(isTabBarActive()&&(commandId===Commands.FILE_SAVE||commandId===Commands.FILE_SAVE_ALL)){const activePane=MainViewManager.getActivePaneId(),currentFile=MainViewManager.getCurrentlyViewedFile(activePane);if(currentFile){const filePath=currentFile.fullPath,isFirstPanePlaceholder=MainViewManager.getCurrentlyViewedFile("first-pane")&&MainViewManager.getCurrentlyViewedFile("first-pane").fullPath===filePath&&!Global.firstPaneWorkingSet.some(entry=>entry.path===filePath),isSecondPanePlaceholder=MainViewManager.getCurrentlyViewedFile("second-pane")&&MainViewManager.getCurrentlyViewedFile("second-pane").fullPath===filePath&&!Global.secondPaneWorkingSet.some(entry=>entry.path===filePath);if(isFirstPanePlaceholder){const fileObj=FileSystem.getFileForPath(filePath);MainViewManager.addToWorkingSet("first-pane",fileObj)}if(isSecondPanePlaceholder){const fileObj=FileSystem.getFileForPath(filePath);MainViewManager.addToWorkingSet("second-pane",fileObj)}}}}function _registerHandlers(){MainViewManager.on("paneCreate paneDestroy paneLayoutChange",createTabBar),MainViewManager.on("activePaneChange",debounceUpdateTabs),MainViewManager.on(MainViewManager.EVENT_CURRENT_FILE_CHANGE,debounceUpdateTabs),window.phoenixGitEvents&&window.phoenixGitEvents.EventEmitter&&window.phoenixGitEvents.EventEmitter.on("GIT_FILE_STATUS_CHANGED",debounceUpdateTabs);const events=["workingSetAdd","workingSetRemove","workingSetSort","workingSetMove","workingSetAddList","workingSetRemoveList","workingSetUpdate","_workingSetDisableAutoSort"];MainViewManager.off(events.join(" "),updateTabs),MainViewManager.on(events.join(" "),updateTabs),$("#sidebar").off("panelCollapsed panelExpanded panelResizeEnd",updateTabs),$("#sidebar").on("panelCollapsed panelExpanded panelResizeEnd",updateTabs),new ResizeObserver(updateTabs).observe($("#main-plugin-panel")[0]),CommandManager.off("beforeExecuteCommand",onFileSave),CommandManager.on("beforeExecuteCommand",onFileSave),DocumentManager.on("dirtyFlagChange",function(event,doc){if(!isTabBarActive())return;const filePath=doc.file.fullPath;if($tabBar){const $tab=$tabBar.find(`.tab[data-path="${filePath}"]`);$tab.toggleClass("dirty",doc.isDirty);for(let i=0;i<Global.firstPaneWorkingSet.length;i++)if(Global.firstPaneWorkingSet[i].path===filePath){Global.firstPaneWorkingSet[i].isDirty=doc.isDirty;break}}if($tabBar2){const $tab2=$tabBar2.find(`.tab[data-path="${filePath}"]`);$tab2.toggleClass("dirty",doc.isDirty);for(let i=0;i<Global.secondPaneWorkingSet.length;i++)if(Global.secondPaneWorkingSet[i].path===filePath){Global.secondPaneWorkingSet[i].isDirty=doc.isDirty;break}}})}function preferenceChanged(){const prefs=PreferencesManager.get(Preference.PREFERENCES_TAB_BAR);Preference.tabBarEnabled=prefs.showTabBar,Preference.tabBarNumberOfTabs=prefs.numberOfTabs,CommandManager.get(Commands.TOGGLE_TABBAR).setChecked(prefs.showTabBar),isTabBarActive()?createTabBar():cleanupTabBar()}function _registerCommands(){CommandManager.register(Strings.CMD_TOGGLE_TABBAR,Commands.TOGGLE_TABBAR,()=>{const currentPref=PreferencesManager.get(Preference.PREFERENCES_TAB_BAR);PreferencesManager.set(Preference.PREFERENCES_TAB_BAR,{...currentPref,showTabBar:!currentPref.showTabBar})})}function setupTabBarScrolling(){function handleMouseWheel(e){const $scrolledTabBar=$(this),scrollAmount=2.5*e.originalEvent.deltaY,newScrollLeft=$scrolledTabBar.scrollLeft()+scrollAmount;$scrolledTabBar.scrollLeft(newScrollLeft)}$(document).on("wheel","#phoenix-tab-bar, #phoenix-tab-bar-2",handleMouseWheel)}AppInit.appReady(function(){_registerCommands();const viewMenu=Menus.getMenu(Menus.AppMenuBar.VIEW_MENU);viewMenu.addMenuItem(Commands.TOGGLE_TABBAR,"",Menus.AFTER,Commands.VIEW_HIDE_SIDEBAR),PreferencesManager.on("change",Preference.PREFERENCES_TAB_BAR,preferenceChanged),preferenceChanged(),_registerHandlers(),handleTabClick(),MoreOptions.init(),Overflow.init(),DragDrop.init($("#phoenix-tab-bar"),$("#phoenix-tab-bar-2")),setupTabBarScrolling()})});