• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atinc / ngx-tethys / a94de615-1a97-46e8-b07e-aa98619b3649

13 Nov 2024 12:34PM UTC coverage: 90.366% (-0.03%) from 90.398%
a94de615-1a97-46e8-b07e-aa98619b3649

push

circleci

minlovehua
feat: support i18n

5517 of 6751 branches covered (81.72%)

Branch coverage included in aggregate %.

67 of 76 new or added lines in 19 files covered. (88.16%)

55 existing lines in 10 files now uncovered.

13225 of 13989 relevant lines covered (94.54%)

1000.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.09
/src/copy/copy.directive.ts
1
import { Directive, ElementRef, EventEmitter, HostListener, Input, OnDestroy, OnInit, Output, inject } from '@angular/core';
2
import { DOCUMENT } from '@angular/common';
3
import { coerceElement } from '@angular/cdk/coercion';
4
import { ThyNotifyService } from 'ngx-tethys/notify';
5
import { ThyTooltipDirective } from 'ngx-tethys/tooltip';
6
import { coerceBooleanProperty } from 'ngx-tethys/util';
7
import { ThyI18nService } from 'ngx-tethys/i18n';
8

9
export interface ThyCopyEvent {
10
    isSuccess: boolean;
11
    event: Event;
12
}
1✔
13

14
/**
22✔
15
 * @name thyCopy
22✔
16
 */
22✔
17
@Directive({
22✔
18
    selector: '[thyCopy]',
22✔
19
    hostDirectives: [ThyTooltipDirective],
22✔
20
    standalone: true
22✔
21
})
22✔
22
export class ThyCopyDirective implements OnInit, OnDestroy {
23
    private document = inject(DOCUMENT);
24
    tooltipDirective = inject(ThyTooltipDirective);
22✔
25
    private notifyService = inject(ThyNotifyService);
22✔
26
    private i18n = inject(ThyI18nService);
27

28
    /**
2!
29
     * 默认为点击标签,可传复制目标标签
2✔
30
     */
31
    @Output() thyCopy = new EventEmitter<ThyCopyEvent>();
32

×
33
    /**
×
34
     * 复制成功时的文案
35
     * @default 复制成功
36
     */
37
    @Input() thyCopySuccessText = this.i18n.translate('copy.success');
2✔
38

2✔
39
    /**
2✔
40
     * 提示文案
2✔
41
     * @default 点击复制
2✔
42
     */
2✔
43
    @Input() thyCopyTips = this.i18n.translate('copy.tips');
2✔
44

2✔
45
    /**
1✔
46
     * 偏移量
47
     */
48
    @Input() thyCopyTipsOffset: number;
UNCOV
49

×
UNCOV
50
    /**
×
51
     * 当为 string 时,复制的是传入的内容;当为 ElementRef | HTMLElement 时,复制的是 dom 节点的 value 或者 textContent
×
52
     */
53
    @Input() thyCopyContent: string | ElementRef | HTMLElement;
54

55
    /**
2✔
56
     * 是否展示通知
57
     */
58
    @Input({ transform: coerceBooleanProperty }) thyShowNotify = true;
59

22✔
60
    ngOnInit() {
61
        this.tooltipDirective.content = this.thyCopyTips ? this.thyCopyTips : this.i18n.translate('copy.tips');
1✔
62
        this.tooltipDirective.tooltipOffset = this.thyCopyTipsOffset;
63
    }
64

65
    private getContent(event: Event) {
66
        if (typeof this.thyCopyContent === 'string') {
67
            return this.thyCopyContent;
68
        } else {
69
            const target = this.thyCopyContent ? coerceElement(this.thyCopyContent) : event.target;
70
            return target.value || target.textContent;
71
        }
1✔
72
    }
73

74
    @HostListener('click', ['$event'])
75
    public onClick(event: Event) {
76
        const textarea = this.document.createElement('textarea');
77
        this.document.body.appendChild(textarea);
78
        textarea.value = this.getContent(event);
79
        textarea.select();
80
        try {
81
            document.execCommand('copy', false, null);
82
            this.thyCopy.emit({ isSuccess: true, event });
83
            if (this.thyShowNotify) {
84
                this.notifyService.success(this.thyCopySuccessText);
85
            }
86
        } catch (err) {
87
            this.thyCopy.emit({ isSuccess: false, event });
88
            if (this.thyShowNotify) {
89
                this.notifyService.error(this.i18n.translate('copy.error'));
90
            }
91
        } finally {
92
            textarea.remove();
93
        }
94
    }
95

96
    ngOnDestroy() {
97
        this.tooltipDirective.hide();
98
    }
99
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc