Meditor.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. class Meditor extends Class
  2. constructor: (@tag_original, body) ->
  3. @log "Create", @
  4. @tag_original.insertAdjacentHTML('beforeBegin', "<div class='meditor'></div>")
  5. @tag_container = @tag_original.previousSibling
  6. @tag_container.insertAdjacentHTML('afterBegin', @tag_original.outerHTML)
  7. @tag_original.style.display = "none"
  8. @tag = @tag_container.firstChild
  9. if body
  10. @tag.innerHTML = marked(body, {gfm: true, breaks: true})
  11. @
  12. load: =>
  13. if not window.AlloyEditor
  14. style = document.createElement("link")
  15. style.href = "alloy-editor/all.css"
  16. style.rel = "stylesheet"
  17. document.head.appendChild(style)
  18. script = document.createElement("script")
  19. script.src = "alloy-editor/all.js"
  20. document.head.appendChild(script)
  21. script.onload = @handleEditorLoad
  22. else
  23. @handleEditorLoad()
  24. handleEditorLoad: =>
  25. # Create ckeditor<>markdown edit mode switch button
  26. @tag.insertAdjacentHTML('beforeBegin', "<a href='#Markdown' class='meditor-editmode' title='Switch to markdown'>&lt;/&gt;</a>")
  27. @tag_editmode = @tag.previousSibling
  28. @tag_editmode.onclick = @handleEditmodeChange
  29. # Create ckeditor
  30. @editor = new CustomAlloyEditor(@tag)
  31. if @handleImageSave then @editor.handleImageSave = @handleImageSave
  32. # Create markdown editor textfield
  33. @tag.insertAdjacentHTML('beforeBegin', @tag_original.outerHTML)
  34. @tag_markdown = @tag.previousSibling
  35. @tag_markdown.innerHTML = "<textarea class='meditor-markdown'>MARKDOWN</textarea>"
  36. @autoHeight(@tag_markdown.firstChild)
  37. @tag_markdown.firstChild.oninput = =>
  38. @autoHeight(@tag_markdown.firstChild)
  39. @tag_markdown.style.display = "none"
  40. # Call onLoad for external scripts
  41. setTimeout ( =>
  42. @onLoad?()
  43. ), 1
  44. autoHeight: (elem) ->
  45. height_before = elem.style.height
  46. if height_before
  47. elem.style.height = "0px"
  48. h = elem.offsetHeight
  49. scrollh = elem.scrollHeight
  50. elem.style.height = height_before
  51. if scrollh > h
  52. elem.style.height = scrollh+"px"
  53. elem.style.scrollTop = "0px"
  54. else
  55. elem.style.height = height_before
  56. getMarkdown: ->
  57. if @tag_editmode.classList.contains("markdown")
  58. back = @tag_markdown.firstChild.value
  59. else
  60. back = toMarkdown(@tag.innerHTML, {gfm: true})
  61. return back
  62. getHtml: ->
  63. if @tag_editmode.classList.contains("markdown")
  64. back = marked(@tag_markdown.firstChild.value, {gfm: true, breaks: true})
  65. else
  66. back = marked(@getMarkdown(), {gfm: true, breaks: true})
  67. handleEditmodeChange: =>
  68. if @tag_editmode.classList.contains("markdown")
  69. # Change to ckeditor
  70. @tag_markdown.style.display = "none"
  71. @tag.style.display = ""
  72. @tag.innerHTML = @getHtml()
  73. else
  74. # Change to markdown
  75. @tag_markdown.style.display = ""
  76. @tag_markdown.style.width = @tag.offsetWidth+"px"
  77. @tag.style.display = "none"
  78. @tag_markdown.firstChild.value = @getMarkdown()
  79. @autoHeight(@tag_markdown.firstChild)
  80. @tag_editmode.classList.toggle("markdown")
  81. return false
  82. save: =>
  83. @tag_original.innerHTML = @getHtml()
  84. remove: =>
  85. @tag_editmode.remove()
  86. @tag_markdown.remove()
  87. @tag_original.style.display = ""
  88. @tag.remove()
  89. val: =>
  90. return @getMarkdown()
  91. window.Meditor = Meditor