0.0.1 • Published 1 year ago

@mtillmann/file-ingest v0.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

File Ingest

A simple class to ingest files in the browser by drag and drop, copy and paste or file input.

Usage

Install the package:

npm install @mtillmann/file-ingest

After creating an instance, you'll receive a custom event file-ingest:files on the target element with the files as event.detail.files.

  • event.detail.files contains an array of files that passed the accept-option's MIME type check.
  • Set the includeRejectedFiles-option to true to include rejected files in event.detail.rejected.
  • If you set the emitWhenEmpty-option to true, the event will be emitted even when no matched files are present.

Bundler

import FileIngest from '@mtillmann/file-ingest';

const fileIngest = new FileIngest();

document.documentElement.addEventListener('file-ingest:files', (event) => {
  console.log(event.detail.files);
});

Module Script Tag

<script type="module">
  import FileIngest from '.../@mtillmann/file-ingest/dist/index.js';

  const fileIngest = new FileIngest();

  document.documentElement.addEventListener('file-ingest:files', (event) => {
    console.log(event.detail.files);
  });
</script>

Classic Script Tag

<script src=".../@mtillmann/file-ingest/dist/index.umd.js"></script>
<script>
  const fileIngest = new FileIngest();

  document.documentElement.addEventListener('file-ingest:files', (event) => {
    console.log(event.detail.files);
  });
</script>

Options

OptionTypeDefaultDescription
targetString\|HTMLElementdocument.documentElementElement or Selector String to attach the events to
acceptstring*/*List of MIME types that are accepted by the instance. Supports wildcard subtypes (e.g. image/*)
pastebooleantrueEnable or disable paste events
dropbooleantrueEnable or disable drop events
changebooleantrueEnable or disable change-events on [type=file]-inputs
preventDefaultbooleantruePrevent the default behavior of the events
dragClassesRecord\<string, string \| string[]\>...Classes to add and remove on drag events
applyDragClassesbooleantrueApply drag classes to the target element
ignorePasteOnInputbooleantrueIgnore paste events on input, textarea and [contenteditable=true]-elements
eventPrefixstringfile-ingestPrefix for the custom events
eventTargetString\|HTMLElementoptions.targetElement to dispatch the custom events on
callbackFunctionnullCallback function to call in addition to events - receives the content of event.detail as argument
includeRejectedFilesbooleanfalseInclude rejected files in the event detail - useful for error messages etc.
emitWhenEmptybooleanfalseEmit event even when no (matched) files are present

API

destroy()

Removes all event listeners and cleans up the instance.