forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathViewCommandHandlers.js
More file actions
1 lines (1 loc) · 10.9 KB
/
ViewCommandHandlers.js
File metadata and controls
1 lines (1 loc) · 10.9 KB
1
define(function(require,exports,module){var Commands=require("command/Commands"),EventDispatcher=require("utils/EventDispatcher"),CommandManager=require("command/CommandManager"),Strings=require("strings"),StringUtils=require("utils/StringUtils"),EditorManager=require("editor/EditorManager"),PreferencesManager=require("preferences/PreferencesManager"),DocumentManager=require("document/DocumentManager"),ThemeSettings=require("view/ThemeSettings"),MainViewManager=require("view/MainViewManager"),AppInit=require("utils/AppInit"),Dialogs=require("widgets/Dialogs"),DefaultDialogs=require("widgets/DefaultDialogs"),KeyBindingManager=require("command/KeyBindingManager"),WorkspaceManager=require("view/WorkspaceManager"),_=require("thirdparty/lodash"),FontRuleTemplate=require("text!view/fontrules/font-based-rules.less"),NotificationUI=require("widgets/NotificationUI"),prefs=PreferencesManager.getExtensionPrefs("fonts"),validFontSizeRegExpStr="^([0-9]+)?(\\.)?([0-9]+)(px|em)$",currFontSize,currFontFamily,DYNAMIC_FONT_STYLE_ID="codemirror-dynamic-fonts",DYNAMIC_FONT_FAMILY_ID="codemirror-dynamic-font-family",MIN_FONT_SIZE=1,MAX_FONT_SIZE=72,DEFAULT_FONT_SIZE=12,DEFAULT_FONT_FAMILY="'SourceCodePro-Medium', MS ゴシック, 'MS Gothic', monospace";const PREF_DESKTOP_ZOOM_SCALE="desktopZoomScale",DEFAULT_ZOOM_SCALE=1,MIN_ZOOM_SCALE=.5,MAX_ZOOM_SCALE=2;function _removeDynamicProperty(propertyID){$("#"+propertyID).remove()}function _addDynamicProperty(propertyID,ruleCfg){var $style=$("<style type='text/css'></style>").attr("id",propertyID);if(ruleCfg.ruleText)$style.html(ruleCfg.ruleText);else{var cssRule=ruleCfg.ruleName||".CodeMirror",styleStr=ruleCfg.ruleText||StringUtils.format("{0}: {1} {2}",ruleCfg.propName,ruleCfg.propValue,ruleCfg.priorityFlag?"!important":"");$style.html(cssRule+"{ "+styleStr+" }")}_removeDynamicProperty(propertyID),$("head").append($style)}function _removeDynamicFontSize(){_removeDynamicProperty(DYNAMIC_FONT_STYLE_ID)}function _addDynamicFontSize(fontSize){var template=FontRuleTemplate.split("{font-size-param}").join(fontSize);less.render(template,null,function onParse(err,tree){err?console.error(err):_addDynamicProperty(DYNAMIC_FONT_STYLE_ID,{ruleText:tree.css})})}function _removeDynamicFontFamily(){_removeDynamicProperty(DYNAMIC_FONT_FAMILY_ID)}function _addDynamicFontFamily(fontFamily){_addDynamicProperty(DYNAMIC_FONT_FAMILY_ID,{propName:"font-family",propValue:fontFamily})}function _updateScroll(editor,fontSize){var oldWidth=editor._codeMirror.defaultCharWidth(),oldFontSize=prefs.get("fontSize"),newFontSize=fontSize,delta=0,adjustment=0,scrollPos=editor.getScrollPos(),line=editor._codeMirror.lineAtHeight(scrollPos.y,"local");delta=/em$/.test(oldFontSize)?10:1,(adjustment=parseInt((parseFloat(newFontSize)-parseFloat(oldFontSize))*delta,10))&&editor.refreshAll();var newWidth=editor._codeMirror.defaultCharWidth(),deltaX=scrollPos.x/oldWidth,scrollPosX=scrollPos.x+Math.round(deltaX*(newWidth-oldWidth)),scrollPosY=editor._codeMirror.heightAtLine(line,"local");editor.setScrollPos(scrollPosX,scrollPosY)}function setFontSize(fontSize){currFontSize!==fontSize&&(_removeDynamicFontSize(),fontSize&&_addDynamicFontSize(fontSize),_.forEach(MainViewManager.getPaneIdList(),function(paneId){var currentPath=MainViewManager.getCurrentlyViewedPath(paneId),doc=currentPath&&DocumentManager.getOpenDocumentForPath(currentPath);doc&&doc._masterEditor&&_updateScroll(doc._masterEditor,fontSize)}),exports.trigger("fontSizeChange",fontSize,currFontSize),currFontSize=fontSize,prefs.set("fontSize",fontSize))}function getFontSize(){return prefs.get("fontSize")}function setFontFamily(fontFamily){var editor=EditorManager.getCurrentFullEditor();currFontFamily!==fontFamily&&(_removeDynamicFontFamily(),fontFamily&&_addDynamicFontFamily(fontFamily),exports.trigger("fontFamilyChange",fontFamily,currFontFamily),currFontFamily=fontFamily,prefs.set("fontFamily",fontFamily),editor&&editor.refreshAll())}function getFontFamily(){return prefs.get("fontFamily")}function _adjustFontSize(adjustment){var fsStyle=prefs.get("fontSize"),validFontSizeRegExp=new RegExp(validFontSizeRegExpStr);if(-1===fsStyle.search(validFontSizeRegExp))return!1;var fsUnits=fsStyle.substring(fsStyle.length-2,fsStyle.length),delta="px"===fsUnits?1:.1,fsOld,fsNew=parseFloat(fsStyle.substring(0,fsStyle.length-2))+delta*adjustment,fsStr;return!(fsNew<MIN_FONT_SIZE*delta||fsNew>MAX_FONT_SIZE*delta)&&(setFontSize(fsNew+fsUnits),!0)}function _handleIncreaseFontSize(){_adjustFontSize(1)}function _handleBrowserZoom(event){if(event.eventSource===CommandManager.SOURCE_KEYBOARD_SHORTCUT)return(new $.Deferred).reject("use browser zoom");{const zoomInKey=KeyBindingManager.getKeyBindingsDisplay(Commands.VIEW_ZOOM_IN)||"",zoomOutKey=KeyBindingManager.getKeyBindingsDisplay(Commands.VIEW_ZOOM_OUT)||"";let message=StringUtils.format(Strings.ZOOM_WITH_SHORTCUTS_DETAILS,zoomInKey,zoomOutKey);return Dialogs.showInfoDialog(Strings.ZOOM_WITH_SHORTCUTS,message),(new $.Deferred).resolve()}}function _showZoomHUD(zoomFactor,zoomingIn){const pct=Math.round(100*zoomFactor),icon=zoomingIn?"fa-solid fa-magnifying-glass-plus":"fa-solid fa-magnifying-glass-minus";NotificationUI.showHUD(icon,pct+"%",{autoCloseTimeS:1})}function _handleZoomIn(event){if(!Phoenix.isNativeApp)return _handleBrowserZoom(event);const currentZoom=prefs.get(PREF_DESKTOP_ZOOM_SCALE);if(currentZoom<MAX_ZOOM_SCALE){const newZoom=currentZoom+.1;prefs.set(PREF_DESKTOP_ZOOM_SCALE,newZoom),PhStore.setItem(PhStore._PHSTORE_BOOT_DESKTOP_ZOOM_SCALE_KEY,newZoom),_showZoomHUD(newZoom,!0)}}function _handleZoomOut(event){if(!Phoenix.isNativeApp)return _handleBrowserZoom(event);const currentZoom=prefs.get(PREF_DESKTOP_ZOOM_SCALE);if(currentZoom>MIN_ZOOM_SCALE){const newZoom=currentZoom-.1;prefs.set(PREF_DESKTOP_ZOOM_SCALE,newZoom),PhStore.setItem(PhStore._PHSTORE_BOOT_DESKTOP_ZOOM_SCALE_KEY,newZoom),_showZoomHUD(newZoom,!1)}}function _handleDecreaseFontSize(){_adjustFontSize(-1)}function _handleRestoreFontSize(){setFontSize(DEFAULT_FONT_SIZE+"px")}function _updateUI(){null!==DocumentManager.getCurrentDocument()?CommandManager.get(Commands.VIEW_INCREASE_FONT_SIZE).getEnabled()||(CommandManager.get(Commands.VIEW_INCREASE_FONT_SIZE).setEnabled(!0),CommandManager.get(Commands.VIEW_DECREASE_FONT_SIZE).setEnabled(!0),CommandManager.get(Commands.VIEW_RESTORE_FONT_SIZE).setEnabled(!0)):(CommandManager.get(Commands.VIEW_INCREASE_FONT_SIZE).setEnabled(!1),CommandManager.get(Commands.VIEW_DECREASE_FONT_SIZE).setEnabled(!1),CommandManager.get(Commands.VIEW_RESTORE_FONT_SIZE).setEnabled(!1))}function init(){_addDynamicFontFamily(currFontFamily=prefs.get("fontFamily")),_addDynamicFontSize(currFontSize=prefs.get("fontSize")),_updateUI()}function restoreFontSize(){var fsStyle=prefs.get("fontSize"),fsAdjustment=PreferencesManager.getViewState("fontSizeAdjustment");fsAdjustment&&(PreferencesManager.setViewState("fontSizeAdjustment"),fsStyle||(fsStyle=DEFAULT_FONT_SIZE+fsAdjustment+"px",prefs.set("fontSize",fsStyle))),fsStyle&&(_removeDynamicFontSize(),_addDynamicFontSize(fsStyle))}function restoreFonts(){setFontFamily(DEFAULT_FONT_FAMILY),setFontSize(DEFAULT_FONT_SIZE+"px")}function _getLinesInView(textHeight,scrollTop,editorHeight){var scrolledTop=scrollTop/textHeight,scrolledBottom=(scrollTop+editorHeight)/textHeight,firstLine,lastLine;return{first:Math.ceil(scrolledTop),last:Math.floor(scrolledBottom)-1}}function _scrollLine(direction){var editor=EditorManager.getCurrentFullEditor(),textHeight=editor.getTextHeight(),cursorPos=editor.getCursorPos(),hasSelecction=editor.hasSelection(),inlineEditors=editor.getInlineWidgets(),scrollInfo=editor._codeMirror.getScrollInfo(),paddingTop=editor._getLineSpaceElement().offsetTop,editorHeight=scrollInfo.clientHeight,scrollTop=scrollInfo.top-paddingTop,removedScroll=paddingTop,line,coords;inlineEditors.forEach(function(inlineEditor){line=editor._getInlineWidgetLineNumber(inlineEditor),(coords=editor._codeMirror.charCoords({line:line,ch:0},"local")).top<scrollInfo.top?(scrollTop-=inlineEditor.info.height,removedScroll+=inlineEditor.info.height):coords.top+inlineEditor.info.height<scrollInfo.top+editorHeight&&(editorHeight-=inlineEditor.info.height)});var linesInView=_getLinesInView(textHeight,scrollTop,editorHeight);hasSelecction||(cursorPos.line<linesInView.first?editor.setCursorPos({line:linesInView.first+direction,ch:cursorPos.ch}):cursorPos.line>linesInView.last?editor.setCursorPos({line:linesInView.last+direction,ch:cursorPos.ch}):(direction>0&&cursorPos.line===linesInView.first||direction<0&&cursorPos.line===linesInView.last)&&editor._codeMirror.moveV(direction,"line"));var lines=linesInView.first+direction;editor.setScrollPos(scrollInfo.left,textHeight*lines+removedScroll)}function _handleScrollLineUp(){_scrollLine(-1)}function _handleScrollLineDown(){_scrollLine(1)}function _handleThemeSettings(){ThemeSettings.showDialog()}CommandManager.register(Strings.CMD_INCREASE_FONT_SIZE,Commands.VIEW_INCREASE_FONT_SIZE,_handleIncreaseFontSize),CommandManager.register(Strings.CMD_DECREASE_FONT_SIZE,Commands.VIEW_DECREASE_FONT_SIZE,_handleDecreaseFontSize),CommandManager.register(Strings.CMD_ZOOM_IN,Commands.VIEW_ZOOM_IN,_handleZoomIn,{eventSource:!0}),CommandManager.register(Strings.CMD_ZOOM_OUT,Commands.VIEW_ZOOM_OUT,_handleZoomOut,{eventSource:!0}),CommandManager.register(Strings.CMD_RESTORE_FONT_SIZE,Commands.VIEW_RESTORE_FONT_SIZE,_handleRestoreFontSize),CommandManager.register(Strings.CMD_SCROLL_LINE_UP,Commands.VIEW_SCROLL_LINE_UP,_handleScrollLineUp),CommandManager.register(Strings.CMD_SCROLL_LINE_DOWN,Commands.VIEW_SCROLL_LINE_DOWN,_handleScrollLineDown),CommandManager.register(Strings.CMD_THEMES,Commands.CMD_THEMES_OPEN_SETTINGS,_handleThemeSettings),prefs.definePreference("fontSize","string",DEFAULT_FONT_SIZE+"px",{description:Strings.DESCRIPTION_FONT_SIZE}).on("change",function(){setFontSize(prefs.get("fontSize"))}),prefs.definePreference("fontFamily","string",DEFAULT_FONT_FAMILY,{description:Strings.DESCRIPTION_FONT_FAMILY}).on("change",function(){setFontFamily(prefs.get("fontFamily"))}),prefs.definePreference(PREF_DESKTOP_ZOOM_SCALE,"number",1,{description:Strings.DESCRIPTION_DESKTOP_ZOOM_SCALE}).on("change",function(){if(Phoenix.isNativeApp){const zoomFactor=prefs.get(PREF_DESKTOP_ZOOM_SCALE);if(PhStore.setItem(PhStore._PHSTORE_BOOT_DESKTOP_ZOOM_SCALE_KEY,zoomFactor),zoomFactor<MIN_ZOOM_SCALE||zoomFactor>MAX_ZOOM_SCALE)return void console.error(`Zoom scale should be between ${MIN_ZOOM_SCALE} and ${MAX_ZOOM_SCALE} but got ${zoomFactor}!`);brackets.app.zoomWebView(zoomFactor);const zoomIn=CommandManager.get(Commands.VIEW_ZOOM_IN),zoomString=StringUtils.format(Strings.CMD_ZOOM_IN_SCALE,Math.round(100*zoomFactor));zoomIn.setName(zoomString)}}),MainViewManager.on("currentFileChange",_updateUI),AppInit.appReady(init),EventDispatcher.makeEventDispatcher(exports),exports.restoreFontSize=restoreFontSize,exports.restoreFonts=restoreFonts,exports.getFontSize=getFontSize,exports.setFontSize=setFontSize,exports.getFontFamily=getFontFamily,exports.setFontFamily=setFontFamily,exports.validFontSizeRegExp=validFontSizeRegExpStr});