2020-06-23 04:45:38 -04:00
import _ from 'lodash'
2018-11-05 05:06:39 -05:00
/ * e s l i n t - d i s a b l e
camelcase ,
handle - callback - err ,
max - len ,
no - return - assign ,
* /
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/ *
* decaffeinate suggestions :
* DS102 : Remove unnecessary code created because of implicit returns
* DS206 : Consider reworking classes to avoid initClass
* DS207 : Consider shorter variations of null checks
* Full docs : https : //github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
* /
2020-05-19 05:02:56 -04:00
import Document from './Document'
import './components/spellMenu'
import './directives/aceEditor'
import './directives/toggleSwitch'
import './controllers/SavingNotificationController'
let EditorManager
export default ( EditorManager = ( function ( ) {
EditorManager = class EditorManager {
static initClass ( ) {
this . prototype . _syncTimeout = null
}
constructor ( ide , $scope , localStorage ) {
this . ide = ide
2020-09-15 08:48:40 -04:00
this . editorOpenDocEpoch = 0 // track pending document loads
2020-05-19 05:02:56 -04:00
this . $scope = $scope
this . localStorage = localStorage
this . $scope . editor = {
sharejs _doc : null ,
open _doc _id : null ,
open _doc _name : null ,
opening : true ,
trackChanges : false ,
wantTrackChanges : false ,
showRichText : this . showRichText ( )
2018-11-05 05:06:39 -05:00
}
2020-05-19 05:02:56 -04:00
this . $scope . $on ( 'entity:selected' , ( event , entity ) => {
if ( this . $scope . ui . view !== 'history' && entity . type === 'doc' ) {
return this . openDoc ( entity )
2018-11-05 05:06:39 -05:00
}
2020-05-19 05:02:56 -04:00
} )
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
this . $scope . $on ( 'entity:deleted' , ( event , entity ) => {
if ( this . $scope . editor . open _doc _id === entity . id ) {
if ( ! this . $scope . project . rootDoc _id ) {
this . $scope . ui . view = null
return
2018-11-05 05:06:39 -05:00
}
2020-05-19 05:02:56 -04:00
const doc = this . ide . fileTreeManager . findEntityById (
this . $scope . project . rootDoc _id
)
if ( doc == null ) {
this . $scope . ui . view = null
return
2018-11-05 05:06:39 -05:00
}
2020-05-19 05:02:56 -04:00
return this . openDoc ( doc )
}
} )
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
let initialized = false
this . $scope . $on ( 'file-tree:initialized' , ( ) => {
if ( ! initialized ) {
initialized = true
return this . autoOpenDoc ( )
}
} )
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
this . $scope . $on ( 'flush-changes' , ( ) => {
return Document . flushAll ( )
} )
window . addEventListener ( 'blur' , ( ) => {
// The browser may put the tab into sleep as it looses focus.
// Flushing the documents should help with keeping the documents in
// sync: we can use any new version of the doc that the server may
// present us. There should be no need to insert local changes into
// the doc history as the user comes back.
sl _console . log ( '[EditorManager] forcing flush onblur' )
Document . flushAll ( )
} )
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
this . $scope . $watch ( 'editor.wantTrackChanges' , value => {
if ( value == null ) {
return
}
return this . _syncTrackChangesState ( this . $scope . editor . sharejs _doc )
} )
}
showRichText ( ) {
return (
this . localStorage ( ` editor.mode. ${ this . $scope . project _id } ` ) ===
'rich-text'
)
}
autoOpenDoc ( ) {
const open _doc _id =
this . ide . localStorage ( ` doc.open_id. ${ this . $scope . project _id } ` ) ||
this . $scope . project . rootDoc _id
if ( open _doc _id == null ) {
return
}
const doc = this . ide . fileTreeManager . findEntityById ( open _doc _id )
if ( doc == null ) {
return
2018-11-05 05:06:39 -05:00
}
2020-05-19 05:02:56 -04:00
return this . openDoc ( doc )
}
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
openDocId ( doc _id , options ) {
if ( options == null ) {
options = { }
2018-11-05 05:06:39 -05:00
}
2020-05-19 05:02:56 -04:00
const doc = this . ide . fileTreeManager . findEntityById ( doc _id )
if ( doc == null ) {
return
}
return this . openDoc ( doc , options )
}
2018-11-05 05:06:39 -05:00
2020-07-28 05:37:46 -04:00
jumpToLine ( options ) {
return this . $scope . $broadcast (
'editor:gotoLine' ,
options . gotoLine ,
2020-08-24 08:20:54 -04:00
options . gotoColumn ,
options . syncToPdf
2020-07-28 05:37:46 -04:00
)
}
2020-05-19 05:02:56 -04:00
openDoc ( doc , options ) {
if ( options == null ) {
options = { }
2018-11-05 05:06:39 -05:00
}
2020-05-19 05:02:56 -04:00
sl _console . log ( ` [openDoc] Opening ${ doc . id } ` )
this . $scope . ui . view = 'editor'
2018-11-05 05:06:39 -05:00
2020-08-18 09:08:49 -04:00
const done = isNewDoc => {
this . $scope . $broadcast ( 'doc:after-opened' , { isNewDoc } )
2020-05-19 05:02:56 -04:00
if ( options . gotoLine != null ) {
// allow Ace to display document before moving, delay until next tick
// added delay to make this happen later that gotoStoredPosition in
// CursorPositionManager
2020-07-28 05:37:46 -04:00
return setTimeout ( ( ) => this . jumpToLine ( options ) , 0 )
2020-05-19 05:02:56 -04:00
} else if ( options . gotoOffset != null ) {
return setTimeout ( ( ) => {
return this . $scope . $broadcast (
'editor:gotoOffset' ,
options . gotoOffset
)
} , 0 )
2018-11-05 05:06:39 -05:00
}
}
2020-05-19 05:02:56 -04:00
// If we already have the document open we can return at this point.
// Note: only use forceReopen:true to override this when the document is
// is out of sync and needs to be reloaded from the server.
if ( doc . id === this . $scope . editor . open _doc _id && ! options . forceReopen ) {
// automatically update the file tree whenever the file is opened
this . ide . fileTreeManager . selectEntity ( doc )
this . $scope . $apply ( ( ) => {
2020-08-18 09:08:49 -04:00
return done ( false )
2020-05-19 05:02:56 -04:00
} )
return
}
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
// We're now either opening a new document or reloading a broken one.
this . $scope . editor . open _doc _id = doc . id
this . $scope . editor . open _doc _name = doc . name
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
this . ide . localStorage ( ` doc.open_id. ${ this . $scope . project _id } ` , doc . id )
this . ide . fileTreeManager . selectEntity ( doc )
this . $scope . editor . opening = true
return this . _openNewDocument ( doc , ( error , sharejs _doc ) => {
2020-09-15 08:48:40 -04:00
if ( error && error . message === 'another document was loaded' ) {
sl _console . log (
` [openDoc] another document was loaded while ${ doc . id } was loading `
)
return
}
2020-05-19 05:02:56 -04:00
if ( error != null ) {
this . ide . showGenericMessageModal (
'Error opening document' ,
'Sorry, something went wrong opening this document. Please try again.'
)
2018-11-05 05:06:39 -05:00
return
}
2020-05-19 05:02:56 -04:00
this . _syncTrackChangesState ( sharejs _doc )
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
this . $scope . $broadcast ( 'doc:opened' )
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
return this . $scope . $apply ( ( ) => {
this . $scope . editor . opening = false
this . $scope . editor . sharejs _doc = sharejs _doc
2020-08-18 09:08:49 -04:00
return done ( true )
2020-05-19 05:02:56 -04:00
} )
} )
}
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
_openNewDocument ( doc , callback ) {
if ( callback == null ) {
callback = function ( error , sharejs _doc ) { }
}
sl _console . log ( '[_openNewDocument] Opening...' )
const current _sharejs _doc = this . $scope . editor . sharejs _doc
const new _sharejs _doc = Document . getDocument ( this . ide , doc . id )
// Leave the current document only when we are opening a different new
// one, to avoid race conditions between leaving and joining the same
// document.
if (
current _sharejs _doc != null &&
current _sharejs _doc !== new _sharejs _doc
) {
sl _console . log ( '[_openNewDocument] Leaving existing open doc...' )
current _sharejs _doc . leaveAndCleanUp ( )
this . _unbindFromDocumentEvents ( current _sharejs _doc )
}
2020-09-15 08:48:40 -04:00
const editorOpenDocEpoch = ++ this . editorOpenDocEpoch
2020-05-19 05:02:56 -04:00
return new _sharejs _doc . join ( error => {
if ( error != null ) {
2020-09-16 05:55:33 -04:00
sl _console . log (
` [_openNewDocument] error joining doc ${ doc . id } ` ,
error
)
2020-05-19 05:02:56 -04:00
return callback ( error )
}
2020-09-15 08:48:40 -04:00
if ( this . editorOpenDocEpoch !== editorOpenDocEpoch ) {
sl _console . log (
` [openNewDocument] editorOpenDocEpoch mismatch ${
this . editorOpenDocEpoch
} vs $ { editorOpenDocEpoch } `
)
return callback ( new Error ( 'another document was loaded' ) )
}
2020-05-19 05:02:56 -04:00
this . _bindToDocumentEvents ( doc , new _sharejs _doc )
return callback ( null , new _sharejs _doc )
} )
}
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
_bindToDocumentEvents ( doc , sharejs _doc ) {
sharejs _doc . on ( 'error' , ( error , meta ) => {
let message
if ( ( error != null ? error . message : undefined ) != null ) {
; ( { message } = error )
} else if ( typeof error === 'string' ) {
message = error
} else {
message = ''
}
if ( /maxDocLength/ . test ( message ) ) {
this . ide . showGenericMessageModal (
'Document Too Long' ,
'Sorry, this file is too long to be edited manually. Please upload it directly.'
)
} else if ( /too many comments or tracked changes/ . test ( message ) ) {
this . ide . showGenericMessageModal (
'Too many comments or tracked changes' ,
'Sorry, this file has too many comments or tracked changes. Please try accepting or rejecting some existing changes, or resolving and deleting some comments.'
)
} else {
this . ide . socket . disconnect ( )
this . ide . reportError ( error , meta )
this . ide . showOutOfSyncModal (
'Out of sync' ,
"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>" ,
sharejs _doc . doc . _doc . snapshot
)
}
const removeHandler = this . $scope . $on ( 'project:joined' , ( ) => {
this . openDoc ( doc , { forceReopen : true } )
removeHandler ( )
2018-11-05 05:06:39 -05:00
} )
2020-05-19 05:02:56 -04:00
} )
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
return sharejs _doc . on ( 'externalUpdate' , update => {
if ( this . _ignoreExternalUpdates ) {
return
2018-11-05 05:06:39 -05:00
}
2020-01-29 10:06:29 -05:00
if (
2020-05-19 05:02:56 -04:00
_ . property ( [ 'meta' , 'type' ] ) ( update ) === 'external' &&
_ . property ( [ 'meta' , 'source' ] ) ( update ) === 'git-bridge'
2020-01-29 10:06:29 -05:00
) {
2020-05-19 05:02:56 -04:00
return
2018-11-05 05:06:39 -05:00
}
2020-05-19 05:02:56 -04:00
return this . 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.'
)
} )
}
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
_unbindFromDocumentEvents ( document ) {
return document . off ( )
}
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
getCurrentDocValue ( ) {
return this . $scope . editor . sharejs _doc != null
? this . $scope . editor . sharejs _doc . getSnapshot ( )
: undefined
}
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
getCurrentDocId ( ) {
return this . $scope . editor . open _doc _id
}
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
startIgnoringExternalUpdates ( ) {
return ( this . _ignoreExternalUpdates = true )
}
2018-11-05 05:06:39 -05:00
2020-05-19 05:02:56 -04:00
stopIgnoringExternalUpdates ( ) {
return ( this . _ignoreExternalUpdates = false )
}
_syncTrackChangesState ( doc ) {
let tryToggle
if ( doc == null ) {
return
2018-11-05 05:06:39 -05:00
}
2020-05-19 05:02:56 -04:00
if ( this . _syncTimeout != null ) {
clearTimeout ( this . _syncTimeout )
this . _syncTimeout = null
2018-11-05 05:06:39 -05:00
}
2020-05-19 05:02:56 -04:00
const want = this . $scope . editor . wantTrackChanges
const have = doc . getTrackingChanges ( )
if ( want === have ) {
this . $scope . editor . trackChanges = want
return
2018-11-05 05:06:39 -05:00
}
2020-05-19 05:02:56 -04:00
return ( tryToggle = ( ) => {
const saved = doc . getInflightOp ( ) == null && doc . getPendingOp ( ) == null
if ( saved ) {
doc . setTrackingChanges ( want )
return this . $scope . $apply ( ( ) => {
return ( this . $scope . editor . trackChanges = want )
} )
} else {
return ( this . _syncTimeout = setTimeout ( tryToggle , 100 ) )
2018-11-05 05:06:39 -05:00
}
2020-05-19 05:02:56 -04:00
} ) ( )
2018-11-05 05:06:39 -05:00
}
2020-05-19 05:02:56 -04:00
}
EditorManager . initClass ( )
return EditorManager
} ) ( ) )