-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add back transloadit-textarea example
- Loading branch information
Showing
5 changed files
with
442 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<link rel="stylesheet" href="https://releases.transloadit.com/uppy/robodog/v3.0.0-beta.4/robodog.css"> | ||
<style> | ||
body { | ||
font-family: Roboto, Open Sans; | ||
background: #f4f5f6; | ||
} | ||
h1, h2, h3, h4, h5, h6 { | ||
font-family: Oxygen, Raleway, Open Sans; | ||
} | ||
|
||
header { | ||
width: 800px; | ||
margin: auto; | ||
} | ||
header h1 { text-align: center; } | ||
|
||
main { | ||
background: white; | ||
margin: auto; | ||
width: 800px; | ||
border: 2px solid #e8eaee; | ||
padding: 36px; | ||
border-radius: 5px; | ||
} | ||
|
||
main p:first-child { margin-top: 0 } | ||
|
||
label { | ||
display: flex; | ||
padding-bottom: 16px; | ||
border-bottom: 1px solid #e8eaee; | ||
margin-bottom: 16px; | ||
} | ||
.label-text { | ||
width: 25%; | ||
text-transform: uppercase; | ||
color: #888888; | ||
line-height: 34px; | ||
} | ||
label input, label textarea { | ||
flex-grow: 1; | ||
height: 34px; | ||
} | ||
label textarea { | ||
box-sizing: border-box; | ||
padding: 6px; | ||
border-radius: 5px; | ||
border: 1px solid #b7b7b4; | ||
font-family: Roboto, Open Sans; | ||
min-height: 160px; | ||
width: 100%; | ||
resize: vertical; | ||
} | ||
|
||
.form-createSnippet { | ||
border: 1px solid #2e6da4; | ||
border-bottom-width: 3px; | ||
padding: 8px; | ||
border-radius: 5px; | ||
background-color: #337ab7; | ||
color: white; | ||
width: 100%; | ||
} | ||
|
||
.mdtxt { | ||
width: 100%; | ||
height: 100%; | ||
} | ||
.mdtxt-controls { | ||
} | ||
.mdtxt-upload { | ||
box-sizing: border-box; | ||
width: 100%; | ||
border-radius: 0 0 5px 5px; | ||
border: 1px solid #b7b7b4; | ||
border-top-width: 0; | ||
padding: 4px 8px; | ||
cursor: pointer; | ||
} | ||
.mdtxt textarea { | ||
margin-bottom: 0; | ||
border-bottom-left-radius: 0; | ||
border-bottom-right-radius: 0; | ||
} | ||
.mdtxt-upload .error { | ||
border-color: red; | ||
color: red; | ||
} | ||
.mdtxt-upload .error .message { | ||
margin-right: 8px; | ||
} | ||
|
||
.snippet { | ||
margin-top: 25px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<header> | ||
<h1>Markdown Bin</h1> | ||
</header> | ||
<main> | ||
<p> | ||
Markdown Bin is a demo app that works a bit like Github Gists or pastebin. You can add markdown snippets, and add file attachments to each snippet by clicking "Upload an attachment" or by dragging files onto the text area. Transloadit generates an inline preview image for images, videos, and audio files. <a target="_blank" href="https://github.com/transloadit/uppy/blob/main/examples/transloadit-textarea/template.json">» View the Assembly Template here.</a> | ||
</p> | ||
|
||
<p> | ||
⚠️ For this demo, snippets are stored locally in your browser. Attachments are stored in Transloadit's temporary storage and expire after about 24 hours. In a real app, you can easily export files to a permanent storage solution like Amazon S3 or Google Cloud. <a target="_blank" href="https://transloadit.com/docs/#17-saving-conversion-results">» Learn more</a> | ||
</p> | ||
|
||
<form id="new"> | ||
<h2>Create a new snippet</h2> | ||
<label> | ||
<span class="label-text">Snippet Title</span> | ||
<input type="text" name="title" placeholder="Snippet Title"> | ||
</label> | ||
<label> | ||
<textarea name="snippet"></textarea> | ||
</label> | ||
<p> | ||
<button class="form-createSnippet" type="submit"> | ||
Create | ||
</button> | ||
</p> | ||
</form> | ||
|
||
<h2>Previous snippets</h2> | ||
|
||
<div id="snippets"> | ||
</div> | ||
</main> | ||
<template id="snippet"> | ||
<div class="snippet"> | ||
<h3 class="snippet-title"></h3> | ||
<div class="snippet-content"></div> | ||
</div> | ||
</template> | ||
<script src="./main.js" type="module"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* eslint-env browser */ | ||
import marked from 'marked' | ||
import dragdrop from 'drag-drop' | ||
|
||
const robodog = {} | ||
|
||
const TRANSLOADIT_EXAMPLE_KEY = '35c1aed03f5011e982b6afe82599b6a0' | ||
const TRANSLOADIT_EXAMPLE_TEMPLATE = '0b2ee2bc25dc43619700c2ce0a75164a' | ||
|
||
/** | ||
* A textarea for markdown text, with support for file attachments. | ||
* | ||
* ## Usage | ||
* | ||
* ```js | ||
* const element = document.querySelector('textarea') | ||
* const mdtxt = new MarkdownTextarea(element) | ||
* mdtxt.install() | ||
* ``` | ||
*/ | ||
class MarkdownTextarea { | ||
constructor (element) { | ||
this.element = element | ||
this.controls = document.createElement('div') | ||
this.controls.classList.add('mdtxt-controls') | ||
this.uploadLine = document.createElement('div') | ||
this.uploadLine.classList.add('mdtxt-upload') | ||
|
||
this.uploadLine.appendChild( | ||
document.createTextNode('Upload an attachment'), | ||
) | ||
} | ||
|
||
install () { | ||
const { element } = this | ||
const wrapper = document.createElement('div') | ||
wrapper.classList.add('mdtxt') | ||
element.parentNode.replaceChild(wrapper, element) | ||
wrapper.appendChild(this.controls) | ||
wrapper.appendChild(element) | ||
wrapper.appendChild(this.uploadLine) | ||
|
||
this.setupUploadLine() | ||
} | ||
|
||
setupTextareaDrop () { | ||
dragdrop(this.element, (files) => { | ||
this.uploadFiles(files) | ||
}) | ||
} | ||
|
||
setupUploadLine () { | ||
this.uploadLine.addEventListener('click', () => { | ||
this.pickFiles() | ||
}) | ||
} | ||
|
||
reportUploadError (err) { | ||
this.uploadLine.classList.add('error') | ||
const message = document.createElement('span') | ||
message.appendChild(document.createTextNode(err.message)) | ||
this.uploadLine.insertChild(message, this.uploadLine.firstChild) | ||
} | ||
|
||
unreportUploadError () { | ||
this.uploadLine.classList.remove('error') | ||
const message = this.uploadLine.querySelector('message') | ||
if (message) { | ||
this.uploadLine.removeChild(message) | ||
} | ||
} | ||
|
||
insertAttachments (attachments) { | ||
attachments.forEach((attachment) => { | ||
const { file, thumb } = attachment | ||
const link = `\n[LABEL](${file.ssl_url})\n` | ||
const labelText = `View File ${file.basename}` | ||
if (thumb) { | ||
this.element.value += link.replace('LABEL', `![${labelText}](${thumb.ssl_url})`) | ||
} else { | ||
this.element.value += link.replace('LABEL', labelText) | ||
} | ||
}) | ||
} | ||
|
||
matchFilesAndThumbs (results) { | ||
const filesById = {} | ||
const thumbsById = {} | ||
|
||
results.forEach((result) => { | ||
if (result.stepName === 'thumbnails') { | ||
thumbsById[result.original_id] = result | ||
} else { | ||
filesById[result.original_id] = result | ||
} | ||
}) | ||
|
||
return Object.keys(filesById).map((key) => ({ | ||
file : filesById[key], | ||
thumb : thumbsById[key], | ||
})) | ||
} | ||
|
||
uploadFiles () { | ||
robodog.upload({ | ||
waitForEncoding: true, | ||
params: { | ||
auth: { key: TRANSLOADIT_EXAMPLE_KEY }, | ||
template_id: TRANSLOADIT_EXAMPLE_TEMPLATE, | ||
}, | ||
}).then((result) => { | ||
// Was cancelled | ||
if (result == null) return | ||
this.insertAttachments( | ||
this.matchFilesAndThumbs(result.results), | ||
) | ||
}).catch((err) => { | ||
console.error(err) | ||
this.reportUploadError(err) | ||
}) | ||
} | ||
|
||
pickFiles () { | ||
robodog.pick({ | ||
waitForEncoding: true, | ||
params: { | ||
auth: { key: TRANSLOADIT_EXAMPLE_KEY }, | ||
template_id: TRANSLOADIT_EXAMPLE_TEMPLATE, | ||
}, | ||
}).then((result) => { | ||
// Was cancelled | ||
if (result == null) return | ||
this.insertAttachments( | ||
this.matchFilesAndThumbs(result.results), | ||
) | ||
}).catch((err) => { | ||
console.error(err) | ||
this.reportUploadError(err) | ||
}) | ||
} | ||
} | ||
|
||
const textarea = new MarkdownTextarea( | ||
document.querySelector('#new textarea'), | ||
) | ||
textarea.install() | ||
|
||
function renderSnippet (title, text) { | ||
const template = document.querySelector('#snippet') | ||
const newSnippet = document.importNode(template.content, true) | ||
const titleEl = newSnippet.querySelector('.snippet-title') | ||
const contentEl = newSnippet.querySelector('.snippet-content') | ||
|
||
titleEl.appendChild(document.createTextNode(title)) | ||
contentEl.innerHTML = marked(text) | ||
|
||
const list = document.querySelector('#snippets') | ||
list.insertBefore(newSnippet, list.firstChild) | ||
} | ||
|
||
function saveSnippet (title, text) { | ||
const id = parseInt(localStorage.numSnippets || 0, 10) | ||
localStorage[`snippet_${id}`] = JSON.stringify({ title, text }) | ||
localStorage.numSnippets = id + 1 | ||
} | ||
|
||
function loadSnippets () { | ||
for (let id = 0; localStorage[`snippet_${id}`] != null; id += 1) { | ||
const { title, text } = JSON.parse(localStorage[`snippet_${id}`]) | ||
renderSnippet(title, text) | ||
} | ||
} | ||
|
||
document.querySelector('#new').addEventListener('submit', (event) => { | ||
event.preventDefault() | ||
|
||
const title = event.target.querySelector('input[name="title"]').value | ||
|| 'Unnamed Snippet' | ||
const text = textarea.element.value | ||
|
||
saveSnippet(title, text) | ||
renderSnippet(title, text) | ||
|
||
event.target.querySelector('input').value = '' | ||
event.target.querySelector('textarea').value = '' | ||
}) | ||
|
||
window.addEventListener('DOMContentLoaded', () => { | ||
loadSnippets() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "@uppy-example/transloadit-textarea", | ||
"version": "0.0.0", | ||
"dependencies": { | ||
"drag-drop": "^7.2.0", | ||
"ecstatic": "^4.1.4", | ||
"marked": "^4.0.18" | ||
}, | ||
"type": "module", | ||
"private": true, | ||
"scripts": { | ||
"start": "ecstatic" | ||
} | ||
} |
Oops, something went wrong.