CursorManager.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. define () ->
  2. class CursorManager
  3. UPDATE_INTERVAL: 500
  4. constructor: (@ide) ->
  5. @clients = {}
  6. @ide.socket.on "clientTracking.clientUpdated", (cursorUpdate) => @onRemoteClientUpdate(cursorUpdate)
  7. @ide.socket.on "clientTracking.clientDisconnected", (client_id) => @onRemoteClientDisconnect(client_id)
  8. @ide.editor.on "change:doc", (session) =>
  9. @bindToAceSession(session)
  10. @ide.editor.on "mousemove", (e) =>
  11. @mousePosition = e.position
  12. @updateVisibleNames()
  13. bindToAceSession: (session) ->
  14. @clients = {}
  15. @ide.editor.aceEditor.on "changeSelection", => @onLocalCursorUpdate()
  16. onLocalCursorUpdate: () ->
  17. if !@cursorUpdateTimeout?
  18. @cursorUpdateTimeout = setTimeout (=>
  19. @_sendLocalCursorUpdate()
  20. delete @cursorUpdateTimeout
  21. ), @UPDATE_INTERVAL
  22. _sendLocalCursorUpdate: () ->
  23. cursor = @ide.editor.getCursorPosition()
  24. if !@currentCursorPosition? or not (cursor.row == @currentCursorPosition.row and cursor.column == @currentCursorPosition.column)
  25. @currentCursorPosition = cursor
  26. @ide.socket.emit "clientTracking.updatePosition", {
  27. row: cursor.row
  28. column: cursor.column
  29. doc_id: @ide.editor.getCurrentDocId()
  30. }
  31. onRemoteClientUpdate: (clientData) ->
  32. if clientData.id != ide.socket.socket.sessionid
  33. client = @clients[clientData.id] ||= {}
  34. client.row = clientData.row
  35. client.column = clientData.column
  36. client.name = clientData.name
  37. client.doc_id = clientData.doc_id
  38. @redrawCursors()
  39. onRemoteClientDisconnect: (client_id) ->
  40. @removeCursor(client_id)
  41. delete @clients[client_id]
  42. removeCursor: (client_id) ->
  43. client = @clients[client_id]
  44. return if !client?
  45. @ide.editor.removeMarker(client.cursorMarkerId)
  46. delete client.cursorMarkerId
  47. redrawCursors: () ->
  48. for clientId, clientData of @clients
  49. do (clientId, clientData) =>
  50. if clientData.cursorMarkerId?
  51. @removeCursor(clientId)
  52. if clientData.doc_id == @ide.editor.getCurrentDocId()
  53. colorId = @getColorIdFromName(clientData.name)
  54. clientData.cursorMarkerId = @ide.editor.addMarker {
  55. row: clientData.row
  56. column: clientData.column
  57. length: 1
  58. }, "sharelatex-remote-cursor", (html, range, left, top, config) ->
  59. div = """
  60. <div
  61. id='cursor-#{clientId}'
  62. class='sharelatex-remote-cursor custom ace_start sharelatex-remote-cursor-#{colorId}'
  63. style='height: #{config.lineHeight}px; top:#{top}px; left:#{left}px;'
  64. >
  65. <div class="nubbin" style="bottom: #{config.lineHeight - 2}px"></div>
  66. <div class="name" style="display: none; bottom: #{config.lineHeight - 2}px">#{$('<div/>').text(clientData.name).html()}</div>
  67. </div>
  68. """
  69. html.push div
  70. , true
  71. setTimeout =>
  72. @updateVisibleNames()
  73. , 0
  74. updateVisibleNames: () ->
  75. for clientId, clientData of @clients
  76. if @mousePosition? and clientData.row == @mousePosition.row and clientData.column == @mousePosition.column
  77. $("#cursor-#{clientId}").find(".name").show()
  78. $("#cursor-#{clientId}").find(".nubbin").hide()
  79. else
  80. $("#cursor-#{clientId}").find(".name").hide()
  81. $("#cursor-#{clientId}").find(".nubbin").show()
  82. getColorIdFromName: (name) ->
  83. @currentColorId ||= 0
  84. @colorIds ||= {}
  85. if !@colorIds[name]?
  86. @colorIds[name] = @currentColorId
  87. @currentColorId++
  88. return @colorIds[name]