npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

file-choice

v1.2.0

Published

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

Downloads

10

Readme

file-choice

npm version License: 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
<script src="https://cdn.jsdelivr.net/npm/[email protected]/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


<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.

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.

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

  • paste "string" like a text/plain , text/html
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

/* 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 */