Skip to content

Instantly share code, notes, and snippets.

@st-h
Last active January 15, 2018 14:07
Show Gist options
  • Save st-h/aa3bd916de444187d43d54e9830e6fa6 to your computer and use it in GitHub Desktop.
Save st-h/aa3bd916de444187d43d54e9830e6fa6 to your computer and use it in GitHub Desktop.
contenteditable
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');
}
}
});
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!');
}
});
<h2>content editable:</h2>
{{content-editable value=val}}
<h2>rendered:</h2>
<pre>{{val}}</pre>
<button {{action modifyText}}>change</button>
{
"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