Skip to content

Commit 6112cad

Browse files
Thiago Bussolaguiseek
andcommitted
feat: copy button
add a copy button in code block Co-authored-by: Guilherme Siquinelli <[email protected]>
1 parent 20b1b96 commit 6112cad

File tree

6 files changed

+74
-2
lines changed

6 files changed

+74
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<button
2+
class="clipboard"
3+
(click)="onCopy()"
4+
[cdkCopyToClipboard]="elRef.nativeElement.lastElementChild.textContent"
5+
>{{buttonText | async}}</button>
6+
<ng-content></ng-content>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
:host {
2+
display: block;
3+
position: relative;
4+
.clipboard {
5+
color: #dfdfdf;
6+
cursor: pointer;
7+
background: transparent;
8+
border: none;
9+
font-weight: 600;
10+
padding: 12px;
11+
box-sizing: border-box;
12+
-webkit-box-sizing: border-box;
13+
position: absolute;
14+
right: 10px;
15+
top: 50px;
16+
z-index: 10;
17+
}
18+
}
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 buttonText = new BehaviorSubject<string>('Copy');
12+
13+
14+
onCopy() {
15+
this.buttonText.next('Copied!');
16+
timer(2000).pipe(take(1)).subscribe(() => {
17+
console.log('OI');
18+
this.buttonText.next('Copy');
19+
});
20+
}
21+
}

src/app/shared/shared.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ 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';
1517

1618
@NgModule({
17-
imports: [CommonModule],
19+
imports: [CommonModule, ClipboardModule],
1820
declarations: [
1921
ExtensionPipe,
2022
TabsComponent,
@@ -27,6 +29,7 @@ import { ExtensionPipe } from './pipes/extension.pipe';
2729
BannerDevtoolsComponent,
2830
BannerCoursesAuthComponent,
2931
ThemeModeToggleComponent,
32+
CopyButtonComponent,
3033
],
3134
exports: [
3235
ExtensionPipe,
@@ -40,6 +43,7 @@ import { ExtensionPipe } from './pipes/extension.pipe';
4043
BannerDevtoolsComponent,
4144
BannerCoursesAuthComponent,
4245
ThemeModeToggleComponent,
46+
CopyButtonComponent,
4347
],
4448
providers: [StorageService],
4549
})

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export function replaceFilename(
2828
const filename = text.slice(startIndex + 1, endIndex);
2929
return (
3030
`
31+
<app-copy-button>
3132
<span class="filename">` +
3233
(filename.length > 0
3334
? `
@@ -36,7 +37,8 @@ export function replaceFilename(
3637
`
3738
<app-tabs #${directiveRef}></app-tabs>
3839
</span>` +
39-
renderer(text.slice(endIndex + 1), directiveRef).trim()
40+
renderer(text.slice(endIndex + 1), directiveRef).trim() +
41+
`</app-copy-button>`
4042
);
4143
}
4244

0 commit comments

Comments
 (0)