2014-07-08 07:02:26 -04:00
define [
" ide/editor/Document "
" ide/editor/directives/aceEditor "
" ide/editor/controllers/SavingNotificationController "
] , (Document) ->
class EditorManager
constructor: (@ide, @$scope) ->
@$scope.editor = {
sharejs_doc: null
open_doc_id: null
2016-10-20 11:03:19 -04:00
open_doc_name: null
2014-07-08 07:02:26 -04:00
opening: true
}
@ $scope . $on " entity:selected " , (event, entity) =>
2016-10-19 05:43:53 -04:00
if ( @ $scope . ui . view != " history " and entity . type == " doc " )
2014-07-08 07:02:26 -04:00
@ openDoc ( entity )
2015-02-03 08:51:56 -05:00
@ $scope . $on " entity:deleted " , (event, entity) =>
if @ $scope . editor . open_doc_id == entity . id
return if ! @ $scope . project . rootDoc_id
doc = @ ide . fileTreeManager . findEntityById ( @ $scope . project . rootDoc_id )
return if ! doc ?
@ openDoc ( doc )
2014-07-08 07:02:26 -04:00
initialized = false
@ $scope . $on " file-tree:initialized " , () =>
if ! initialized
initialized = true
@ autoOpenDoc ( )
2015-04-17 06:22:26 -04:00
@ $scope . $on " flush-changes " , () =>
Document . flushAll ( )
2014-07-08 07:02:26 -04:00
autoOpenDoc: () ->
open_doc_id =
2015-02-12 06:32:27 -05:00
@ ide . localStorage ( " doc.open_id. #{ @ $scope . project_id } " ) or
2014-07-08 07:02:26 -04:00
@ $scope . project . rootDoc_id
return if ! open_doc_id ?
doc = @ ide . fileTreeManager . findEntityById ( open_doc_id )
return if ! doc ?
@ openDoc ( doc )
2016-11-17 10:55:18 -05:00
openDocId: (doc_id, options = {}) ->
doc = @ ide . fileTreeManager . findEntityById ( doc_id )
return if ! doc ?
@ openDoc ( doc , options )
2014-07-08 07:02:26 -04:00
openDoc: (doc, options = {}) ->
2016-05-27 09:14:08 -04:00
sl_console . log " [openDoc] Opening #{ doc . id } "
2014-07-08 07:02:26 -04:00
@$scope.ui.view = " editor "
done = () =>
if options . gotoLine ?
2016-08-11 11:46:12 -04:00
# allow Ace to display document before moving, delay until next tick
2016-08-16 05:59:27 -04:00
# added delay to make this happen later that gotoStoredPosition in
# CursorPositionManager
2016-08-11 11:46:12 -04:00
setTimeout () =>
@ $scope . $broadcast " editor:gotoLine " , options . gotoLine , options . gotoColumn
2016-11-22 11:34:39 -05:00
, 0
else if options . gotoOffset ?
setTimeout () =>
@ $scope . $broadcast " editor:gotoOffset " , options . gotoOffset
, 0
2016-08-11 11:46:12 -04:00
2014-07-08 07:02:26 -04:00
if doc . id == @ $scope . editor . open_doc_id and ! options . forceReopen
@ $scope . $apply () =>
done ( )
return
@$scope.editor.open_doc_id = doc . id
2016-10-20 11:03:19 -04:00
@$scope.editor.open_doc_name = doc . name
2014-07-08 07:02:26 -04:00
2015-02-12 06:32:27 -05:00
@ ide . localStorage " doc.open_id. #{ @ $scope . project_id } " , doc . id
2014-07-08 07:02:26 -04:00
@ ide . fileTreeManager . selectEntity ( doc )
@$scope.editor.opening = true
@ _openNewDocument doc , (error, sharejs_doc) =>
if error ?
2014-07-24 11:07:43 -04:00
@ ide . showGenericMessageModal (
" Error opening document "
" Sorry, something went wrong opening this document. Please try again. "
)
2014-07-08 07:02:26 -04:00
return
@ $scope . $broadcast " doc:opened "
@ $scope . $apply () =>
@$scope.editor.opening = false
@$scope.editor.sharejs_doc = sharejs_doc
done ( )
_openNewDocument: ( doc , callback = (error, sharejs_doc) -> ) ->
2016-05-27 09:14:08 -04:00
sl_console . log " [_openNewDocument] Opening... "
2014-07-08 07:02:26 -04:00
current_sharejs_doc = @ $scope . editor . sharejs_doc
if current_sharejs_doc ?
2016-05-27 09:14:08 -04:00
sl_console . log " [_openNewDocument] Leaving existing open doc... "
2014-07-08 07:02:26 -04:00
current_sharejs_doc . leaveAndCleanUp ( )
@ _unbindFromDocumentEvents ( current_sharejs_doc )
new_sharejs_doc = Document . getDocument @ ide , doc . id
new_sharejs_doc . join (error) =>
return callback ( error ) if error ?
@ _bindToDocumentEvents ( doc , new_sharejs_doc )
callback null , new_sharejs_doc
2015-12-04 03:43:34 -05:00
2014-07-08 07:02:26 -04:00
_bindToDocumentEvents: (doc, sharejs_doc) ->
2015-11-06 07:51:43 -05:00
sharejs_doc . on " error " , (error, meta) =>
if error ? . message ? . match " maxDocLength "
@ ide . showGenericMessageModal (
" Document Too Long "
" Sorry, this file is too long to be edited manually. Please upload it directly. "
)
else
@ ide . socket . disconnect ( )
@ ide . reportError ( error , meta )
@ ide . showGenericMessageModal (
" Out of sync "
2016-11-04 06:25:08 -04:00
" Sorry, this file has gone out of sync and we need to do a full refresh. <br> <a href= ' /learn/Kb/Editor_out_of_sync_problems ' >Please see this help guide for more information</a> "
2015-11-06 07:51:43 -05:00
)
2014-07-08 07:02:26 -04:00
@ openDoc ( doc , forceReopen: true )
2014-10-15 11:37:14 -04:00
sharejs_doc . on " externalUpdate " , (update) =>
return if @ _ignoreExternalUpdates
2014-07-08 07:02:26 -04:00
@ ide . showGenericMessageModal (
" Document Updated Externally "
" This document was just updated externally. Any recent changes you have made may have been overwritten. To see previous versions please look in the history. "
)
_unbindFromDocumentEvents: (document) ->
document . off ( )
getCurrentDocValue: () ->
@ $scope . editor . sharejs_doc ? . getSnapshot ( )
getCurrentDocId: () ->
@ $scope . editor . open_doc_id
2014-10-15 11:37:14 -04:00
startIgnoringExternalUpdates: () ->
@_ignoreExternalUpdates = true
stopIgnoringExternalUpdates: () ->
@_ignoreExternalUpdates = false