Skip to content

Commit a0bcac8

Browse files
authored
Merge pull request #27 from xpquiz/feature/19
#19 adding clipboard score copy
2 parents f0b75e7 + efcc672 commit a0bcac8

File tree

6 files changed

+60
-3
lines changed

6 files changed

+60
-3
lines changed

src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {ScoreWindowComponent} from './score-window/score-window.component';
1313
import { CorrectAnswerWindowComponent } from './correct-answer-window/correct-answer-window.component';
1414
import { WrongAnswerWindowComponent } from './wrong-answer-window/wrong-answer-window.component';
1515
import { AboutWindowComponent } from './about-window/about-window.component';
16+
import {CopyClipboardDirective} from "./directives/CopyClipboardDirective";
1617

1718
@NgModule({
1819
declarations: [
@@ -25,6 +26,7 @@ import { AboutWindowComponent } from './about-window/about-window.component';
2526
CorrectAnswerWindowComponent,
2627
WrongAnswerWindowComponent,
2728
AboutWindowComponent,
29+
CopyClipboardDirective
2830
],
2931
imports: [
3032
BrowserModule,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {Directive, Input, Output, EventEmitter, HostListener} from "@angular/core";
2+
3+
@Directive({selector: '[copy-clipboard]'})
4+
export class CopyClipboardDirective {
5+
6+
@Input("copy-clipboard")
7+
public payload: string = '';
8+
9+
@Output("copied")
10+
public copied: EventEmitter<string> = new EventEmitter<string>();
11+
12+
@HostListener("click", ["$event"])
13+
public onClick(event: MouseEvent): void {
14+
15+
event.preventDefault();
16+
if (!this.payload)
17+
return;
18+
19+
let listener = (e: ClipboardEvent) => {
20+
// @ts-ignore
21+
let clipboard = e.clipboardData || window["clipboardData"];
22+
clipboard.setData("text", this.payload.toString());
23+
e.preventDefault();
24+
25+
this.copied.emit(this.payload);
26+
};
27+
28+
document.addEventListener("copy", listener, false)
29+
document.execCommand("copy");
30+
document.removeEventListener("copy", listener, false);
31+
}
32+
}

src/app/score-window/score-window.component.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
</div>
1717
</div>
1818
</div>
19-
<app-icon-button iconPath="home.png" title="Return to home"
20-
(onButtonClick)="this.returnHome()"></app-icon-button>
19+
20+
<div class="window-body_buttons">
21+
<app-icon-button iconPath="copy.png" title="Copy score to clipboard" [copy-clipboard]="this.clipboardMessage"></app-icon-button>
22+
<app-icon-button iconPath="home.png" title="Return to home"
23+
(onButtonClick)="this.returnHome()"></app-icon-button>
24+
</div>
2125
</div>
2226
</div>

src/app/score-window/score-window.component.sass

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@
3131
display: flex
3232
flex-direction: column
3333
margin: 3px
34+
35+
&_buttons
36+
display: flex
37+
flex-direction: row
38+
align-items: center
39+
justify-content: space-between
40+
41+
app-icon-button
42+
margin: 3px

src/app/score-window/score-window.component.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class ScoreWindowComponent implements OnInit {
1515
public currentWeek: number = 0;
1616
public rightAnswers: number = 0;
1717
public wrongAnswers: number = 0;
18+
public clipboardMessage: string = '';
1819

1920
constructor(
2021
private readonly router: Router,
@@ -29,17 +30,26 @@ export class ScoreWindowComponent implements OnInit {
2930

3031
this.currentWeek = currentWeek;
3132

32-
if(currentWeekScoreMap === undefined) return;
33+
if (currentWeekScoreMap === undefined) return;
3334

3435
const currentWeekScore: WeekScore = currentWeekScoreMap.get(currentWeek)!;
3536

3637
this.currentScore = currentWeekScore.score;
3738
this.rightAnswers = currentWeekScore.rightAnswers;
3839
this.wrongAnswers = currentWeekScore.wrongAnswers;
40+
this.assembleClipboardMessage();
3941
}
4042

4143
public async returnHome(): Promise<void> {
4244
await this.router.navigateByUrl(PathsEnum.HOME);
4345
}
4446

47+
private assembleClipboardMessage() {
48+
this.clipboardMessage = `Here's my score for the week on xpquiz.github.io!
49+
50+
Right answers: ${this.rightAnswers}
51+
Wrong answers: ${this.wrongAnswers}
52+
Total score: ${this.currentScore}`;
53+
54+
}
4555
}

src/assets/icons/20x20/copy.png

6.82 KB
Loading

0 commit comments

Comments
 (0)