Skip to content

Commit 4489e03

Browse files
committed
Refactor initCopyContent using the new registerGlobalEventFunc
1 parent 43c8d85 commit 4489e03

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

templates/repo/view_file.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
{{end}}
5656
</div>
5757
<a download class="btn-octicon" data-tooltip-content="{{ctx.Locale.Tr "repo.download_file"}}" href="{{$.RawFileLink}}">{{svg "octicon-download"}}</a>
58-
<a id="copy-content" class="btn-octicon {{if not .CanCopyContent}} disabled{{end}}"{{if or .IsImageFile (and .HasSourceRenderedToggle (not .IsDisplayingSource))}} data-link="{{$.RawFileLink}}"{{end}} data-tooltip-content="{{if .CanCopyContent}}{{ctx.Locale.Tr "copy_content"}}{{else}}{{ctx.Locale.Tr "copy_type_unsupported"}}{{end}}">{{svg "octicon-copy"}}</a>
58+
<a class="btn-octicon {{if not .CanCopyContent}} disabled{{end}}" data-global-click="onCopyContentButtonClick" {{if or .IsImageFile (and .HasSourceRenderedToggle (not .IsDisplayingSource))}} data-link="{{$.RawFileLink}}"{{end}} data-tooltip-content="{{if .CanCopyContent}}{{ctx.Locale.Tr "copy_content"}}{{else}}{{ctx.Locale.Tr "copy_type_unsupported"}}{{end}}">{{svg "octicon-copy"}}</a>
5959
{{if .EnableFeed}}
6060
<a class="btn-octicon" href="{{$.RepoLink}}/rss/{{$.RefTypeNameSubURL}}/{{PathEscapeSegments .TreePath}}" data-tooltip-content="{{ctx.Locale.Tr "rss_feed"}}">
6161
{{svg "octicon-rss"}}

web_src/js/features/copycontent.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@ import {clippie} from 'clippie';
22
import {showTemporaryTooltip} from '../modules/tippy.ts';
33
import {convertImage} from '../utils.ts';
44
import {GET} from '../modules/fetch.ts';
5+
import {registerGlobalEventFunc} from '../modules/observer.ts';
56

67
const {i18n} = window.config;
78

89
export function initCopyContent() {
9-
const btn = document.querySelector('#copy-content');
10-
if (!btn || btn.classList.contains('disabled')) return;
11-
12-
btn.addEventListener('click', async () => {
13-
if (btn.classList.contains('is-loading')) return;
10+
registerGlobalEventFunc('click', 'onCopyContentButtonClick', async (el: HTMLInputElement) => {
11+
if (el.classList.contains('is-loading')) return;
1412
let content;
1513
let isRasterImage = false;
16-
const link = btn.getAttribute('data-link');
14+
const link = el.getAttribute('data-link');
1715

1816
// when data-link is present, we perform a fetch. this is either because
1917
// the text to copy is not in the DOM or it is an image which should be
2018
// fetched to copy in full resolution
2119
if (link) {
22-
btn.classList.add('is-loading', 'loading-icon-2px');
20+
el.classList.add('is-loading', 'loading-icon-2px');
2321
try {
2422
const res = await GET(link, {credentials: 'include', redirect: 'follow'});
2523
const contentType = res.headers.get('content-type');
@@ -31,9 +29,9 @@ export function initCopyContent() {
3129
content = await res.text();
3230
}
3331
} catch {
34-
return showTemporaryTooltip(btn, i18n.copy_error);
32+
return showTemporaryTooltip(el, i18n.copy_error);
3533
} finally {
36-
btn.classList.remove('is-loading', 'loading-icon-2px');
34+
el.classList.remove('is-loading', 'loading-icon-2px');
3735
}
3836
} else { // text, read from DOM
3937
const lineEls = document.querySelectorAll('.file-view .lines-code');
@@ -43,13 +41,13 @@ export function initCopyContent() {
4341
// try copy original first, if that fails and it's an image, convert it to png
4442
const success = await clippie(content);
4543
if (success) {
46-
showTemporaryTooltip(btn, i18n.copy_success);
44+
showTemporaryTooltip(el, i18n.copy_success);
4745
} else {
4846
if (isRasterImage) {
4947
const success = await clippie(await convertImage(content as Blob, 'image/png'));
50-
showTemporaryTooltip(btn, success ? i18n.copy_success : i18n.copy_error);
48+
showTemporaryTooltip(el, success ? i18n.copy_success : i18n.copy_error);
5149
} else {
52-
showTemporaryTooltip(btn, i18n.copy_error);
50+
showTemporaryTooltip(el, i18n.copy_error);
5351
}
5452
}
5553
});

0 commit comments

Comments
 (0)