drop.that 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +91 -0
- package/dist/lib.d.ts +32 -0
- package/dist/lib.js +330 -0
- package/package.json +18 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Martin Rohrmeier <roryphant@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# drop.that
|
|
2
|
+
|
|
3
|
+
A drop input and download output component for web applications.
|
|
4
|
+
Typically to process an input file provided by the user, like in
|
|
5
|
+
online file conversion tools. This is useful in pure frontend
|
|
6
|
+
web applications, where you can't or don't want to deal with any
|
|
7
|
+
backend API or database storage.
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
In your web app (pseudocode)
|
|
12
|
+
|
|
13
|
+
``` JavaScript
|
|
14
|
+
input = receiveInput() // handled by drop.that
|
|
15
|
+
output = process(input) // you only implement the logic
|
|
16
|
+
downloadOutput(output) // handled by drop.that
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Example: https://oyo.github.io/drop.that/
|
|
20
|
+
|
|
21
|
+
### Get started
|
|
22
|
+
|
|
23
|
+
Here is a simple example. Drop, upload, paste or enter some text.
|
|
24
|
+
It will split the words into lines.
|
|
25
|
+
|
|
26
|
+
``` html
|
|
27
|
+
<body>
|
|
28
|
+
<script type="module">
|
|
29
|
+
import { dropIn, dropOut } from 'https://oyo.github.io/drop.that/lib.js'
|
|
30
|
+
const input = await dropIn()
|
|
31
|
+
const output = input.split(' ').join('\n')
|
|
32
|
+
await dropOut(output)
|
|
33
|
+
</script>
|
|
34
|
+
</body>
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
See https://oyo.github.io/drop.that/0-simple.html
|
|
38
|
+
|
|
39
|
+
### Customize
|
|
40
|
+
|
|
41
|
+
Here is another example: formatting a JSON file.
|
|
42
|
+
This time we don't show any overlay.
|
|
43
|
+
Simply drag a JSON file into the browser window and it gets
|
|
44
|
+
processed and downloaded immediately on dropping a valid input.
|
|
45
|
+
|
|
46
|
+
``` JavaScript
|
|
47
|
+
const input = await dropIn({
|
|
48
|
+
showUI: false,
|
|
49
|
+
autoStart: true,
|
|
50
|
+
valid: valid.isJSON,
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
const output = JSON.stringify(
|
|
54
|
+
JSON.parse(input), null, 2
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
await dropOut(
|
|
58
|
+
output,
|
|
59
|
+
{
|
|
60
|
+
autoDownload: true,
|
|
61
|
+
downloadFileMimeType: 'application/json'
|
|
62
|
+
}
|
|
63
|
+
)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
See https://oyo.github.io/drop.that/1-json.html
|
|
67
|
+
|
|
68
|
+
### Node.js
|
|
69
|
+
|
|
70
|
+
In a Node.js environment add the library
|
|
71
|
+
|
|
72
|
+
``` bash
|
|
73
|
+
npm i drop.that
|
|
74
|
+
# or
|
|
75
|
+
yarn add drop.that
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
then import
|
|
79
|
+
|
|
80
|
+
``` JavaScript
|
|
81
|
+
import { dropIn, dropOut } from 'drop.that'
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Usage
|
|
85
|
+
|
|
86
|
+
Currently only single files in any text file format are supported as input.
|
|
87
|
+
|
|
88
|
+
### Caveats
|
|
89
|
+
|
|
90
|
+
Not meant to process huge amounts of data.
|
|
91
|
+
The style is not customizable yet.
|
package/dist/lib.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
type Validator = (text: string) => boolean
|
|
2
|
+
|
|
3
|
+
declare const valid: Record<string, Validator>
|
|
4
|
+
|
|
5
|
+
declare interface DropInOptions {
|
|
6
|
+
parent: HTMLElement,
|
|
7
|
+
fetchDefault: string,
|
|
8
|
+
autoStart: boolean,
|
|
9
|
+
showUI: boolean,
|
|
10
|
+
valid: Validator,
|
|
11
|
+
placeholderText: string,
|
|
12
|
+
startButtonText: string,
|
|
13
|
+
clearButtonText: string,
|
|
14
|
+
pasteButtonText: string,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare interface DropOutOptions {
|
|
18
|
+
parent: HTMLElement,
|
|
19
|
+
autoDownload: boolean,
|
|
20
|
+
autoCopy: boolean,
|
|
21
|
+
copyButtonText: string,
|
|
22
|
+
downloadButtonText: string,
|
|
23
|
+
downloadFileName: string,
|
|
24
|
+
downloadFileMimeType: string,
|
|
25
|
+
rawButtonText: string,
|
|
26
|
+
previewButtonText: string,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare function dropIn(options: Partial<DropInOptions>): Promise<string>
|
|
30
|
+
declare function dropOut(output: string, options: Partial<DropOutOptions>): Promise<void>
|
|
31
|
+
|
|
32
|
+
export { dropIn, dropOut, valid, type Validator }
|
package/dist/lib.js
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
const styleDefinitions = `
|
|
2
|
+
.dropin,.dropout {
|
|
3
|
+
position: fixed;
|
|
4
|
+
margin: 0;
|
|
5
|
+
padding: 0;
|
|
6
|
+
top: 0;
|
|
7
|
+
left: 0;
|
|
8
|
+
width: 100%;
|
|
9
|
+
height: 100%;
|
|
10
|
+
min-height: 100%;
|
|
11
|
+
overflow: hidden;
|
|
12
|
+
display: flex;
|
|
13
|
+
flex-direction: column;
|
|
14
|
+
justify-content: center;
|
|
15
|
+
align-items: center;
|
|
16
|
+
background-color: rgb(15, 15, 35);
|
|
17
|
+
color: white;
|
|
18
|
+
font-family: "Source Code Pro", monospace;
|
|
19
|
+
font-size: 2vh;
|
|
20
|
+
z-index: 999;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.drag {
|
|
24
|
+
opacity: 0.5;
|
|
25
|
+
background-color: #858140;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.fill {
|
|
29
|
+
width: 100%;
|
|
30
|
+
min-width: 260px;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.dropinput,
|
|
34
|
+
.dropoutput {
|
|
35
|
+
width: 70%;
|
|
36
|
+
min-width: 360px;
|
|
37
|
+
top: 15%;
|
|
38
|
+
height: 70%;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.dropinput > span,
|
|
42
|
+
.dropoutput > span {
|
|
43
|
+
width: 100%;
|
|
44
|
+
display: flex;
|
|
45
|
+
flex-direction: row;
|
|
46
|
+
justify-content: space-between;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
#outputarea {
|
|
50
|
+
height: 90%;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.dropinput textarea,
|
|
54
|
+
.dropoutput #outputarea #rawarea,
|
|
55
|
+
.dropoutput #outputarea #previewarea {
|
|
56
|
+
width: 100%;
|
|
57
|
+
height: 90%;
|
|
58
|
+
padding: 10px;
|
|
59
|
+
box-sizing: border-box;
|
|
60
|
+
font-size: 16px;
|
|
61
|
+
white-space: pre;
|
|
62
|
+
overflow-wrap: normal;
|
|
63
|
+
overflow-x: scroll;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
#inputarea,
|
|
67
|
+
#rawarea {
|
|
68
|
+
background: #222;
|
|
69
|
+
color: #eee;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
#previewarea {
|
|
73
|
+
background: #808080;
|
|
74
|
+
color: black;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
.action {
|
|
78
|
+
width: 160px;
|
|
79
|
+
height: 40px;
|
|
80
|
+
margin: 0 auto;
|
|
81
|
+
font-family: monospace;
|
|
82
|
+
font-size: 24px;
|
|
83
|
+
}
|
|
84
|
+
`
|
|
85
|
+
const style = document.createElement('style')
|
|
86
|
+
style.textContent = styleDefinitions
|
|
87
|
+
|
|
88
|
+
export const valid = {
|
|
89
|
+
isNotEmpty: v => v.length > 0,
|
|
90
|
+
isJSON: v => {
|
|
91
|
+
try {
|
|
92
|
+
JSON.parse(v)
|
|
93
|
+
} catch (e) {
|
|
94
|
+
return false
|
|
95
|
+
}
|
|
96
|
+
return true
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const optionsIn = {
|
|
101
|
+
parent: document.body,
|
|
102
|
+
fetchDefault: '',
|
|
103
|
+
autoStart: false,
|
|
104
|
+
showUI: true,
|
|
105
|
+
valid: valid.isNotEmpty,
|
|
106
|
+
placeholderText: 'paste input or drop input file',
|
|
107
|
+
startButtonText: 'start',
|
|
108
|
+
clearButtonText: 'clear',
|
|
109
|
+
pasteButtonText: 'paste',
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const optionsOut = {
|
|
113
|
+
parent: document.body,
|
|
114
|
+
autoDownload: false,
|
|
115
|
+
autoCopy: false,
|
|
116
|
+
copyButtonText: 'copy',
|
|
117
|
+
downloadButtonText: 'download',
|
|
118
|
+
downloadFileName: 'output',
|
|
119
|
+
downloadFileMimeType: 'text/plain',
|
|
120
|
+
rawButtonText: 'raw',
|
|
121
|
+
previewButtonText: 'preview',
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export const dropIn = async (opts = {}) =>
|
|
125
|
+
|
|
126
|
+
new Promise((resolve) => {
|
|
127
|
+
|
|
128
|
+
const config = { ...optionsIn, ...opts }
|
|
129
|
+
|
|
130
|
+
const debounce = (func, timeout = 250) => {
|
|
131
|
+
let timer
|
|
132
|
+
return (...args) => {
|
|
133
|
+
clearTimeout(timer)
|
|
134
|
+
timer = setTimeout(() => func.apply(this, args), timeout)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const processText = debounce((text) => {
|
|
139
|
+
startButton.disabled = !config.valid(text)
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
const overlay = document.createElement('div')
|
|
143
|
+
overlay.className = 'dropin'
|
|
144
|
+
config.parent.appendChild(overlay)
|
|
145
|
+
|
|
146
|
+
if (!config.showUI) {
|
|
147
|
+
overlay.style='background-color: rgba(15,15,35,0.5);'
|
|
148
|
+
} else {
|
|
149
|
+
overlay.innerHTML = `
|
|
150
|
+
<div class="dropinput">
|
|
151
|
+
<span>
|
|
152
|
+
<input type="file" class="fill" />
|
|
153
|
+
<button id="paste">${config.pasteButtonText}</button>
|
|
154
|
+
<button id="clear">${config.clearButtonText}</button>
|
|
155
|
+
</span>
|
|
156
|
+
<textarea id="inputarea" placeholder="${config.placeholderText}"></textarea>
|
|
157
|
+
<span>
|
|
158
|
+
<button id="start" class="action" ${config.valid('') ? '' : 'disabled'}>${config.startButtonText}</button>
|
|
159
|
+
</span>
|
|
160
|
+
</div>`
|
|
161
|
+
const inputarea = document.getElementById('inputarea')
|
|
162
|
+
inputarea.focus()
|
|
163
|
+
inputarea.addEventListener('input', (evt) => processText(evt.target.value))
|
|
164
|
+
const fileInput = document.querySelector('input[type=file]')
|
|
165
|
+
fileInput.addEventListener('change', (evt) => readFile(evt.target.files))
|
|
166
|
+
document.getElementById('paste').addEventListener('click', () => paste())
|
|
167
|
+
document.getElementById('clear').addEventListener('click', () => setInput(''))
|
|
168
|
+
const startButton = document.getElementById('start')
|
|
169
|
+
startButton.addEventListener('click', () => {
|
|
170
|
+
config.parent.removeChild(overlay)
|
|
171
|
+
resolve(inputarea.value)
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
overlay.appendChild(style)
|
|
175
|
+
|
|
176
|
+
config.fetchDefault && fetch(config.fetchDefault)
|
|
177
|
+
.then(response => response.ok ? response.text() : '')
|
|
178
|
+
.then(data => setInput(data))
|
|
179
|
+
|
|
180
|
+
overlay.addEventListener('dragover', (e) => {
|
|
181
|
+
e.preventDefault()
|
|
182
|
+
e.stopPropagation()
|
|
183
|
+
overlay.classList.add('drag')
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
overlay.addEventListener('dragleave', (e) => {
|
|
187
|
+
e.preventDefault()
|
|
188
|
+
e.stopPropagation()
|
|
189
|
+
overlay.classList.remove('drag')
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
overlay.addEventListener('drop', (e) => {
|
|
193
|
+
e.preventDefault()
|
|
194
|
+
e.stopPropagation()
|
|
195
|
+
|
|
196
|
+
overlay.classList.remove('drag')
|
|
197
|
+
readFile(e.dataTransfer.files)
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
const setInput = (text) => {
|
|
201
|
+
const isValid = config.valid(text)
|
|
202
|
+
if (config.showUI) {
|
|
203
|
+
inputarea.value = text
|
|
204
|
+
inputarea.select()
|
|
205
|
+
startButton.disabled = !isValid
|
|
206
|
+
startButton.focus()
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (config.autoStart && isValid) {
|
|
210
|
+
config.parent.removeChild(overlay)
|
|
211
|
+
resolve(text)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const readFile = (files) => {
|
|
216
|
+
if (files.length === 1) {
|
|
217
|
+
const file = files[0]
|
|
218
|
+
const reader = new FileReader()
|
|
219
|
+
reader.onload = (event) => {
|
|
220
|
+
setInput(event.target.result)
|
|
221
|
+
}
|
|
222
|
+
reader.onerror = (error) => {
|
|
223
|
+
console.error('error reading file:', error)
|
|
224
|
+
inputarea.value = '<error reading file>'
|
|
225
|
+
}
|
|
226
|
+
reader.readAsText(file)
|
|
227
|
+
} else {
|
|
228
|
+
inputarea.value = '<please drop a single input text file>'
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const paste = async () => {
|
|
233
|
+
try {
|
|
234
|
+
const text = await navigator.clipboard.readText()
|
|
235
|
+
setInput(text)
|
|
236
|
+
} catch (err) {
|
|
237
|
+
console.error('Failed to read clipboard contents: ', err)
|
|
238
|
+
inputarea.value = '<error reading clipboard>'
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
export const dropOut = async (output, opts = {}) =>
|
|
245
|
+
|
|
246
|
+
new Promise((resolve) => {
|
|
247
|
+
|
|
248
|
+
const config = { ...optionsOut, ...opts }
|
|
249
|
+
|
|
250
|
+
const download = () => {
|
|
251
|
+
const link = document.createElement('a')
|
|
252
|
+
const url = URL.createObjectURL(
|
|
253
|
+
new File([output], config.downloadFileName, {
|
|
254
|
+
type: config.downloadFileMimeType,
|
|
255
|
+
})
|
|
256
|
+
)
|
|
257
|
+
link.href = url
|
|
258
|
+
link.download = config.downloadFileName
|
|
259
|
+
config.parent.appendChild(link)
|
|
260
|
+
link.click()
|
|
261
|
+
config.parent.removeChild(link)
|
|
262
|
+
window.URL.revokeObjectURL(url)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const copy = async () => {
|
|
266
|
+
try {
|
|
267
|
+
await navigator.clipboard.writeText(output)
|
|
268
|
+
} catch (err) {
|
|
269
|
+
console.error('Failed to write to clipboard', err)
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (config.autoDownload || config.autoCopy) {
|
|
274
|
+
config.autoCopy && copy()
|
|
275
|
+
config.autoDownload && download()
|
|
276
|
+
resolve(true)
|
|
277
|
+
return
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const overlay = document.createElement('div')
|
|
281
|
+
overlay.className = 'dropout'
|
|
282
|
+
overlay.innerHTML = `
|
|
283
|
+
<div class="dropoutput">
|
|
284
|
+
<span>
|
|
285
|
+
<span class="fill"></span>
|
|
286
|
+
<button id="preview" disabled>${config.previewButtonText}</button>
|
|
287
|
+
<button id="raw">${config.rawButtonText}</button>
|
|
288
|
+
</span>
|
|
289
|
+
<div id="outputarea">
|
|
290
|
+
<textarea id="rawarea" disabled>${output}</textarea>
|
|
291
|
+
<div id="previewarea">${output}</div>
|
|
292
|
+
</div>
|
|
293
|
+
<span>
|
|
294
|
+
<button id="download" class="action">${config.downloadButtonText}</button>
|
|
295
|
+
<button id="copy" class="action">${config.copyButtonText}</button>
|
|
296
|
+
</span>
|
|
297
|
+
</div>`
|
|
298
|
+
overlay.appendChild(style)
|
|
299
|
+
config.parent.appendChild(overlay)
|
|
300
|
+
const outputarea = document.getElementById('outputarea')
|
|
301
|
+
const rawarea = document.getElementById('rawarea')
|
|
302
|
+
rawarea.focus()
|
|
303
|
+
rawarea.select()
|
|
304
|
+
outputarea.removeChild(rawarea)
|
|
305
|
+
const previewarea = document.getElementById('previewarea')
|
|
306
|
+
const rawButton = document.getElementById('raw')
|
|
307
|
+
rawButton.addEventListener('click', () => {
|
|
308
|
+
outputarea.removeChild(previewarea)
|
|
309
|
+
outputarea.appendChild(rawarea)
|
|
310
|
+
previewButton.removeAttribute('disabled')
|
|
311
|
+
rawButton.setAttribute('disabled', true)
|
|
312
|
+
})
|
|
313
|
+
const previewButton = document.getElementById('preview')
|
|
314
|
+
previewButton.addEventListener('click', () => {
|
|
315
|
+
outputarea.removeChild(rawarea)
|
|
316
|
+
outputarea.appendChild(previewarea)
|
|
317
|
+
rawButton.removeAttribute('disabled')
|
|
318
|
+
previewButton.setAttribute('disabled', true)
|
|
319
|
+
})
|
|
320
|
+
const downloadButton = document.getElementById('download')
|
|
321
|
+
downloadButton.addEventListener('click', () => {
|
|
322
|
+
download()
|
|
323
|
+
resolve(true)
|
|
324
|
+
})
|
|
325
|
+
downloadButton.addEventListener('click', () => {
|
|
326
|
+
copy()
|
|
327
|
+
resolve(true)
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
})
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "drop.that",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Generic drop/paste input and download/copy output component for web applications.",
|
|
5
|
+
"author": "Martin Rohrmeier <roryphant@gmail.com> (oyo)",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/lib.js",
|
|
9
|
+
"types": "./dist/lib.d.ts",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/oyo/drop.that.git"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "mkdir -p dist && cp src/lib* dist",
|
|
16
|
+
"clear": "rm -rf dist"
|
|
17
|
+
}
|
|
18
|
+
}
|