Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function expanded_print_text(sep, end, elmn) {
- for (var part = 3; part < arguments.length; part += 1)
- $(elmn).html($(elmn).html()+arguments[part].toString()+(part != arguments.length-1 ? sep : ''))
- $(elmn).html($(elmn).html()+end)
- }
- function print_text() {
- expanded_print_text.bind(null, " ", "<br/>", "body").apply(null, arguments)
- }
- function getCursorPos(input) {
- if ("selectionStart" in input && document.activeElement == input) {
- return {
- start: input.selectionStart,
- end: input.selectionEnd
- };
- }
- else if (input.createTextRange) {
- var sel = document.selection.createRange();
- if (sel.parentElement() === input) {
- var rng = input.createTextRange();
- rng.moveToBookmark(sel.getBookmark());
- for (var len = 0;
- rng.compareEndPoints("EndToStart", rng) > 0;
- rng.moveEnd("character", -1)) {
- len++;
- }
- rng.setEndPoint("StartToStart", input.createTextRange());
- for (var pos = { start: 0, end: len };
- rng.compareEndPoints("EndToStart", rng) > 0;
- rng.moveEnd("character", -1)) {
- pos.start++;
- pos.end++;
- }
- return pos;
- }
- }
- return -1;
- }
- function setCursorPos(input, start, end) {
- if (arguments.length < 3) end = start;
- if ("selectionStart" in input) {
- setTimeout(function() {
- input.selectionStart = start;
- input.selectionEnd = end;
- }, 1);
- }
- else if (input.createTextRange) {
- var rng = input.createTextRange();
- rng.moveStart("character", start);
- rng.collapse();
- rng.moveEnd("character", end - start);
- rng.select();
- }
- }
- AVAILABLE = 8
- function init() {
- print_text("<textarea id='area'/>")
- $(document).on('keypress', '#area', area_handler)
- }
- function get_slots() {
- split_it = (string) => { return string.split("") }
- restrict_it = (array) => { return array.slice(0, AVAILABLE) }
- return $('#area').val().split(/(\n)/g).map(split_it).map(restrict_it)
- }
- function set_from_slots(slots) {
- n_function = (slot) => slot.indexOf("\n")==-1 ? slot.length>=AVAILABLE ? slot.concat(["\n"]) : slot : slot
- end_result = slots.map(n_function)
- end_result = [].concat.apply([], end_result)
- end_result = end_result.join("")
- $('#area').val(end_result)
- }
- function shift(direction, slots, line, col, filler='') {
- common_checks_fail = () => (
- line < 0 ||
- line >= slots.length ||
- col < 0 ||
- col >= AVAILABLE
- )
- non_empty_array = (elmn) => elmn.length > 0
- compose_result = (insertion) => Array.prototype.concat(slots.slice(0, line), [insertion], slots.slice(line+1)).filter(non_empty_array)
- right_check = () => slots[line].length >= AVAILABLE
- left_result = () => slots[line].slice(0, col).concat(slots[line].slice(col+1, AVAILABLE+1))
- right_result = () => slots[line].slice(0, col).concat(filler, slots[line].slice(col, AVAILABLE-1), slots[line].length >= AVAILABLE ? "\n" : "")
- if (common_checks_fail()) return slots
- switch(direction) {
- case 'left': return compose_result(left_result())
- case 'right': return right_check() ? slots : compose_result(right_result())
- }
- }
- function area_handler(event) {
- key = event.which
- slots = get_slots()
- cur_pos = getCursorPos($('#area')[0])["start"]
- cur_line = Math.floor(cur_pos / (AVAILABLE+1))
- cur_col = cur_pos % (AVAILABLE+1)
- // ENTER = 13, not allowed
- // BACKSPACE = 8: LSHIFT
- // DELETE = 46: LSHIFT FOR NEXT
- // ASSUME NO SELECTIONS
- if (key == 13) {
- event.preventDefault() // Forbidden key
- return
- }
- if (key == 8 || key == 46) {
- event.preventDefault()
- new_slots = shift('left', slots, cur_line, cur_col+(key == 8 ? -1 : 1))
- set_from_slots(new_slots)
- setCursorPos($('#area')[0], cur_pos-1, cur_pos-1)
- return
- }
- if (event.key.length == 1) { // Ultimate shitcode
- event.preventDefault()
- new_slots = shift('right', slots, cur_line, cur_col, String.fromCharCode(key))
- set_from_slots(new_slots)
- return
- }
- }
- $(document).ready(init)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement