HTML Text Editor

HTML
<h1>HTML Editor</h1> <textarea id="area-1"><h1>The MIT License (MIT)</h1> <blockquote> <p>Copyright © 2016 Taufik Nurrohman</p> </blockquote> <p>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:</p> <p>The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.</p> <p>THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p> a b c d</textarea> <p> <button onclick="return toggle(['<b>', '</b>']), false;"><b>B</b></button> <button onclick="return toggle(['<i>', '</i>']), false;"><i>I</i></button> <button onclick="return toggle(['<u>', '</u>']), false;"><u>U</u></button> <button onclick="return link({ href: 'http://', title: ''}), false;">Link</button> <button onclick="return list(), false;">Bullet</button> <button onclick="return list('ol'), false;">List</button> <button onclick="return clean(), false;">Remove Format</button> <button onclick="return editor.undo(), false;">Undo</button> <button onclick="return editor.redo(), false;">Redo</button> </p>
CSS
* { -webkit-box-sizing:border-box; -moz-box-sizing:border-box; box-sizing:border-box; } textarea { display:block; width:100%; height:200px; }
JAVASCRIPT
var editor = new TE(document.getElementById('area-1')); function toggle(a, wrap) { var b = editor._.x(a), $ = editor.$(), m = new RegExp('^' + b[0] + '(.*?)' + b[1] + '$'), m_B = new RegExp(b[0] + '$'), m_A = new RegExp('^' + b[1]); editor.toggle( // when ... wrap ? !m.test($.value) : (!m_B.test($.before) && !m_A.test($.after)), // do ... [ // first toggle function(r) { r.wrap(a[0], a[1], wrap); }, // second toggle (the reset state) function(r) { r.unwrap(a[0], a[1], wrap); } ] ); } function clean() { editor [0]() .replace(/<.+?>/g, "") // remove all HTML tag(s) in selection .unwrap(/<[^\/<>]+?>/, /<\/[^<>]+?>/) // unwrap from any HTML tag(s) [1]() } function list(type) { type = type || 'ul'; editor [0]() // disable history feature .trim('\n\n', '\n\n') // force line-break .replace(/\n+/g, '</li>\n <li>') // convert line-break to a list item .wrap('<' + type + '>\n <li>', '</li>\n</' + type + '>', true) // surround with the HTML list(s) markup [1]() // enable history feature } function link(attrs) { var x; if (!editor.$().length) { alert('please select some text'); return; } for (var i in attrs) { if (x = prompt(i, attrs[i])) { attrs[i] = x; } } var a = '<a'; for (var i in attrs) { a += ' ' + i + '="' + attrs[i] + '"'; }edi a += '>'; editor .record() // record current selection .trim(' ', ' ') // force white-space .wrap(a, '</a>'); // wrap with a HTML link }
Expand for more options Login