37
37
define ( function ( require , exports , module ) {
38
38
"use strict" ;
39
39
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
+
49
50
50
51
/**
51
52
* @const
@@ -95,17 +96,14 @@ define(function (require, exports, module) {
95
96
/**
96
97
* @private
97
98
* 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.
99
99
* @param {string } fontSizeStyle A string with the font size and the size unit
100
100
* @param {string } lineHeightStyle A string with the line height and a the size unit
101
101
*/
102
102
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 ( ) ;
109
107
110
108
// It's necessary to inject a new rule to address all editors.
111
109
_removeDynamicFontSize ( ) ;
@@ -117,17 +115,17 @@ define(function (require, exports, module) {
117
115
118
116
editor . refreshAll ( ) ;
119
117
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
-
128
118
// Scroll the document back to its original position, but not on the first load since the position
129
119
// was saved with the new height and already been restored.
130
120
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
+
131
129
editor . setScrollPos ( scrollPosX , scrollPosY ) ;
132
130
}
133
131
}
@@ -241,19 +239,17 @@ define(function (require, exports, module) {
241
239
* @param {number } textHeight
242
240
* @param {number } scrollTop
243
241
* @param {number } editorHeight
244
- * @param {number } viewportFrom
245
242
* @return {{first: number, last: number} }
246
243
*/
247
- function _getLinesInView ( textHeight , scrollTop , editorHeight , viewportFrom ) {
244
+ function _getLinesInView ( textHeight , scrollTop , editorHeight ) {
248
245
var scrolledTop = scrollTop / textHeight ,
249
246
scrolledBottom = ( scrollTop + editorHeight ) / textHeight ;
250
247
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 ;
255
251
256
- return { first : viewportFrom + firstLine , last : viewportFrom + lastLine } ;
252
+ return { first : firstLine , last : lastLine } ;
257
253
}
258
254
259
255
/**
@@ -268,35 +264,26 @@ define(function (require, exports, module) {
268
264
hasSelecction = editor . hasSelection ( ) ,
269
265
inlineEditors = editor . getInlineWidgets ( ) ,
270
266
scrollInfo = editor . _codeMirror . getScrollInfo ( ) ,
271
- viewportFrom = editor . _codeMirror . getViewport ( ) . from ,
272
267
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 ;
284
272
285
273
// 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 ;
287
275
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" ) ;
290
278
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 ) ;
300
287
}
301
288
} ) ;
302
289
@@ -317,15 +304,9 @@ define(function (require, exports, module) {
317
304
}
318
305
}
319
306
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 ) ;
329
310
}
330
311
331
312
/** Scrolls one line up */
0 commit comments