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

atinc / ngx-tethys / e62d3b10-1466-49c3-aabd-707148681fc8

14 Jun 2024 08:24AM UTC coverage: 90.422%. Remained the same
e62d3b10-1466-49c3-aabd-707148681fc8

push

circleci

minlovehua
feat: use the ngx-tethys/util's coerceBooleanProperty instead of booleanAttribute #INFR-12648

5467 of 6692 branches covered (81.69%)

Branch coverage included in aggregate %.

117 of 120 new or added lines in 66 files covered. (97.5%)

183 existing lines in 46 files now uncovered.

13216 of 13970 relevant lines covered (94.6%)

985.91 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, Inject, Input, OnDestroy, OnInit, Output } 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

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

13
/**
22✔
14
 * @name thyCopy
22✔
15
 */
22✔
16
@Directive({
22✔
17
    selector: '[thyCopy]',
22✔
18
    hostDirectives: [ThyTooltipDirective],
22✔
19
    standalone: true
22✔
20
})
21
export class ThyCopyDirective implements OnInit, OnDestroy {
22
    /**
22✔
23
     * 默认为点击标签,可传复制目标标签
22✔
24
     */
25
    @Output() thyCopy = new EventEmitter<ThyCopyEvent>();
26

2!
27
    /**
2✔
28
     * 复制成功时的文案
29
     */
UNCOV
30
    @Input() thyCopySuccessText = '复制成功';
×
UNCOV
31

×
32
    /**
33
     * 提示文案
34
     */
35
    @Input() thyCopyTips = '点击复制';
2✔
36

2✔
37
    /**
2✔
38
     * 偏移量
2✔
39
     */
2✔
40
    @Input() thyCopyTipsOffset: number;
2✔
41

2✔
42
    /**
2✔
43
     * 当为 string 时,复制的是传入的内容;当为 ElementRef | HTMLElement 时,复制的是 dom 节点的 value 或者 textContent
1✔
44
     */
45
    @Input() thyCopyContent: string | ElementRef | HTMLElement;
46

UNCOV
47
    /**
×
UNCOV
48
     * 是否展示通知
×
UNCOV
49
     */
×
50
    @Input({ transform: coerceBooleanProperty }) thyShowNotify = true;
51

52
    constructor(
53
        @Inject(DOCUMENT) private document: any,
2✔
54
        public tooltipDirective: ThyTooltipDirective,
55
        private notifyService: ThyNotifyService
56
    ) {}
57

22✔
58
    ngOnInit() {
59
        this.tooltipDirective.content = this.thyCopyTips ? this.thyCopyTips : '点击复制';
1✔
60
        this.tooltipDirective.tooltipOffset = this.thyCopyTipsOffset;
61
    }
62

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

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

94
    ngOnDestroy() {
95
        this.tooltipDirective.hide();
96
    }
97
}
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