Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. var lastSpaceIndex = null;
  2. var lastLineBreak = 0;
  3.  
  4. function checkLineLength (str) {
  5. var strLength = str.length;
  6.  
  7. if(str.length === 0) {
  8. lastSpaceindex = 0;
  9. lastLineBreak = 0;
  10. }
  11.  
  12. if (strLength % 30 === 0) {
  13. lastlineBreak = strLength;
  14. insertLineBreak(str, lastLineBreak);
  15. }
  16. console.log((strLength - lastLineBreak));
  17. if (((strLength - lastLineBreak) > 30)) {
  18. lastLineBreak = lastSpaceIndex;
  19. insertLineBreak(str, lastLineBreak - 1);
  20. }
  21. lastSpaceIndex = strLength;
  22. }
  23.  
  24.  
  25. function insertLineBreak (str, index) {
  26. var newStr = str.split("");
  27. newStr[index] = '\n';
  28. console.log(newStr.join(''))
  29. }
  30.  
  31. document.addEventListener('keyup', function (evt) {
  32. if(evt.target.id === 'ta' && evt.keyCode === 13){
  33. lastLineBreak = evt.target.value.length;
  34. }
  35. if(evt.target.id === 'ta' && evt.keyCode === 32){
  36. checkLineLength(evt.target.value)
  37. }
  38. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement