diff options
author | Nolann <62215577+Nolann71@users.noreply.github.com> | 2022-11-11 18:02:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-11 18:02:50 +0100 |
commit | 9f8e7789187013ab6752c2635b24c9dd1d70cd90 (patch) | |
tree | fe0e6c933d866cebe2aa09d322ba5d18f0ccea99 /web_src | |
parent | 9db221780f28e161c02c4106f63d0b6f185933d4 (diff) | |
download | gitea-9f8e7789187013ab6752c2635b24c9dd1d70cd90.tar.gz gitea-9f8e7789187013ab6752c2635b24c9dd1d70cd90.zip |
Copy citation file content, in APA and BibTex format, on repo home page (#19999)
Add feature to easily copy CITATION.cff content in APA and BibTex format.
Diffstat (limited to 'web_src')
-rw-r--r-- | web_src/js/features/citation.js | 60 | ||||
-rw-r--r-- | web_src/js/features/repo-legacy.js | 2 | ||||
-rw-r--r-- | web_src/less/_repository.less | 52 |
3 files changed, 113 insertions, 1 deletions
diff --git a/web_src/js/features/citation.js b/web_src/js/features/citation.js new file mode 100644 index 0000000000..01fcd95a1c --- /dev/null +++ b/web_src/js/features/citation.js @@ -0,0 +1,60 @@ +import $ from 'jquery'; + +const {pageData} = window.config; + +const initInputCitationValue = async ($citationCopyBibtex, $citationCopyApa) => { + const [{Cite, plugins}] = await Promise.all([ + import(/* webpackChunkName: "citation-js-core" */'@citation-js/core'), + import(/* webpackChunkName: "citation-js-formats" */'@citation-js/plugin-software-formats'), + import(/* webpackChunkName: "citation-js-bibtex" */'@citation-js/plugin-bibtex'), + import(/* webpackChunkName: "citation-js-bibtex" */'@citation-js/plugin-csl'), + ]); + const {citationFileContent} = pageData; + const config = plugins.config.get('@bibtex'); + config.constants.fieldTypes.doi = ['field', 'literal']; + config.constants.fieldTypes.version = ['field', 'literal']; + const citationFormatter = new Cite(citationFileContent); + const lang = document.documentElement.lang || 'en-US'; + const apaOutput = citationFormatter.format('bibliography', {template: 'apa', lang}); + const bibtexOutput = citationFormatter.format('bibtex', {lang}); + $citationCopyBibtex.attr('data-text', bibtexOutput); + $citationCopyApa.attr('data-text', apaOutput); +}; + +export function initCitationFileCopyContent() { + const defaultCitationFormat = 'apa'; // apa or bibtex + + if (!pageData.citationFileContent) return; + + const $citationCopyApa = $('#citation-copy-apa'); + const $citationCopyBibtex = $('#citation-copy-bibtex'); + const $inputContent = $('#citation-copy-content'); + + if ((!$citationCopyApa.length && !$citationCopyBibtex.length) || !$inputContent.length) return; + const updateUi = () => { + const isBibtex = (localStorage.getItem('citation-copy-format') || defaultCitationFormat) === 'bibtex'; + const copyContent = (isBibtex ? $citationCopyBibtex : $citationCopyApa).attr('data-text'); + + $inputContent.val(copyContent); + $citationCopyBibtex.toggleClass('primary', isBibtex); + $citationCopyApa.toggleClass('primary', !isBibtex); + }; + initInputCitationValue($citationCopyApa, $citationCopyBibtex).then(updateUi); + + $citationCopyApa.on('click', () => { + localStorage.setItem('citation-copy-format', 'apa'); + updateUi(); + }); + $citationCopyBibtex.on('click', () => { + localStorage.setItem('citation-copy-format', 'bibtex'); + updateUi(); + }); + + $inputContent.on('click', () => { + $inputContent.select(); + }); + + $('#cite-repo-button').on('click', () => { + $('#cite-repo-modal').modal('show'); + }); +} diff --git a/web_src/js/features/repo-legacy.js b/web_src/js/features/repo-legacy.js index 2c93ca0342..e6a7c6dcd1 100644 --- a/web_src/js/features/repo-legacy.js +++ b/web_src/js/features/repo-legacy.js @@ -21,6 +21,7 @@ import { initRepoCommonFilterSearchDropdown, initRepoCommonLanguageStats, } from './repo-common.js'; +import {initCitationFileCopyContent} from './citation.js'; import {initCompLabelEdit} from './comp/LabelEdit.js'; import {initRepoDiffConversationNav} from './repo-diff.js'; import attachTribute from './tribute.js'; @@ -505,6 +506,7 @@ export function initRepository() { } initRepoCloneLink(); + initCitationFileCopyContent(); initRepoCommonLanguageStats(); initRepoSettingBranches(); diff --git a/web_src/less/_repository.less b/web_src/less/_repository.less index 193a73c706..bf1fb53e2b 100644 --- a/web_src/less/_repository.less +++ b/web_src/less/_repository.less @@ -227,7 +227,7 @@ border-right: none; } - #download-btn { + #more-btn { border-left: none; } @@ -2468,6 +2468,56 @@ // End of .repository +#cite-repo-modal { + #citation-panel { + width: 500px; + + @media @mediaSm { + width: 100%; + } + + input { + border-radius: 0; + padding: 5px 10px; + width: 50%; + line-height: 1.4; + } + + .citation.button { + font-size: 13px; + padding: 7.5px 5px; + } + + #citation-copy-content { + border-radius: 0; + padding: 5px 10px; + font-size: 1.2em; + line-height: 1.4; + } + + #citation-copy-apa, + #citation-copy-bibtex { + border-right: none; + } + + #goto-citation-btn { + border-left: none; + } + + >:first-child { + border-radius: var(--border-radius) 0 0 var(--border-radius) !important; + } + + >:last-child { + border-radius: 0 var(--border-radius) var(--border-radius) 0 !important; + } + + .icon.button { + padding: 0 10px; + } + } +} + &.user-cards { .list { padding: 0; |