JSFiddle - React, Tailwind, and code Playground

by tlarson

HTML

<script src="http://cloud.github.com/downloads/SteveSanderson/knockout/knockout-2.2.0.js"></script>
<script src="http://akzhan.github.io/jwysiwyg/jquery.wysiwyg.js"></script>
<link rel="stylesheet" href="http://akzhan.github.io/jwysiwyg/jquery.wysiwyg.css">
<p>Editor Here</p>
<textarea data-bind="wysiwyg: content"></textarea>
<p>Value shown here</p>
<div data-bind="html: content"></div>

JavaScript

ko.bindingHandlers.wysiwyg = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        var options = allBindingsAccessor().wysiwygOptions || {};
        var value = ko.utils.unwrapObservable(valueAccessor());
        var $e = $(element);

        $e.wysiwyg(options);

        //handle the field changing
        function detectFn() {
            var observable = valueAccessor();
            var newvalue = $e.wysiwyg("getContent");
            observable(newvalue);
        }

        var current = $e.wysiwyg('document');
        var timer;
        current.bind({    
            keyup: function(){
                clearTimeout(timer);
                timer = setTimeout(detectFn, 1000);
            }
        });

        //handle disposal (if KO removes by the template binding)
        ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
            $e.wysiwyg('destroy');
        });
    },
    update: function (element, valueAccessor) {
        var newValue = ko.utils.unwrapObservable(valueAccessor());
        var oldValue = $(element).wysiwyg("getContent");
        if (newValue !== oldValue) {
            $(element).wysiwyg("setContent", newValue);
        }
    }
};

var viewModel = {
    content: ko.observable("here is some content"),
    clear: function() {
       this.content("");   
    }
};

ko.applyBindings(viewModel);