diff --git a/app/components/SimpleEditor/ContainerEditor.js b/app/components/SimpleEditor/ContainerEditor.js index 9b74f70702a7347543e6f8851c4729ace53f87ba..4e18d0d5a437a5bc0803c39aa32738fdefe5bea4 100644 --- a/app/components/SimpleEditor/ContainerEditor.js +++ b/app/components/SimpleEditor/ContainerEditor.js @@ -10,14 +10,16 @@ import { class ContainerEditor extends SubstanceContainerEditor { constructor (...args) { super(...args) - this.disableBackButton = this.disableBackButton.bind(this) + this.controlBackButton = this.controlBackButton.bind(this) } render ($$) { let el = super.render($$) - el.addClass('my-custom-class') + // native spellcheck - el.attr('spellcheck', this.props.spellcheck === 'native') + // TODO -- there is a hasNativeSpellcheck fn + const isSpellcheckNative = (this.props.spellcheck === 'native') + el.attr('spellcheck', isSpellcheckNative) // open for editing // TODO -- should maybe change to isEditable ? @@ -32,41 +34,53 @@ class ContainerEditor extends SubstanceContainerEditor { didMount () { super.didMount() + if (this.isEmpty()) this.createText() - this.el.focus() // TODO notworking + + // TODO -- why this and not this.focus ? + this.el.focus() if (this.isReadOnlyMode()) { this.editorSession.onUpdate('', this.disableToolbar, this) - this.addTargetToLinks() } + this.props.history.listenBefore((location, callback) => { const commandStates = this.getCommandStates() + if (commandStates['save'].disabled === false) { - this._owner.send('ChangesNotSaved') - this._owner.emit('send:route', location.pathname) + const editor = this.getEditor() + + editor.send('ChangesNotSaved') + editor.emit('send:route', location.pathname) + return callback(false) - } else { - return callback() } + + return callback() }) window.history.pushState(null, null, document.URL) - window.addEventListener('popstate', this.disableBackButton) + window.addEventListener('popstate', this.controlBackButton) } - disableBackButton () { - const url = '/books/' + this.props.book.id + '/book-builder' + // TODO -- review // messes up browser history + controlBackButton () { const commandStates = this.getCommandStates() - window.removeEventListener('popstate', this.disableBackButton) + const url = '/books/' + this.props.book.id + '/book-builder' + + window.removeEventListener('popstate', this.controlBackButton) + if (commandStates['save'].disabled === false) { + const editor = this.getEditor() + window.history.pushState(null, null, document.URL) - this._owner.send('ChangesNotSaved') - this._owner.emit('send:route', url) + + editor.send('ChangesNotSaved') + editor.emit('send:route', url) } else { this.props.history.push(url) } - return } onTextInput (event) { @@ -183,6 +197,7 @@ class ContainerEditor extends SubstanceContainerEditor { newSel = tx.createSelection({ type: 'property', + // TODO -- both id's ?? containerId: 'body', surfaceId: 'body', path: [ node.id, 'content' ], @@ -190,6 +205,7 @@ class ContainerEditor extends SubstanceContainerEditor { endOffset: 0 }) }.bind(this)) + this.rerender() this.editorSession.setSelection(newSel) } @@ -220,6 +236,10 @@ class ContainerEditor extends SubstanceContainerEditor { link.attr('target', '_blank') ) } + + getEditor () { + return this.context.editor + } } export default ContainerEditor