Skip to content

Commit 47193db

Browse files
authored
Add option to copy line permalink (#17145)
* Add option to copy line permalink * Fix lint * Apply review suggestions * Update code and fix lint * Use features/clipboard.js framework
1 parent c64e2a3 commit 47193db

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
lines changed

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,7 @@ file_view_rendered = View Rendered
970970
file_view_raw = View Raw
971971
file_permalink = Permalink
972972
file_too_large = The file is too large to be shown.
973+
file_copy_permalink = Copy Permalink
973974
video_not_supported_in_browser = Your browser does not support the HTML5 'video' tag.
974975
audio_not_supported_in_browser = Your browser does not support the HTML5 'audio' tag.
975976
stored_lfs = Stored with Git LFS

templates/repo/view_file.tmpl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,20 @@
112112
{{end}}
113113
</tbody>
114114
</table>
115-
{{if $.Permission.CanRead $.UnitTypeIssues}}
116-
<div class="code-line-menu ui fluid popup transition hidden">
117-
<div class="ui column relaxed equal height">
118-
<div class="column">
119-
<div class="ui link list">
120-
<a class="item ref-in-new-issue" href="{{.RepoLink}}/issues/new?body={{URLJoin AppUrl .RepoLink}}/src/commit/{{.CommitID}}/{{EscapePound .TreePath}}">{{.i18n.Tr "repo.issues.context.reference_issue"}}</a>
121-
</div>
115+
<div class="code-line-menu ui fluid popup transition hidden">
116+
<div class="ui column relaxed equal height">
117+
<div class="column">
118+
{{if $.Permission.CanRead $.UnitTypeIssues}}
119+
<div class="ui link list">
120+
<a class="item ref-in-new-issue" href="{{.RepoLink}}/issues/new?body={{URLJoin AppUrl .RepoLink}}/src/commit/{{.CommitID}}/{{EscapePound .TreePath}}">{{.i18n.Tr "repo.issues.context.reference_issue"}}</a>
122121
</div>
122+
{{end}}
123+
<div class="ui link list">
124+
<a data-clipboard-text="{{URLJoin AppUrl .RepoLink}}/src/commit/{{.CommitID}}/{{EscapePound .TreePath}}" class="item copy-line-permalink">{{.i18n.Tr "repo.file_copy_permalink"}}</a>
123125
</div>
124126
</div>
125-
{{end}}
127+
</div>
128+
</div>
126129
{{end}}
127130
{{end}}
128131
</div>

web_src/js/index.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2917,6 +2917,27 @@ function deSelect() {
29172917

29182918
function selectRange($list, $select, $from) {
29192919
$list.removeClass('active');
2920+
2921+
// add hashchange to permalink
2922+
const $issue = $('a.ref-in-new-issue');
2923+
const $copyPermalink = $('a.copy-line-permalink');
2924+
2925+
if ($issue.length === 0 || $copyPermalink.length === 0) {
2926+
return;
2927+
}
2928+
2929+
const updateIssueHref = function(anchor) {
2930+
let href = $issue.attr('href');
2931+
href = `${href.replace(/%23L\d+$|%23L\d+-L\d+$/, '')}%23${anchor}`;
2932+
$issue.attr('href', href);
2933+
};
2934+
2935+
const updateCopyPermalinkHref = function(anchor) {
2936+
let link = $copyPermalink.attr('data-clipboard-text');
2937+
link = `${link.replace(/#L\d+$|#L\d+-L\d+$/, '')}#${anchor}`;
2938+
$copyPermalink.attr('data-clipboard-text', link);
2939+
};
2940+
29202941
if ($from) {
29212942
let a = parseInt($select.attr('rel').substr(1));
29222943
let b = parseInt($from.attr('rel').substr(1));
@@ -2934,38 +2955,16 @@ function selectRange($list, $select, $from) {
29342955
$list.filter(classes.join(',')).addClass('active');
29352956
changeHash(`#L${a}-L${b}`);
29362957

2937-
// add hashchange to permalink
2938-
const $issue = $('a.ref-in-new-issue');
2939-
2940-
if ($issue.length === 0) {
2941-
return;
2942-
}
2943-
2944-
const matched = $issue.attr('href').match(/%23L\d+$|%23L\d+-L\d+$/);
2945-
if (matched) {
2946-
$issue.attr('href', $issue.attr('href').replace($issue.attr('href').substr(matched.index), `%23L${a}-L${b}`));
2947-
} else {
2948-
$issue.attr('href', `${$issue.attr('href')}%23L${a}-L${b}`);
2949-
}
2958+
updateIssueHref(`L${a}-L${b}`);
2959+
updateCopyPermalinkHref(`L${a}-L${b}`);
29502960
return;
29512961
}
29522962
}
29532963
$select.addClass('active');
29542964
changeHash(`#${$select.attr('rel')}`);
29552965

2956-
// add hashchange to permalink
2957-
const $issue = $('a.ref-in-new-issue');
2958-
2959-
if ($issue.length === 0) {
2960-
return;
2961-
}
2962-
2963-
const matched = $issue.attr('href').match(/%23L\d+$|%23L\d+-L\d+$/);
2964-
if (matched) {
2965-
$issue.attr('href', $issue.attr('href').replace($issue.attr('href').substr(matched.index), `%23${$select.attr('rel')}`));
2966-
} else {
2967-
$issue.attr('href', `${$issue.attr('href')}%23${$select.attr('rel')}`);
2968-
}
2966+
updateIssueHref($select.attr('rel'));
2967+
updateCopyPermalinkHref($select.attr('rel'));
29692968
}
29702969

29712970
$(() => {

0 commit comments

Comments
 (0)