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

atinc / ngx-tethys / d9ae709b-3c27-4b69-b125-b8b80b54f90b

pending completion
d9ae709b-3c27-4b69-b125-b8b80b54f90b

Pull #2757

circleci

mengshuicmq
fix: fix code review
Pull Request #2757: feat(color-picker): color-picker support disabled (#INFR-8645)

98 of 6315 branches covered (1.55%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

2392 of 13661 relevant lines covered (17.51%)

83.12 hits per line

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

10.64
/src/copy/copy.directive.ts
1
import { InputBoolean } from 'ngx-tethys/core';
2
import { ThyNotifyService } from 'ngx-tethys/notify';
3
import { TooltipService } from 'ngx-tethys/tooltip';
4

5
import { coerceElement } from '@angular/cdk/coercion';
6
import { DOCUMENT } from '@angular/common';
7
import {
8
    Directive,
9
    ElementRef,
10
    EventEmitter,
11
    HostListener,
1✔
12
    Inject,
13
    Input,
×
14
    OnDestroy,
×
15
    OnInit,
×
16
    Output,
×
17
    ViewContainerRef
×
18
} from '@angular/core';
×
19

×
20
export interface ThyCopyEvent {
×
21
    isSuccess: boolean;
×
22
    event: Event;
23
}
24

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

×
39
    /**
×
40
     * 复制成功时的文案
×
41
     */
×
42
    @Input() thyCopySuccessText = '复制成功';
×
43

×
44
    /**
×
45
     * 提示文案
×
46
     */
×
47
    @Input() thyCopyTips = '点击复制';
48

49
    /**
50
     * 偏移量
×
51
     */
×
52
    @Input() thyCopyTipsOffset: number;
×
53

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

59
    /**
60
     * 是否展示通知
×
61
     */
62
    @Input() @InputBoolean() thyShowNotify = true;
1✔
63

64
    constructor(
65
        @Inject(DOCUMENT) private document: any,
66
        public tooltipService: TooltipService,
67
        private elementRef: ElementRef<HTMLElement>,
68
        private viewContainerRef: ViewContainerRef,
69
        private notifyService: ThyNotifyService
1✔
70
    ) {}
71

72
    ngOnInit() {
73
        this.tooltipService.attach(this.elementRef, this.viewContainerRef, 'hover');
74
        this.tooltipService.thyTooltipDirective.content = this.thyCopyTips ? this.thyCopyTips : '点击复制';
75
        this.tooltipService.thyTooltipDirective.tooltipOffset = this.thyCopyTipsOffset;
76
    }
77

78
    private getContent(event: Event) {
79
        if (typeof this.thyCopyContent === 'string') {
1✔
80
            return this.thyCopyContent;
81
        } else {
82
            const target = this.thyCopyContent ? coerceElement(this.thyCopyContent) : event.target;
83
            return target.value || target.textContent;
1✔
84
        }
85
    }
86
    @HostListener('click', ['$event'])
87
    public onClick(event: Event) {
88
        const textarea = this.document.createElement('textarea');
89
        this.document.body.appendChild(textarea);
90
        textarea.value = this.getContent(event);
91
        textarea.select();
92
        try {
93
            document.execCommand('copy', false, null);
94
            this.thyCopy.emit({ isSuccess: true, event });
95
            if (this.thyShowNotify) {
96
                this.notifyService.success(this.thyCopySuccessText);
97
            }
98
        } catch (err) {
99
            this.thyCopy.emit({ isSuccess: false, event });
100
            if (this.thyShowNotify) {
101
                this.notifyService.error('复制失败');
102
            }
103
        } finally {
104
            textarea.remove();
105
        }
106
    }
107

108
    ngOnDestroy() {
109
        this.tooltipService.detach();
110
    }
111
}
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