Skip to content

Commit

Permalink
feat: 馃幐 catch truncate errors like "Must have child node"
Browse files Browse the repository at this point in the history
  • Loading branch information
ThRintelen committed Dec 3, 2020
1 parent 1c5d101 commit 3cadff6
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions projects/line-truncation-lib/src/lib/line-truncation.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import {
Directive,
ElementRef,
EventEmitter,
HostListener,
Input,
OnDestroy,
OnInit,
Output,
Renderer2,
OnDestroy,
HostListener
} from "@angular/core";
import { getContentHeight, getLineHeight, truncate } from "line-truncation";
import { Subject, Subscription, BehaviorSubject } from "rxjs";
import { BehaviorSubject, Subject, Subscription } from "rxjs";
import { debounceTime, skip } from "rxjs/operators";

/**
Expand All @@ -33,7 +33,7 @@ interface Options {
}

@Directive({
selector: "[line-truncation]"
selector: "[line-truncation]",
})
export class LineTruncationDirective
implements AfterViewInit, OnInit, OnDestroy {
Expand Down Expand Up @@ -68,12 +68,15 @@ export class LineTruncationDirective
this.windowResize$.next(event);
}

constructor(private elementRef: ElementRef<HTMLElement>, private renderer: Renderer2) {}
constructor(
private elementRef: ElementRef<HTMLElement>,
private renderer: Renderer2
) {}
/**
* Hide the original text content until we've finished the truncation
*/
ngOnInit() {
this._disabled$.pipe(skip(1)).subscribe(disable => {
this._disabled$.pipe(skip(1)).subscribe((disable) => {
// If there is elementClone, then recover
if (!!this.elementClone) {
this.putbackElement();
Expand Down Expand Up @@ -124,12 +127,16 @@ export class LineTruncationDirective
const targetHeight = this.lines * lineHeight;

if (contentHeight > targetHeight) {
truncate(
element,
this.lines,
this.options.ellipsis,
this.handler.bind(this)
);
try {
truncate(
element,
this.lines,
this.options.ellipsis,
this.handler.bind(this)
);
} catch (e) {
this.handler(true);
}
} else {
// when there is no need, simply show the element, emit false and unsubscribe from MutationObserver if `watchChanges` prop was falsy
this.handler(false);
Expand Down Expand Up @@ -163,7 +170,7 @@ export class LineTruncationDirective
}

// push child node to element shell
childNodes.forEach(node => {
childNodes.forEach((node) => {
this.element.appendChild(node);
});

Expand All @@ -188,7 +195,7 @@ export class LineTruncationDirective
});

this.mutationObserver.observe(element, {
childList: true
childList: true,
});
}

Expand Down

0 comments on commit 3cadff6

Please sign in to comment.