JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>LM Maker</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.2/angular.min.js"></script>
</head>
<body ng-app="lmmaker">
<div class="container">
<div class="row">
<h1>Let's make some tests</h1>
</div>
<div class="row">
<div contentEditable="true" ng-model="pouet" id="editable">Yeah edit me !</div>
</div>
<div class="row">
<p>
{{ pouet }}
</p>
</div>
</div>
</body>
</html>
JavaScript
// vim: fileencoding=utf8 tw=120 ts=4 sw=4 expandtab :
/* --- JSLint Configuration --- */
/*jslint unparam: true, indent: 4, maxlen: 120 */
/*global angular, jQuery, console */
/* ---------------------------- */
/*
* LM Maker
*
* Under the terms of the WTFPL
* Copyright 2012 Rémy Sanchez <remy.sanchez@hyperthese.net>
*
*/
(function ($) {
'use strict';
angular
.module('lmmaker', [])
.directive('contenteditable', function ($browser, $window, $document) {
return {
restrict: 'A',
require: '?ngModel',
link: function (scope, element, attrs, ctrl) {
var curStartPos,
curStopPos,
dummyHtml = element.clone(),
getCursorPos,
setCursorPos;
function read() {
var value = element.text(),
oldLen = dummyHtml.text().length,
newLen = value.length;
// Did the value actually change ?
if (value === ctrl.$viewValue) {
return;
}
// But maybe the current content is not pure text, and that's what we're trying to find out and
// rectify right here.
dummyHtml.text(value);
console.log({
value: JSON.stringify(value),
elmt: element.html().replace(/<br>$/, ''), // This stupid replace is here for Firefox
dummy: dummyHtml.html()
});
if (element.html().replace(/<br>$/, '') !== dummyHtml.html()) {
console.log('rectifying the content');
element.html(value);
// Allright, here comes the...