Skip to content

Commit 0349393

Browse files
authored
Merge pull request #510 from MurhafSousli/cdk-clipboard
feat: Use the CDK Clipboard
2 parents 4261a5d + 783c03a commit 0349393

File tree

6 files changed

+19
-35
lines changed

6 files changed

+19
-35
lines changed

CHANGELOG.MD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 8.0.3
4+
5+
- feat(core): Use Angular CDK Clipboard module for the copy link button, in [431cf9a](https://github.com/MurhafSousli/ngx-sharebuttons/pull/510/commits/431cf9af4f246cb741375d9e0784584a4e52cf54).
6+
37
## 8.0.2
48

59
- fix(core): Update Xing share URL, closes [#498](https://github.com/MurhafSousli/ngx-sharebuttons/issues/498) in [4b0c745](https://github.com/JustDoItSascha/ngx-sharebuttons/commit/4b0c7458779a87d6823b81ed21696dab5776692f).

projects/ngx-sharebuttons/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-sharebuttons",
3-
"version": "8.0.2",
3+
"version": "8.0.3",
44
"author": {
55
"name": "Murhaf Sousli",
66
"url": "https://github.com/MurhafSousli",
@@ -25,7 +25,6 @@
2525
"pinterest",
2626
"tumblr",
2727
"stumbleupon",
28-
"googleplus",
2928
"linkedin",
3029
"reddit",
3130
"telegram",

projects/ngx-sharebuttons/src/lib/share-button.directive.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { DOCUMENT } from '@angular/common';
1717
import { Meta } from '@angular/platform-browser';
1818
import { Platform } from '@angular/cdk/platform';
19+
import { Clipboard } from '@angular/cdk/clipboard';
1920
import { EMPTY, Observable, Subject, Subscriber } from 'rxjs';
2021
import { takeUntil, tap } from 'rxjs/operators';
2122

@@ -83,6 +84,7 @@ export class ShareDirective implements OnInit, OnChanges, OnDestroy {
8384
constructor(_el: ElementRef,
8485
private _meta: Meta,
8586
private _platform: Platform,
87+
private _clipboard: Clipboard,
8688
private _share: ShareService,
8789
private _cd: ChangeDetectorRef,
8890
@Inject(DOCUMENT) private _document: any) {
@@ -103,7 +105,7 @@ export class ShareDirective implements OnInit, OnChanges, OnDestroy {
103105
const click = this.shareButton.share ? this.open(params) : this.shareButton.func({
104106
params,
105107
data: this.shareButton.data,
106-
platform: this._platform,
108+
clipboard: this._clipboard,
107109
updater: this._updater
108110
});
109111

projects/ngx-sharebuttons/src/lib/share.models.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { InjectionToken } from '@angular/core';
2-
import { Platform } from '@angular/cdk/platform';
2+
import { Clipboard } from '@angular/cdk/clipboard';
33
import { Observable, Subject } from 'rxjs';
44

55
export const SHARE_BUTTONS_CONFIG = new InjectionToken<ShareButtonsConfig>('shareButtonsConfig');
@@ -91,7 +91,7 @@ export class IShareButton {
9191
/**
9292
* Some button do not open a share window, instead they execute a function like Print and Copy buttons.
9393
*/
94-
func?: (args: { data: any; params: ShareParams; platform: Platform; updater: Subject<ShareDirectiveUpdater> }) => Observable<any>;
94+
func?: (args: ShareButtonFuncArgs<any>) => Observable<any>;
9595
}
9696

9797
/**
@@ -127,7 +127,7 @@ interface ShareParamsFuncMetaData {
127127
* Share button function arguments (used in copyToClipboard as function arguments)
128128
*/
129129
export interface ShareButtonFuncArgs<T> {
130-
platform?: Platform;
130+
clipboard?: Clipboard;
131131
params?: ShareParams;
132132
data?: T;
133133
updater?: Subject<ShareDirectiveUpdater>;

projects/ngx-sharebuttons/src/lib/share.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { NgModule, ModuleWithProviders } from '@angular/core';
22
import { PlatformModule } from '@angular/cdk/platform';
3+
import { ClipboardModule } from '@angular/cdk/clipboard';
34

45
import { ShareDirective } from './share-button.directive';
56
import { SHARE_BUTTONS_CONFIG, ShareButtonsConfig } from './share.models';
67

78
@NgModule({
89
imports: [
9-
PlatformModule
10+
PlatformModule,
11+
ClipboardModule
1012
],
1113
declarations: [
1214
ShareDirective

projects/ngx-sharebuttons/src/lib/utils.ts

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { EMPTY, Observable, of, Subscriber } from 'rxjs';
2-
import { catchError, delay, take, tap } from 'rxjs/operators';
1+
import { Observable, of, Subscriber } from 'rxjs';
2+
import { delay, take, tap } from 'rxjs/operators';
33
import { ShareButtonFuncArgs } from './share.models';
44

55
/**
@@ -50,39 +50,16 @@ export function printPage(): Observable<void> {
5050
return new Observable((sub: Subscriber<any>) => document.defaultView.print());
5151
}
5252

53-
export function copyToClipboard({params, data, platform, updater}: ShareButtonFuncArgs<CopyToClipboardDataArgs>): Observable<void> {
53+
export function copyToClipboard({params, data, clipboard, updater}: ShareButtonFuncArgs<CopyToClipboardDataArgs>): Observable<void> {
5454
return of(null).pipe(
5555
tap(() => {
56-
const textArea: HTMLTextAreaElement = document.createElement('textarea') as HTMLTextAreaElement;
57-
58-
textArea.value = decodeURIComponent(params.url);
59-
document.body.appendChild(textArea);
60-
61-
// highlight TextArea to copy the sharing link
62-
if (platform.IOS) {
63-
const range = document.createRange();
64-
range.selectNodeContents(textArea);
65-
const selection = document.defaultView.getSelection();
66-
selection.removeAllRanges();
67-
selection.addRange(range);
68-
textArea.readOnly = true;
69-
textArea.setSelectionRange(0, 999999);
70-
} else {
71-
textArea.select();
72-
}
73-
document.execCommand('copy');
74-
document.body.removeChild(textArea);
75-
56+
clipboard.copy(params.url);
7657
// Disable copy button
7758
updater.next({icon: data.successIcon, text: data.successText, disabled: true});
7859
}),
7960
delay(data.delay),
8061
tap(() => updater.next({icon: data.icon, text: data.text, disabled: false})),
81-
take(1),
82-
catchError(err => {
83-
console.warn('Copy link failed!', err.message);
84-
return EMPTY;
85-
})
62+
take(1)
8663
);
8764
}
8865

0 commit comments

Comments
 (0)