Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 34ff3d8

Browse files
committed
Merge pull request #7118 from adobe/tom/remove-viewport-hack
Remove the use of the viewport hack
2 parents 474a8a0 + 8666dfd commit 34ff3d8

File tree

1 file changed

+45
-64
lines changed

1 file changed

+45
-64
lines changed

src/view/ViewCommandHandlers.js

Lines changed: 45 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@
3737
define(function (require, exports, module) {
3838
"use strict";
3939

40-
var Commands = require("command/Commands"),
41-
CommandManager = require("command/CommandManager"),
42-
KeyBindingManager = require("command/KeyBindingManager"),
43-
Strings = require("strings"),
44-
ProjectManager = require("project/ProjectManager"),
45-
EditorManager = require("editor/EditorManager"),
46-
PreferencesManager = require("preferences/PreferencesManager"),
47-
DocumentManager = require("document/DocumentManager"),
48-
AppInit = require("utils/AppInit");
40+
var Commands = require("command/Commands"),
41+
CommandManager = require("command/CommandManager"),
42+
KeyBindingManager = require("command/KeyBindingManager"),
43+
Strings = require("strings"),
44+
ProjectManager = require("project/ProjectManager"),
45+
EditorManager = require("editor/EditorManager"),
46+
PreferencesManager = require("preferences/PreferencesManager"),
47+
DocumentManager = require("document/DocumentManager"),
48+
AppInit = require("utils/AppInit");
49+
4950

5051
/**
5152
* @const
@@ -95,17 +96,14 @@ define(function (require, exports, module) {
9596
/**
9697
* @private
9798
* Sets the font size and restores the scroll position as best as possible.
98-
* TODO: Remove the viewportTop hack and direclty use scrollPos.y once #3115 is fixed.
9999
* @param {string} fontSizeStyle A string with the font size and the size unit
100100
* @param {string} lineHeightStyle A string with the line height and a the size unit
101101
*/
102102
function _setSizeAndRestoreScroll(fontSizeStyle, lineHeightStyle) {
103-
var editor = EditorManager.getCurrentFullEditor(),
104-
oldWidth = editor._codeMirror.defaultCharWidth(),
105-
oldHeight = editor.getTextHeight(),
106-
scrollPos = editor.getScrollPos(),
107-
viewportTop = $(".CodeMirror-lines", editor.getRootElement()).parent().position().top,
108-
scrollTop = scrollPos.y - viewportTop;
103+
var editor = EditorManager.getCurrentFullEditor(),
104+
oldWidth = editor._codeMirror.defaultCharWidth(),
105+
oldHeight = editor.getTextHeight(),
106+
scrollPos = editor.getScrollPos();
109107

110108
// It's necessary to inject a new rule to address all editors.
111109
_removeDynamicFontSize();
@@ -117,17 +115,17 @@ define(function (require, exports, module) {
117115

118116
editor.refreshAll();
119117

120-
// Calculate the new scroll based on the old font sizes and scroll position
121-
var newWidth = editor._codeMirror.defaultCharWidth(),
122-
newHeight = editor.getTextHeight(),
123-
deltaX = scrollPos.x / oldWidth,
124-
deltaY = scrollTop / oldHeight,
125-
scrollPosX = scrollPos.x + Math.round(deltaX * (newWidth - oldWidth)),
126-
scrollPosY = scrollPos.y + Math.round(deltaY * (newHeight - oldHeight));
127-
128118
// Scroll the document back to its original position, but not on the first load since the position
129119
// was saved with the new height and already been restored.
130120
if (_fontSizePrefsLoaded) {
121+
// Calculate the new scroll based on the old font sizes and scroll position
122+
var newWidth = editor._codeMirror.defaultCharWidth(),
123+
newHeight = editor.getTextHeight(),
124+
deltaX = scrollPos.x / oldWidth,
125+
deltaY = scrollPos.y / oldHeight,
126+
scrollPosX = scrollPos.x + Math.round(deltaX * (newWidth - oldWidth)),
127+
scrollPosY = scrollPos.y + Math.round(deltaY * (newHeight - oldHeight));
128+
131129
editor.setScrollPos(scrollPosX, scrollPosY);
132130
}
133131
}
@@ -241,19 +239,17 @@ define(function (require, exports, module) {
241239
* @param {number} textHeight
242240
* @param {number} scrollTop
243241
* @param {number} editorHeight
244-
* @param {number} viewportFrom
245242
* @return {{first: number, last: number}}
246243
*/
247-
function _getLinesInView(textHeight, scrollTop, editorHeight, viewportFrom) {
244+
function _getLinesInView(textHeight, scrollTop, editorHeight) {
248245
var scrolledTop = scrollTop / textHeight,
249246
scrolledBottom = (scrollTop + editorHeight) / textHeight;
250247

251-
// Subtract a line from both for zero-based index. Also adjust last line
252-
// to round inward to show a whole lines.
253-
var firstLine = Math.ceil(scrolledTop) - 1,
254-
lastLine = Math.floor(scrolledBottom) - 2;
248+
// Adjust last line to round inward to show a whole lines.
249+
var firstLine = Math.ceil(scrolledTop),
250+
lastLine = Math.floor(scrolledBottom) - 1;
255251

256-
return { first: viewportFrom + firstLine, last: viewportFrom + lastLine };
252+
return { first: firstLine, last: lastLine };
257253
}
258254

259255
/**
@@ -268,35 +264,26 @@ define(function (require, exports, module) {
268264
hasSelecction = editor.hasSelection(),
269265
inlineEditors = editor.getInlineWidgets(),
270266
scrollInfo = editor._codeMirror.getScrollInfo(),
271-
viewportFrom = editor._codeMirror.getViewport().from,
272267
paddingTop = editor._getLineSpaceElement().offsetTop,
273-
viewportTop = $(".CodeMirror-lines", editor.getRootElement()).parent().position().top,
274-
editorHeight = scrollInfo.clientHeight;
275-
276-
// To make it snap better to lines and dont cover the cursor when the scroll is lower than the top padding,
277-
// we make it start direclty from the top padding
278-
var scrolledTop = scrollInfo.top < paddingTop && direction > 0 ? paddingTop : scrollInfo.top;
279-
280-
// CodeMirror has a strange behaviour when it comes to calculate the height of the not rendered lines,
281-
// so instead, we calculate the amount of hidden rendered lines at top and add it to the first rendered line.
282-
var scrollTop = scrolledTop - viewportTop,
283-
linesInView = _getLinesInView(textHeight, scrollTop, editorHeight, viewportFrom);
268+
editorHeight = scrollInfo.clientHeight,
269+
scrollTop = scrollInfo.top - paddingTop,
270+
linesInView = _getLinesInView(textHeight, scrollTop, editorHeight),
271+
inlinesHeight = 0;
284272

285273
// Go through all the editors and reduce the scroll top and editor height to recalculate the lines in view
286-
var line, total;
274+
var line, coords;
287275
inlineEditors.forEach(function (inlineEditor) {
288-
line = editor._getInlineWidgetLineNumber(inlineEditor);
289-
total = inlineEditor.info.height / textHeight;
276+
line = editor._getInlineWidgetLineNumber(inlineEditor);
277+
coords = editor._codeMirror.charCoords({line: line, ch: 0}, "local");
290278

291-
if (line >= viewportFrom) {
292-
if (line < linesInView.first) {
293-
scrollTop -= inlineEditor.info.height;
294-
linesInView = _getLinesInView(textHeight, scrollTop, editorHeight, viewportFrom);
295-
296-
} else if (line + total < linesInView.last) {
297-
editorHeight -= inlineEditor.info.height;
298-
linesInView = _getLinesInView(textHeight, scrollTop, editorHeight, viewportFrom);
299-
}
279+
if (coords.top < scrollInfo.top) {
280+
scrollTop -= inlineEditor.info.height;
281+
linesInView = _getLinesInView(textHeight, scrollTop, editorHeight);
282+
inlinesHeight += inlineEditor.info.height;
283+
284+
} else if (coords.top + inlineEditor.info.height < scrollInfo.top + editorHeight) {
285+
editorHeight -= inlineEditor.info.height;
286+
linesInView = _getLinesInView(textHeight, scrollTop, editorHeight);
300287
}
301288
});
302289

@@ -317,15 +304,9 @@ define(function (require, exports, module) {
317304
}
318305
}
319306

320-
// If there are inline editors just add/remove 1 line to the scroll top.
321-
if (inlineEditors.length) {
322-
editor.setScrollPos(scrollInfo.left, scrolledTop + (textHeight * direction));
323-
324-
// If there arent, we can make it snap to the line.
325-
} else {
326-
var lines = linesInView.first - viewportFrom + direction + 1;
327-
editor.setScrollPos(scrollInfo.left, viewportTop + (textHeight * lines));
328-
}
307+
// Scroll and make it snap to lines
308+
var lines = linesInView.first + direction;
309+
editor.setScrollPos(scrollInfo.left, (textHeight * lines) + paddingTop + inlinesHeight);
329310
}
330311

331312
/** Scrolls one line up */

0 commit comments

Comments
 (0)