ref: Formatted code using Prettier rules
This commit is contained in:
parent
1badcaf080
commit
1c902e2d90
3 changed files with 133 additions and 128 deletions
2
package-lock.json
generated
2
package-lock.json
generated
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "ng2-fittext",
|
"name": "ng2-fittext",
|
||||||
"version": "1.2.9",
|
"version": "1.2.10",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
||||||
|
|
@ -2,20 +2,21 @@ import {
|
||||||
AfterViewChecked,
|
AfterViewChecked,
|
||||||
AfterViewInit,
|
AfterViewInit,
|
||||||
Directive,
|
Directive,
|
||||||
ElementRef, HostListener,
|
ElementRef,
|
||||||
|
HostListener,
|
||||||
Input,
|
Input,
|
||||||
Output,
|
Output,
|
||||||
EventEmitter,
|
EventEmitter,
|
||||||
OnChanges,
|
OnChanges,
|
||||||
OnInit,
|
OnInit,
|
||||||
Renderer2
|
Renderer2,
|
||||||
} from '@angular/core';
|
} from '@angular/core';
|
||||||
|
|
||||||
@Directive({
|
@Directive({
|
||||||
selector: '[fittext]'
|
selector: '[fittext]',
|
||||||
})
|
})
|
||||||
export class Ng2FittextDirective implements AfterViewInit, OnInit, OnChanges, AfterViewChecked {
|
export class Ng2FittextDirective
|
||||||
|
implements AfterViewInit, OnInit, OnChanges, AfterViewChecked {
|
||||||
@Input('fittext') fittext: any;
|
@Input('fittext') fittext: any;
|
||||||
@Input('activateOnResize') activateOnResize: boolean;
|
@Input('activateOnResize') activateOnResize: boolean;
|
||||||
@Input('container') container: HTMLElement;
|
@Input('container') container: HTMLElement;
|
||||||
|
|
@ -34,21 +35,23 @@ export class Ng2FittextDirective implements AfterViewInit, OnInit, OnChanges, Af
|
||||||
private speed = 1.05;
|
private speed = 1.05;
|
||||||
private done = false;
|
private done = false;
|
||||||
|
|
||||||
constructor(public el: ElementRef,
|
constructor(public el: ElementRef, public renderer: Renderer2) {}
|
||||||
public renderer: Renderer2) {}
|
|
||||||
|
|
||||||
setFontSize(fontSize: number) {
|
setFontSize(fontSize: number) {
|
||||||
if (this.isVisible() && !this.done) {
|
if (this.isVisible() && !this.done) {
|
||||||
if (fontSize < this.minFontSize) {
|
if (fontSize < this.minFontSize) {
|
||||||
fontSize = this.minFontSize;
|
fontSize = this.minFontSize;
|
||||||
}
|
}
|
||||||
if(fontSize > this.maxFontSize){
|
if (fontSize > this.maxFontSize) {
|
||||||
fontSize = this.maxFontSize;
|
fontSize = this.maxFontSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.fontSize = fontSize;
|
this.fontSize = fontSize;
|
||||||
this.fontSizeChanged.emit(fontSize);
|
this.fontSizeChanged.emit(fontSize);
|
||||||
return this.el.nativeElement.style.setProperty('font-size', (fontSize).toString() + 'px');
|
return this.el.nativeElement.style.setProperty(
|
||||||
|
'font-size',
|
||||||
|
fontSize.toString() + 'px'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -59,7 +62,7 @@ export class Ng2FittextDirective implements AfterViewInit, OnInit, OnChanges, Af
|
||||||
checkOverflow(parent: any, children: any) {
|
checkOverflow(parent: any, children: any) {
|
||||||
const overflowX = children.scrollWidth - parent.clientWidth;
|
const overflowX = children.scrollWidth - parent.clientWidth;
|
||||||
const overflowY = children.clientHeight - parent.clientHeight;
|
const overflowY = children.clientHeight - parent.clientHeight;
|
||||||
return (overflowX > 1 || overflowY > 1);
|
return overflowX > 1 || overflowY > 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('window:resize', ['$event'])
|
@HostListener('window:resize', ['$event'])
|
||||||
|
|
@ -94,8 +97,12 @@ export class Ng2FittextDirective implements AfterViewInit, OnInit, OnChanges, Af
|
||||||
ngAfterViewInit() {
|
ngAfterViewInit() {
|
||||||
if (this.isVisible() && !this.done) {
|
if (this.isVisible() && !this.done) {
|
||||||
if (this.fittext) {
|
if (this.fittext) {
|
||||||
const overflow = this.container ? this.checkOverflow(this.container, this.el.nativeElement)
|
const overflow = this.container
|
||||||
: this.checkOverflow(this.el.nativeElement.parentElement, this.el.nativeElement);
|
? this.checkOverflow(this.container, this.el.nativeElement)
|
||||||
|
: this.checkOverflow(
|
||||||
|
this.el.nativeElement.parentElement,
|
||||||
|
this.el.nativeElement
|
||||||
|
);
|
||||||
if (overflow) {
|
if (overflow) {
|
||||||
if (this.fontSize > this.minFontSize) {
|
if (this.fontSize > this.minFontSize) {
|
||||||
// iterate only until font size is bigger than minimal value
|
// iterate only until font size is bigger than minimal value
|
||||||
|
|
@ -128,11 +135,15 @@ export class Ng2FittextDirective implements AfterViewInit, OnInit, OnChanges, Af
|
||||||
}
|
}
|
||||||
|
|
||||||
private getStartFontSizeFromHeight(): number {
|
private getStartFontSizeFromHeight(): number {
|
||||||
return this.container ? this.container.clientHeight : this.el.nativeElement.parentElement.clientHeight;
|
return this.container
|
||||||
|
? this.container.clientHeight
|
||||||
|
: this.el.nativeElement.parentElement.clientHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
private getStartFontSizeFromWeight(): number {
|
private getStartFontSizeFromWeight(): number {
|
||||||
return this.container ? this.container.clientWidth : this.el.nativeElement.parentElement.clientWidth;
|
return this.container
|
||||||
|
? this.container.clientWidth
|
||||||
|
: this.el.nativeElement.parentElement.clientWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
private isVisible(): boolean {
|
private isVisible(): boolean {
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,17 @@
|
||||||
import { Ng2FittextDirective } from './directives/ng2-fittext.directive';
|
import { Ng2FittextDirective } from './directives/ng2-fittext.directive';
|
||||||
import {CommonModule} from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import {NgModule} from "@angular/core";
|
import { NgModule } from '@angular/core';
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [
|
declarations: [Ng2FittextDirective],
|
||||||
Ng2FittextDirective
|
exports: [Ng2FittextDirective],
|
||||||
],
|
imports: [CommonModule],
|
||||||
exports: [
|
|
||||||
Ng2FittextDirective
|
|
||||||
],
|
|
||||||
imports: [
|
|
||||||
CommonModule
|
|
||||||
]
|
|
||||||
})
|
})
|
||||||
export class Ng2FittextModule {
|
export class Ng2FittextModule {
|
||||||
static forRoot() {
|
static forRoot() {
|
||||||
return {
|
return {
|
||||||
ngModule: Ng2FittextModule,
|
ngModule: Ng2FittextModule,
|
||||||
providers: []
|
providers: [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue