code.gitea.io/gitea@v1.21.7/web_src/js/features/citation.js (about)

     1  import $ from 'jquery';
     2  
     3  const {pageData} = window.config;
     4  
     5  async function initInputCitationValue($citationCopyApa, $citationCopyBibtex) {
     6    const [{Cite, plugins}] = await Promise.all([
     7      import(/* webpackChunkName: "citation-js-core" */'@citation-js/core'),
     8      import(/* webpackChunkName: "citation-js-formats" */'@citation-js/plugin-software-formats'),
     9      import(/* webpackChunkName: "citation-js-bibtex" */'@citation-js/plugin-bibtex'),
    10      import(/* webpackChunkName: "citation-js-csl" */'@citation-js/plugin-csl'),
    11    ]);
    12    const {citationFileContent} = pageData;
    13    const config = plugins.config.get('@bibtex');
    14    config.constants.fieldTypes.doi = ['field', 'literal'];
    15    config.constants.fieldTypes.version = ['field', 'literal'];
    16    const citationFormatter = new Cite(citationFileContent);
    17    const lang = document.documentElement.lang || 'en-US';
    18    const apaOutput = citationFormatter.format('bibliography', {template: 'apa', lang});
    19    const bibtexOutput = citationFormatter.format('bibtex', {lang});
    20    $citationCopyBibtex.attr('data-text', bibtexOutput);
    21    $citationCopyApa.attr('data-text', apaOutput);
    22  }
    23  
    24  export async function initCitationFileCopyContent() {
    25    const defaultCitationFormat = 'apa'; // apa or bibtex
    26  
    27    if (!pageData.citationFileContent) return;
    28  
    29    const $citationCopyApa = $('#citation-copy-apa');
    30    const $citationCopyBibtex = $('#citation-copy-bibtex');
    31    const $inputContent = $('#citation-copy-content');
    32  
    33    if ((!$citationCopyApa.length && !$citationCopyBibtex.length) || !$inputContent.length) return;
    34    const updateUi = () => {
    35      const isBibtex = (localStorage.getItem('citation-copy-format') || defaultCitationFormat) === 'bibtex';
    36      const copyContent = (isBibtex ? $citationCopyBibtex : $citationCopyApa).attr('data-text');
    37  
    38      $inputContent.val(copyContent);
    39      $citationCopyBibtex.toggleClass('primary', isBibtex);
    40      $citationCopyApa.toggleClass('primary', !isBibtex);
    41    };
    42  
    43    try {
    44      await initInputCitationValue($citationCopyApa, $citationCopyBibtex);
    45    } catch (e) {
    46      console.error(`initCitationFileCopyContent error: ${e}`, e);
    47      return;
    48    }
    49    updateUi();
    50  
    51    $citationCopyApa.on('click', () => {
    52      localStorage.setItem('citation-copy-format', 'apa');
    53      updateUi();
    54    });
    55    $citationCopyBibtex.on('click', () => {
    56      localStorage.setItem('citation-copy-format', 'bibtex');
    57      updateUi();
    58    });
    59  
    60    $inputContent.on('click', () => {
    61      $inputContent.trigger('select');
    62    });
    63  
    64    $('#cite-repo-button').on('click', () => {
    65      $('#cite-repo-modal').modal('show');
    66    });
    67  }