From 3cadff656282bd6599f0a6fd48b52e093b153894 Mon Sep 17 00:00:00 2001 From: Thorsten Date: Thu, 3 Dec 2020 13:39:54 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20catch=20truncate=20error?= =?UTF-8?q?s=20like=20"Must=20have=20child=20node"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/lib/line-truncation.directive.ts | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/projects/line-truncation-lib/src/lib/line-truncation.directive.ts b/projects/line-truncation-lib/src/lib/line-truncation.directive.ts index f0f57d4..8214cae 100644 --- a/projects/line-truncation-lib/src/lib/line-truncation.directive.ts +++ b/projects/line-truncation-lib/src/lib/line-truncation.directive.ts @@ -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"; /** @@ -33,7 +33,7 @@ interface Options { } @Directive({ - selector: "[line-truncation]" + selector: "[line-truncation]", }) export class LineTruncationDirective implements AfterViewInit, OnInit, OnDestroy { @@ -68,12 +68,15 @@ export class LineTruncationDirective this.windowResize$.next(event); } - constructor(private elementRef: ElementRef, private renderer: Renderer2) {} + constructor( + private elementRef: ElementRef, + 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(); @@ -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); @@ -163,7 +170,7 @@ export class LineTruncationDirective } // push child node to element shell - childNodes.forEach(node => { + childNodes.forEach((node) => { this.element.appendChild(node); }); @@ -188,7 +195,7 @@ export class LineTruncationDirective }); this.mutationObserver.observe(element, { - childList: true + childList: true, }); }