All Projects → diz517 → ngx-line-truncation

diz517 / ngx-line-truncation

Licence: MIT license
An Angular line truncating solution. Truncate text block by given line number and add ellipsis to the end.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to ngx-line-truncation

strings-truncation
Truncate strings with fullwidth characters and ANSI codes.
Stars: ✭ 45 (+246.15%)
Mutual labels:  truncate, shorten, truncation
tailwindcss-truncate-multiline
Tailwind CSS plugin to generate truncate multiline utilities
Stars: ✭ 92 (+607.69%)
Mutual labels:  truncate, ellipsis
v-clamp
Vue directive plugin for text truncate
Stars: ✭ 15 (+15.38%)
Mutual labels:  truncate, ellipsis
Shave
💈 Shave is a 0 dep JS plugin that truncates text to fit within an element based on a set max-height ✁
Stars: ✭ 2,113 (+16153.85%)
Mutual labels:  truncate, ellipsis
shave
💈 Shave is a 0 dep JS plugin that truncates text to fit within an element based on a set max-height ✁
Stars: ✭ 126 (+869.23%)
Mutual labels:  ellipsis, truncation
react-ellipsis
A high performance and customized ellipsis component for react, support rich text. 高性能、高度自定义、支持富文本的 react 缩略组件。
Stars: ✭ 63 (+384.62%)
Mutual labels:  ellipsis, richtext
react-middle-truncate
A React component for intelligently truncating text in the middle of the string.
Stars: ✭ 27 (+107.69%)
Mutual labels:  truncate, truncation
shear.js
✂️truncate text across DOM nodes
Stars: ✭ 61 (+369.23%)
Mutual labels:  truncate, ellipsis
react-text-more-less
📃React component to show more text or show less
Stars: ✭ 16 (+23.08%)
Mutual labels:  truncate, shave
trim-canvas
A tiny (< 100 LoC) library for trimming whitespace from a canvas element with no dependencies
Stars: ✭ 48 (+269.23%)
Mutual labels:  trim
disktrim
Windows application to send TRIM / UNMAP / DISCARD to SSD. Similar to blkdiscard.
Stars: ✭ 17 (+30.77%)
Mutual labels:  trim
String.prototype.trim
ES5 spec-compliant shim for String.prototype.trim
Stars: ✭ 13 (+0%)
Mutual labels:  trim
str
str is a string module with useful methods that don't exist in Lua's core
Stars: ✭ 30 (+130.77%)
Mutual labels:  trim
SQLiteHelper
🗄 This project comes in handy when you want to write a sql statement easily and smarter.
Stars: ✭ 57 (+338.46%)
Mutual labels:  trim
Fairygui Unity
A flexible UI framework for Unity
Stars: ✭ 2,007 (+15338.46%)
Mutual labels:  richtext
angular-ellipsis
A simple lightweight library for Angular which removes excess text and add ellipsis symbol to end of text before text overflows container
Stars: ✭ 16 (+23.08%)
Mutual labels:  ellipsis
node-truncate
🐙 Truncate text and keeps urls safe.
Stars: ✭ 52 (+300%)
Mutual labels:  truncate
VideoTimelineView
Video timeline UI for iOS Apps
Stars: ✭ 103 (+692.31%)
Mutual labels:  trim
Voca
The ultimate JavaScript string library
Stars: ✭ 3,387 (+25953.85%)
Mutual labels:  truncate
react-signature-canvas
A React wrapper component around signature_pad (in < 150 LoC). Unopinionated and heavily updated fork of react-signature-pad
Stars: ✭ 420 (+3130.77%)
Mutual labels:  trim

Logo

NGX Line Truncation

Ngx Line Truncation is line truncation implementation for Angular that truncate text by user defined line number. (demo)

In addition to Line Truncation, this package has few performance optimizations not only improved usability but also reliability in Angular platform. It uses retry logic to guarantee we get Client Height text block all the time, which is an essential value of the truncation input. It also watches the dom changes,to catch the case when the text value get applied at a later time.

Feature

  • Tailor made for Angular platform
  • Smart, just declare how many lines you wish to truncate for, no need to provide max-height, line-height
  • Works with nest DOM element
  • Support rich text, maintain original HTML element tags and styles
  • Lightening fast and capable
  • Custom ellipsis character
  • Re-execute on window resize
  • Dynamic content truncation

Installation

To install, simply run

npm install ngx-line-truncation

And import to the module that use truncation

import { LineTruncationLibModule } from 'ngx-line-truncation';

  ...

@NgModule({
imports: [
  ...

LineTruncationLibModule
]
})
export class MyModule { }

if you import this package into a shared module, you need to export LineTruncationDirective

@NgModule({
  imports: [LineTruncationLibModule],
  declarations: [...components],
  exports: [...components, LineTruncationDirective],
  entryComponents: []
})
export class MySharedModule {

How to use

Declare [line-truncation] with div, p, and pass a number that indicates how many lines of text you are expected to truncate

<p [line-truncation]="2">
  Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nesciunt consequatur
  ipsum unde doloremque aliquid hic vitae iure necessitatibus, maiores
  repellendus, quos dignissimos Quis necessitatibus quos voluptas nesciunt
  facere mollitia cupiditate.
</p>
<p [line-truncation]="2" [innerHTML]="myText"></p>
<div [line-truncation]="2">
  Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nesciunt consequatur
  ipsum unde doloremque aliquid hic vitae iure necessitatibus, maiores
  repellendus, quos dignissimos? Quis necessitatibus quos voluptas nesciunt
  facere mollitia cupiditate.
</div>

Optionally, an output function can help to know if the text has been truncate

<p
  [line-truncation]="numOfLines"
  (hasTruncated)="handler($event)"
  [innerHTML]="myText"
></p>

in your component.ts file

export class myComponent implements OnInit {

  hasTruncated = false;
  numberOfLines = 2;

  myText=`Lorem ipsum dolor sit amet consectetur, adipisicing elit. Fuga itaque voluptatibus sequi laborum, consequatur aut nisi.
  Eaque nulla animi qui exercitationem suscipit voluptas cum est dicta, magnam odio et distinctio?`;

  //...

  handler(res: boolean){
    this.hasTruncated = res;
  }

By default, '...' will be added to the end of the truncated body, if you wish to use your desired ellipsis, you can pass an object like this

<p [line-truncation]="numOfLines" [options]="{ellipsis: "🚀"}" (hasTruncated)="handler(booleanValue)" [innerHTML]="myText" [disabled]="disabled"></p>

Known issue:

  • When you specify emoji as ellipsis 🚀, or use rich text( <p [innerHTML]>), the truncation result might ended up with less lines than you desired(e.g. desire 3, but only have 1). I will be looking into this issue in the future, current work around for this issue is say you realize you get 1 line instead 3, you could declare with 5, it will be truncated to 3.

List of Input & Output

@Input("line-truncation") lines = 1; -- Lines that you desire, default to 1

@Input() options: Options = { ellipsis: "\u2026" }; -- Ellipsis Character, default to ...

@Input() set disabled(val: boolean) { this._disabled$.next(val); -- To disable the truncation, default to false }

@Input() watchChanges = false; -- To watch the text change and truncate, default to false

@Output() hasTruncated = new EventEmitter(); -- $event to true if truncation happen (every time)

Update

2022

  • 06-06 For lib version v1.9.1 now support peer dependency Angular ~14
  • 05-25 For lib version v1.8.1 now support peer dependency Angular ~13

2021

2020

2019

  • 12-02 add input watchChanges to provide truncation on dynamic text content

  • 10-27 add input disabled fix an issue when not truncating, hasTruncated is not emitting value

Contact me

If you have more idea about improving this package, feel free to reach me at [email protected]

License

The repository code is open-sourced software licensed under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].