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) · 12 KB
/
Copy pathmain.js
File metadata and controls
1 lines (1 loc) · 12 KB
1
define(function(require,exports,module){const _=require("thirdparty/lodash"),CodeMirror=require("thirdparty/CodeMirror/lib/codemirror"),CommandManager=require("command/CommandManager"),Commands=require("command/Commands"),KeyBindingManager=require("command/KeyBindingManager"),MainViewManager=require("view/MainViewManager"),Menus=require("command/Menus"),Mustache=require("thirdparty/mustache/mustache"),EditorManager=require("editor/EditorManager"),Dialogs=require("widgets/Dialogs"),Metrics=require("utils/Metrics"),WorkspaceManager=require("view/WorkspaceManager"),StringUtils=require("utils/StringUtils"),AppInit=require("utils/AppInit"),DropdownButton=require("widgets/DropdownButton").DropdownButton,Strings=require("strings");require("./vscode");const panelHtml=require("text!./templates/bottom-panel.html"),shortcutsHtml=require("text!./templates/shortcut-table.html"),TOGGLE_SHORTCUTS_ID=Commands.HELP_TOGGLE_SHORTCUTS_PANEL,DEFAULT_PACK_PLACEHOLDER="default";let keyList=[],panel,$shortcutsPanel,$filterField,currentFilter,presetPicker,_updateKeyBindings,sortByBase=1,sortByBinding=2,sortByCmdId=3,sortByCmdName=4,sortByOrig=5,sortColumn=sortByBase,sortAscending=!0,origBrackets=Strings.APP_NAME,origCodeMirror=window.debugMode?"CodeMirror":Strings.APP_NAME,origExtension=Strings.KEYBOARD_SHORTCUT_ORIG_EXTENSION;function _getBaseKey(keyBinding){let keyBase=keyBinding.replace(/Ctrl-/,"").replace(/Shift-/,"").replace(/Alt-/,"");return"mac"===brackets.platform&&(keyBase=keyBase.replace(/Cmd-/,"")),keyBase}function _findKeyBinding(kl,keyBinding){let j;for(j=0;j<kl.length;j++)if(keyBinding===kl[j].keyBinding)return j;return-1}function _ucFirst(string){return string.charAt(0).toUpperCase()+string.slice(1)}function _humanizeString(string){return string=(string=(string=string.replace(/_/g," ").replace(/([A-Z])/g," $1")).replace(/(^\s+)/,"").replace(/(\s+$)/,"")).split(/\s+/).map(_ucFirst).join(" ")}function _getOriginFromCommandId(cmdID,keyBinding){const customOrigin=KeyBindingManager._getCustomShortcutOrigin(keyBinding);if(customOrigin)return customOrigin;let idArray=cmdID.split(".");const defaultCommands=Object.values(Commands);let q1=idArray[0].toLowerCase();return defaultCommands.includes(cmdID)||"file"===q1||"edit"===q1||"view"===q1||"navigate"===q1||"debug"===q1||"help"===q1||cmdID.startsWith("AltMenu-")||cmdID.startsWith("codefolding.")||cmdID.startsWith("navigation.")||cmdID.startsWith("recent-files")||cmdID.startsWith("refactoring.")||cmdID.startsWith("recentProjects")||cmdID.startsWith("tabbar.")||"showParameterHint"===cmdID?origBrackets:idArray.length>2?origExtension+" ("+_humanizeString(idArray[1])+")":(idArray.length,origExtension)}function _filterFromKeyBinding(text){return`${(text=text||"").toLowerCase()} ${text.replace("-","+").toLowerCase()}`}function _getkeyList(){let keyBinding,base,command,key;const allCommandsWithShortcuts=new Set,menuCommands=Menus.getAllMenuItemCommands(),knownBindableCommands=KeyBindingManager._getKnownBindableCommands(),allCommandsToList=new Set([...menuCommands,...knownBindableCommands]);let bracketsKeymap=KeyBindingManager.getKeymap();if(bracketsKeymap)for(keyBinding in bracketsKeymap)if(bracketsKeymap.hasOwnProperty(keyBinding)&&(key=bracketsKeymap[keyBinding])){if(base=_getBaseKey(keyBinding),!(command=CommandManager.get(key.commandID)))continue;allCommandsWithShortcuts.add(key.commandID),keyList.push({keyBase:KeyBindingManager.formatKeyDescriptor(base),keyBinding:keyBinding,keyBindingDisplay:KeyBindingManager.formatKeyDescriptor(keyBinding),command:command,commandID:key.commandID,commandName:command.getName(),origin:_getOriginFromCommandId(key.commandID,keyBinding),filter:command.getName().toLowerCase()+_filterFromKeyBinding(keyBinding)+_getOriginFromCommandId(key.commandID,keyBinding).toLowerCase()})}if(CodeMirror.keyMap){let cmKeymap="mac"===brackets.platform?CodeMirror.keyMap.macDefault:CodeMirror.keyMap.pcDefault;if(cmKeymap)for(keyBinding in cmKeymap)cmKeymap.hasOwnProperty(keyBinding)&&"fallthrough"!==keyBinding&&-1===_findKeyBinding(keyList,keyBinding)&&(base=_getBaseKey(keyBinding),allCommandsWithShortcuts.add(cmKeymap[keyBinding]),keyList.push({keyBase:KeyBindingManager.formatKeyDescriptor(base),keyBinding:keyBinding,keyBindingDisplay:KeyBindingManager.formatKeyDescriptor(keyBinding),commandID:cmKeymap[keyBinding],commandName:cmKeymap[keyBinding],origin:window.debugMode?origCodeMirror:origBrackets,filter:cmKeymap[keyBinding].toLowerCase()+_filterFromKeyBinding(keyBinding)+origCodeMirror.toLowerCase()}))}for(let commandID of allCommandsToList)allCommandsWithShortcuts.has(commandID)||keyList.push({keyBase:"",keyBinding:"",keyBindingDisplay:"",commandID:commandID,commandName:commandID,origin:_getOriginFromCommandId(commandID,keyBinding),filter:commandID.toLowerCase()+_getOriginFromCommandId(commandID,keyBinding).toLowerCase()});return keyList}function _strcmp(a,b){return a<b?sortAscending?-1:1:a>b?sortAscending?1:-1:0}function _stricmp(a,b){return _strcmp(a.toLowerCase(),b.toLowerCase())}function _keyBaseSort(a,b){let a2,b2,c=_strcmp((1===a.keyBase.length?"0":"1")+a.keyBase,(1===b.keyBase.length?"0":"1")+b.keyBase);return 0!==c?c:_strcmp(a.keyBinding,b.keyBinding)}function _keyBindingSort(a,b){return _strcmp(a.keyBinding,b.keyBinding)}function _keyCmdIdSort(a,b){return _stricmp(a.commandID,b.commandID)}function _keyCmdNameSort(a,b){return _strcmp(a.commandName,b.commandName)}function _keyOrigSort(a,b){return _strcmp(a.origin,b.origin)}function _getSortFunc(){return sortColumn===sortByBinding?_keyBindingSort:sortColumn===sortByCmdId?_keyCmdIdSort:sortColumn===sortByCmdName?_keyCmdNameSort:sortColumn===sortByOrig?_keyOrigSort:_keyBaseSort}function _getShortcutsHtml(){var msData={};return msData.keyList=keyList.sort(_getSortFunc()),msData.Strings=Strings,Mustache.render(shortcutsHtml,msData)}function _changeSorting(newSortColumn){newSortColumn===sortColumn?sortAscending=!sortAscending:sortColumn=newSortColumn,_showShortcuts()}function _filterShortcuts(forceFiltering){var terms=$filterField.val().trim().toLocaleLowerCase();(forceFiltering||terms!==currentFilter)&&(currentFilter=terms,terms=terms.split(/\s+?/),$.each(keyList,function(i,key){let match;""===terms?match=!0:$.each(terms,function(i,term){!1!==match&&(match=key.filter.indexOf(term)>-1)}),key.filterMatch=match}))}function _clearSortingEventHandlers(){var $shortcuts=$("#shortcuts-panel");$("thead .shortcut-base a",$shortcuts).off("click"),$("thead .shortcut-binding a",$shortcuts).off("click"),$("thead .shortcut-cmd-id a",$shortcuts).off("click"),$("thead .shortcut-cmd-name a",$shortcuts).off("click"),$("thead .shortcut-orig a",$shortcuts).off("click")}function _showShortcuts(){_updatePresets();let $shortcuts=$("#shortcuts-panel");_filterShortcuts(!0),_clearSortingEventHandlers(),$shortcuts.find(".resizable-content").html(_getShortcutsHtml()),$shortcuts.find("thead th").eq(sortColumn-1).addClass("sort-"+(sortAscending?"ascending":"descending")),$("thead .shortcut-base a",$shortcuts).on("click",function(){_changeSorting(sortByBase)}),$("thead .shortcut-binding a",$shortcuts).on("click",function(){_changeSorting(sortByBinding)}),$("thead .shortcut-cmd-id a",$shortcuts).on("click",function(){_changeSorting(sortByCmdId)}),$("thead .shortcut-cmd-name a",$shortcuts).on("click",function(){_changeSorting(sortByCmdName)}),$("thead .shortcut-orig a",$shortcuts).on("click",function(){_changeSorting(sortByOrig)}),_showCommandIdsInPanelIfNeeded(),$shortcutsPanel.find(".copy-command").click(event=>{const commandId=event.target.getAttribute("data-commandID");Phoenix.app.copyToClipboard(commandId)})}function initKeyList(){0===keyList.length&&(keyList=_getkeyList())}function destroyKeyList(){$.each(keyList,function(i,key){key.command&&key.command.off(".bds")}),keyList=[]}function _updatePresets(){if(!panel||!panel.isVisible())return;const allPacks=KeyBindingManager.getAllCustomKeymapPacks(),currentKeymapPack=KeyBindingManager.getCurrentCustomKeymapPack();currentKeymapPack?presetPicker.$button.text(StringUtils.format(Strings.KEYBOARD_SHORTCUT_PRESET_USING,currentKeymapPack.packageName)):presetPicker.$button.text(Strings.KEYBOARD_SHORTCUT_PRESET_SELECT),presetPicker.items=[DEFAULT_PACK_PLACEHOLDER,...allPacks.map(pack=>pack.packID)]}function _handleShowHideShortcuts(){panel.isVisible()?(destroyKeyList(),_clearSortingEventHandlers(),panel.hide(),CommandManager.get(TOGGLE_SHORTCUTS_ID).setChecked(!1),MainViewManager.focusActivePane()):(Metrics.countEvent(Metrics.EVENT_TYPE.KEYBOARD,"shortcutPanel","shown"),panel.show(),CommandManager.get(TOGGLE_SHORTCUTS_ID).setChecked(!0),$filterField.val("").focus(),initKeyList(),_showShortcuts()),WorkspaceManager.recomputeLayout()}function _showCommandIdsInPanelIfNeeded(){const editor=EditorManager.getActiveEditor();editor&&editor.document&&editor.document.file.fullPath===KeyBindingManager._getUserKeyMapFilePath()?$shortcutsPanel.find("#phoenix-keyboard-shortcuts-table").removeClass("hide-third-column"):$shortcutsPanel.find("#phoenix-keyboard-shortcuts-table").addClass("hide-third-column")}function _presetRenderer(packID){if(packID===DEFAULT_PACK_PLACEHOLDER)return Strings.DEFAULT;const allPacks=KeyBindingManager.getAllCustomKeymapPacks();for(let pack of allPacks)if(pack.packID===packID)return pack.packageName;return packID}_updateKeyBindings=_.debounce(function(){destroyKeyList(),initKeyList(),_showShortcuts()},300),AppInit.appReady(function(){let s,file_menu;CommandManager.register(Strings.KEYBOARD_SHORTCUT_MENU_SHOW_SHORTCUTS,TOGGLE_SHORTCUTS_ID,_handleShowHideShortcuts,{supportsDesignMode:!0}),(file_menu=Menus.getMenu(Menus.AppMenuBar.FILE_MENU))&&file_menu.addMenuItem(TOGGLE_SHORTCUTS_ID,"",Menus.BEFORE,Commands.FILE_EXTENSION_MANAGER),s=Mustache.render(panelHtml,Strings),(panel=WorkspaceManager.createBottomPanel(TOGGLE_SHORTCUTS_ID,$(s),300,Strings.KEYBOARD_SHORTCUT_PANEL_TITLE,{iconSvg:"styles/images/panel-icon-shortcuts.svg"})).hide(),$shortcutsPanel=$("#shortcuts-panel"),presetPicker=new DropdownButton(Strings.KEYBOARD_SHORTCUT_PRESET_SELECT,[],_presetRenderer,{cssClasses:"presetPicker no-focus"}),$shortcutsPanel.find(".presetPickerContainer").append(presetPicker.$button),presetPicker.on("select",function(e,selectedPackID){selectedPackID===DEFAULT_PACK_PLACEHOLDER&&KeyBindingManager._setCurrentCustomKeymapPack(null),KeyBindingManager._setCurrentCustomKeymapPack(selectedPackID),_updatePresets()}),$shortcutsPanel.on("dblclick",function(e){var $rowEl=$(e.target).closest("tr");$rowEl.length>0&&KeyBindingManager.showShortcutSelectionDialog(CommandManager.get($rowEl[0].dataset.commandid))}),$shortcutsPanel.find(".reset-to-default").click(function(){Dialogs.showConfirmDialog(Strings.KEYBOARD_SHORTCUT_RESET_DIALOG_TITLE,Strings.KEYBOARD_SHORTCUT_RESET_DIALOG_MESSAGE).done(function(selection){selection===Dialogs.DIALOG_BTN_OK&&(Metrics.countEvent(Metrics.EVENT_TYPE.KEYBOARD,"shortcut","reset"),KeyBindingManager.resetUserShortcutsAsync())})}),($filterField=$shortcutsPanel.find(".toolbar .filter")).on("keyup",event=>{event&&"Escape"===event.key?MainViewManager.focusActivePane():_showShortcuts()}),EditorManager.on("activeEditorChange",(_event,newEditor)=>{newEditor&&newEditor.document&&newEditor.document.file.fullPath===KeyBindingManager._getUserKeyMapFilePath()&&(CommandManager.get(TOGGLE_SHORTCUTS_ID).getChecked()||CommandManager.get(TOGGLE_SHORTCUTS_ID).execute()),_showCommandIdsInPanelIfNeeded()}),KeyBindingManager.on(KeyBindingManager.EVENT_KEY_BINDING_ADDED,_updateKeyBindings),KeyBindingManager.on(KeyBindingManager.EVENT_KEY_BINDING_REMOVED,_updateKeyBindings),KeyBindingManager.on(KeyBindingManager.EVENT_NEW_PRESET,_updatePresets),KeyBindingManager.on(KeyBindingManager.EVENT_PRESET_CHANGED,_updatePresets);let _shortcutsTabOpen=!1;WorkspaceManager.on(WorkspaceManager.EVENT_WORKSPACE_PANEL_HIDDEN,function(event,panelID){panelID===TOGGLE_SHORTCUTS_ID&&panel&&(_shortcutsTabOpen=!1,destroyKeyList(),_clearSortingEventHandlers(),CommandManager.get(TOGGLE_SHORTCUTS_ID).setChecked(!1)),panelID===WorkspaceManager.DEFAULT_PANEL_ID&&CommandManager.get(TOGGLE_SHORTCUTS_ID).setChecked(!1)}),WorkspaceManager.on(WorkspaceManager.EVENT_WORKSPACE_PANEL_SHOWN,function(event,panelID){panelID===TOGGLE_SHORTCUTS_ID&&(_shortcutsTabOpen=!0),_shortcutsTabOpen&&CommandManager.get(TOGGLE_SHORTCUTS_ID).setChecked(!0)})})});