Trigger events instead of calling cursor manager

This improves readability and prevents race conditions in compat
between Ace/CM
This commit is contained in:
Alasdair Smith 2018-07-06 15:37:51 +01:00
parent da77c06774
commit 5806101bd0
2 changed files with 22 additions and 15 deletions

View file

@ -308,13 +308,13 @@ define [
editor.renderer.updateFontSize() editor.renderer.updateFontSize()
scope.$watch "sharejsDoc", (sharejs_doc, old_sharejs_doc) -> scope.$watch "sharejsDoc", (sharejs_doc, old_sharejs_doc) ->
cursorPositionManager.onBeforeSessionChange(!!old_sharejs_doc)
if old_sharejs_doc? if old_sharejs_doc?
scope.$broadcast('beforeChangeDocument')
detachFromAce(old_sharejs_doc) detachFromAce(old_sharejs_doc)
if sharejs_doc? if sharejs_doc?
attachToAce(sharejs_doc) attachToAce(sharejs_doc)
if sharejs_doc? and old_sharejs_doc?
scope.$broadcast('afterChangeDocument')
scope.$watch "text", (text) -> scope.$watch "text", (text) ->
if text? if text?
@ -391,7 +391,6 @@ define [
cursorPositionManager.onUnload(editor.getSession()) cursorPositionManager.onUnload(editor.getSession())
initCursorPosition = () -> initCursorPosition = () ->
cursorPositionManager.init()
editor.on 'changeSession', onSessionChangeForCursorPosition editor.on 'changeSession', onSessionChangeForCursorPosition
onSessionChangeForCursorPosition({ session: editor.getSession() }) # Force initial setup onSessionChangeForCursorPosition({ session: editor.getSession() }) # Force initial setup
$(window).on "unload", onUnloadForCursorPosition $(window).on "unload", onUnloadForCursorPosition
@ -400,6 +399,14 @@ define [
editor.off 'changeSession', onSessionChangeForCursorPosition editor.off 'changeSession', onSessionChangeForCursorPosition
$(window).off "unload", onUnloadForCursorPosition $(window).off "unload", onUnloadForCursorPosition
initCursorPosition()
# Trigger the event once *only* - this is called after Ace is connected
# to the ShareJs instance but this event should only be triggered the
# first time the editor is opened. Not every time the docs opened
triggerEditorInitEvent = _.once () ->
scope.$broadcast('editorInit')
attachToAce = (sharejs_doc) -> attachToAce = (sharejs_doc) ->
lines = sharejs_doc.getSnapshot().split("\n") lines = sharejs_doc.getSnapshot().split("\n")
session = editor.getSession() session = editor.getSession()
@ -445,8 +452,8 @@ define [
editor.initing = false editor.initing = false
# now ready to edit document # now ready to edit document
editor.setReadOnly(scope.readOnly) # respect the readOnly setting, normally false editor.setReadOnly(scope.readOnly) # respect the readOnly setting, normally false
triggerEditorInitEvent()
initSpellCheck() initSpellCheck()
initCursorPosition()
resetScrollMargins() resetScrollMargins()

View file

@ -1,6 +1,14 @@
define [], () -> define [], () ->
class CursorPositionManager class CursorPositionManager
constructor: (@$scope, @adapter, @localStorage) -> constructor: (@$scope, @adapter, @localStorage) ->
@$scope.$on 'editorInit', @jumpToPositionInNewDoc
@$scope.$on 'beforeChangeDocument', () =>
@storeCursorPosition()
@storeFirstVisibleLine()
@$scope.$on 'afterChangeDocument', @jumpToPositionInNewDoc
@$scope.$on "#{@$scope.name}:gotoLine", (e, line, column) => @$scope.$on "#{@$scope.name}:gotoLine", (e, line, column) =>
if line? if line?
setTimeout () => setTimeout () =>
@ -16,16 +24,8 @@ define [], () ->
@$scope.$on "#{@$scope.name}:clearSelection", (e) => @$scope.$on "#{@$scope.name}:clearSelection", (e) =>
@adapter.clearSelection() @adapter.clearSelection()
init: () -> jumpToPositionInNewDoc: () =>
@emitCursorUpdateEvent()
onBeforeSessionChange: (hasPrevSession = false) =>
if hasPrevSession
@storeCursorPosition()
@storeFirstVisibleLine()
@doc_id = @$scope.sharejsDoc?.doc_id @doc_id = @$scope.sharejsDoc?.doc_id
setTimeout () => setTimeout () =>
@gotoStoredPosition() @gotoStoredPosition()
, 0 , 0