Store first visible line instead of scrollTop

scrollTop is affected by changing viewport size and switching between
Ace and CM
This commit is contained in:
Alasdair Smith 2018-07-05 17:24:45 +01:00
parent 6c7e942470
commit ba9fa9a0be
2 changed files with 10 additions and 13 deletions

View file

@ -7,19 +7,16 @@ define [
getCursor: () -> getCursor: () ->
@editor.getCursorPosition() @editor.getCursorPosition()
getCursorForSession: (session) -> getEditorScrollPosition: () ->
session.selection.getCursor() @editor.getFirstVisibleRow()
getScrollTopForSession: (session) ->
session.getScrollTop()
setCursor: (pos) -> setCursor: (pos) ->
pos = pos.cursorPosition or { row: 0, column: 0 } pos = pos.cursorPosition or { row: 0, column: 0 }
@editor.moveCursorToPosition(pos) @editor.moveCursorToPosition(pos)
setScrollTop: (pos) -> setEditorScrollPosition: (pos) ->
pos = pos.scrollTop or 0 pos = pos.firstVisibleLine or 0
@editor.getSession().setScrollTop(pos) @editor.scrollToLine(pos)
clearSelection: () -> clearSelection: () ->
@editor.selection.clearSelection() @editor.selection.clearSelection()

View file

@ -22,7 +22,7 @@ define [], () ->
onSessionChange: (e) => onSessionChange: (e) =>
if e.oldSession? if e.oldSession?
@storeCursorPosition(e.oldSession) @storeCursorPosition(e.oldSession)
@storeScrollTopPosition(e.oldSession) @storeFirstVisibleLine()
@doc_id = @$scope.sharejsDoc?.doc_id @doc_id = @$scope.sharejsDoc?.doc_id
@ -32,15 +32,15 @@ define [], () ->
onUnload: (session) => onUnload: (session) =>
@storeCursorPosition(session) @storeCursorPosition(session)
@storeScrollTopPosition(session) @storeFirstVisibleLine()
onCursorChange: () => onCursorChange: () =>
@emitCursorUpdateEvent() @emitCursorUpdateEvent()
storeScrollTopPosition: (session) -> storeFirstVisibleLine: () ->
if @doc_id? if @doc_id?
docPosition = @localStorage("doc.position.#{@doc_id}") || {} docPosition = @localStorage("doc.position.#{@doc_id}") || {}
docPosition.scrollTop = @adapter.getScrollTopForSession(session) docPosition.firstVisibleLine = @adapter.getEditorScrollPosition()
@localStorage("doc.position.#{@doc_id}", docPosition) @localStorage("doc.position.#{@doc_id}", docPosition)
storeCursorPosition: (session) -> storeCursorPosition: (session) ->
@ -57,4 +57,4 @@ define [], () ->
return if !@doc_id? return if !@doc_id?
pos = @localStorage("doc.position.#{@doc_id}") || {} pos = @localStorage("doc.position.#{@doc_id}") || {}
@adapter.setCursor(pos) @adapter.setCursor(pos)
@adapter.setScrollTop(pos) @adapter.setEditorScrollPosition(pos)