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

atinc / ngx-tethys / c0ef8457-a839-451f-8b72-80fd73106231

02 Apr 2024 02:27PM UTC coverage: 90.524% (-0.06%) from 90.585%
c0ef8457-a839-451f-8b72-80fd73106231

Pull #3062

circleci

minlovehua
refactor(all): use the transform attribute of @Input() instead of @InputBoolean() and @InputNumber()
Pull Request #3062: refactor(all): use the transform attribute of @input() instead of @InputBoolean() and @InputNumber()

4987 of 6108 branches covered (81.65%)

Branch coverage included in aggregate %.

217 of 223 new or added lines in 82 files covered. (97.31%)

202 existing lines in 53 files now uncovered.

12246 of 12929 relevant lines covered (94.72%)

1055.59 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 {
2
    Directive,
3
    ElementRef,
4
    EventEmitter,
5
    HostListener,
6
    Inject,
7
    Input,
8
    OnDestroy,
9
    OnInit,
10
    Output,
1✔
11
    booleanAttribute
12
} from '@angular/core';
22✔
13
import { DOCUMENT } from '@angular/common';
22✔
14
import { coerceElement } from '@angular/cdk/coercion';
22✔
15
import { ThyNotifyService } from 'ngx-tethys/notify';
22✔
16
import { ThyTooltipDirective } from 'ngx-tethys/tooltip';
22✔
17

22✔
18
export interface ThyCopyEvent {
22✔
19
    isSuccess: boolean;
20
    event: Event;
21
}
22✔
22

22✔
23
/**
24
 * @name thyCopy
25
 */
2!
26
@Directive({
2✔
27
    selector: '[thyCopy]',
28
    hostDirectives: [ThyTooltipDirective],
UNCOV
29
    standalone: true
×
UNCOV
30
})
×
31
export class ThyCopyDirective implements OnInit, OnDestroy {
32
    /**
33
     * 默认为点击标签,可传复制目标标签
34
     */
2✔
35
    @Output() thyCopy = new EventEmitter<ThyCopyEvent>();
2✔
36

2✔
37
    /**
2✔
38
     * 复制成功时的文案
2✔
39
     */
2✔
40
    @Input() thyCopySuccessText = '复制成功';
2✔
41

2✔
42
    /**
1✔
43
     * 提示文案
44
     */
45
    @Input() thyCopyTips = '点击复制';
UNCOV
46

×
UNCOV
47
    /**
×
UNCOV
48
     * 偏移量
×
49
     */
50
    @Input() thyCopyTipsOffset: number;
51

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

22✔
57
    /**
58
     * 是否展示通知
1✔
59
     */
60
    @Input({ transform: booleanAttribute }) thyShowNotify = true;
61

62
    constructor(
63
        @Inject(DOCUMENT) private document: any,
1✔
64
        public tooltipDirective: ThyTooltipDirective,
65
        private notifyService: ThyNotifyService
66
    ) {}
67

68
    ngOnInit() {
69
        this.tooltipDirective.content = this.thyCopyTips ? this.thyCopyTips : '点击复制';
70
        this.tooltipDirective.tooltipOffset = this.thyCopyTipsOffset;
71
    }
72

73
    private getContent(event: Event) {
1✔
74
        if (typeof this.thyCopyContent === 'string') {
75
            return this.thyCopyContent;
76
        } else {
77
            const target = this.thyCopyContent ? coerceElement(this.thyCopyContent) : event.target;
78
            return target.value || target.textContent;
79
        }
80
    }
81

82
    @HostListener('click', ['$event'])
83
    public onClick(event: Event) {
84
        const textarea = this.document.createElement('textarea');
85
        this.document.body.appendChild(textarea);
86
        textarea.value = this.getContent(event);
87
        textarea.select();
88
        try {
89
            document.execCommand('copy', false, null);
90
            this.thyCopy.emit({ isSuccess: true, event });
91
            if (this.thyShowNotify) {
92
                this.notifyService.success(this.thyCopySuccessText);
93
            }
94
        } catch (err) {
95
            this.thyCopy.emit({ isSuccess: false, event });
96
            if (this.thyShowNotify) {
97
                this.notifyService.error('复制失败');
98
            }
99
        } finally {
100
            textarea.remove();
101
        }
102
    }
103

104
    ngOnDestroy() {
105
        this.tooltipDirective.hide();
106
    }
107
}
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