├── .gitignore ├── bower.json ├── LICENSE ├── package.json ├── dist ├── ng-content-editable.min.js └── ng-content-editable.js ├── README.md └── src └── content-editable.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *~ 3 | *.swp 4 | *.orig 5 | npm-debug.log 6 | node_modules/ 7 | bower_components/ 8 | /.idea 9 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ng-content-editable", 3 | "version": "1.1.0", 4 | "authors": [ 5 | "Fabrício Ferrari de Campos ", 6 | "Letov Nikita ", 7 | "Oliver Georgi " 8 | ], 9 | "description": "Use ngModel and validations with contenteditable HTML5 attribute", 10 | "main": "./dist/ng-content-editable.js", 11 | "keywords": [ 12 | "angular", 13 | "AngularJS", 14 | "contenteditable", 15 | "content-editable" 16 | ], 17 | "license": "MIT", 18 | "ignore": [ 19 | "node_modules", 20 | "bower_components" 21 | ], 22 | "dependencies": { 23 | "angular": "> 1.2.0 < 2.0" 24 | }, 25 | "devDependencies": {} 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Vizir 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ng-content-editable", 3 | "version": "1.0.0", 4 | "description": "Use ngModel and validations with contenteditable HTML5 attribute", 5 | "scripts": { 6 | "test": "echo \"Pending tests implementation\" && exit 1", 7 | "build": "\\cp src/content-editable.js dist/ng-content-editable.js && uglifyjs dist/ng-content-editable.js -o dist/ng-content-editable.min.js -c -m" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/Vizir/ng-contenteditable.git" 12 | }, 13 | "keywords": [ 14 | "angular", 15 | "AngularJS", 16 | "contenteditable", 17 | "content-editable" 18 | ], 19 | "author": "Fabrício Ferrari de Campos (https://github.com/Vizir/ng-contenteditable)", 20 | "contributors": [ 21 | "Oliver Georgi ", 22 | "Letov Nikita (https://github.com/Nikitian/ng-contenteditable)" 23 | ], 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/Vizir/ng-contenteditable/issues" 27 | }, 28 | "homepage": "https://github.com/Vizir/ng-contenteditable", 29 | "main": "dist/ng-content-editable.min.js", 30 | "devDependencies": { 31 | "uglify-js": "~3.4.9" 32 | }, 33 | "dependencies": {} 34 | } 35 | -------------------------------------------------------------------------------- /dist/ng-content-editable.min.js: -------------------------------------------------------------------------------- 1 | !function(){"use strict";angular.module("content-editable",[]).directive("contenteditable",["$timeout",function(u){return{restrict:"A",require:["^?ngModel","^?form"],link:function(n,r,t,e){var l=e[0];if(null===l)return null;var i=function(){if(void 0===t.ngModel)return null;var e=t.ngModel.split(".");return e[e.length-1]}(),o={onlyText:!1,convertNewLines:!1,noLf:!1,onlyNum:!1,noTrim:!1};angular.forEach(["onlyText","convertNewLines","noLf","onlyNum","noTrim"],function(e){t.hasOwnProperty(e)&&t[e]&&"false"!==t[e]&&(o[e]=!0)}),u(function(){return o.onlyText&&o.noLf||o.onlyNum?r.text(l.$modelValue):r.html(l.$modelValue)});var a=function(){var e,n="";o.onlyText&&o.noLf||o.onlyNum?n=r.text():(n=r.html())&&(n=function(e){if(e=e.replace(/ /g," "),o.convertNewLines||o.noLf){var n="\r\n",r=/\r\n$/;o.noLf&&(n=" ",r=/ $/),e=(e=(e=(e=(e=(e=e.replace(//gi,n)).replace(/<[div>]+>/gi,n)).replace(/<\/[div>]+>/gm,"")).replace(/<[p>]+>/gi,n)).replace(/<\/[p>]+>/gm,"")).replace(r,"")}o.onlyText&&(e=e.replace(/<\S[^><]*>/g,""));return e}(n)),!1===o.noTrim&&""!==n&&(n=(n=n.replace(/ /g," ")).trim()),l.$setViewValue(n),(e=n.length)>t.ngMaxlength||e=t.ngMaxlength&&-1===n.indexOf(e.which))return e.preventDefault(),!1}})}}}])}(); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ng-content-editable 2 | 3 | This is a slightly extended fork of the npm package [ng-content-editable](https://www.npmjs.com/package/ng-content-editable). 4 | 5 | ### Demo 6 | You can tryout **ng-content-editable** [here](https://vizir.github.io/ng-contenteditable/). 7 | 8 | ### Getting Started 9 | Download the package, and include the `dist/ng-content-editable.min.js` file in your page. 10 | 11 | ```bash 12 | npm install ng-content-editable --save 13 | ``` 14 | 15 | Then add the content-editable module to your Angular App file, e.g. 16 | 17 | ```js 18 | var app = angular.module('app', ["content-editable"]); 19 | ``` 20 | 21 | ### Usage 22 | 23 | ```html 24 |
34 | ``` 35 | 36 | ### Description of optional attributes 37 | | Attribute | Description| Example | 38 | | :------------- | :-------------| :----- | 39 | | ng-maxlength | The max-length for the attribute | 255 | 40 | | ng-minlength | The min-length for the attribute | 3 | 41 | | only-text | Remove all the html tags for the attribute value | true or false | 42 | | convert-new-lines | Convert all `
`, `

` and `

` to `\r\n` | true or false | 43 | | only-num | Allow numbers 0-9, `.` and `,` only | true or false | 44 | | no-lf | Line breaks not allowed, results in single line | true or false | 45 | | no-trim | Disable default trim (removes whitespace from both ends of a string) | true or false | 46 | 47 | 48 | ### Contributing 49 | 50 | It's easy for you to make a contribution, just open a PR on GitHub :) 51 | 52 | But if this will be your first contribution to a JavaScript project, below are some steps that are useful during development. 53 | 54 | Install the dev dependencies: 55 | 56 | ```bash 57 | npm install 58 | ``` 59 | 60 | Generate the dist files: 61 | 62 | ```bash 63 | npm run build 64 | ``` -------------------------------------------------------------------------------- /src/content-editable.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | 3 | 'use strict'; 4 | 5 | angular.module('content-editable', []) 6 | .directive('contenteditable', ['$timeout', function ($timeout) { 7 | return { 8 | restrict: 'A', 9 | require: ['^?ngModel', '^?form'], 10 | link: function (scope, element, attrs, args) { 11 | var ngModel = args[0]; 12 | 13 | if (ngModel === null) { 14 | return null; 15 | } 16 | 17 | var modelKey = getModelKey(), 18 | opts = { 19 | onlyText: false, 20 | convertNewLines: false, 21 | noLf: false, 22 | onlyNum: false, 23 | noTrim: false 24 | }; 25 | 26 | angular.forEach(['onlyText', 'convertNewLines', 'noLf', 'onlyNum', 'noTrim'], function (opt) { 27 | if (attrs.hasOwnProperty(opt) && attrs[opt] && attrs[opt] !== 'false') { 28 | opts[opt] = true; 29 | } 30 | }); 31 | 32 | // when model has already a value 33 | $timeout(function () { 34 | return (opts.onlyText && opts.noLf) || opts.onlyNum ? element.text(ngModel.$modelValue) : element.html(ngModel.$modelValue); 35 | }); 36 | 37 | var validate = function (content) { 38 | var length = content.length; 39 | 40 | if (length > attrs.ngMaxlength || length < attrs.ngMinlength) { 41 | ngModel.$setValidity(modelKey, false); 42 | return element.addClass('-error'); 43 | } 44 | 45 | if (element.hasClass('-error')) { 46 | ngModel.$setValidity(modelKey, true); 47 | return element.removeClass('-error'); 48 | } 49 | }; 50 | 51 | var read = function () { 52 | var content = ''; 53 | if ((opts.onlyText && opts.noLf) || opts.onlyNum) { 54 | content = element.text(); 55 | } else { 56 | content = element.html(); 57 | if (content) { 58 | content = parseHtml(content); 59 | } 60 | } 61 | 62 | if (opts.noTrim === false && content !== '') { 63 | content = content.replace(/ /g, ' '); 64 | content = content.trim(); 65 | } 66 | 67 | ngModel.$setViewValue(content); 68 | validate(content); 69 | }; 70 | 71 | ngModel.$render = function () { 72 | if ((opts.onlyText && opts.noLf) || opts.onlyNum) { 73 | element.text(ngModel.$viewValue || ''); 74 | } else { 75 | element.html(ngModel.$viewValue || ''); 76 | } 77 | }; 78 | 79 | element.bind('blur keyup change', function (event) { 80 | scope.$apply(read); 81 | if (event.type === 'blur') { 82 | scope.$apply(ngModel.$render); 83 | } 84 | }); 85 | 86 | element.bind('keydown', function (e) { 87 | var cntrlKeys = [8, 37, 38, 39, 40, 46]; 88 | // comma, dot, 0-9 89 | if (opts.onlyNum && cntrlKeys.indexOf(e.which) === -1 && e.which !== 188 && e.which !== 190 && !((e.which >= 48 && e.which <= 57) || (e.which >= 96 && e.which <= 105))) { 90 | e.preventDefault(); 91 | return false; 92 | } 93 | if (opts.noLf) { 94 | if (e.which === 13) { 95 | e.preventDefault(); 96 | return false; 97 | } else if (attrs.ngMaxlength && element.text().length >= attrs.ngMaxlength && cntrlKeys.indexOf(e.which) === -1) { 98 | // !e.shiftKey && !e.altKey && !e.ctrlKey && 99 | e.preventDefault(); 100 | return false; 101 | } 102 | } 103 | }); 104 | 105 | function getModelKey() { 106 | if (typeof attrs.ngModel === 'undefined') { 107 | return null; 108 | } 109 | 110 | var split = attrs.ngModel.split('.'); 111 | 112 | return split[split.length - 1]; 113 | } 114 | 115 | function parseHtml(html) { 116 | html = html.replace(/ /g, ' '); 117 | 118 | if (opts.convertNewLines || opts.noLf) { 119 | var lf = '\r\n', 120 | rxl = /\r\n$/; 121 | 122 | if (opts.noLf) { 123 | lf = ' '; 124 | rxl = / $/; 125 | } 126 | 127 | html = html.replace(//ig, lf); // replace br for newlines 128 | html = html.replace(/<[div>]+>/ig, lf); // replace div for newlines 129 | html = html.replace(/<\/[div>]+>/gm, ''); // remove remaining divs 130 | html = html.replace(/<[p>]+>/ig, lf); // replace p for newlines 131 | html = html.replace(/<\/[p>]+>/gm, ''); // remove remaining p 132 | html = html.replace(rxl, ''); // remove last newline 133 | } 134 | 135 | if (opts.onlyText) { 136 | html = html.replace(/<\S[^><]*>/g, ''); 137 | } 138 | 139 | return html; 140 | } 141 | } 142 | }; 143 | }]); 144 | })(); 145 | -------------------------------------------------------------------------------- /dist/ng-content-editable.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | 3 | 'use strict'; 4 | 5 | angular.module('content-editable', []) 6 | .directive('contenteditable', ['$timeout', function ($timeout) { 7 | return { 8 | restrict: 'A', 9 | require: ['^?ngModel', '^?form'], 10 | link: function (scope, element, attrs, args) { 11 | var ngModel = args[0]; 12 | 13 | if (ngModel === null) { 14 | return null; 15 | } 16 | 17 | var modelKey = getModelKey(), 18 | opts = { 19 | onlyText: false, 20 | convertNewLines: false, 21 | noLf: false, 22 | onlyNum: false, 23 | noTrim: false 24 | }; 25 | 26 | angular.forEach(['onlyText', 'convertNewLines', 'noLf', 'onlyNum', 'noTrim'], function (opt) { 27 | if (attrs.hasOwnProperty(opt) && attrs[opt] && attrs[opt] !== 'false') { 28 | opts[opt] = true; 29 | } 30 | }); 31 | 32 | // when model has already a value 33 | $timeout(function () { 34 | return (opts.onlyText && opts.noLf) || opts.onlyNum ? element.text(ngModel.$modelValue) : element.html(ngModel.$modelValue); 35 | }); 36 | 37 | var validate = function (content) { 38 | var length = content.length; 39 | 40 | if (length > attrs.ngMaxlength || length < attrs.ngMinlength) { 41 | ngModel.$setValidity(modelKey, false); 42 | return element.addClass('-error'); 43 | } 44 | 45 | if (element.hasClass('-error')) { 46 | ngModel.$setValidity(modelKey, true); 47 | return element.removeClass('-error'); 48 | } 49 | }; 50 | 51 | var read = function () { 52 | var content = ''; 53 | if ((opts.onlyText && opts.noLf) || opts.onlyNum) { 54 | content = element.text(); 55 | } else { 56 | content = element.html(); 57 | if (content) { 58 | content = parseHtml(content); 59 | } 60 | } 61 | 62 | if (opts.noTrim === false && content !== '') { 63 | content = content.replace(/ /g, ' '); 64 | content = content.trim(); 65 | } 66 | 67 | ngModel.$setViewValue(content); 68 | validate(content); 69 | }; 70 | 71 | ngModel.$render = function () { 72 | if ((opts.onlyText && opts.noLf) || opts.onlyNum) { 73 | element.text(ngModel.$viewValue || ''); 74 | } else { 75 | element.html(ngModel.$viewValue || ''); 76 | } 77 | }; 78 | 79 | element.bind('blur keyup change', function (event) { 80 | scope.$apply(read); 81 | if (event.type === 'blur') { 82 | scope.$apply(ngModel.$render); 83 | } 84 | }); 85 | 86 | element.bind('keydown', function (e) { 87 | var cntrlKeys = [8, 37, 38, 39, 40, 46]; 88 | // comma, dot, 0-9 89 | if (opts.onlyNum && cntrlKeys.indexOf(e.which) === -1 && e.which !== 188 && e.which !== 190 && !((e.which >= 48 && e.which <= 57) || (e.which >= 96 && e.which <= 105))) { 90 | e.preventDefault(); 91 | return false; 92 | } 93 | if (opts.noLf) { 94 | if (e.which === 13) { 95 | e.preventDefault(); 96 | return false; 97 | } else if (attrs.ngMaxlength && element.text().length >= attrs.ngMaxlength && cntrlKeys.indexOf(e.which) === -1) { 98 | // !e.shiftKey && !e.altKey && !e.ctrlKey && 99 | e.preventDefault(); 100 | return false; 101 | } 102 | } 103 | }); 104 | 105 | function getModelKey() { 106 | if (typeof attrs.ngModel === 'undefined') { 107 | return null; 108 | } 109 | 110 | var split = attrs.ngModel.split('.'); 111 | 112 | return split[split.length - 1]; 113 | } 114 | 115 | function parseHtml(html) { 116 | html = html.replace(/ /g, ' '); 117 | 118 | if (opts.convertNewLines || opts.noLf) { 119 | var lf = '\r\n', 120 | rxl = /\r\n$/; 121 | 122 | if (opts.noLf) { 123 | lf = ' '; 124 | rxl = / $/; 125 | } 126 | 127 | html = html.replace(//ig, lf); // replace br for newlines 128 | html = html.replace(/<[div>]+>/ig, lf); // replace div for newlines 129 | html = html.replace(/<\/[div>]+>/gm, ''); // remove remaining divs 130 | html = html.replace(/<[p>]+>/ig, lf); // replace p for newlines 131 | html = html.replace(/<\/[p>]+>/gm, ''); // remove remaining p 132 | html = html.replace(rxl, ''); // remove last newline 133 | } 134 | 135 | if (opts.onlyText) { 136 | html = html.replace(/<\S[^><]*>/g, ''); 137 | } 138 | 139 | return html; 140 | } 141 | } 142 | }; 143 | }]); 144 | })(); 145 | --------------------------------------------------------------------------------