Improve copy command component (#17068)

Signed-off-by: AllForNothing <sshijun@vmware.com>
This commit is contained in:
Shijun Sun 2022-06-28 14:41:13 +08:00 committed by GitHub
parent c639257ba7
commit acb0c09bd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 9 deletions

View File

@ -1,5 +1,5 @@
import { TestBed } from '@angular/core/testing'; import { TestBed } from '@angular/core/testing';
import { EventService } from './event.service'; import { EventService, HarborEvent } from './event.service';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
describe('EventServiceService', () => { describe('EventServiceService', () => {
@ -16,12 +16,12 @@ describe('EventServiceService', () => {
it('able to subscribe', () => { it('able to subscribe', () => {
let result: string; let result: string;
const sub1 = service.subscribe('testEvent', data => { const sub1 = service.subscribe(HarborEvent.SCROLL, data => {
result = data; result = data;
}); });
expect(sub1).toBeTruthy(); expect(sub1).toBeTruthy();
expect(sub1 instanceof Subscription).toEqual(true); expect(sub1 instanceof Subscription).toEqual(true);
service.publish('testEvent', 'resultString'); service.publish(HarborEvent.SCROLL, 'resultString');
sub1.unsubscribe(); sub1.unsubscribe();
expect(result).toEqual('resultString'); expect(result).toEqual('resultString');
}); });

View File

@ -13,7 +13,7 @@ export class EventService {
* @param {function} handler the event handler * @param {function} handler the event handler
* @return A Subscription to unsubscribe * @return A Subscription to unsubscribe
*/ */
subscribe(topic: string, handler: Function): Subscription { subscribe(topic: HarborEvent, handler: Function): Subscription {
if (!this._channels[topic]) { if (!this._channels[topic]) {
this._channels[topic] = []; this._channels[topic] = [];
} }
@ -30,7 +30,7 @@ export class EventService {
* @param {function} handler the event handler * @param {function} handler the event handler
* *
*/ */
private unsubscribe(topic: string, handler: Function = null) { private unsubscribe(topic: HarborEvent, handler: Function = null) {
let t = this._channels[topic]; let t = this._channels[topic];
if (!t) { if (!t) {
// Wasn't found, wasn't removed // Wasn't found, wasn't removed
@ -60,7 +60,7 @@ export class EventService {
* @param topic * @param topic
* @param data * @param data
*/ */
publish(topic: string, data?: any) { publish(topic: HarborEvent, data?: any) {
const t = this._channels[topic]; const t = this._channels[topic];
if (!t) { if (!t) {
return; return;

View File

@ -1,4 +1,5 @@
import { Component, Input, Output, EventEmitter } from '@angular/core'; import { Component, Input, Output, EventEmitter } from '@angular/core';
import { ClipboardService } from '../third-party/ngx-clipboard';
export const enum CopyStatus { export const enum CopyStatus {
NORMAL, NORMAL,
@ -10,8 +11,6 @@ export const enum CopyStatus {
selector: 'hbr-copy-input', selector: 'hbr-copy-input',
templateUrl: './copy-input.coponent.html', templateUrl: './copy-input.coponent.html',
styleUrls: ['./push-image.scss'], styleUrls: ['./push-image.scss'],
providers: [],
}) })
export class CopyInputComponent { export class CopyInputComponent {
@Input() inputSize: number = 40; @Input() inputSize: number = 40;
@ -25,6 +24,8 @@ export class CopyInputComponent {
@Output() onCopySuccess: EventEmitter<any> = new EventEmitter<any>(); @Output() onCopySuccess: EventEmitter<any> = new EventEmitter<any>();
// eslint-disable-next-line @angular-eslint/no-output-on-prefix // eslint-disable-next-line @angular-eslint/no-output-on-prefix
@Output() onCopyError: EventEmitter<any> = new EventEmitter<any>(); @Output() onCopyError: EventEmitter<any> = new EventEmitter<any>();
constructor(private clipboardSrv: ClipboardService) {}
onSuccess($event: any): void { onSuccess($event: any): void {
this.state = CopyStatus.SUCCESS; this.state = CopyStatus.SUCCESS;
this.onCopySuccess.emit($event); this.onCopySuccess.emit($event);
@ -44,7 +45,10 @@ export class CopyInputComponent {
} }
public get isCopied(): boolean { public get isCopied(): boolean {
return this.state === CopyStatus.SUCCESS; return (
this.state === CopyStatus.SUCCESS &&
this.clipboardSrv?.getCurrentCopiedText() === this.defaultValue
);
} }
public get hasCopyError(): boolean { public get hasCopyError(): boolean {

View File

@ -11,6 +11,7 @@ import { WINDOW } from '../ngx-window-token/window-token';
@Injectable() @Injectable()
export class ClipboardService { export class ClipboardService {
private tempTextArea: HTMLTextAreaElement; private tempTextArea: HTMLTextAreaElement;
private _currentCopiedText: string;
constructor( constructor(
@Inject(DOCUMENT) private document: any, @Inject(DOCUMENT) private document: any,
@Inject(WINDOW) private window: any @Inject(WINDOW) private window: any
@ -22,6 +23,10 @@ export class ClipboardService {
); );
} }
getCurrentCopiedText(): string {
return this._currentCopiedText;
}
public isTargetValid( public isTargetValid(
element: HTMLInputElement | HTMLTextAreaElement element: HTMLInputElement | HTMLTextAreaElement
): boolean { ): boolean {
@ -50,6 +55,7 @@ export class ClipboardService {
this.selectTarget(targetElm, renderer); this.selectTarget(targetElm, renderer);
const re = this.copyText(); const re = this.copyText();
this.clearSelection(targetElm, this.window); this.clearSelection(targetElm, this.window);
this._currentCopiedText = targetElm?.value;
return re; return re;
} catch (error) { } catch (error) {
return false; return false;
@ -69,6 +75,7 @@ export class ClipboardService {
this.document.body.appendChild(this.tempTextArea); this.document.body.appendChild(this.tempTextArea);
} }
this.tempTextArea.value = content; this.tempTextArea.value = content;
this._currentCopiedText = content;
return this.copyFromInputElement(this.tempTextArea, renderer); return this.copyFromInputElement(this.tempTextArea, renderer);
} }