diff --git a/src/editor/Editor.js b/src/editor/Editor.js index 61d1a4f298..64594a8317 100644 --- a/src/editor/Editor.js +++ b/src/editor/Editor.js @@ -435,6 +435,19 @@ define(function (require, exports, module) { return $(this.getRootElement()); } }); + + const $cmElement = this.$el; + $cmElement[0].addEventListener("wheel", (event) => { + const $editor = $cmElement.find(".CodeMirror-scroll"); + // we need to slow down the scroll by the factor of line height. else the scrolling is too fast. + // this became a problem after we added the custom line height feature causing jumping scrolls esp in safari + // and mac if we dont do this scroll scaling. + const lineHeight = parseFloat(getComputedStyle($editor[0]).lineHeight); + const scrollDelta = event.deltaY; + const defaultHeight = 14, scrollScaleFactor = lineHeight/defaultHeight; + $editor[0].scrollTop += (scrollDelta/scrollScaleFactor); + event.preventDefault(); + }); } EventDispatcher.makeEventDispatcher(Editor.prototype); diff --git a/src/extensionsIntegrated/indentGuides/main.js b/src/extensionsIntegrated/indentGuides/main.js index fc090b2b3a..8f1d5d7bb7 100644 --- a/src/extensionsIntegrated/indentGuides/main.js +++ b/src/extensionsIntegrated/indentGuides/main.js @@ -36,7 +36,8 @@ define(function (require, exports, module) { const COMMAND_NAME = Strings.CMD_TOGGLE_INDENT_GUIDES, COMMAND_ID = Commands.TOGGLE_INDENT_GUIDES, - GUIDE_CLASS = "phcode-indent-guides"; + GUIDE_CLASS = "phcode-indent-guides", + GUIDE_CLASS_NONE = "phcode-indent-guides-none"; const PREFERENCES_EDITOR_INDENT_GUIDES = "editor.indentGuides", PREFERENCES_EDITOR_INDENT_HIDE_FIRST = "editor.indentHideFirst"; @@ -54,40 +55,42 @@ define(function (require, exports, module) { }); // CodeMirror overlay code - const indentGuidesOverlay = { - token: function (stream, _state) { - let char = "", - colNum = 0, - spaceUnits = 0, - isTabStart = false; - - char = stream.next(); - colNum = stream.column(); - - // Check for "hide first guide" preference - if ((hideFirst) && (colNum === 0)) { - return null; - } + function _createOverlayObject(spaceUnits) { + return { + _spaceUnits: spaceUnits, + _cmRefreshPending: false, + token: function (stream, _state) { + let char = "", + colNum = 0, + isTabStart = false; + + char = stream.next(); + colNum = stream.column(); + + // Check for "hide first guide" preference + if ((hideFirst) && (colNum === 0)) { + return null; + } - if (char === "\t") { - return GUIDE_CLASS; - } + if (char === "\t") { + return enabled? GUIDE_CLASS : GUIDE_CLASS_NONE; + } - if (char !== " ") { - stream.skipToEnd(); - return null; - } + if (char !== " ") { + stream.skipToEnd(); + return null; + } - spaceUnits = Editor.getSpaceUnits(); - isTabStart = (colNum % spaceUnits) ? false : true; + isTabStart = (colNum % this._spaceUnits) === 0 ? true : false; - if ((char === " ") && (isTabStart)) { - return GUIDE_CLASS; - } - return null; - }, - flattenSpans: false - }; + if ((char === " ") && (isTabStart)) { + return enabled? GUIDE_CLASS : GUIDE_CLASS_NONE; + } + return null; + }, + flattenSpans: false + }; + } function applyPreferences() { enabled = PreferencesManager.get(PREFERENCES_EDITOR_INDENT_GUIDES); @@ -95,28 +98,46 @@ define(function (require, exports, module) { } function updateUI() { - const editor = EditorManager.getActiveEditor(), - cm = editor ? editor._codeMirror : null; - - // Update CodeMirror overlay if editor is available - if (cm) { - if(editor._overlayPresent){ - if(!enabled){ - cm.removeOverlay(indentGuidesOverlay); - editor._overlayPresent = false; - cm.refresh(); + const editor = EditorManager.getActiveEditor(); + if(!editor || !editor._codeMirror) { + return; + } + const cm = editor._codeMirror; + if(!editor.__indentGuidesOverlay) { + editor.__indentGuidesOverlay = _createOverlayObject(Editor.getSpaceUnits(editor.document.file.fullPath)); + } + function _reRenderOverlay() { + cm.removeOverlay(editor.__indentGuidesOverlay); + cm.addOverlay(editor.__indentGuidesOverlay); + } + editor.off(Editor.EVENT_OPTION_CHANGE+".indentGuide"); + editor.on(Editor.EVENT_OPTION_CHANGE+".indentGuide", ()=>{ + const newSpaceUnits = Editor.getSpaceUnits(editor.document.file.fullPath); + if(newSpaceUnits !== editor.__indentGuidesOverlay._spaceUnits){ + editor.__indentGuidesOverlay._spaceUnits = newSpaceUnits; + if(EditorManager.getActiveEditor() === editor){ + console.log("space units changed, refreshing indent guides for", + newSpaceUnits, editor.document.file.fullPath); + _reRenderOverlay(); } - } else if(enabled){ - cm.removeOverlay(indentGuidesOverlay); - cm.addOverlay(indentGuidesOverlay); - editor._overlayPresent = true; - cm.refresh(); } + }); + + let shouldRerender = false; + if(!cm.__indentGuidesOverlayAttached){ + cm.addOverlay(editor.__indentGuidesOverlay); + cm.__indentGuidesOverlayAttached = editor.__indentGuidesOverlay; + cm.__overlayEnabled = enabled; + } else if(cm.__indentGuidesOverlayAttached && + cm.__indentGuidesOverlayAttached !== editor.__indentGuidesOverlay) { + cm.removeOverlay(cm.__indentGuidesOverlayAttached); + cm.addOverlay(editor.__indentGuidesOverlay); + cm.__overlayEnabled = enabled; + } else if (shouldRerender || cm.__overlayEnabled !== enabled) { + cm.__overlayEnabled = enabled; + _reRenderOverlay(); + console.log("Refreshing indent guides"); } - - // Update menu - CommandManager.get(COMMAND_ID) - .setChecked(enabled); } function handleToggleGuides() { @@ -127,6 +148,8 @@ define(function (require, exports, module) { function preferenceChanged() { applyPreferences(); updateUI(); + CommandManager.get(COMMAND_ID) + .setChecked(enabled); } // Initialize extension diff --git a/src/htmlContent/themes-settings.html b/src/htmlContent/themes-settings.html index 204f8acb14..997e32b51a 100644 --- a/src/htmlContent/themes-settings.html +++ b/src/htmlContent/themes-settings.html @@ -41,6 +41,21 @@