Guest User

Untitled

a guest
Oct 15th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. var firstLineLimit = 30;
  2. var secondLineLimit = 55;
  3.  
  4. $("<Textarea id>").keyup(function () {
  5. var value = $(this).val();
  6.  
  7. var lines = value.split("\n");
  8. var totalLines = lines.length;
  9.  
  10. var firstLine = lines[0];
  11. var firstLineLength = firstLine.length;
  12. var firstLineRest = '';
  13.  
  14. var cursorPosition = $(this).caret().start;
  15.  
  16. // If the first line is too long, susbtract the rest.
  17. if (firstLineLength > firstLineLimit) {
  18. // Get the rest of the first line.
  19. var restLength = firstLineLength - firstLineLimit;
  20. firstLineRest = firstLine.substr(firstLineLimit, restLength);
  21.  
  22. // Substract the first line to its maximum length.
  23. firstLine = firstLine.substr(0, firstLineLimit);
  24. }
  25.  
  26. // Write rest of chars for first line to screen.
  27. $('<wrapper id> .first-line').html(firstLineLimit - firstLine.length);
  28.  
  29. // Remove the first line from the text part.
  30. var restLines = lines.splice(1, (lines.length - 1));
  31.  
  32. // Paste the rest of the lines together as the second line.
  33. var secondLine = restLines.join(' ');
  34.  
  35. // Paste the rest of the first line together with the second line.
  36. secondLine = firstLineRest + secondLine;
  37.  
  38. var secondLineLength = secondLine.length;
  39.  
  40. // If the second line is too long, susbtract the rest.
  41. if (secondLineLength > secondLineLimit) {
  42. // Substract the second line to its maximum length.
  43. secondLine = secondLine.substr(0, secondLineLimit);
  44. }
  45.  
  46. // Write rest of chars for second line to screen.
  47. $('<wrapper id> .second-line').html(secondLineLimit - secondLine.length);
  48.  
  49. // If there is a second line or there were multiple lines,
  50. // add the newline to the first line.
  51. if (secondLine.length || (totalLines > 1)) {
  52. // Paste first and second line together with a newline in between.
  53. value = firstLine + "\n" + secondLine;
  54. }
  55.  
  56. // Else, just keep the first line as a value.
  57. else {
  58. value = firstLine;
  59. }
  60.  
  61. // Fill in the new value.
  62. $(this).val(value).caret(cursorPosition, cursorPosition);
  63. });
Add Comment
Please, Sign In to add comment