Last active
January 15, 2018 14:07
-
-
Save st-h/aa3bd916de444187d43d54e9830e6fa6 to your computer and use it in GitHub Desktop.
contenteditable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Component.extend({ | |
attributeBindings: ['contenteditable'], | |
contenteditable: true, | |
didInsertElement() { | |
this._super(...arguments); | |
var observer = new MutationObserver((mutationsList) => { | |
const mutation = mutationsList[mutationsList.length - 1] | |
Ember.run.once('render', ()=> { | |
console.log("setting: " + this.element.innerText); | |
// this works fine, but triggers the didUpdateAttrs hook, which will replace | |
// the innerText of the element which causes the cursor to be put at the beginning of the text | |
this.set('value', this.element.innerText); | |
this.set('_value', this.element.innerText); | |
}); | |
}); | |
observer.observe(this.element, {attributes: false, childList: true, characterData: true,subtree:true}); | |
this.element.innerText = this.get('value'); | |
}, | |
didUpdateAttrs() { | |
this._super(...arguments); | |
// workaround to prevent modifying the dom when we only wanted to update the value property | |
if (this.get('value') !== this.get('_value')) { | |
this.element.innerText = this.get('value'); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
val: 'press the button!\n<b>this newline would be bold</b></br>', | |
modifyText() { | |
this.set('val', '<b>move along</b>\nnothing to see here!'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.13.0", | |
"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.16.2", | |
"ember-template-compiler": "2.16.2", | |
"ember-testing": "2.16.2" | |
}, | |
"addons": { | |
"ember-data": "2.16.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment