Guest User

Untitled

a guest
Jun 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. /*
  2. - `»` and `«` to delimit indent/outdent tokens
  3. - two text areas side by side, one with »«, one without
  4. - edits to indent editor update the brackets, edits to bracket editor update indents in both to match brackets
  5. */
  6. mnt = document.body.querySelector('div')
  7. mnt && mnt.remove()
  8. document.body.appendChild(mnt = document.createElement('div'))
  9. out.innerText=''; (new Date)+"\n"+document.body.outerHTML
  10. mnt.innerHTML = `
  11. <pre contenteditable id=e_ind style=display:inline-block;background:#eff></pre>
  12. <pre contenteditable id=e_paren style=display:inline-block;background:#eff></pre>
  13. <pre id=e_json style=background:#eee></pre>
  14. `
  15. e_ind.innerText = `
  16. .${' '.repeat(20)}
  17. fn arg1
  18. arg2
  19. arg2a
  20. arg3
  21. `.trim()
  22. e_ind.onkeyup= ()=>{
  23. ind = []; indc=0
  24. stack = []; cur = []
  25. for(s of e_ind.innerText.split("\n")){
  26. n = s.match(/^ */)[0].length
  27. s = ( (n<indc && "«"+(indc-n)) || (n>indc && "»"+(n-indc)) || " " )+"|"+s
  28. while(n<indc){
  29. stack[0].cur.push(cur)
  30. cur = stack[0].cur
  31. indc = stack[0].indc
  32. stack.shift()
  33. }
  34. stack.unshift({indc,cur}); cur=[]
  35. cur.push(s.slice(3).replace(/^ */,""))
  36. indc = n
  37. ind.push(s)
  38. }
  39. if(e_paren.innerText != ind.join("\n")){
  40. e_paren.innerText = ind.join("\n")
  41. }
  42. while(stack[0]){
  43. stack[0].cur.push(cur)
  44. cur=stack[0].cur
  45. stack.shift()
  46. }
  47. e_json.innerText = JSON.stringify(cur,null,2)
  48. }
  49. e_ind.onkeyup()
  50. e_paren.onkeyup= ()=>{
  51. ind = []; indc = 0
  52. for(s of e_paren.innerText.split("\n")){
  53. c = s[0]; n=Number(s[1])||2; s=s.slice(3)
  54. indc += n*[-1,0,1][1+(c=="»")-(c=="«")]
  55. s = s.replace(/^ */," ".repeat(indc))
  56. ind.push(s)
  57. }
  58. e_ind.innerText = ind.join("\n")
  59. e_ind.onkeyup()
  60. }
  61. e_paren.onkeyup()
Add Comment
Please, Sign In to add comment