Skip to content

Commit d870c0b

Browse files
authoredJul 6, 2020
fix: copy to clipboard button always copying content of first tab (#821)
There were a couple of issues that were causing the "Copy to clipboard" button to copy the content of the first tab: 1. It was hardcoded to look at the first `snippet` component. 2. Even if we were using the `selectedTab` to get a reference to the open tab content, it wouldn't have worked because we kept setting the `selectedTab` based on the passed in `file`. These changes make it so the tab group propagates its selected index back to the `selectedTab`. Fixes #19867.
1 parent c1d7e44 commit d870c0b

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed
 

‎material.angular.io/src/app/shared/example-viewer/example-viewer.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
</div>
2222

2323
<div class="docs-example-viewer-source" *ngIf="view === 'full'">
24-
<mat-tab-group animationDuration="0ms" [selectedIndex]="selectedTab" (selectedIndexChange)="selectCorrectTab()">
24+
<mat-tab-group animationDuration="0ms" [(selectedIndex)]="selectedTab">
2525
<mat-tab *ngFor="let tabName of _getExampleTabNames()" [label]="tabName">
2626
<div class="button-bar">
27-
<button mat-icon-button type="button" (click)="copySource(snippet.first.viewer.textContent)"
27+
<button mat-icon-button type="button" (click)="copySource(snippet.toArray()[selectedTab].viewer.textContent)"
2828
class="docs-example-source-copy docs-example-button" [matTooltip]="'Copy example source'"
2929
title="Copy example source" aria-label="Copy example source to clipboard">
3030
<mat-icon>content_copy</mat-icon>

‎material.angular.io/src/app/shared/example-viewer/example-viewer.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class ExampleViewer implements OnInit {
2626
@ViewChildren(CodeSnippet) readonly snippet: QueryList<CodeSnippet>;
2727

2828
/** The tab to jump to when expanding from snippet view. */
29-
selectedTab: number;
29+
selectedTab: number = 0;
3030

3131
/** Map of example files that should be displayed in the view-source tab. */
3232
exampleTabs: {[tabName: string]: string};
@@ -118,12 +118,15 @@ export class ExampleViewer implements OnInit {
118118
}
119119

120120
generateUrl(file: string): string {
121+
const lastDotIndex = file.lastIndexOf('.');
122+
const contentBeforeDot = file.substring(0, lastDotIndex);
123+
const contentAfterDot = file.substring(lastDotIndex + 1);
121124
let fileName: string;
122-
const last = file.lastIndexOf('.');
125+
123126
if (this.region) {
124-
fileName = file.substring(0, last) + '_' + this.region + '-' + file.substring(last + 1) + '.html';
127+
fileName = `${contentBeforeDot}_${this.region}-${contentAfterDot}.html`;
125128
} else {
126-
fileName = file.substring(0, last) + '-' + file.substring(last + 1) + '.html';
129+
fileName = `${contentBeforeDot}-${contentAfterDot}.html`;
127130
}
128131

129132
const examplePath = `${this.exampleData.module.importSpecifier}/${this.example}`;
@@ -166,7 +169,7 @@ export class ExampleViewer implements OnInit {
166169

167170
const additionalFiles = this.exampleData.additionalFiles || [];
168171

169-
additionalFiles.forEach(fileName => {
172+
additionalFiles.forEach((fileName: string) => {
170173
// Since the additional files refer to the original file name, we need to transform
171174
// the file name to match the highlighted HTML file that displays the source.
172175
const fileSourceName = fileName.replace(fileExtensionRegex, '$1-$2.html');

0 commit comments

Comments
 (0)
Please sign in to comment.