aboutsummaryrefslogtreecommitdiffstats
path: root/web_src
diff options
context:
space:
mode:
authorqwerty287 <80460567+qwerty287@users.noreply.github.com>2021-10-02 09:46:43 +0200
committerGitHub <noreply@github.com>2021-10-02 10:46:43 +0300
commit47193dbcd97555d0c72d3219a372d9d5d01755d3 (patch)
tree6708016560731889622cd6c9fde59586f661a41d /web_src
parentc64e2a319fcbff2dc2f5d5d62570928394c89d08 (diff)
downloadgitea-47193dbcd97555d0c72d3219a372d9d5d01755d3.tar.gz
gitea-47193dbcd97555d0c72d3219a372d9d5d01755d3.zip
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
Diffstat (limited to 'web_src')
-rw-r--r--web_src/js/index.js51
1 files changed, 25 insertions, 26 deletions
diff --git a/web_src/js/index.js b/web_src/js/index.js
index 5c1510fb89..12539c4ec9 100644
--- a/web_src/js/index.js
+++ b/web_src/js/index.js
@@ -2917,6 +2917,27 @@ function deSelect() {
function selectRange($list, $select, $from) {
$list.removeClass('active');
+
+ // add hashchange to permalink
+ const $issue = $('a.ref-in-new-issue');
+ const $copyPermalink = $('a.copy-line-permalink');
+
+ if ($issue.length === 0 || $copyPermalink.length === 0) {
+ return;
+ }
+
+ const updateIssueHref = function(anchor) {
+ let href = $issue.attr('href');
+ href = `${href.replace(/%23L\d+$|%23L\d+-L\d+$/, '')}%23${anchor}`;
+ $issue.attr('href', href);
+ };
+
+ const updateCopyPermalinkHref = function(anchor) {
+ let link = $copyPermalink.attr('data-clipboard-text');
+ link = `${link.replace(/#L\d+$|#L\d+-L\d+$/, '')}#${anchor}`;
+ $copyPermalink.attr('data-clipboard-text', link);
+ };
+
if ($from) {
let a = parseInt($select.attr('rel').substr(1));
let b = parseInt($from.attr('rel').substr(1));
@@ -2934,38 +2955,16 @@ function selectRange($list, $select, $from) {
$list.filter(classes.join(',')).addClass('active');
changeHash(`#L${a}-L${b}`);
- // add hashchange to permalink
- const $issue = $('a.ref-in-new-issue');
-
- if ($issue.length === 0) {
- return;
- }
-
- const matched = $issue.attr('href').match(/%23L\d+$|%23L\d+-L\d+$/);
- if (matched) {
- $issue.attr('href', $issue.attr('href').replace($issue.attr('href').substr(matched.index), `%23L${a}-L${b}`));
- } else {
- $issue.attr('href', `${$issue.attr('href')}%23L${a}-L${b}`);
- }
+ updateIssueHref(`L${a}-L${b}`);
+ updateCopyPermalinkHref(`L${a}-L${b}`);
return;
}
}
$select.addClass('active');
changeHash(`#${$select.attr('rel')}`);
- // add hashchange to permalink
- const $issue = $('a.ref-in-new-issue');
-
- if ($issue.length === 0) {
- return;
- }
-
- const matched = $issue.attr('href').match(/%23L\d+$|%23L\d+-L\d+$/);
- if (matched) {
- $issue.attr('href', $issue.attr('href').replace($issue.attr('href').substr(matched.index), `%23${$select.attr('rel')}`));
- } else {
- $issue.attr('href', `${$issue.attr('href')}%23${$select.attr('rel')}`);
- }
+ updateIssueHref($select.attr('rel'));
+ updateCopyPermalinkHref($select.attr('rel'));
}
$(() => {