Skip to content

Commit 4ba57cf

Browse files
Merge branch 'feat-clipboard' of https://github.com/ThiagoBussola/docs.nestjs.com into ThiagoBussola-feat-clipboard
2 parents 94364bc + fa8e951 commit 4ba57cf

File tree

6 files changed

+83
-2
lines changed

6 files changed

+83
-2
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<button
2+
class="clipboard"
3+
(click)="onCopy()"
4+
[cdkCopyToClipboard]="elRef.nativeElement.lastElementChild.textContent"
5+
>
6+
<mat-icon>{{ buttonIcon | async }}</mat-icon>
7+
</button>
8+
<ng-content></ng-content>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
:host {
2+
display: block;
3+
position: relative;
4+
5+
.clipboard {
6+
color: #dfdfdf;
7+
cursor: pointer;
8+
background: transparent;
9+
border: none;
10+
font-weight: 600;
11+
padding: 12px;
12+
box-sizing: border-box;
13+
-webkit-box-sizing: border-box;
14+
position: absolute;
15+
right: 10px;
16+
top: 60px;
17+
z-index: 10;
18+
19+
& > * {
20+
width: 18px;
21+
height: 18px;
22+
font-size: 18px;
23+
}
24+
}
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { CopyButtonComponent } from './copy-button.component';
4+
5+
describe('CopyButtonComponent', () => {
6+
let component: CopyButtonComponent;
7+
let fixture: ComponentFixture<CopyButtonComponent>;
8+
9+
beforeEach(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [CopyButtonComponent]
12+
});
13+
fixture = TestBed.createComponent(CopyButtonComponent);
14+
component = fixture.componentInstance;
15+
fixture.detectChanges();
16+
});
17+
18+
it('should create', () => {
19+
expect(component).toBeTruthy();
20+
});
21+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Component, ElementRef, inject } from '@angular/core';
2+
import { BehaviorSubject, take, timer } from 'rxjs';
3+
4+
@Component({
5+
selector: 'app-copy-button',
6+
templateUrl: './copy-button.component.html',
7+
styleUrls: ['./copy-button.component.scss']
8+
})
9+
export class CopyButtonComponent {
10+
public elRef = inject<ElementRef<HTMLElement>>(ElementRef<HTMLElement>);
11+
public buttonIcon = new BehaviorSubject<string>('content_copy');
12+
13+
14+
onCopy() {
15+
this.buttonIcon.next('check');
16+
timer(2000).pipe(take(1)).subscribe(() => {
17+
this.buttonIcon.next('content_copy');
18+
});
19+
}
20+
}

src/app/shared/shared.module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ import { TabsComponent } from './components/tabs/tabs.component';
1212
import { TocComponent } from './components/toc/toc.component';
1313
import { HeaderAnchorDirective } from './directives/header-anchor.directive';
1414
import { ExtensionPipe } from './pipes/extension.pipe';
15+
import { CopyButtonComponent } from './components/copy-button/copy-button.component';
16+
import { ClipboardModule } from '@angular/cdk/clipboard';
17+
import { MatIconModule } from '@angular/material/icon';
1518

1619
@NgModule({
17-
imports: [CommonModule],
20+
imports: [CommonModule, ClipboardModule, MatIconModule],
1821
declarations: [
1922
ExtensionPipe,
2023
TabsComponent,
@@ -27,6 +30,7 @@ import { ExtensionPipe } from './pipes/extension.pipe';
2730
BannerDevtoolsComponent,
2831
BannerCoursesAuthComponent,
2932
ThemeModeToggleComponent,
33+
CopyButtonComponent,
3034
],
3135
exports: [
3236
ExtensionPipe,
@@ -40,6 +44,7 @@ import { ExtensionPipe } from './pipes/extension.pipe';
4044
BannerDevtoolsComponent,
4145
BannerCoursesAuthComponent,
4246
ThemeModeToggleComponent,
47+
CopyButtonComponent
4348
],
4449
providers: [StorageService],
4550
})

tools/transforms/shared/utils/markdown-utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function replaceFilename(
3232
const filename = text.slice(startIndex + 1, endIndex);
3333
return (
3434
`
35+
<app-copy-button>
3536
<span class="filename">` +
3637
(filename.length > 0
3738
? `
@@ -40,7 +41,8 @@ export function replaceFilename(
4041
`
4142
<app-tabs #${directiveRef}></app-tabs>
4243
</span>` +
43-
renderer(text.slice(endIndex + 1), directiveRef).trim()
44+
renderer(text.slice(endIndex + 1), directiveRef).trim() +
45+
`</app-copy-button>`
4446
);
4547
}
4648

0 commit comments

Comments
 (0)