Skip to content

Commit fb2ac43

Browse files
committed
initCopyContent
1 parent 91090b1 commit fb2ac43

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
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: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ 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 (btn: HTMLInputElement) => {
11+
if (btn.classList.contains('disabled') || btn.classList.contains('is-loading')) return;
1412
let content;
1513
let isRasterImage = false;
1614
const link = btn.getAttribute('data-link');
1715

1816
// when data-link is present, we perform a fetch. this is either because
19-
// the text to copy is not in the DOM or it is an image which should be
17+
// 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) {
2220
btn.classList.add('is-loading', 'loading-icon-2px');
@@ -40,7 +38,7 @@ export function initCopyContent() {
4038
content = Array.from(lineEls, (el) => el.textContent).join('');
4139
}
4240

43-
// try copy original first, if that fails and it's an image, convert it to png
41+
// 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) {
4644
showTemporaryTooltip(btn, i18n.copy_success);

0 commit comments

Comments
 (0)