mirror of
https://github.com/overleaf/overleaf.git
synced 2025-04-07 21:05:23 +00:00
Jump to position based on offset since we don't know doc lines for unopen docs
This commit is contained in:
parent
decaaab7ea
commit
33f6c0dd1b
5 changed files with 57 additions and 27 deletions
29
services/web/public/coffee/ide/editor/AceShareJsCodec.coffee
Normal file
29
services/web/public/coffee/ide/editor/AceShareJsCodec.coffee
Normal file
|
@ -0,0 +1,29 @@
|
|||
define [], () ->
|
||||
AceShareJsCodec =
|
||||
aceRangeToShareJs: (range, lines) ->
|
||||
offset = 0
|
||||
for line, i in lines
|
||||
offset += if i < range.row
|
||||
line.length
|
||||
else
|
||||
range.column
|
||||
offset += range.row # Include newlines
|
||||
return offset
|
||||
|
||||
aceChangeToShareJs: (delta, lines) ->
|
||||
offset = AceShareJsCodec.aceRangeToShareJs(delta.start, lines)
|
||||
|
||||
text = delta.lines.join('\n')
|
||||
switch delta.action
|
||||
when 'insert'
|
||||
return { i: text, p: offset }
|
||||
when 'remove'
|
||||
return { d: text, p: offset }
|
||||
else throw new Error "unknown action: #{delta.action}"
|
||||
|
||||
shareJsOffsetToAcePosition: (offset, lines) ->
|
||||
row = 0
|
||||
for line, row in lines
|
||||
break if offset <= line.length
|
||||
offset -= lines[row].length + 1 # + 1 for newline char
|
||||
return {row:row, column:offset}
|
|
@ -57,7 +57,12 @@ define [
|
|||
# CursorPositionManager
|
||||
setTimeout () =>
|
||||
@$scope.$broadcast "editor:gotoLine", options.gotoLine, options.gotoColumn
|
||||
,0
|
||||
, 0
|
||||
else if options.gotoOffset?
|
||||
setTimeout () =>
|
||||
@$scope.$broadcast "editor:gotoOffset", options.gotoOffset
|
||||
, 0
|
||||
|
||||
|
||||
if doc.id == @$scope.editor.open_doc_id and !options.forceReopen
|
||||
@$scope.$apply () =>
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
define [], () ->
|
||||
define [
|
||||
"ide/editor/AceShareJsCodec"
|
||||
], (AceShareJsCodec) ->
|
||||
class CursorPositionManager
|
||||
constructor: (@$scope, @editor, @element, @localStorage) ->
|
||||
|
||||
|
@ -23,11 +25,17 @@ define [], () ->
|
|||
@storeCursorPosition(@editor.getSession())
|
||||
@storeScrollTopPosition(@editor.getSession())
|
||||
|
||||
@$scope.$on "#{@$scope.name}:gotoLine", (editor, line, column) =>
|
||||
@$scope.$on "#{@$scope.name}:gotoLine", (e, line, column) =>
|
||||
if line?
|
||||
setTimeout () =>
|
||||
@gotoLine(line, column)
|
||||
, 10 # Hack: Must happen after @gotoStoredPosition
|
||||
|
||||
@$scope.$on "#{@$scope.name}:gotoOffset", (e, offset) =>
|
||||
if offset?
|
||||
setTimeout () =>
|
||||
@gotoOffset(offset)
|
||||
, 10 # Hack: Must happen after @gotoStoredPosition
|
||||
|
||||
storeScrollTopPosition: (session) ->
|
||||
if @doc_id?
|
||||
|
@ -57,3 +65,8 @@ define [], () ->
|
|||
@editor.gotoLine(line, column)
|
||||
@editor.scrollToLine(line,true,true) # centre and animate
|
||||
@editor.focus()
|
||||
|
||||
gotoOffset: (offset) ->
|
||||
lines = @editor.getSession().getDocument().getAllLines()
|
||||
position = AceShareJsCodec.shareJsOffsetToAcePosition(offset, lines)
|
||||
@gotoLine(position.row + 1, position.column)
|
|
@ -2,7 +2,8 @@ define [
|
|||
"ace/ace"
|
||||
"utils/EventEmitter"
|
||||
"ide/colors/ColorManager"
|
||||
], (_, EventEmitter, ColorManager) ->
|
||||
"ide/editor/AceShareJsCodec"
|
||||
], (_, EventEmitter, ColorManager, AceShareJsCodec) ->
|
||||
class TrackChangesManager
|
||||
Range = ace.require("ace/range").Range
|
||||
|
||||
|
@ -377,32 +378,15 @@ define [
|
|||
|
||||
_aceRangeToShareJs: (range) ->
|
||||
lines = @editor.getSession().getDocument().getLines 0, range.row
|
||||
offset = 0
|
||||
for line, i in lines
|
||||
offset += if i < range.row
|
||||
line.length
|
||||
else
|
||||
range.column
|
||||
offset += range.row # Include newlines
|
||||
return AceShareJsCodec.aceRangeToShareJs(range, lines)
|
||||
|
||||
_aceChangeToShareJs: (delta) ->
|
||||
offset = @_aceRangeToShareJs(delta.start)
|
||||
|
||||
text = delta.lines.join('\n')
|
||||
switch delta.action
|
||||
when 'insert'
|
||||
return { i: text, p: offset }
|
||||
when 'remove'
|
||||
return { d: text, p: offset }
|
||||
else throw new Error "unknown action: #{delta.action}"
|
||||
lines = @editor.getSession().getDocument().getLines 0, range.rowf
|
||||
return AceShareJsCodec.aceChangeToShareJs(delta, lines)
|
||||
|
||||
_shareJsOffsetToAcePosition: (offset) ->
|
||||
lines = @editor.getSession().getDocument().getAllLines()
|
||||
row = 0
|
||||
for line, row in lines
|
||||
break if offset <= line.length
|
||||
offset -= lines[row].length + 1 # + 1 for newline char
|
||||
return {row:row, column:offset}
|
||||
return AceShareJsCodec.shareJsOffsetToAcePosition(offset, lines)
|
||||
|
||||
_onChangesMoved: (changes) ->
|
||||
# TODO: PERFORMANCE: Only run through the Ace lines once, and calculate all
|
||||
|
|
|
@ -331,8 +331,7 @@ define [
|
|||
$scope.reviewPanel.subView = subView
|
||||
|
||||
$scope.gotoEntry = (doc_id, entry) ->
|
||||
console.log "Going to entry", entry.docPos
|
||||
ide.editorManager.openDocId(doc_id, { gotoLine: entry.docPos.row + 1, gotoColumn: entry.docPos.column })
|
||||
ide.editorManager.openDocId(doc_id, { gotoOffset: entry.offset })
|
||||
|
||||
DOC_ID_NAMES = {}
|
||||
$scope.getFileName = (doc_id) ->
|
||||
|
|
Loading…
Add table
Reference in a new issue