cop.create('WordCompletionLayer').refineClass(lively.morphic.Text, {
showWordCompletionMorph: function() {
if (this.wordCompletionMorph === undefined) {
var m = new lively.morphic.Text(new Rectangle(), '').beLabel();
this.wordCompletionMorph = m;
}
this.wordCompletionMorph.applyStyle({textColor: Color.gray.darker(), fill: Color.white});
this.wordCompletionMorph.setFontSize(this.getFontSize());
this.addMorph(this.wordCompletionMorph);
return this.wordCompletionMorph;
},
hideWordCompletionMorph: function() {
if (this.wordCompletionMorph)
this.wordCompletionMorph.remove();
},
isShowingWordCompletionMorph: function() {
return this.wordCompletionMorph && (this.wordCompletionMorph.owner === this);
},
onKeyPress: function(evt) {
var key = evt.getKeyChar();
if (!key.match(/\w/)) {
this.hideWordCompletionMorph();
return;
}
var range = this.getSelectionRange()
var cursor = range[0];
if (cursor > 0) {
var lastWordRange = this.selectWord(this.textString, cursor - 1),
lastWord = this.textString.slice(lastWordRange[0], lastWordRange[1] + 1)
previewWord = WordCompletion.current().complete(lastWord);
if (previewWord) {
var m = this.showWordCompletionMorph();
m.setTextString(previewWord);
m.setPosition(this.getCharBounds(lastWordRange[0]).bottomLeft());
}
}
return cop.proceed(evt)
}
});