main.js 5.4 KB

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