main.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { marked } from 'marked'
  2. import Uppy from '@uppy/core'
  3. import DropTarget from '@uppy/drop-target'
  4. import Dashboard from '@uppy/dashboard'
  5. import Transloadit from '@uppy/transloadit'
  6. import RemoteSources from '@uppy/remote-sources'
  7. import Webcam from '@uppy/webcam'
  8. import ImageEditor from '@uppy/image-editor'
  9. import '@uppy/core/dist/style.css'
  10. import '@uppy/dashboard/dist/style.css'
  11. import '@uppy/image-editor/dist/style.css'
  12. const TRANSLOADIT_EXAMPLE_KEY = '35c1aed03f5011e982b6afe82599b6a0'
  13. const TRANSLOADIT_EXAMPLE_TEMPLATE = '0b2ee2bc25dc43619700c2ce0a75164a'
  14. /**
  15. * A textarea for markdown text, with support for file attachments.
  16. */
  17. class MarkdownTextarea {
  18. constructor (element) {
  19. this.element = element
  20. this.controls = document.createElement('div')
  21. this.controls.classList.add('mdtxt-controls')
  22. this.uploadLine = document.createElement('button')
  23. this.uploadLine.setAttribute('type', 'button')
  24. this.uploadLine.classList.add('form-upload')
  25. this.uploadLine.appendChild(
  26. document.createTextNode('Tap here to upload an attachment'),
  27. )
  28. }
  29. install () {
  30. const { element } = this
  31. const wrapper = document.createElement('div')
  32. wrapper.classList.add('mdtxt')
  33. element.parentNode.replaceChild(wrapper, element)
  34. wrapper.appendChild(this.controls)
  35. wrapper.appendChild(element)
  36. wrapper.appendChild(this.uploadLine)
  37. this.setupUppy()
  38. }
  39. setupUppy = () => {
  40. this.uppy = new Uppy({ autoProceed: true })
  41. .use(Transloadit, {
  42. waitForEncoding: true,
  43. params: {
  44. auth: { key: TRANSLOADIT_EXAMPLE_KEY },
  45. template_id: TRANSLOADIT_EXAMPLE_TEMPLATE,
  46. },
  47. })
  48. .use(DropTarget, { target: this.element })
  49. .use(Dashboard, { closeAfterFinish: true, trigger: '.form-upload' })
  50. .use(ImageEditor, { target: Dashboard })
  51. .use(Webcam, { target: Dashboard })
  52. .use(RemoteSources, { companionUrl: Transloadit.COMPANION })
  53. this.uppy.on('complete', (result) => {
  54. const { successful, failed, transloadit } = result
  55. if (successful.length !== 0) {
  56. this.insertAttachments(
  57. matchFilesAndThumbs(transloadit[0].results),
  58. )
  59. } else {
  60. failed.forEach(error => {
  61. console.error(error)
  62. this.reportUploadError(error)
  63. })
  64. }
  65. this.uppy.cancelAll()
  66. })
  67. }
  68. reportUploadError (err) {
  69. this.uploadLine.classList.add('error')
  70. const message = document.createElement('span')
  71. message.appendChild(document.createTextNode(err.message))
  72. this.uploadLine.insertChild(message, this.uploadLine.firstChild)
  73. }
  74. unreportUploadError () {
  75. this.uploadLine.classList.remove('error')
  76. const message = this.uploadLine.querySelector('message')
  77. if (message) {
  78. this.uploadLine.removeChild(message)
  79. }
  80. }
  81. insertAttachments (attachments) {
  82. attachments.forEach((attachment) => {
  83. const { file, thumb } = attachment
  84. const link = `\n[LABEL](${file.ssl_url})\n`
  85. const labelText = `View File ${file.basename}`
  86. if (thumb) {
  87. this.element.value += link.replace('LABEL', `![${labelText}](${thumb.ssl_url})`)
  88. } else {
  89. this.element.value += link.replace('LABEL', labelText)
  90. }
  91. })
  92. }
  93. uploadFiles = (files) => {
  94. const filesForUppy = files.map(file => {
  95. return {
  96. data: file,
  97. type: file.type,
  98. name: file.name,
  99. meta: file.meta || {},
  100. }
  101. })
  102. this.uppy.addFiles(filesForUppy)
  103. }
  104. }
  105. const textarea = new MarkdownTextarea(document.querySelector('#new textarea'))
  106. textarea.install()
  107. function renderSnippet (title, text) {
  108. const template = document.querySelector('#snippet')
  109. const newSnippet = document.importNode(template.content, true)
  110. const titleEl = newSnippet.querySelector('.snippet-title')
  111. const contentEl = newSnippet.querySelector('.snippet-content')
  112. titleEl.appendChild(document.createTextNode(title))
  113. contentEl.innerHTML = marked(text)
  114. const list = document.querySelector('#snippets')
  115. list.insertBefore(newSnippet, list.firstChild)
  116. }
  117. function saveSnippet (title, text) {
  118. const id = parseInt(localStorage.numSnippets || 0, 10)
  119. localStorage[`snippet_${id}`] = JSON.stringify({ title, text })
  120. localStorage.numSnippets = id + 1
  121. }
  122. function loadSnippets () {
  123. for (let id = 0; localStorage[`snippet_${id}`] != null; id += 1) {
  124. const { title, text } = JSON.parse(localStorage[`snippet_${id}`])
  125. renderSnippet(title, text)
  126. }
  127. }
  128. function matchFilesAndThumbs (results) {
  129. const filesById = {}
  130. const thumbsById = {}
  131. for (const [stepName, result] of Object.entries(results)) {
  132. result.forEach(result => {
  133. if (stepName === 'thumbnails') {
  134. thumbsById[result.original_id] = result
  135. } else {
  136. filesById[result.original_id] = result
  137. }
  138. })
  139. }
  140. return Object.keys(filesById).map((key) => ({
  141. file: filesById[key],
  142. thumb: thumbsById[key],
  143. }))
  144. }
  145. document.querySelector('#new').addEventListener('submit', (event) => {
  146. event.preventDefault()
  147. const title = event.target.elements['title'].value
  148. || 'Unnamed Snippet'
  149. const text = textarea.element.value
  150. saveSnippet(title, text)
  151. renderSnippet(title, text)
  152. // eslint-disable-next-line no-param-reassign
  153. event.target.querySelector('input').value = ''
  154. // eslint-disable-next-line no-param-reassign
  155. event.target.querySelector('textarea').value = ''
  156. })
  157. window.addEventListener('DOMContentLoaded', loadSnippets, { once: true })