Created
February 4, 2015 10:27
-
-
Save Manuel-S/1447a37dc552f5e7dcce to your computer and use it in GitHub Desktop.
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
//tested this myself | |
//should probably also try | |
//https://github.com/knockout/knockout/wiki/Bindings---tinyMCE | |
tinymce.init({ | |
setup: function (ed) { | |
ed.on('keyup', function (e) { | |
tinyMceChange(ed); | |
}); | |
ed.on('change', function(e) { | |
tinyMceChange(ed); | |
}); | |
} | |
}); | |
ko.bindingHandlers.tinymce = { | |
init: function (element, valueAccessor, allBindingsAccessor, context) { | |
var options = allBindingsAccessor().tinymceOptions || {}; | |
var modelValue = valueAccessor(); | |
var value = ko.utils.unwrapObservable(valueAccessor()); | |
var el = $(element) | |
//handle edits made in the editor. Updates after an undo point is reached. | |
options.setup = function (ed) { | |
var updateContent = function(){ | |
if (ko.isWriteableObservable(modelValue)) { | |
modelValue(ed.getContent()); | |
} | |
} | |
console.log(ed) | |
ed.onChange.add(updateContent); | |
ed.onKeyUp.add(updateContent); | |
ed.onNodeChange.add(updateContent); | |
}; | |
//handle destroying an editor | |
ko.utils.domNodeDisposal.addDisposeCallback(element, function () { | |
setTimeout(function(){$(element).tinymce().remove()}, 0) | |
}); | |
//$(element).tinymce(options); | |
setTimeout(function() { $(element).tinymce(options); }, 0); | |
el.html(value); | |
}, | |
update: function (element, valueAccessor, allBindingsAccessor, context) { | |
var el = $(element) | |
var value = ko.utils.unwrapObservable(valueAccessor()); | |
var id = el.attr('id') | |
//handle programmatic updates to the observable | |
// also makes sure it doesn't update it if it's the same. | |
// otherwise, it will reload the instance, causing the cursor to jump. | |
if (id !== undefined ){ | |
var content = tinyMCE.getInstanceById(id).getContent({format: 'raw'}) | |
if (content !== value) { | |
el.html(value); | |
} | |
} | |
} | |
}; | |
var viewModel = { | |
fieldOne: ko.observable("one"), | |
fieldTwo: ko.observable("two"), | |
fieldThree: ko.observable("three") | |
}; | |
ko.applyBindings(viewModel); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment