Digit linkifier simple
by dipish
HTML
<h1>Digit linkifier simple</h1>
<p><i>Plain ol' textarea...</i></p>
<p class="toolbar">
<button id="btn-0">0</button>
<button id="btn-1">1</button>
<button id="btn-2">2</button>
<button id="btn-3">3</button>
<button id="btn-4">4</button>
<button id="btn-5">5</button>
<button id="btn-6">6</button>
<button id="btn-7">7</button>
<button id="btn-8">8</button>
<button id="btn-9">9</button>
</p>
<div id="myeditor" class="editor-wrapper">
<div class="editor" contenteditable>
1 banana<br />
2 banana<br />
3 banana<br />
four<br />
4 make a bunch<br />
and so do many more<br />
</div>
<div class="editor-ghost" contenteditable>
1 banana<br />
2 banana<br />
3 banana<br />
four<br />
4 make a bunch<br />
and so do many more<br />
</div>
</div>
CSS
* {
box-sizing: border-box;
}
body {
margin: 20px;
}
h1 {
font-size: 2em;
font-family: sans-serif;
margin: 0.3em 0;
}
.editor-wrapper {
position: relative;
width: 400px;
height: 200px;
}
.editor {
border: solid 1px black;
width: 100%;
height: 100%;
padding: 5px;
overflow: auto;
line-height: 1.5;
}
.editor a {
background: rgb(169, 214, 255);
text-decoration: none;
padding: 0px 4px;
margin: 1px;
color: #000;
border: solid 1px rgb(0, 122, 255);
border-radius: 5px;
}
.editor-ghost {
display: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: solid 1px black;
padding: 5px;
z-index: -1;
overflow: auto;
line-height: 1.5;
}
.toolbar {
margin-top: 10px;
}
.toolbar button {
}
.toolbar .active {
background: #55D155;
}
JavaScript
var wrapper = $('#myeditor'),
editor = wrapper.find('.editor'),
ghost = wrapper.find('.editor-ghost'),
usedDigits = {};
function detectDigits(s) {
var res = s.match(/\d/g);
usedDigits = {};
if(res) {
for(var i = 0, len = res.length; i < len; i++) {
usedDigits[res[i]] = true;
}
}
}
function updateToolbar() {
for(var i = 0; i < 10; i++) {
$('#btn-' + i).toggleClass('active', !!usedDigits[i]);
}
}
function refresh() {
var html = editor.html();
detectDigits(html);
updateToolbar();
}
editor.on('keyup', function(e) {
//ghost.html( linkify(editor.html()) );
refresh();
});
// kickoff
refresh();
//editor.on('click', 'a', function() {console.log('link click!');})