Browse Source
* Change appSelectCopy to accept a dynamic input on what to copy * Renamed select-copy directive to copy-text directive to be more accurate with the new behaviour Signed-off-by: Andre Rosado <arosado@bitwarden.com> * Moved CopyTextDirective on jslib module to be in alphabetic ordering --------- Signed-off-by: Andre Rosado <arosado@bitwarden.com> Co-authored-by: Andre Rosado <arosado@bitwarden.com>pull/5850/head
11 changed files with 49 additions and 50 deletions
@ -0,0 +1,20 @@ |
|||||||
|
import { Directive, ElementRef, HostListener, Input } from "@angular/core"; |
||||||
|
|
||||||
|
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; |
||||||
|
|
||||||
|
@Directive({ |
||||||
|
selector: "[appCopyText]", |
||||||
|
}) |
||||||
|
export class CopyTextDirective { |
||||||
|
constructor(private el: ElementRef, private platformUtilsService: PlatformUtilsService) {} |
||||||
|
|
||||||
|
@Input("appCopyText") copyText: string; |
||||||
|
|
||||||
|
@HostListener("copy") onCopy() { |
||||||
|
if (window == null) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
this.platformUtilsService.copyToClipboard(this.copyText, { window: window }); |
||||||
|
} |
||||||
|
} |
||||||
@ -1,37 +0,0 @@ |
|||||||
import { Directive, ElementRef, HostListener } from "@angular/core"; |
|
||||||
|
|
||||||
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; |
|
||||||
|
|
||||||
@Directive({ |
|
||||||
selector: "[appSelectCopy]", |
|
||||||
}) |
|
||||||
export class SelectCopyDirective { |
|
||||||
constructor(private el: ElementRef, private platformUtilsService: PlatformUtilsService) {} |
|
||||||
|
|
||||||
@HostListener("copy") onCopy() { |
|
||||||
if (window == null) { |
|
||||||
return; |
|
||||||
} |
|
||||||
let copyText = ""; |
|
||||||
const selection = window.getSelection(); |
|
||||||
for (let i = 0; i < selection.rangeCount; i++) { |
|
||||||
const range = selection.getRangeAt(i); |
|
||||||
const text = range.toString(); |
|
||||||
|
|
||||||
// The selection should only contain one line of text. In some cases however, the
|
|
||||||
// selection contains newlines and space characters from the indentation of following
|
|
||||||
// sibling nodes. To avoid copying passwords containing trailing newlines and spaces
|
|
||||||
// that aren't part of the password, the selection has to be trimmed.
|
|
||||||
let stringEndPos = text.length; |
|
||||||
const newLinePos = text.search(/(?:\r\n|\r|\n)/); |
|
||||||
if (newLinePos > -1) { |
|
||||||
const otherPart = text.substr(newLinePos).trim(); |
|
||||||
if (otherPart === "") { |
|
||||||
stringEndPos = newLinePos; |
|
||||||
} |
|
||||||
} |
|
||||||
copyText += text.substring(0, stringEndPos); |
|
||||||
} |
|
||||||
this.platformUtilsService.copyToClipboard(copyText, { window: window }); |
|
||||||
} |
|
||||||
} |
|
||||||
Loading…
Reference in new issue