Giter VIP home page Giter VIP logo

ellipsed's Introduction

Ellipsed

license   GitHub forks GitHub stars
npm version npm compatibility

contributions welcome Join the chat at https://gitter.im/ellipsed/Lobby   Demo

What is Ellipsed?

Ellipsed is a JavaScript library for generating multi-lined ellipsed text.
Indicating the number of visible lines, it will truncate the text at the correct position in targeted elements.

Example screenshot

Demo

You can see the demo here: https://nzambello.github.io/ellipsed/

The example/ directory of this repo contains the demo showed above. To start it on your local machine, clone the repository and install it by following the installation instructions below, then start it with npm start (or with yarn start).

Install

# Yarn
yarn add ellipsed

# NPM
npm install --save ellipsed

# Bower
bower install ellipsed

Usage

Call the ellipsis method with three parameters:

ellipsis(selector, rows, options);
  • selector - A CSS selector string or the DOM Node or Node List used to target the element for truncation
  • rows - An integer value used as the number of rows that should be visible, following words will be trucated with the replaceStr (default is '...').
  • options - Allows an options object to be passed to further customise the functionality. See options section below for details.

If responsive option is set, the function returns the ID of the listener returned by setTimeout. It can be useful to clear resize handler timeout.

Options

Options can be passed through the method using the third parameter, which provides greater control.

Name Type Default Description
replaceStr string '...' The string that substituted following the trucated string.
responsive boolean false If true, the method is re-called on resize event, so the number of rows is preserved.
debounceDelay integer 250 If responsive option is set, it's the delay value for resizing events debouncing.
delimiter string ' ' Use '' for text written in languages such as Japanese, Chinese, etc. since there's usually no space break in such languages; or for text contains long word (word that is longer than the width of the container).

Import

Ellipsed is provided as a UMD module.

You can use it as a ES6 module:

import { ellipsis } from 'ellipsed';

As a CommonJS module:

var { ellipsis } = require('ellipsed');

As an AMD module:

require([
  'ellipsed'
], function(ellipsed) {
  var ellipsis = ellipsed.ellipsis;
});

Or as a global module in the browser:

var ellipsis = window.ellipsed.ellipsis;

Performance

It seems that having multiple calls of ellipsis with responsive option enabled would add multiple event listeners and would be recommended to use a single call with a multiple selector. A single event listener should be more efficient and events handling more fluid.

Browser support

The library has been tested on:

  • Google Chrome 60+
  • Firefox 55+
  • Safari 10+
  • Edge 14+
  • IE 9+

Contributing

New contributors are warmly welcome, see the CONTRIBUTING.md file for other information.

Many thanks to @nekorina for project's logo.

ellipsed's People

Contributors

annuhdo avatar dependabot[bot] avatar giuliaghisini avatar huyld avatar jaydp17 avatar johnakerman avatar nzambello avatar pnicolli avatar sergeydevjatov avatar sesubash avatar yuanchuan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ellipsed's Issues

Fail to break text that has no space break (e.g. Japanese text, long word)

When using this library, I found an issue. When the text doesn't have any space, e.g.

  • The text is in Japanese (there's rarely space break in Japanese text).
  • The content is a long word, longer than the width of the element.

The library couldn't handle such cases. The result is such text remains intact.

Issues with multiple calls with responsive option

If you add more than one call to ellipsis() with responsive option (on different elements), it will fail: event listeners won't work because it's not unique.
Adding only one call with responsive handling it'll work, but it's needed to work with N calls.

Lineheight or wrapper should be used for calculations

My Problem was i used a grid and all tiles should get the same height, the problem here is very simple all tiles have the same height.

First container nothing is rendered yet, 20px height (lineheight) with 2 lines max it will become 40px in height, after this is done the second container is affected by flexbox and automatically grown up and has a starting height of 40px.

40px = 2 lines since the lineHeight is 20px, but the calculation base is the 40px, so after the height is bigger then 40px rowsWrapper will increase to 2 which means now i have printed 2 lines. Which is basicly wrong since i allready have 2 lines.

What helps for me now was simply add the text into a wrapper so it would not be affected by flexbox, but i think the calculation should use the lineHeight and not the full height of the container.

Or simply render the text into a wrapper so it is not affected from outside in height.

    ...
    public ngAfterViewInit() {
        const wrapper = this.renderer.createElement("div");
        const oldHtml = this.el.nativeElement.innerHTML;
        wrapper.innerHTML = oldHtml;

        this.el.nativeElement.innerHTML = "";
        this.renderer.appendChild(this.el.nativeElement, wrapper);

        ellipsis(wrapper, 2, {
          responsive: true
        });
    }
    ...

Returning final truncated value from main method

Hello

Just discovered your lib for ellipse text and it is such a well designed solution. Great work, it helped me very much.

I was curious if you considered returning a value that was a final result that you set inside the element that you are targeting.
This can be useful if you want to take that truncated text and use it elsewhere.

Additionally, i know that this lib is for the truncation of a text in an element, but it could be useful to add a method or extend the existing ellipsis function with a option to prevent modification of the targeted element in case you want to use only the result of the truncation (this goes in conjunction with the above mentioned returning of truncated value).

EDIT:
To add to the last part, motivation for preventing the element modification was that i was trying out your lib in a React app and you should not modify elements inside DOM, rather as a value that you want to insert via VDOM.

Ellipsed logo

I believe this lib is mature enough and it deserves it's own logo. How about it? Any ideas?

ES6 import issue - Could not find a declaration file for module 'ellipsed'.

Hey,

I'm trying to import the lib the ES6 way:
import { ellipsis } from 'ellipsed';

However, I'm getting the following error.

Could not find a declaration file for module 'ellipsed'. 'project_path/node_modules/ellipsed/lib/ellipsed.js' implicitly has an 'any' type.
Try npm install @types/ellipsed if it exists or add a new declaration (.d.ts) file containing declare module 'ellipsed'; [7016]

Any idea why is this happening?
Thanks!

Responsive checkbox in demo not working after first call

The responsive checkbox in the demo is correctly read when you first click uno of the five ellipsis buttons. After you use an ellipsis with the responsive option active, though, you can't switch it off. Well, the checkbox does switch off, but the window resize event handler is already there and won't go away.

I haven't found a quick way to remove it from the demo, I am submitting a PR in the next few minutes with a workaround in order to make it work, which is reloading the page when you switch off the checkbox.

In order to remove a listener from the window object, you need to have a reference to the function that was called, and only the lib code has it, so my guess is that we have to export another function from the lib, that resets the responsive option by removing the listener.

Uncaught ReferenceError: ellipsis is not defined

I'm attempting to truncate a paragraph with the class "carousel-item-description" to 3 lines, and add some options, but I continuously get the following error:

Uncaught ReferenceError: ellipsis is not defined

ellipsis('.carousel-item-description', 3, {replaceStr: '...', responsive: true, debounceDelay: 100});
How is the above call incorrect?

sometimes get truncation by word instead of by line

I have noticed sometimes that when my preact component loads to the page, instead of truncating by number of lines it truncates by words. I put the ellipsis call inside componentDidMount() initially, but have tried several other places as well. I only see the issue when loading my component via other components, not when running the demo of my component.

Add documentation for disabling responsiveness

One of the latest PRs introduced a possible return value for the ellipsis function. It also introduced a function used to disable responsiveness for a previous ellipsis call.

I think these new feature should be added to the README. It's not useful to everybody, you normally don't want to remove the responsive option, but since it's there, some documentation on it would be good.

Add option for title attribute that contains the longer text to be added to tag

I think we should add an option which will be a boolean that defaults to false, that when true will take the pre-formatted longer text and add it as a title attribute on the wrapping tag. This will allow the user to hover over the element to get the pre-trucated version in a tooltip.

This would also introduce the idea of options or settings that the user can customise, perhaps the ability to change the "..." to something else instead, perhaps just a single "."?

Example screenshot
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.