Skip to content

Commit

Permalink
feat(module:typography): support i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
hsuanxyz committed Jun 19, 2019
1 parent 4225d0a commit f5dcd81
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 67 deletions.
8 changes: 8 additions & 0 deletions components/core/util/style-checke.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* @license
* Copyright Alibaba.com All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

export function isStyleSupport(styleName: string | string[]): boolean {
if (typeof window !== 'undefined' && window.document && window.document.documentElement) {
const styleNameList = Array.isArray(styleName) ? styleName : [styleName];
Expand Down
9 changes: 9 additions & 0 deletions components/i18n/languages/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,14 @@ export default {
},
Empty: {
description: 'No Data'
},
Text: {
edit: 'edit',
copy: 'copy',
copied: 'copy success',
expand: 'expand'
},
PageHeader: {
back: 'back'
}
};
9 changes: 9 additions & 0 deletions components/i18n/languages/es_ES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,14 @@ export default {
},
Empty: {
description: 'No hay datos'
},
Text: {
edit: 'editar',
copy: 'copiar',
copied: 'copiado',
expand: 'expandir'
},
PageHeader: {
back: 'volver'
}
};
6 changes: 6 additions & 0 deletions components/i18n/languages/it_IT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,11 @@ export default {
},
Empty: {
description: 'Nessun dato'
},
Text: {
edit: 'modifica',
copy: 'copia',
copied: 'copia effettuata',
expand: 'espandi'
}
};
9 changes: 9 additions & 0 deletions components/i18n/languages/ru_RU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,14 @@ export default {
},
Empty: {
description: 'Нет данных'
},
Text: {
edit: 'редактировать',
copy: 'копировать',
copied: 'скопировано',
expand: 'раскрыть'
},
PageHeader: {
back: 'назад'
}
};
6 changes: 6 additions & 0 deletions components/i18n/languages/tr_TR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,11 @@ export default {
},
Empty: {
description: 'Veri Yok'
},
Text: {
edit: 'düzenle',
copy: 'kopyala',
copied: 'kopyalandı',
expand: 'genişlet'
}
};
9 changes: 9 additions & 0 deletions components/i18n/languages/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,14 @@ export default {
},
Empty: {
description: '暂无数据'
},
Text: {
edit: '编辑',
copy: '复制',
copied: '复制成功',
expand: '展开'
},
PageHeader: {
back: '返回'
}
};
3 changes: 3 additions & 0 deletions components/i18n/languages/zh_TW.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,8 @@ export default {
},
Empty: {
description: '無此資料'
},
PageHeader: {
back: '返回'
}
};
2 changes: 1 addition & 1 deletion components/input/nz-autosize.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class NzAutosizeDirective implements AfterViewInit, OnDestroy, DoCheck {
textareaClone.style.overflow = 'hidden';

this.el.parentNode!.appendChild(textareaClone);
this.cachedLineHeight = textareaClone.clientHeight - this.inputGap - 1;
this.cachedLineHeight = textareaClone.clientHeight;
this.el.parentNode!.removeChild(textareaClone);

// Min and max heights have to be re-calculated if the cached line height changes
Expand Down
2 changes: 1 addition & 1 deletion components/typography/nz-text-copy.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<nz-tooltip [nzTitle]="copied ? '复制成功' : '复制'">
<nz-tooltip [nzTitle]="copied ? locale?.copied : locale?.copy">
<button
nz-tooltip
nz-trans-button
Expand Down
21 changes: 18 additions & 3 deletions components/typography/nz-text-copy.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ import {
EventEmitter,
Input,
OnDestroy,
OnInit,
Output,
ViewEncapsulation
} from '@angular/core';

import { NzCopyToClipboardService } from 'ng-zorro-antd/core';
import { NzI18nService } from 'ng-zorro-antd/i18n';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

@Component({
selector: 'nz-text-copy',
Expand All @@ -27,19 +31,30 @@ import { NzCopyToClipboardService } from 'ng-zorro-antd/core';
encapsulation: ViewEncapsulation.None,
preserveWhitespaces: false
})
export class NzTextCopyComponent implements OnDestroy {
export class NzTextCopyComponent implements OnInit, OnDestroy {
copied = false;
copyId: number;
@Input() getText: () => string;
// tslint:disable-next-line:no-any
locale: any = {};
private destroy$ = new Subject();

@Input() getText: () => string;
@Output() readonly textCopy = new EventEmitter<string>();

constructor(
public host: ElementRef,
private cdr: ChangeDetectorRef,
private copyToClipboard: NzCopyToClipboardService
private copyToClipboard: NzCopyToClipboardService,
private i18n: NzI18nService
) {}

ngOnInit(): void {
this.i18n.localeChange.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.locale = this.i18n.getLocaleData('Text');
this.cdr.markForCheck();
});
}

ngOnDestroy(): void {
clearTimeout(this.copyId);
}
Expand Down
2 changes: 1 addition & 1 deletion components/typography/nz-text-edit.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<nz-tooltip *ngIf="!editing" [nzTitle]="'编辑'">
<nz-tooltip *ngIf="!editing" [nzTitle]="locale?.edit">
<button
nz-tooltip
nz-trans-button
Expand Down
28 changes: 24 additions & 4 deletions components/typography/nz-text-edit.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,56 @@

import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
Input,
OnDestroy,
OnInit,
Output,
ViewChild,
ViewEncapsulation
} from '@angular/core';

import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

import { NzI18nService } from 'ng-zorro-antd/i18n';

@Component({
selector: 'nz-text-edit',
templateUrl: './nz-text-edit.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
preserveWhitespaces: false
})
export class NzTextEditComponent implements OnDestroy {
export class NzTextEditComponent implements OnInit, OnDestroy {
editing = false;
// tslint:disable-next-line:no-any
locale: any = {};
private destroy$ = new Subject();

@Input() getText: () => string;
@Output() readonly startEditing = new EventEmitter<void>();
@Output() readonly endEditing = new EventEmitter<string>();
@ViewChild('textarea') textarea: ElementRef<HTMLTextAreaElement>;
@ViewChild('textarea', { static: false }) textarea: ElementRef<HTMLTextAreaElement>;

beforeText: string;
currentText: string;
constructor(public host: ElementRef) {}
constructor(public host: ElementRef, private cdr: ChangeDetectorRef, private i18n: NzI18nService) {}

ngOnInit(): void {
this.i18n.localeChange.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.locale = this.i18n.getLocaleData('Text');
this.cdr.markForCheck();
});
}

ngOnDestroy(): void {}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}

onClick(): void {
this.beforeText = this.getText();
Expand Down
8 changes: 4 additions & 4 deletions components/typography/nz-typography.component.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<ng-template #contentTemplate>
<ng-content></ng-content>
</ng-template>
<ng-container *ngIf="!editing && nzEllipsisRows === 1">
<ng-container *ngIf="!editing && (expanded || (!nzExpandable && nzEllipsisRows === 1))">
<ng-template [ngTemplateOutlet]="contentTemplate"></ng-template>
</ng-container>
<ng-container *ngIf="nzEllipsis && nzEllipsisRows > 1">s
<ng-container *ngIf="nzEllipsis && !expanded && (nzEllipsisRows > 1 || nzExpandable)">
<span #ellipsisContainer></span>
<ng-container *ngIf="isEllipsis">...</ng-container>
<a #expandable class="ant-typography-expand">展开</a>
<ng-container *ngIf="isEllipsis">{{ellipsisStr}}</ng-container>
<a #expandable class="ant-typography-expand" (click)="onExpand()">{{locale?.expand}}</a>
</ng-container>
<nz-text-copy *ngIf="nzCopyable" [getText]="getCopyText" (textCopy)="onTextCopy($event)"></nz-text-copy>
<nz-text-edit
Expand Down
62 changes: 39 additions & 23 deletions components/typography/nz-typography.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { fromEvent, Subject } from 'rxjs';
import { auditTime, takeUntil } from 'rxjs/operators';

import { cancelRequestAnimationFrame, isStyleSupport, measure, reqAnimFrame, InputBoolean } from 'ng-zorro-antd/core';
import { NzI18nService } from 'ng-zorro-antd/i18n';

import { NzTextCopyComponent } from './nz-text-copy.component';
import { NzTextEditComponent } from './nz-text-edit.component';
Expand All @@ -52,8 +53,8 @@ import { NzTextEditComponent } from './nz-text-edit.component';
'[class.ant-typography-warning]': 'nzType === "warning"',
'[class.ant-typography-danger]': 'nzType === "danger"',
'[class.ant-typography-disabled]': 'nzDisabled',
'[class.ant-typography-ellipsis]': 'nzEllipsis',
'[class.ant-typography-ellipsis-single-line]': 'nzEllipsis && nzEllipsisRows === 1'
'[class.ant-typography-ellipsis]': 'nzEllipsis && !expanded',
'[class.ant-typography-ellipsis-single-line]': 'nzEllipsis && !expanded && nzEllipsisRows === 1'
}
})
export class NzTypographyComponent implements OnInit, AfterContentInit, OnDestroy {
Expand All @@ -69,20 +70,22 @@ export class NzTypographyComponent implements OnInit, AfterContentInit, OnDestro
@Output() readonly nzCopy = new EventEmitter<string>();
@Output() readonly nzExpand = new EventEmitter<void>();

@ViewChild(NzTextCopyComponent) textCopyRef: NzTextCopyComponent;
@ViewChild(NzTextEditComponent) textEditRef: NzTextEditComponent;
@ViewChild('ellipsisContainer') ellipsisContainer: ElementRef<HTMLSpanElement>;
@ViewChild('expandable') expandableBtn: ElementRef<HTMLSpanElement>;
@ViewChild('contentTemplate') contentTemplate: TemplateRef<void>;
@ViewChild(NzTextCopyComponent, { static: false }) textCopyRef: NzTextCopyComponent;
@ViewChild(NzTextEditComponent, { static: false }) textEditRef: NzTextEditComponent;
@ViewChild('ellipsisContainer', { static: false }) ellipsisContainer: ElementRef<HTMLSpanElement>;
@ViewChild('expandable', { static: false }) expandableBtn: ElementRef<HTMLSpanElement>;
@ViewChild('contentTemplate', { static: false }) contentTemplate: TemplateRef<void>;

hasOtherChildren = false;
editing = false;
ellipsisText: string | undefined;
isEllipsis: boolean = false;
ellipsisContent: Node[] = [];
expanded: boolean = false;
ellipsisStr = '...';
// tslint:disable-next-line:no-any
locale: any = {};

private rfaId: number = -1;
private expanded: boolean = false;
private destroy$ = new Subject();
readonly getText = () => this.host.nativeElement.innerText;
readonly getCopyText = () => (typeof this.nzCopyText === 'string' ? this.nzCopyText : this.getText());
Expand All @@ -92,10 +95,16 @@ export class NzTypographyComponent implements OnInit, AfterContentInit, OnDestro
private viewContainerRef: ViewContainerRef,
private renderer: Renderer2,
private ngZone: NgZone,
private platform: Platform
private platform: Platform,
private i18n: NzI18nService
) {}

ngOnInit(): void {}
ngOnInit(): void {
this.i18n.localeChange.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.locale = this.i18n.getLocaleData('Text');
this.cdr.markForCheck();
});
}

onTextCopy(text: string): void {
this.nzCopy.emit(text);
Expand All @@ -110,6 +119,11 @@ export class NzTypographyComponent implements OnInit, AfterContentInit, OnDestro
this.nzChange.emit(text);
}

onExpand(): void {
this.expanded = true;
this.nzExpand.emit();
}

canUseCSSEllipsis(): boolean {
if (this.nzEditable || this.nzCopyable || this.nzExpandable) {
return false;
Expand Down Expand Up @@ -139,7 +153,7 @@ export class NzTypographyComponent implements OnInit, AfterContentInit, OnDestro
this.nzEllipsisRows,
viewRef.rootNodes,
this.expandableBtn.nativeElement,
'...'
this.ellipsisStr
);
this.viewContainerRef.remove(this.viewContainerRef.indexOf(viewRef));
if (this.ellipsisText !== text || this.isEllipsis !== ellipsis || true) {
Expand All @@ -157,17 +171,19 @@ export class NzTypographyComponent implements OnInit, AfterContentInit, OnDestro
}

ngAfterContentInit(): void {
this.resizeOnNextFrame();

if (this.platform.isBrowser && this.nzEllipsis) {
this.ngZone.runOutsideAngular(() => {
fromEvent(window, 'resize')
.pipe(
auditTime(16),
takeUntil(this.destroy$)
)
.subscribe(() => this.resizeOnNextFrame());
});
if (this.platform.isBrowser) {
this.resizeOnNextFrame();

if (this.nzEllipsis) {
this.ngZone.runOutsideAngular(() => {
fromEvent(window, 'resize')
.pipe(
auditTime(16),
takeUntil(this.destroy$)
)
.subscribe(() => this.resizeOnNextFrame());
});
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion components/typography/nz-typography.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';

import { NzTransButtonDirective } from 'ng-zorro-antd/core';
import { NzI18nModule } from 'ng-zorro-antd/i18n';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzInputModule } from 'ng-zorro-antd/input';
import { NzToolTipModule } from 'ng-zorro-antd/tooltip';
Expand All @@ -19,7 +20,7 @@ import { NzTextEditComponent } from './nz-text-edit.component';
import { NzTypographyComponent } from './nz-typography.component';

@NgModule({
imports: [CommonModule, NzIconModule, NzToolTipModule, NzInputModule],
imports: [CommonModule, NzIconModule, NzToolTipModule, NzInputModule, NzI18nModule],
exports: [NzTypographyComponent, NzTextCopyComponent, NzTextEditComponent, NzTransButtonDirective],
declarations: [NzTypographyComponent, NzTextCopyComponent, NzTextEditComponent, NzTransButtonDirective]
})
Expand Down
Loading

0 comments on commit f5dcd81

Please sign in to comment.