Working on a Knockout bindinghandler that is backed by a markdown string, and renders (using markdown.js and the built-in 'html' bindinghandler).
Works fine, but tried to add contenteditable behavior, and having trouble with the observable value not updating on blur, or updating the observable with only the text with all markdown formatting removed.
Any ideas or an alternate approach you can think of?
Full fiddle here: http://jsfiddle.net/ZdxAS/3/
Binding Handler:
ko.bindingHandlers["editable"] = {
init: function (element, valueAccessor) {
$(element).on('blur', function (event) {
var observable = valueAccessor();
observable($(this).text());
return true;
});
$(element).on('focus', function (event) {
var observable = ko.utils.unwrapObservable(valueAccessor());
$(this).text(observable);
return true;
});
return $(element).on('keydown', function (event) {
var esc, observable, submit;
esc = event.which === 27;
element = event.target;
if (esc) {
document.execCommand("undo");
element.blur();
return true;
} else {
return true;
}
});
},
update: function (element, valueAccessor, allBindings, viewModel, context) {
var accessor = function () {
var text = ko.utils.unwrapObservable(valueAccessor());
return markdown.toHTML(text);
}
return ko.bindingHandlers.html.update(element, accessor, allBindings, viewModel, context)
}
};