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

content-editable-extension-js

v1.0.1

Published

Extend the content-editable-js to insert emoji images, common format images, and implement the @input association function.

Downloads

2

Readme

content-editable-extension-js

Extend the content-editable-js to insert emoji images, common format images, and implement the @input association function.

Getting started

Add the library

<!-- With CDN -->
<script src="https://unpkg.com/content-editable-extension-js/dist/content-editable-extension-1.0.0.js"></script>
<!-- Locally -->
<script src="dist/content-editable-extension-1.0.0.js"></script>

Installation

npm install content-editable-extension-js --save

Usage

<div class="div-text"></div>
<script src="https://unpkg.com/content-editable-extension-js/dist/content-editable-extension-1.0.0.js"></script>
<script>
const textarea = document.getElementsByClassName('div-text')[0];
const ceExtension = new CEExtension(
  textarea,                   // Set content-editable dom
  1,                          // Determine if you need to associate, default value 1
  autoCompleteHandle          // associate callback, default empty method
);

function autoCompleteHandle(isNeedObj) {
  // your logic
}
</script>

Documentation

Methods

insert()

The function inset( htmlStr: string, isAssociate: boolean | number ) inserts content into the cursor in the current element

const img = '<img src="./images/oip-c.jpg" alt="图片" />';
ceExtension.insert(img);

static setCursorToLast()

The function CeExtension.setCursorToLast( dom: HTMLElement ) sets the rear side of the cursor positioning content.

CEExtension.setCursorToLast( textarea );

static setPositionOfAssociateList()

The function static setPositionOfAssociateList(associateList: HTMLElement, outermostEleForAssociateList: HTMLElement) Sets the position of the association list.

getValue()

The function getValue() get element value.

const value = ceExtension.getValue();

Input the completion

Input '@ds', automatically associate 'dsewsd', 'dseewwe', 'dsios', etc. You can watch the demo

Usage

  • First you need to specify a parent container for the association list and set position to relative.
  • The association list needs to be customized, And abbreviate the click event, and insert the current value when clicking.
  • Entering' @sew' will trigger associations. At this time, you need to request the background interface to get the entered associations and render the association list.
  • You need to call the CEExtension.SetPositionOfAssociateList() method to reposition the associative list.
<!DOCTYPE html>
<html>
<head>
  <title>Div contenteditable</title>
  <meta charset="utf-8"/>
  <meta httP-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  <style>
    * { box-sizing: border-box;}
    .container {
      display: flex;
      flex-direction: column;
      flex-wrap: wrap;
      align-items: center;
      position:relative;
    }
    .div-text {
      width: 500px;
      height: 300px;
      overflow: hidden;
      border: 1px solid #eee;
      outline: blue;
    }
    ul, li {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    .auto-complete {
      display: none;
      min-width: 200px;
      border: solid 1px #eee;
      background: #fff;
    }
    .auto-complete:last-child {
      border-bottom: 0;
    }
    .auto-complete li {
      height: 40px;
      padding: 0px 10px;
      line-height: 40px;
      border-bottom: solid 1px #eee;
      cursor: pointer;
    }

  </style>
</head>
<body>
<div class="container">
  <div class="div-text"></div>
  <div>
    weasdfkalsdfjawefwwwe
    safsasfa
  </div>
  <!-- Auto-complete as you enter -->
  <ul class="auto-complete">
    <li>eweeewwwe</li>
    <li>uuiiius</li>
  </ul>
</div>

<script src="../dist/content-editable-extension-1.0.0.js"></script>
<script>
(function(){
  const textarea = document.getElementsByClassName('div-text')[0];
  const outermostEleForAssociateList = document.querySelector('.container');
  const autoCompleteContainer = document.getElementsByClassName('auto-complete')[0];
  const ceExtension = new CEExtension(textarea, 1, autoCompleteHandle);

  // Input associative callback processing
  function autoCompleteHandle(isNeedObj) {
    const timer = setTimeout(function() {
      const { isNeed, inputStr } = isNeedObj;
      let data = [];
      if ( isNeed ) {
        const res = httpGet(inputStr);
        const { status } = res;

        if ( status === 200 ) {
          data = res.data;
        }

        CEExtension.setPositionOfAssociateList(autoCompleteContainer, outermostEleForAssociateList);
      }
      let lis = ''
      data.forEach(function(value) {
        lis += `<li>${value}</li>`;
      });
      autoCompleteContainer.innerHTML = lis;
      const { display } = getComputedStyle(autoCompleteContainer);
      
      if ( display === 'none' ) {
        autoCompleteContainer.style.display = 'block';
      }
    }, 500);
  }

  // Mock HTTP requests (input associations)
  function httpGet(inputStr) {
    return {
      status: 200,
      data: inputStr ? ['eweeewwwe', 'uuiiius', 'ooowwwtt', 'ppssssewwe', 'ewxxsssekk'] : [],
      message: 'success'
    };
  }

  autoCompleteContainer.addEventListener('click', function(e) {
    const target = e.target || e.srcElement;
    const { tagName } = target;

    if ( tagName === 'LI' ) {
      const [value, isAssociate] = [target.innerHTML, 1]
      ceExtension.insert(`${value}&nbsp;`, isAssociate);
      autoCompleteContainer.style.display = 'none';
    }
  })
})()
</script>
</body>
</html>

Support

Content Editable is built for modern web browsers. Supports for IE. For bugs and suggestions, open an issue here.

Contribute

Feel free to make a PR! Once cloned, use these commands:

npm install # or yarn install
npm run dev
npm run build # before commit 

Licence

MIT