Commit 7879770c authored by Christos's avatar Christos
Browse files

Merge branch 'move-autosave' into 'master'

move autosave to editorSession

See merge request !183
parents 722b3bcb a2db4638
......@@ -12,7 +12,6 @@ class Editor extends AbstractEditor {
constructor (parent, props) {
super(parent, props)
this.autoSaveFlag = true
this.handleActions({
trackChangesViewToggle: this.trackChangesViewToggle, // TODO -- move
closeModal: this.closeModal,
......@@ -24,17 +23,6 @@ class Editor extends AbstractEditor {
this.on('diacritics:modal', this.toggleDiacritics, this)
this.on('findAndReplace:modal', this.toggleFindAndReplace, this)
this.on('shortcuts:modal', this.toggleShortCutsModal, this)
this.editorSession.onUpdate('document', this.autoSave, this)
}
autoSave () {
if (!this.autoSaveFlag) return
this.autoSaveFlag = false
setTimeout( () => {
this.editorSession.save()
this.autoSaveFlag = true
}, 20000)
}
initializeProviders () {
......
import { EditorSession as SubstanceEditorSession } from 'substance'
class EditorSession extends SubstanceEditorSession {
constructor (...props) {
super(...props)
this.autoSaveFlag = true
this.onUpdate('', this.autoSave, this)
}
save () {
super.save()
this.document.emit('document:saved')
......@@ -15,6 +21,47 @@ class EditorSession extends SubstanceEditorSession {
this.document.emit('UndoRedo')
super.redo()
}
autoSave () {
if (!this.autoSaveFlag || !this._hasUnsavedChanges) return
this.autoSaveFlag = false
setTimeout( () => {
this.save()
this.autoSaveFlag = true
}, 10000)
}
save() {
var saveHandler = this.saveHandler
if (this._hasUnsavedChanges && !this._isSaving) {
this._isSaving = true
// Pass saving logic to the user defined callback if available
if (saveHandler) {
let saveParams = {
editorSession: this,
fileManager: this.fileManager
}
return saveHandler.saveDocument(saveParams)
.then(() => {
this._hasUnsavedChanges = false
// We update the selection, just so a selection update flow is
// triggered (which will update the save tool)
// TODO: model this kind of update more explicitly. It could be an 'update' to the
// document resource (hasChanges was modified)
// this.setSelection(this.getSelection())
})
.catch((err) => {
console.error('Error during save', err)
}).then(() => { // finally
this._isSaving = false
})
} else {
console.error('Document saving is not handled at the moment. Make sure saveHandler instance provided to editorSession')
return Promise.reject()
}
}
}
}
export default EditorSession
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment