textarea.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. var textarea;
  2. onload=function(){
  3. textarea=document.getElementsByTagName('textarea')[0];
  4. }
  5. function getNewlinePos_back(text,pos){
  6. while(pos>=0 && text.charAt(pos)!='\n') pos--;
  7. return pos;
  8. }
  9. function getNewlinePos_forth(text,pos){
  10. let len = text.length;
  11. while(pos<len && text.charAt(pos)!='\n') pos++;
  12. return pos;
  13. }
  14. function deleteLine(ta){
  15. let text = ta.value;
  16. let lStart = getNewlinePos_back(text,ta.selectionStart-1);
  17. let lEnd = getNewlinePos_forth(text,ta.selectionEnd);
  18. ta.value = text.substring(0,lStart+1) + text.substring(lEnd+1);
  19. ta.selectionStart = ta.selectionEnd = lStart +1;
  20. ta.focus();
  21. }
  22. function moveUp(ta){
  23. let text = ta.value;
  24. let start = ta.selectionStart;
  25. let end = ta.selectionEnd;
  26. let lStart = getNewlinePos_back(text,start-1);
  27. let lEnd = getNewlinePos_forth(text,end);
  28. let prevS = getNewlinePos_back(text,lStart-1);
  29. ta.value = text.substring(0,prevS+1)+text.substring(lStart+1,lEnd+1)+
  30. text.substring(prevS+1,lStart+1) + text.substring(lEnd+1);
  31. ta.selectionStart = start - (lStart - prevS);
  32. ta.selectionEnd = end - (lStart - prevS);
  33. ta.focus();
  34. }
  35. function move2Top(ta){
  36. let text = ta.value;
  37. let start = ta.selectionStart;
  38. let end = ta.selectionEnd;
  39. let lStart = getNewlinePos_back(text,start-1);
  40. let lEnd = getNewlinePos_forth(text,end);
  41. ta.value = text.substring(lStart+1,lEnd+1)+text.substring(0,lStart+1)+
  42. text.substring(lEnd+1);
  43. ta.selectionStart = start - lStart-1;
  44. ta.selectionEnd = end - lStart-1;
  45. ta.focus();
  46. }
  47. function move2Bottom(ta){
  48. let text = ta.value;
  49. let start = ta.selectionStart;
  50. let end = ta.selectionEnd;
  51. let lStart = getNewlinePos_back(text,start-1);
  52. let lEnd = getNewlinePos_forth(text,end);
  53. ta.value = text.substring(0,lStart+1)+text.substring(lEnd+1)+
  54. text.substring(lStart+1,lEnd+1);
  55. ta.selectionStart = start + text.length - lEnd-1;
  56. ta.selectionEnd = end + text.length - lEnd-1;
  57. ta.focus();
  58. }
  59. function moveDown(ta){
  60. let text = ta.value;
  61. let start = ta.selectionStart;
  62. let end = ta.selectionEnd;
  63. let lStart = getNewlinePos_back(text,start-1);
  64. let lEnd = getNewlinePos_forth(text,end);
  65. let nextE = getNewlinePos_forth(text,lEnd+1);
  66. ta.value = text.substring(0,lStart+1) + text.substring(lEnd+1,nextE+1) +
  67. text.substring(lStart+1,lEnd+1)+text.substring(nextE+1);
  68. ta.selectionStart = start + (nextE - lEnd);
  69. ta.selectionEnd = end + (nextE - lEnd);
  70. ta.focus();
  71. }
  72. function save(fn){
  73. let u8=new TextEncoder().encode(textarea.value);let r='';for(let i=0;i<u8.byteLength;i++)r+=String.fromCharCode(u8[i]);location.href='i:0l'+fn+':'+btoa(r);
  74. }
  75. function onSave(){
  76. save(location.pathname);
  77. }