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

waterfall_virtual

v1.1.1

Published

the waterfall flow uses virtual lists.

Downloads

2

Readme

:smile:waterfall_virtual :smile:


This is a typescript module, which is used for rendering a waterfall layout of items in an HTML container. The class has several properties and methods, including container (the HTML container element), datas (an array of data to be rendered), generateFun (a function that generates the data for rendering), render (a method that renders the data in the container using the waterfall layout algorithm), refreshAble (a method that binds the resize and scroll events to update the rendering), getRange (a method that gets the rendering range based on the current scroll position), update (a method that updates the rendering by adding or removing items based on the current rendering range), and pushData (a method that adds more data to the rendering).

The Render class uses the WaterFallItem type, which represents an item in the waterfall layout and contains the HTML element (ele), height (height), and translation values (translateX and translateY). The GenerateDataFun type represents a function that generates the data for rendering, and takes an array of data and an item width as input, and returns a promise that resolves to an array of WaterFallItem objects.

The Render class also uses a debounce function to limit the frequency of rendering or updating the rendering, by wrapping the rendering or updating function in a timer that delays its execution by a specified amount of time.

Overall, this module provides a flexible and customizable way to render a waterfall layout of items in an HTML container, with support for dynamic data loading and adaptive layout based on the container size.

:thumbsup:usage

to use this tool,you should install it first

     npm i waterfall_virtual

​:leaves:then import it in the file where you need use wv

​:leaves:here is a demo for react

     import Render, { WaterFallItem } from 'waterfall_virtual'
     
     
     

     let r = new Render(wraper.current, data,generateData, 200, 300, 30)
     
     //when arriving at the bottom of page,lode more datas 
     
     r.onReachBottom = () => {
       r.fetching = true
       axios.get('/20data').then(async res => {
        
         await r.pushData(res.data.datas)
         r.fetching = false
         
       }).catch(error => {

       })
     }

:rabbit::rabbit:the only thing you need to do is write the generateDatafunction ,it should processes data you pass to Render, and returns a object like fllowing

   {
     ele:HTMLDivElement, //the element will be rendered to the page as an item
     height:number,//the height of ele ,you shoule calculate it in "generateData"
     translateY:number,//just make it 0 is ok
     translateX:number,//just make it 0 is ok
   }

:sunny::sunny:An example of generateData , in this demo ,each item contains an image and a description

function generateData(datas: any[], itemWidth: number): Promise<WaterFallItem[]> {


   return new Promise((resove, rej) => {

     let res: WaterFallItem[] = []
     let count = 0
     let total = datas.length

     function checkOver() {
       count++
       if (count === total) {
         resove(res)
       }
     }

     for (let i = 0; i < datas.length; i++) {
       let img = new Image()
       img.src = datas[i].img
       img.style.width = '100%'

       img.onload = () => {

         img.style.borderRadius = '10px'
         let imgHeight = img.height * itemWidth / img.width

         let desc = document.createElement('p')
         desc.innerText = datas[i].desc
         let dl = datas[i].desc.length * 16
         let dh
         if (dl > itemWidth) {
           dh = 42
           desc.style.webkitLineClamp = '2'
           desc.style.display = '-webkit-box'
           desc.style.webkitBoxOrient = 'vertical'
           desc.style.overflow = 'hidden'
           desc.style.textOverflow = 'ellipsis'
         } else {
           dh = 21
         }
         desc.style.width = itemWidth + 'px'
         desc.style.padding = '0 2px 0 5px'
         desc.style.marginTop = '5px'

         let itemContainer = document.createElement('div')
         itemContainer.style.width = itemWidth + 'px'

         itemContainer.appendChild(img)
         itemContainer.appendChild(desc)
         let o = {
           id: datas[i].id,
           ele: itemContainer,
           height: imgHeight + dh + 5,
           user: datas[i].user,
           translateX: 0,
           translateY: 0
         }
         res[i] = o

         checkOver()


       }

       img.onerror = () => {
         console.log('image lode failed')
       }
     }
   })
 }

versions

  • v1.1.0 modified the entries of constructor,the container is just container and will create a wrapper automatically in constructor.