# file-choice

> Helper class for selecting files by drag and drop,file reference

Latest version **1.2.0** (published 2021-10-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install file-choice
pnpm add file-choice
yarn add file-choice
bun add file-choice
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2021-10-04 |
| First published | 2020-02-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 27.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Tom Misawa |
| Maintainers | riversun |

## Links

- npm: https://www.npmjs.com/package/file-choice
- Repository: https://github.com/riversun/file-choice
- Homepage: https://github.com/riversun/file-choice#readme
- Issues: https://github.com/riversun/file-choice/issues
- npm.io page: https://npm.io/package/file-choice

## Dependencies (1)

- [@riversun/event-emitter](https://npm.io/package/@riversun/event-emitter.md) ^1.5.2

## Recent versions

- 1.2.0 (latest) — 2021-10-04
- 1.1.2 — 2021-05-27
- 1.1.1 — 2020-03-06
- 1.1.0 — 2020-03-06
- 1.0.2 — 2020-02-14
- 1.0.1 — 2020-02-14
- 1.0.0 — 2020-02-14

## README

# file-choice

[![npm version](https://badge.fury.io/js/%40riversun%2Fsimple-date-format.svg)](https://badge.fury.io/js/%40riversun%2Fsimple-date-format)
[![](https://data.jsdelivr.com/v1/package/npm/@riversun/simple-date-format/badge)](https://www.jsdelivr.com/package/npm/@riversun/simple-date-format)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Helper library for dragging and dropping files.

This library allows you to do the following:

- Files can be obtained by drag and drop
- Click on the link to browse local files
- You can paste the image data on the clipboard and get it as a file

# Demo

https://riversun.github.io/file-choice

# Install

- **NPM**

```
npm install file-choice
```

or

- **use `<script>` tag**  from CDN

```html                                      
<script src="https://cdn.jsdelivr.net/npm/file-choice@1.1.1/lib/file-choice.js"></script>
```

# Usage

## HTML

Here is the typical html for file-choice

'file-choice' enables file drag & drop function for an element with 'fc-drop-area' specified as CSS class name.

If there are multiple elements with fc-drop-area in the dom tree, all of them will be drag-drop enabled.

![filechoice_ex1](https://user-images.githubusercontent.com/11747460/74494346-25310380-4f18-11ea-86e3-dbcc8327f6bd.gif)

 ```html

<div id="file-drop-area" class="fc-drop-area">
    <div class="fc-drop-area-inside">
        <div class="fc-disp-default">
            <p>You can paste an image from the clipboard as well as dropping a file.</p>
        </div>
        <div class="fc-disp-drag">
            <p>Drop here to add the file</p>
        </div>
        <div class="fc-disp-drag-not-allow">
            <p>The object cannot be dropped</p>
        </div>
        <p class="fc-visible-default">OR</p>
        <div class="fc-file-ref">
            <span>Select file</span>
        </div>
    </div>
</div>
``` 

## Code

By creating a `FileChoice` object and setting an event handler by
`fileChoice.events().on('filedrop', (data) => {});`, you can get the file that was dropped or picked up from the dialog.

 ```javascript 
import FileChoice from 'file-choice';// for npm environment

    const fileChoice = new FileChoice();

    //set 'filedrop' event of file-choice
    fileChoice.events().on('filedrop', (data) => {

        //data.target is the element with class="fc-drop-area".
        const target = data.target;
        const files = data.files;

        const droppedFileInfo = [];

        for (const file of files) {

            droppedFileInfo.push(
                {
                    name: file.name,
                    size: file.size,
                    type: file.type
                }
            );
        }

        alert(JSON.stringify(droppedFileInfo));
    });


```

## Enable "paste" from Clipboard

- pasete "image"
  Create `FileChoice` object with option like below.

You can paste "image" data as a file by "ctrl+V" from clipboard.

```javascript
const fileChoice = new FileChoice({
  pasteEnabled: true
});
const files = data.files;

for (const file of files) {

  if (file.type.startsWith('image/')) {
    const image = await fileChoice.getImageFromFileAsync(file, image => {
      // to do handling image
    });
  }
}
```

![filechoice_ex2](https://user-images.githubusercontent.com/11747460/74494349-295d2100-4f18-11ea-9073-b804a100b320.gif)

- paste "string" like a text/plain , text/html

```js
const fileChoice = new FileChoice({
  pasteEnabled: true
});

fileChoice.events().on('paste', async (data) => {
  const clipboardItems = data.itemMap;

  const previewArea = document.querySelector('#image-preview');
  previewArea.innerHTML = '';

  let item = clipboardItems.get('text/html');
  if (!item) {
    // get the first item of the map
    item = clipboardItems.values().next().value;
  }

  const clipboardText = await fileChoice.getStringFromClipboardItemAsync(item);
  console.log('clipboardText', clipboardText);
});
```

## styling

```css
/* Entire file drop area */
.fc-drop-area {
    border: solid 1px #ccc;
    background: #eee;
    padding: 0px;
    text-align: center;
}

.fc-drop-area p {
    color: #666666;
}

/* Default file-drop-area-view style before dragging files */

.fc-drop-area-inside {
    color: #333;
    padding: 6px;
    margin: 6px;
    border: 5px solid transparent;
}

.fc-drop-area-inside div.fc-disp-default, .fc-drop-area-inside div.fc-disp-drag, .fc-drop-area-inside div.fc-disp-drag-not-allow {
    font-weight: bold;
}


.fc-disp-default {
    display: block;
}

.fc-visible-default {
    visibility: visible;
}

.fc-disp-drag {
    display: none;

}

.fc-disp-drag-not-allow {
    display: none;

}

.fc-disp-drag-not-allow > p {
    color: red;
}

.fc-file-ref {
    visibility: visible;

}

/* /Default file-drop-area-view style before dragging files */


/* When an allowed object is being dragged */
.fc-drop-area.dragover .fc-drop-area-inside {
    border: 5px dashed #ddd;
}

.fc-drop-area.dragover .fc-disp-drag {
    display: block;
}

.fc-drop-area.dragover .fc-disp-drag-not-allow {
    display: none;
}

.fc-drop-area.dragover .fc-disp-default {
    display: none;
}

.fc-drop-area.dragover .fc-visible-default {
    visibility: hidden;
}

.fc-drop-area.dragover .fc-file-ref {
    visibility: hidden;
}

/* /When an allowed object is being dragged */

/* When a not-allowed object is being dragged */
.fc-drop-area.dragover-not-allowed .fc-drop-area-inside {
    border: 5px dashed #ddd;
}

.fc-drop-area.dragover-not-allowed .fc-disp-drag {
    display: none;
}

.fc-drop-area.dragover-not-allowed .fc-disp-drag-not-allow {
    display: block;
}

.fc-drop-area.dragover-not-allowed .fc-disp-default {
    display: none;
}

.fc-drop-area.dragover-not-allowed .fc-visible-default {
    visibility: hidden;
}

.fc-drop-area.dragover-not-allowed .fc-file-ref {
    visibility: hidden;
}

/* /When a not-allowed object is being dragged */

/* style around file chooser */
.fc-file-ref a, .fc-file-ref a:visited {
    color: dodgerblue;
}

.fc-input {
    visibility: hidden;
    width: 0px;
    height: 0px;
}

/* /style around file chooser */

/* /Entire file drop area */

```

---
_Source: https://npm.io/package/file-choice · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
