JSFiddle - React, Tailwind, and code Playground

by anujpshah

HTML

<div ng-app="myApp">
  <div class="dn-description" contenteditable spellcheck ng-model="description" dn-data-placeholder="Card Description"></div>
<div>{{description}}</div>
<button ng-click="description = null">clear model</button>
</div>

CSS

.dn-description {
    border: 1px solid #bbb;
}
.dn-data-placeholder::after {
  content: attr(dn-data-placeholder);
  color: #ccc;
}

JavaScript

angular.module('myApp', [])
.directive('contenteditable', function() {
    return {
      restrict: 'A',          // only activate on element attribute
      require: '?ngModel',    // get a hold of NgModelController
      link: function(scope, element, attrs, ngModel) {
        if(!ngModel) return;  // do nothing if no ng-model

        // Specify how UI should be updated
        ngModel.$render = function() {
          var mv = ngModel.$modelValue;
          if (mv === null || mv === undefined || mv === "") {
            element.addClass('dn-data-placeholder');
          } else {
            element.html(ngModel.$modelValue);
          }
        };

        // On focus remoe the placeholder class
        element.bind('focus', function() {
          element.removeClass('dn-data-placeholder');
        });

        // Listen for change events to enable binding
        element.bind('blur keyup change', function() {
          scope.$apply(read);
          var mv = ngModel.$modelValue;
          if (mv === null || mv === undefined || mv === "") {
            element.addClass('dn-data-placeholder');
          } else {
            element.removeClass('dn-data-placeholder');
          }
        });

        // Write user input data to the model
        function read() {
          ngModel.$setViewValue(element.html());
        }
      }
    };
  })

;