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