Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load citation JS only when needed #29855

Merged
merged 3 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions web_src/css/modules/animations.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
opacity: 0.3;
}

.button.is-loading > * {
opacity: 0;
}

.is-loading::after {
content: "";
position: absolute;
Expand Down
45 changes: 26 additions & 19 deletions web_src/js/features/citation.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,35 @@ export async function initCitationFileCopyContent() {
$citationCopyApa.toggleClass('primary', !isBibtex);
};

try {
await initInputCitationValue($citationCopyApa, $citationCopyBibtex);
} catch (e) {
console.error(`initCitationFileCopyContent error: ${e}`, e);
return;
}
updateUi();
$('#cite-repo-button').on('click', async (e) => {
const dropdownBtn = e.target.closest('.ui.dropdown.button');
dropdownBtn.classList.add('is-loading');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sidenote: for the worry about "some refactoring may break this logic", I have 2 ideas for it:

  1. Just leave it as-is. Because we have error-catch mechanism, so if it breaks, there should be immediate error reports on the UI (dropdownBtn.xxx will fail if it is null)
  2. Add a helper function like closestVisibleElem(), maybe it could be useful for some more cases.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.closest is straightforward, I think such a helper would only very rarely be useful.


$citationCopyApa.on('click', () => {
localStorage.setItem('citation-copy-format', 'apa');
updateUi();
});
$citationCopyBibtex.on('click', () => {
localStorage.setItem('citation-copy-format', 'bibtex');
updateUi();
});
try {
try {
await initInputCitationValue($citationCopyApa, $citationCopyBibtex);
} catch (e) {
console.error(`initCitationFileCopyContent error: ${e}`, e);
return;
}
updateUi();

$inputContent.on('click', () => {
$inputContent.trigger('select');
});
$citationCopyApa.on('click', () => {
localStorage.setItem('citation-copy-format', 'apa');
updateUi();
});
$citationCopyBibtex.on('click', () => {
localStorage.setItem('citation-copy-format', 'bibtex');
updateUi();
});

$inputContent.on('click', () => {
$inputContent.trigger('select');
});
} finally {
dropdownBtn.classList.remove('is-loading');
}

$('#cite-repo-button').on('click', () => {
$('#cite-repo-modal').modal('show');
});
}