Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<button
class="clipboard"
(click)="onCopy()"
[cdkCopyToClipboard]="elRef.nativeElement.lastElementChild.textContent"
>
<mat-icon>{{ buttonIcon | async }}</mat-icon>
</button>
<ng-content></ng-content>
18 changes: 18 additions & 0 deletions src/app/shared/components/copy-button/copy-button.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
:host {
display: block;
position: relative;
.clipboard {
color: #dfdfdf;
cursor: pointer;
background: transparent;
border: none;
font-weight: 600;
padding: 12px;
box-sizing: border-box;
-webkit-box-sizing: border-box;
position: absolute;
right: 10px;
top: 50px;
z-index: 10;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { CopyButtonComponent } from './copy-button.component';

describe('CopyButtonComponent', () => {
let component: CopyButtonComponent;
let fixture: ComponentFixture<CopyButtonComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [CopyButtonComponent]
});
fixture = TestBed.createComponent(CopyButtonComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
20 changes: 20 additions & 0 deletions src/app/shared/components/copy-button/copy-button.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Component, ElementRef, inject } from '@angular/core';
import { BehaviorSubject, take, timer } from 'rxjs';

@Component({
selector: 'app-copy-button',
templateUrl: './copy-button.component.html',
styleUrls: ['./copy-button.component.scss']
})
export class CopyButtonComponent {
public elRef = inject<ElementRef<HTMLElement>>(ElementRef<HTMLElement>);
public buttonIcon = new BehaviorSubject<string>('content_copy');


onCopy() {
this.buttonIcon.next('check');
timer(2000).pipe(take(1)).subscribe(() => {
this.buttonIcon.next('content_copy');
});
}
}
7 changes: 6 additions & 1 deletion src/app/shared/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import { TabsComponent } from './components/tabs/tabs.component';
import { TocComponent } from './components/toc/toc.component';
import { HeaderAnchorDirective } from './directives/header-anchor.directive';
import { ExtensionPipe } from './pipes/extension.pipe';
import { CopyButtonComponent } from './components/copy-button/copy-button.component';
import { ClipboardModule } from '@angular/cdk/clipboard';
import { MatIconModule } from '@angular/material/icon';

@NgModule({
imports: [CommonModule],
imports: [CommonModule, ClipboardModule, MatIconModule],
declarations: [
ExtensionPipe,
TabsComponent,
Expand All @@ -27,6 +30,7 @@ import { ExtensionPipe } from './pipes/extension.pipe';
BannerDevtoolsComponent,
BannerCoursesAuthComponent,
ThemeModeToggleComponent,
CopyButtonComponent,
],
exports: [
ExtensionPipe,
Expand All @@ -40,6 +44,7 @@ import { ExtensionPipe } from './pipes/extension.pipe';
BannerDevtoolsComponent,
BannerCoursesAuthComponent,
ThemeModeToggleComponent,
CopyButtonComponent
],
providers: [StorageService],
})
Expand Down
4 changes: 3 additions & 1 deletion tools/transforms/shared/utils/markdown-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function replaceFilename(
const filename = text.slice(startIndex + 1, endIndex);
return (
`
<app-copy-button>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we show the copy button for code snippets that don't have the filename specified?

Example:

image

I would think so

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, copy button for code snippets without filenames would improve usability. Let’s go ahead with this change!

<span class="filename">` +
(filename.length > 0
? `
Expand All @@ -36,7 +37,8 @@ export function replaceFilename(
`
<app-tabs #${directiveRef}></app-tabs>
</span>` +
renderer(text.slice(endIndex + 1), directiveRef).trim()
renderer(text.slice(endIndex + 1), directiveRef).trim() +
`</app-copy-button>`
);
}

Expand Down