Skip to content

Commit fa074ae

Browse files
fix: add copy button to every js and ts snippet
1 parent 4ba57cf commit fa074ae

File tree

9 files changed

+38
-45
lines changed

9 files changed

+38
-45
lines changed

src/app/homepage/pages/faq/faq.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const routes: Routes = [
7171
RequestLifecycleComponent,
7272
ErrorsComponent,
7373
ServerlessComponent,
74+
RawBodyComponent,
7475
],
7576
})
7677
export class FaqModule {}

src/app/homepage/pages/graphql/graphql.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { QuickStartComponent } from './quick-start/quick-start.component';
1818
import { ResolversMapComponent } from './resolvers-map/resolvers-map.component';
1919
import { ScalarsComponent } from './scalars/scalars.component';
2020
import { SchemaGeneratorComponent } from './schema-generator/schema-generator.component';
21-
import { SharingModelsComponent } from "./sharing-models/sharing-models.component";
21+
import { SharingModelsComponent } from './sharing-models/sharing-models.component';
2222
import { SubscriptionsComponent } from './subscriptions/subscriptions.component';
2323
import { UnionsAndEnumsComponent } from './unions-and-enums/unions.component';
2424

@@ -121,7 +121,7 @@ const routes: Routes = [
121121
{
122122
path: 'sharing-models',
123123
component: SharingModelsComponent,
124-
data: { title: "GraphQL + TypeScript - Sharing models"}
124+
data: { title: 'GraphQL + TypeScript - Sharing models' },
125125
},
126126
{
127127
path: 'mapped-types',
@@ -161,6 +161,7 @@ const routes: Routes = [
161161
ExtensionsComponent,
162162
FieldMiddlewareComponent,
163163
MigrationComponent,
164+
InterfacesComponent,
164165
],
165166
})
166167
export class GraphqlModule {}

src/app/homepage/pages/openapi/openapi.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const routes: Routes = [
7171
IntroductionComponent,
7272
DecoratorsComponent,
7373
CliPluginComponent,
74+
OpenApiSecurityComponent,
7475
],
7576
})
7677
export class OpenApiModule {}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<button
22
class="clipboard"
3+
[class.copied]="copied"
34
(click)="onCopy()"
4-
[cdkCopyToClipboard]="elRef.nativeElement.lastElementChild.textContent"
55
>
6-
<mat-icon>{{ buttonIcon | async }}</mat-icon>
6+
<mat-icon>{{ copied ? 'check': 'content_copy' }}</mat-icon>
77
</button>
88
<ng-content></ng-content>

src/app/shared/components/copy-button/copy-button.component.scss

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
cursor: pointer;
88
background: transparent;
99
border: none;
10-
font-weight: 600;
11-
padding: 12px;
12-
box-sizing: border-box;
13-
-webkit-box-sizing: border-box;
10+
padding: 10px;
1411
position: absolute;
1512
right: 10px;
16-
top: 60px;
13+
top: 65px;
1714
z-index: 10;
15+
opacity: 0.7;
1816

17+
&:hover:not(.copied) {
18+
opacity: 1;
19+
}
20+
1921
& > * {
2022
width: 18px;
2123
height: 18px;

src/app/shared/components/copy-button/copy-button.component.spec.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { Component, ElementRef, inject } from '@angular/core';
2-
import { BehaviorSubject, take, timer } from 'rxjs';
32

43
@Component({
54
selector: 'app-copy-button',
65
templateUrl: './copy-button.component.html',
7-
styleUrls: ['./copy-button.component.scss']
6+
styleUrls: ['./copy-button.component.scss'],
87
})
98
export class CopyButtonComponent {
109
public elRef = inject<ElementRef<HTMLElement>>(ElementRef<HTMLElement>);
11-
public buttonIcon = new BehaviorSubject<string>('content_copy');
12-
10+
public copied = false;
1311

1412
onCopy() {
15-
this.buttonIcon.next('check');
16-
timer(2000).pipe(take(1)).subscribe(() => {
17-
this.buttonIcon.next('content_copy');
18-
});
13+
const preRef = this.elRef.nativeElement.querySelector('pre:not(.hide)');
14+
if (!preRef) {
15+
return;
16+
}
17+
navigator.clipboard.writeText(preRef.firstChild.textContent);
18+
this.copied = true;
1919
}
2020
}

tools/transforms/content-package/services/renderer/code.renderer.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,28 @@ export function applyCodeRenderer(renderer: Renderer) {
2626
language: string,
2727
isEscaped: boolean,
2828
switcherKey?: string,
29+
skipCopyButton?: boolean,
2930
) {
3031
const filenameKey = '@@filename';
3132
const filenameIndex = code.indexOf(filenameKey);
3233
if (filenameIndex >= 0) {
33-
return replaceFilename(
34+
const output = replaceFilename(
3435
(text, directiveRef) =>
3536
// @ts-ignore
36-
renderer.code(text, language, isEscaped, directiveRef),
37+
renderer.code(text, language, isEscaped, directiveRef, true),
3738
code,
3839
filenameKey,
3940
filenameIndex,
4041
);
42+
43+
return `<app-copy-button>${output}</app-copy-button>`;
4144
}
4245

4346
const switchKey = '@@switch';
4447
const switchIndex = code.indexOf(switchKey);
4548
if (switchIndex >= 0) {
4649
const result = parseSwitcher(
47-
(text, lang) => renderer.code(text, lang, isEscaped),
50+
(text, lang) => renderer.code(text, lang, isEscaped, undefined, true),
4851
code,
4952
switchKey,
5053
switchIndex,
@@ -59,6 +62,14 @@ export function applyCodeRenderer(renderer: Renderer) {
5962
isEscaped,
6063
);
6164
output = switcherKey ? output : appendEmptyLine(output);
62-
return escapeAts(escapeBrackets(output));
65+
66+
const escaped = escapeAts(escapeBrackets(output));
67+
if (language === 'typescript' || language === 'ts') {
68+
if (!skipCopyButton) {
69+
return `<app-copy-button>${escaped}</app-copy-button>`;
70+
}
71+
return escaped;
72+
}
73+
return escaped;
6374
};
6475
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export function replaceFilename(
3232
const filename = text.slice(startIndex + 1, endIndex);
3333
return (
3434
`
35-
<app-copy-button>
3635
<span class="filename">` +
3736
(filename.length > 0
3837
? `
@@ -41,8 +40,7 @@ export function replaceFilename(
4140
`
4241
<app-tabs #${directiveRef}></app-tabs>
4342
</span>` +
44-
renderer(text.slice(endIndex + 1), directiveRef).trim() +
45-
`</app-copy-button>`
43+
renderer(text.slice(endIndex + 1), directiveRef).trim()
4644
);
4745
}
4846

0 commit comments

Comments
 (0)