textpaste.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. upload.modules.addmodule({
  2. name: 'textpaste',
  3. init: function () {
  4. $(document).on('submit', '#textview', this.save.bind(this))
  5. $(document).on('click', '#retbtn', this.closethis.bind(this))
  6. $(document).on('keydown', this.keypress.bind(this))
  7. },
  8. keypress: function(e) {
  9. if (!this.current) {
  10. return
  11. }
  12. if (!(e.which == 83 && (e.ctrlKey || e.metaKey))) {
  13. return
  14. }
  15. this.save()
  16. e.preventDefault()
  17. },
  18. save: function(e) {
  19. e ? e.preventDefault() : undefined
  20. var blob = new Blob([this.current.find('textarea').val()],
  21. {
  22. type: this.current.find('#create_mime').val()
  23. }
  24. )
  25. blob.name = this.current.find('#create_filename').val()
  26. window.location = '#noref'
  27. upload.route.setroute(upload.home)
  28. upload.home.doupload(blob)
  29. },
  30. cleanup: function() {
  31. delete this['closeback']
  32. delete this['current']
  33. },
  34. closethis: function() {
  35. var closeback = this.closeback
  36. this.current.remove()
  37. this.cleanup()
  38. closeback()
  39. },
  40. render: function(view, filename, data, mime, closeback) {
  41. var main = $('<form>').prop('id', 'textview').prop('autocomplete', 'off')
  42. main.appendTo(view)
  43. this.closeback = closeback
  44. this.current = main
  45. main.append($('<div>').addClass('topbar').append($('<div>').addClass('viewswitcher').append(
  46. $('<button>').prop('type', 'submit').text('Save').addClass('btn')
  47. ).append(
  48. $('<a>').prop('id', 'retbtn').text('Return').addClass('btn')
  49. )))
  50. var filenamefield = $('<input>').prop('id', 'create_filename').val(filename)
  51. var mimefield = $('<input>').prop('hidden', true).prop('id', 'create_mime').val(mime)
  52. main.append(filenamefield).append(mimefield)
  53. var area = $('<textarea>')
  54. var text = $('<div>').prop('id', 'create_text').addClass('previewtext')
  55. .append($('<div>').prop('id', 'create_linenos').append("&gt;"))
  56. .append(area)
  57. main.append(text)
  58. area.val(data).focus()[0].setSelectionRange(0, 0)
  59. area.scrollTop(0)
  60. }
  61. })