From 85d58b6f743244ab36305666c65bab57fa280ca0 Mon Sep 17 00:00:00 2001 From: hepfnepf Date: Thu, 5 Mar 2026 10:59:06 +0100 Subject: [PATCH 1/2] First draft. --- chrome/content/zotero/bibliography.js | 94 ++++++++++++++++++++++++ chrome/content/zotero/bibliography.xhtml | 6 +- 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/chrome/content/zotero/bibliography.js b/chrome/content/zotero/bibliography.js index 5be96e216a..06b395e837 100644 --- a/chrome/content/zotero/bibliography.js +++ b/chrome/content/zotero/bibliography.js @@ -61,6 +61,8 @@ window.Zotero_File_Interface_Bibliography = new function () { window.addEventListener('dialogaccept', () => this.acceptSelection()); window.addEventListener('dialoghelp', () => this.openHelpLink()); styleConfigurator = document.querySelector("#style-configurator"); + + this.updateIframe(Zotero.getString('styles.preview.instructions')); // Disable accept button until CE is initialized document.querySelector("dialog").getButton('accept').setAttribute('disabled', 'true'); @@ -142,11 +144,14 @@ window.Zotero_File_Interface_Bibliography = new function () { styleConfigurator.addEventListener("select", event => this.styleChanged(event)); styleConfigurator.addEventListener("manage-styles", this.manageStyles.bind(this)); + this.initBibWindow(); this.initDocPrefsWindow(); + this.refresh() + setTimeout(() => this.updateWindowSize(), 0); // set style to false, in case this is cancelled @@ -175,6 +180,9 @@ window.Zotero_File_Interface_Bibliography = new function () { document.getElementById('output-method-radio').selectedItem = document.getElementById(method); + + document.getElementById('output-mode-radio').addEventListener("select",this.refresh.bind(this)) + this.onBibWindowStyleChange(); }; @@ -248,6 +256,8 @@ window.Zotero_File_Interface_Bibliography = new function () { this.onDocPrefsWindowStyleChange(selectedStyleObj); } this.updateWindowSize(); + this.refresh(); + }; this.onBibWindowStyleChange = function (style = undefined) { @@ -388,6 +398,90 @@ window.Zotero_File_Interface_Bibliography = new function () { document.querySelector('dialog').acceptDialog(); } }; + + this.refresh = function () { + var items = Zotero.getActiveZoteroPane().getSelectedItems(); + if (items.length === 0) { + this.updateIframe(Zotero.getString('styles.editor.warning.noItems'), 'warning'); + return; + } + + let style = Zotero.Styles.get(styleConfigurator.style); + var d = new Date(); + var str = '
'; + + Zotero.debug("Generate bibliography for " + style.title); + let bib; + let err = false; + try { + bib = this.generateBibliography(style); + } + catch (e) { + err = e; + Zotero.logError(e); + } + if (bib || err) { + str += bib || `

${Zotero.Utilities.htmlSpecialChars(err)}

`; + str += '
'; + } + + + str += '
'; + this.updateIframe(str); + + Zotero.debug(`Generated previews in ${new Date() - d} ms`); + }; + + this.generateBibliography = function (style) { + var items = Zotero.getActiveZoteroPane().getSelectedItems(); + if (items.length === 0) { + return ''; + } + + + var locale = styleConfigurator.locale; + + let mode = document.getElementById("output-mode-radio").selectedItem.id; + var styleEngine = style.getCiteProc(locale, 'html'); + + // Generate multiple citations + let result = ""; + if(mode=="citations"){ + result = styleEngine.previewCitationCluster( + { + citationItems: items.map(item => ({ id: item.id })), + properties: {} + }, + [], [], "html" + ); + }else{ + if (style.hasBibliography) { + styleEngine.updateItems(items.map(item => item.id)); + result = Zotero.Cite.makeFormattedBibliography(styleEngine, "html"); + } + } + styleEngine.free(); + + return result; + }; + + this.updateIframe = function (content, containerClass = 'preview') { + this.lastContent = { content, containerClass }; + const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches; + let iframe = document.getElementById('zotero-csl-preview-box'); + iframe.contentDocument.documentElement.innerHTML = ` + + + + + +
${content}
+ `; + }; }; window.cancel = () => { diff --git a/chrome/content/zotero/bibliography.xhtml b/chrome/content/zotero/bibliography.xhtml index 91b8e246cd..38af9d7273 100644 --- a/chrome/content/zotero/bibliography.xhtml +++ b/chrome/content/zotero/bibliography.xhtml @@ -12,7 +12,7 @@ class="bibliography-window" data-l10n-id="bibliography-window" drawintitlebar-platforms="mac" - onload="Zotero_File_Interface_Bibliography.init()"> + onload="Zotero_File_Interface_Bibliography.init();"> @@ -35,7 +36,7 @@ - + @@ -44,6 +45,7 @@ +