2022-11-11 18:02:50 +01:00
|
|
|
import $ from 'jquery';
|
2024-03-23 00:17:28 +01:00
|
|
|
import {getCurrentLocale} from '../utils.js';
|
2022-11-11 18:02:50 +01:00
|
|
|
|
|
|
|
const {pageData} = window.config;
|
|
|
|
|
2024-07-20 15:46:38 +02:00
|
|
|
async function initInputCitationValue(inputContent) {
|
2022-11-11 18:02:50 +01:00
|
|
|
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'),
|
|
|
|
]);
|
|
|
|
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);
|
2024-03-23 00:17:28 +01:00
|
|
|
const lang = getCurrentLocale() || 'en-US';
|
2022-11-11 18:02:50 +01:00
|
|
|
const bibtexOutput = citationFormatter.format('bibtex', {lang});
|
2024-07-20 15:46:38 +02:00
|
|
|
inputContent.value = bibtexOutput;
|
2023-12-12 12:31:11 +01:00
|
|
|
}
|
2022-11-11 18:02:50 +01:00
|
|
|
|
2023-12-12 12:31:11 +01:00
|
|
|
export async function initCitationFileCopyContent() {
|
2022-11-11 18:02:50 +01:00
|
|
|
if (!pageData.citationFileContent) return;
|
|
|
|
|
2024-03-23 00:17:28 +01:00
|
|
|
const inputContent = document.getElementById('citation-copy-content');
|
|
|
|
|
2024-07-20 15:46:38 +02:00
|
|
|
if (!inputContent) return;
|
2023-12-12 12:31:11 +01:00
|
|
|
|
2024-03-23 00:17:28 +01:00
|
|
|
document.getElementById('cite-repo-button')?.addEventListener('click', async (e) => {
|
2024-03-17 11:04:59 +01:00
|
|
|
const dropdownBtn = e.target.closest('.ui.dropdown.button');
|
|
|
|
dropdownBtn.classList.add('is-loading');
|
2022-11-11 18:02:50 +01:00
|
|
|
|
2024-03-17 11:04:59 +01:00
|
|
|
try {
|
|
|
|
try {
|
2024-07-20 15:46:38 +02:00
|
|
|
await initInputCitationValue(inputContent);
|
2024-03-17 11:04:59 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(`initCitationFileCopyContent error: ${e}`, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-23 00:17:28 +01:00
|
|
|
inputContent.addEventListener('click', () => {
|
|
|
|
inputContent.select();
|
2024-03-17 11:04:59 +01:00
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
dropdownBtn.classList.remove('is-loading');
|
|
|
|
}
|
2022-11-11 18:02:50 +01:00
|
|
|
|
|
|
|
$('#cite-repo-modal').modal('show');
|
|
|
|
});
|
|
|
|
}
|