- 
                Notifications
    
You must be signed in to change notification settings  - Fork 2k
 
feat: copy button #3084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    feat: copy button #3084
Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            6 changes: 6 additions & 0 deletions
          
          6 
        
  src/app/shared/components/copy-button/copy-button.component.html
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <button | ||
| class="clipboard" | ||
| (click)="onCopy()" | ||
| [cdkCopyToClipboard]="elRef.nativeElement.lastElementChild.textContent" | ||
| >{{buttonText | async}}</button> | ||
| <ng-content></ng-content> | ||
        
          
          
            18 changes: 18 additions & 0 deletions
          
          18 
        
  src/app/shared/components/copy-button/copy-button.component.scss
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | 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; | ||
| } | ||
| } | ||
                
      
                  kamilmysliwiec marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
        
          
          
            21 changes: 21 additions & 0 deletions
          
          21 
        
  src/app/shared/components/copy-button/copy-button.component.spec.ts
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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(); | ||
| }); | ||
| }); | 
        
          
          
            21 changes: 21 additions & 0 deletions
          
          21 
        
  src/app/shared/components/copy-button/copy-button.component.ts
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| 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 buttonText = new BehaviorSubject<string>('Copy'); | ||
| 
     | 
||
| 
     | 
||
| onCopy() { | ||
| this.buttonText.next('Copied!'); | ||
| timer(2000).pipe(take(1)).subscribe(() => { | ||
| console.log('OI'); | ||
                
      
                  ThiagoBussola marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| this.buttonText.next('Copy'); | ||
| }); | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -28,6 +28,7 @@ export function replaceFilename( | |
| const filename = text.slice(startIndex + 1, endIndex); | ||
| return ( | ||
| ` | ||
| <app-copy-button> | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ? ` | ||
| 
        
          
        
         | 
    @@ -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>` | ||
| ); | ||
| } | ||
| 
     | 
||
| 
          
            
          
           | 
    ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.