-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Scroll line up/down - Take 3 #3068
Changes from 2 commits
0394312
43acbc1
ccbcc25
974ed1e
2585be7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,11 +27,12 @@ | |
| define(function (require, exports, module) { | ||
| "use strict"; | ||
|
|
||
| var Commands = require("command/Commands"), | ||
| CommandManager = require("command/CommandManager"), | ||
| Strings = require("strings"), | ||
| ProjectManager = require("project/ProjectManager"), | ||
| EditorManager = require("editor/EditorManager"); | ||
| var Commands = require("command/Commands"), | ||
| CommandManager = require("command/CommandManager"), | ||
| KeyBindingManager = require("command/KeyBindingManager"), | ||
| Strings = require("strings"), | ||
| ProjectManager = require("project/ProjectManager"), | ||
| EditorManager = require("editor/EditorManager"); | ||
|
|
||
| /** | ||
| * @const | ||
|
|
@@ -126,7 +127,99 @@ define(function (require, exports, module) { | |
| _removeDynamicFontSize(true); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * @private | ||
| * Calculates the first and last visible lines of the focused editor | ||
| * @param {!Editor} editor | ||
| * @param {!number} scrollTop | ||
| * @param {!number} editorHeight | ||
| * @return {{first: number, last: number}} | ||
| */ | ||
| function _getLinesInView(editor, scrollTop, editorHeight) { | ||
| var textHeight = editor.getTextHeight(), | ||
| scrolledTop = scrollTop / textHeight, | ||
| scrolledBottom = (scrollTop + editorHeight) / textHeight; | ||
|
|
||
| // Subtract a line from both for zero-based index. Also adjust last line | ||
| // to round inward to show a whole lines. | ||
| var firstLine = Math.ceil(scrolledTop) - 1, | ||
| lastLine = Math.floor(scrolledBottom) - 2; | ||
|
|
||
| return { first: firstLine, last: lastLine }; | ||
| } | ||
|
|
||
| /** | ||
| * @private | ||
| * Scroll the viewport one line up or down. | ||
| * @param {number} -1 to scroll one line up; 1 to scroll one line down. | ||
| */ | ||
| function _scrollLine(direction) { | ||
| var editor = EditorManager.getCurrentFullEditor(), | ||
| scrollInfo = editor._codeMirror.getScrollInfo(), | ||
| textHeight = editor.getTextHeight(), | ||
| cursorPos = editor.getCursorPos(), | ||
| hasSelecction = editor.hasSelection(), | ||
| paddingTop = editor._getLineSpaceElement().offsetTop, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that Editor should have a new public method
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding |
||
| scrollTop = scrollInfo.top < paddingTop && direction > 0 ? paddingTop : scrollInfo.top, | ||
| scrolledTop = scrollTop, | ||
| editorHeight = scrollInfo.clientHeight, | ||
| linesInView = _getLinesInView(editor, scrollTop, editorHeight); | ||
|
|
||
| // Go through all the editors and reduce the scroll top and editor height to recalculate the lines in view | ||
| var line, total; | ||
| editor.getInlineWidgets().forEach(function (inlineEditor) { | ||
| line = editor._getInlineWidgetLineNumber(inlineEditor); | ||
| total = inlineEditor.info.height / textHeight; | ||
|
|
||
| if (line < linesInView.first) { | ||
| scrollTop -= inlineEditor.info.height; | ||
| linesInView = _getLinesInView(editor, scrollTop, editorHeight); | ||
|
|
||
| } else if (line + total < linesInView.last) { | ||
| editorHeight -= inlineEditor.info.height; | ||
| linesInView = _getLinesInView(editor, scrollTop, editorHeight); | ||
| } | ||
| }); | ||
|
|
||
| // If there is no selection move the cursor so that is always visible | ||
| if (!hasSelecction) { | ||
| // Move the cursor to the first visible line | ||
| if (direction > 0 && cursorPos.line < linesInView.first) { | ||
| editor.setCursorPos({line: linesInView.first + 1, ch: cursorPos.ch}); | ||
|
|
||
| // Move the cursor to the last visible line | ||
| } else if (direction < 0 && cursorPos.line > linesInView.last) { | ||
| editor.setCursorPos({line: linesInView.last - 1, ch: cursorPos.ch}); | ||
|
|
||
| // Move the cursor up or down using CodeMirror function | ||
| } else if ((direction > 0 && cursorPos.line === linesInView.first) || | ||
| (direction < 0 && cursorPos.line === linesInView.last)) { | ||
| editor._codeMirror.moveV(direction, "line"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of code seems to do (for the ===) the same as the 2 blocks above (for the > and < cases). Can't you change the first 2 blocks to be >= and <= and get rid of this block? Or maybe use this block instead of the 2 blocks above, because I think the moveV() function handles variable-pitch fonts which Brackets might want some day.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could, but this block handles the goal column. Set cursor sets it to null internally, so it doesn't move from column 30 to 20 and then back to column 30, instead it would stay at column 20. Unfortunately the goal column is only kept when moving with the cursor inside the visible range, and lost on the first movement, but restored to a new one if not. I added this after a suggestion from @njx at #2343 (comment).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah. Please add a comment to explain this. |
||
| } | ||
| } | ||
|
|
||
| // Scroll the editor | ||
| editor.setScrollPos(scrollInfo.left, scrolledTop + (textHeight * direction)); | ||
| } | ||
|
|
||
|
|
||
| function _handleScrollLineUp() { | ||
| _scrollLine(-1); | ||
| } | ||
|
|
||
| function _handleScrollLineDown() { | ||
| _scrollLine(1); | ||
| } | ||
|
|
||
|
|
||
| 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_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); | ||
|
|
||
| // There are no menu items, so bind commands directly | ||
| KeyBindingManager.addBinding(Commands.VIEW_SCROLL_LINE_UP); | ||
| KeyBindingManager.addBinding(Commands.VIEW_SCROLL_LINE_DOWN); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please document what these magic numbers are.