Skip to content

Commit

Permalink
avoid infinite loop, allow exiting with double-space
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Aug 11, 2017
1 parent 0d075fe commit 31736c5
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lib/camel-case-mode.js
Expand Up @@ -11,6 +11,7 @@ export default {
editor: null,
editorView: null,
capitalizeNextDisposable: null,
isCapitalizing: false,

activate (state) {
this.capitalizeNextDisposable = new CompositeDisposable()
Expand Down Expand Up @@ -39,24 +40,37 @@ export default {

disable () {
this.isEnabled = false
this.isCapitalizing = false
this.capitalizeNextDisposable && this.capitalizeNextDisposable.dispose()
this.editorView.classList.remove(MODE_CLASS)
this.editorView && this.editorView.classList.remove(MODE_CLASS)
this.editorView = null
},

toggle () {
console.log(this)
return this.isEnabled
? this.disable()
: this.enable()
},

capitalizeNext () {
this.capitalizeNextDisposable.add(this.editor.onWillInsertText(({text}) => {
if (text === ' ') {
return this.disable()
}
this.editor.insertText(text.toUpperCase())
}))
if (this.isCapitalizing) {
this.disable()
return this.editor.insertText(' ')
}

this.isCapitalizing = true

this.capitalizeNextDisposable = new CompositeDisposable(
this.editor.onWillInsertText(({text, cancel}) => {
if (!text || text.length !== 1) return
if (text === '\n') {
return this.disable()
}
cancel()
this.capitalizeNextDisposable.dispose()
this.editor.insertText(text.toUpperCase())
this.isCapitalizing = false
})
)
}
}

0 comments on commit 31736c5

Please sign in to comment.