Advertisement
EditorRUS

Text area script

Feb 15th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function expanded_print_text(sep, end, elmn) {
  2.     for (var part = 3; part < arguments.length; part += 1)
  3.         $(elmn).html($(elmn).html()+arguments[part].toString()+(part != arguments.length-1 ? sep : ''))
  4.     $(elmn).html($(elmn).html()+end)
  5. }
  6.  
  7. function print_text() {
  8.     expanded_print_text.bind(null, " ", "<br/>", "body").apply(null, arguments)
  9. }
  10.  
  11. function getCursorPos(input) {
  12.     if ("selectionStart" in input && document.activeElement == input) {
  13.         return {
  14.             start: input.selectionStart,
  15.             end: input.selectionEnd
  16.         };
  17.     }
  18.     else if (input.createTextRange) {
  19.         var sel = document.selection.createRange();
  20.         if (sel.parentElement() === input) {
  21.             var rng = input.createTextRange();
  22.             rng.moveToBookmark(sel.getBookmark());
  23.             for (var len = 0;
  24.                      rng.compareEndPoints("EndToStart", rng) > 0;
  25.                      rng.moveEnd("character", -1)) {
  26.                 len++;
  27.             }
  28.             rng.setEndPoint("StartToStart", input.createTextRange());
  29.             for (var pos = { start: 0, end: len };
  30.                      rng.compareEndPoints("EndToStart", rng) > 0;
  31.                      rng.moveEnd("character", -1)) {
  32.                 pos.start++;
  33.                 pos.end++;
  34.             }
  35.             return pos;
  36.         }
  37.     }
  38.     return -1;
  39. }
  40.  
  41. function setCursorPos(input, start, end) {
  42.     if (arguments.length < 3) end = start;
  43.     if ("selectionStart" in input) {
  44.         setTimeout(function() {
  45.             input.selectionStart = start;
  46.             input.selectionEnd = end;
  47.         }, 1);
  48.     }
  49.     else if (input.createTextRange) {
  50.         var rng = input.createTextRange();
  51.         rng.moveStart("character", start);
  52.         rng.collapse();
  53.         rng.moveEnd("character", end - start);
  54.         rng.select();
  55.     }
  56. }
  57.  
  58.  
  59. AVAILABLE = 8
  60.  
  61. function init() {
  62.     print_text("<textarea id='area'/>")
  63.     $(document).on('keypress', '#area', area_handler)
  64. }
  65.  
  66. function get_slots() {
  67.     split_it = (string) => { return string.split("") }
  68.     restrict_it = (array) => { return array.slice(0, AVAILABLE) }
  69.     return $('#area').val().split(/(\n)/g).map(split_it).map(restrict_it)
  70. }
  71.  
  72. function set_from_slots(slots) {
  73.     n_function = (slot) => slot.indexOf("\n")==-1 ? slot.length>=AVAILABLE ? slot.concat(["\n"]) : slot : slot
  74.     end_result = slots.map(n_function)
  75.     end_result = [].concat.apply([], end_result)
  76.     end_result = end_result.join("")
  77.     $('#area').val(end_result)
  78. }
  79.  
  80. function shift(direction, slots, line, col, filler='') {
  81.     common_checks_fail = () => (
  82.             line < 0 ||
  83.             line >= slots.length ||
  84.             col < 0 ||
  85.             col >= AVAILABLE
  86.             )
  87.  
  88.     non_empty_array = (elmn) => elmn.length > 0
  89.     compose_result = (insertion) => Array.prototype.concat(slots.slice(0, line), [insertion], slots.slice(line+1)).filter(non_empty_array)
  90.     right_check = () => slots[line].length >= AVAILABLE
  91.     left_result = () => slots[line].slice(0, col).concat(slots[line].slice(col+1, AVAILABLE+1))
  92.     right_result = () => slots[line].slice(0, col).concat(filler, slots[line].slice(col, AVAILABLE-1), slots[line].length >= AVAILABLE ? "\n" : "")
  93.  
  94.     if (common_checks_fail()) return slots
  95.  
  96.     switch(direction) {
  97.         case 'left': return compose_result(left_result())
  98.         case 'right': return right_check() ? slots : compose_result(right_result())
  99.     }
  100. }
  101.  
  102. function area_handler(event) {
  103.     key = event.which
  104.     slots = get_slots()
  105.     cur_pos = getCursorPos($('#area')[0])["start"]
  106.     cur_line = Math.floor(cur_pos / (AVAILABLE+1))
  107.     cur_col = cur_pos % (AVAILABLE+1)
  108.  
  109.     // ENTER = 13, not allowed
  110.     // BACKSPACE = 8: LSHIFT
  111.     // DELETE = 46: LSHIFT FOR NEXT
  112.     // ASSUME NO SELECTIONS
  113.  
  114.     if (key == 13) {
  115.         event.preventDefault() // Forbidden key
  116.         return
  117.     }
  118.  
  119.     if (key == 8 || key == 46) {
  120.         event.preventDefault()
  121.         new_slots = shift('left', slots, cur_line, cur_col+(key == 8 ? -1 : 1))
  122.         set_from_slots(new_slots)
  123.         setCursorPos($('#area')[0], cur_pos-1, cur_pos-1)
  124.         return
  125.     }
  126.  
  127.     if (event.key.length == 1) { // Ultimate shitcode
  128.         event.preventDefault()
  129.         new_slots = shift('right', slots, cur_line, cur_col, String.fromCharCode(key))
  130.         set_from_slots(new_slots)
  131.         return
  132.     }
  133. }
  134.  
  135. $(document).ready(init)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement