From df51d962c052ac344669ff88f4962fe6c9e05b4a Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Fri, 24 Sep 2021 17:36:44 +0200 Subject: [PATCH 1/5] Add option to copy line permalink --- options/locale/locale_en-US.ini | 1 + templates/repo/view_file.tmpl | 25 ++++++++++++++++--------- web_src/js/index.js | 20 ++++++++++++++++++-- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 2ee0f5b56664b..68f63444167f6 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -969,6 +969,7 @@ file_view_rendered = View Rendered file_view_raw = View Raw file_permalink = Permalink file_too_large = The file is too large to be shown. +file_copy_permalink = Copy Permalink video_not_supported_in_browser = Your browser does not support the HTML5 'video' tag. audio_not_supported_in_browser = Your browser does not support the HTML5 'audio' tag. stored_lfs = Stored with Git LFS diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index b6198bd0e2082..e4068ce453939 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -112,18 +112,21 @@ {{end}} - {{if $.Permission.CanRead $.UnitTypeIssues}} - - {{end}} + {{end}} {{end}} From 58a6bc9dd2f6a5e68b33c53d58561386f42512a9 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Sat, 25 Sep 2021 15:55:13 +0200 Subject: [PATCH 3/5] Apply review suggestions --- templates/repo/view_file.tmpl | 4 +- web_src/js/index.js | 73 +++++++++++++++-------------------- 2 files changed, 33 insertions(+), 44 deletions(-) diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 2272d0a5a2c61..0bfbc1292426a 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -121,7 +121,7 @@ {{end}} @@ -140,7 +140,7 @@ function submitDeleteForm() { $("#delete-file-form").submit() } } -function copy(link) { +function copyLinkHref(link) { navigator.clipboard.writeText(link.href); return false; } diff --git a/web_src/js/index.js b/web_src/js/index.js index 450400861b609..8329b4b304785 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -2908,6 +2908,33 @@ 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; + } + + let updateIssueHref = function(anchor) { + 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${anchor}`)); + } else { + $issue.attr('href', `${$issue.attr('href')}%23${anchor}`); + } + } + + let updateCopyPermalinkHref = function(anchor) { + const matchedPermalink = $copyPermalink.attr('href').match(/#L\d+$|#L\d+-L\d+$/); + if (matchedPermalink) { + $copyPermalink.attr('href', $copyPermalink.attr('href').replace($copyPermalink.attr('href').substr(matchedPermalink.index), `#${anchor}`)); + } else { + $copyPermalink.attr('href', `${$copyPermalink.attr('href')}#${anchor}`); + } + } + if ($from) { let a = parseInt($select.attr('rel').substr(1)); let b = parseInt($from.attr('rel').substr(1)); @@ -2925,54 +2952,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'); - const $copyPermalink = $('a.copy-line-permalink'); - - if ($issue.length === 0 || $copyPermalink.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}`); - } - - const matchedPermalink = $copyPermalink.attr('href').match(/#L\d+$|#L\d+-L\d+$/); - if (matchedPermalink) { - $copyPermalink.attr('href', $copyPermalink.attr('href').replace($copyPermalink.attr('href').substr(matchedPermalink.index), `#L${a}-L${b}`)); - } else { - $copyPermalink.attr('href', `${$copyPermalink.attr('href')}#L${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'); - const $copyPermalink = $('a.copy-line-permalink'); - - if ($issue.length === 0 || $copyPermalink.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')}`); - } - - const matchedPermalink = $copyPermalink.attr('href').match(/#L\d+$|#L\d+-L\d+$/); - if (matchedPermalink) { - $copyPermalink.attr('href', $copyPermalink.attr('href').replace($copyPermalink.attr('href').substr(matchedPermalink.index), `#${$select.attr('rel')}`)); - } else { - $copyPermalink.attr('href', `${$copyPermalink.attr('href')}#${$select.attr('rel')}`); - } + updateIssueHref($select.attr('rel')); + updateCopyPermalinkHref($select.attr('rel')); } $(() => { From 1ed4f09b8e332abbdc51352537eda2f615899e4a Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Sat, 25 Sep 2021 19:32:36 +0200 Subject: [PATCH 4/5] Update code and fix lint --- web_src/js/index.js | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/web_src/js/index.js b/web_src/js/index.js index 8329b4b304785..fa95f83d4d581 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -2917,23 +2917,17 @@ function selectRange($list, $select, $from) { return; } - let updateIssueHref = function(anchor) { - 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${anchor}`)); - } else { - $issue.attr('href', `${$issue.attr('href')}%23${anchor}`); - } - } + const updateIssueHref = function(anchor) { + let href = $issue.attr('href'); + href = `${href.replace(/%23L\d+$|%23L\d+-L\d+$/, '')}%23${anchor}`; + $issue.attr('href', href); + }; - let updateCopyPermalinkHref = function(anchor) { - const matchedPermalink = $copyPermalink.attr('href').match(/#L\d+$|#L\d+-L\d+$/); - if (matchedPermalink) { - $copyPermalink.attr('href', $copyPermalink.attr('href').replace($copyPermalink.attr('href').substr(matchedPermalink.index), `#${anchor}`)); - } else { - $copyPermalink.attr('href', `${$copyPermalink.attr('href')}#${anchor}`); - } - } + const updateCopyPermalinkHref = function(anchor) { + let href = $copyPermalink.attr('href'); + href = `${href.replace(/#L\d+$|#L\d+-L\d+$/, '')}#${anchor}`; + $copyPermalink.attr('href', href); + }; if ($from) { let a = parseInt($select.attr('rel').substr(1)); From 3adc23d8f7471446f83abc81ee63f57068efb7c1 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Mon, 27 Sep 2021 12:43:21 +0200 Subject: [PATCH 5/5] Use features/clipboard.js framework --- templates/repo/view_file.tmpl | 6 +----- web_src/js/index.js | 6 +++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 0bfbc1292426a..8e75bcb4ca495 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -121,7 +121,7 @@ {{end}} @@ -140,8 +140,4 @@ function submitDeleteForm() { $("#delete-file-form").submit() } } -function copyLinkHref(link) { - navigator.clipboard.writeText(link.href); - return false; -} diff --git a/web_src/js/index.js b/web_src/js/index.js index fa95f83d4d581..2b1317a12b7e2 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -2924,9 +2924,9 @@ function selectRange($list, $select, $from) { }; const updateCopyPermalinkHref = function(anchor) { - let href = $copyPermalink.attr('href'); - href = `${href.replace(/#L\d+$|#L\d+-L\d+$/, '')}#${anchor}`; - $copyPermalink.attr('href', href); + 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) {