Skip to content

Instantly share code, notes, and snippets.

@KoKuToru
Last active January 12, 2017 19:12
Show Gist options
  • Save KoKuToru/71660c52cc87015cba2dc19906b8e753 to your computer and use it in GitHub Desktop.
Save KoKuToru/71660c52cc87015cba2dc19906b8e753 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
const events = [
//keyboard
'keydown',
'keyup',
'keypress',
//im
'compositionstart',
'compositionupdate',
'compositionend',
//dnd
'drag',
'dragend',
'dragenter',
'dragexit',
'dragleave',
'dragover',
'dragstart',
'drop',
//actions
'cut',
'copy',
'past',
];
export default Ember.Component.extend({
attributeBindings: ['contenteditable'],
contenteditable: true,
init() {
this._super(...arguments);
//rebind
this._selection = this._selection.bind(this);
this._keydown = this._keydown.bind(this);
this._keypress = this._keypress.bind(this);
},
willInsertElement() {
for (var x of events) {
if (this[`_${x}`]) {
this.$().on(x, this[`_${x}`]);
}
}
},
willDestroyElement() {
//TODO: delete all events
},
_selection() {
return window.getSelection() || document.getSelection();
},
_keydown(e) {
//quick and diry prevent all commands
if(e.ctrlKey){
e.preventDefault();
return;
}
},
_keypress(e) {
//TODO: keycode into big list
// ignore arrow keys
switch (e.keyCode) {
case 37: //left
case 38: //up
case 39: //right
case 40: //down
return;
}
e.preventDefault();
// get selection
const selection = this._selection();
console.log("Selection ", selection);
//replace if not collapsed selection
//add if collapsed
//+ move cursor by one
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
{{my-component}}
{
"version": "0.10.7",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.10.0",
"ember-data": "2.10.0",
"ember-template-compiler": "2.10.0",
"ember-testing": "2.10.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment