diff --git a/chrome/content/zotero/tools/cslpreview.js b/chrome/content/zotero/tools/cslpreview.js index dc177dd966..6fb87dee4a 100644 --- a/chrome/content/zotero/tools/cslpreview.js +++ b/chrome/content/zotero/tools/cslpreview.js @@ -24,25 +24,27 @@ Contributed by Julian Onions */ -var Zotero_CSL_Preview = new function() { - this.init = init; - this.refresh = refresh; - this.generateBibliography = generateBibliography; +// eslint-disable-next-line no-unused-vars +var Zotero_CSL_Preview = new function () { + this.lastContent = null; - function init() { + this.init = function () { var menulist = document.getElementById("locale-menu"); Zotero.Styles.populateLocaleList(menulist); - menulist.value = Zotero.Prefs.get('export.lastLocale');; + menulist.value = Zotero.Prefs.get('export.lastLocale'); - var iframe = document.getElementById('zotero-csl-preview-box'); - iframe.contentDocument.documentElement.innerHTML = '

' + Zotero.getString('styles.preview.instructions') + '

'; - } - function refresh() { - var iframe = document.getElementById('zotero-csl-preview-box'); + this.updateIframe(Zotero.getString('styles.preview.instructions')); + + window.matchMedia('(prefers-color-scheme: dark)').addEventListener("change", () => { + this.updateIframe(this.lastContent.content, this.lastContent.containerClass); + }); + }; + + this.refresh = function () { var items = Zotero.getActiveZoteroPane().getSelectedItems(); if (items.length === 0) { - iframe.contentDocument.documentElement.innerHTML = '

' + Zotero.getString('styles.editor.warning.noItems') + '

'; + this.updateIframe(Zotero.getString('styles.editor.warning.noItems'), 'warning'); return; } var progressWin = new Zotero.ProgressWindow(); @@ -52,11 +54,12 @@ var Zotero_CSL_Preview = new function() { progressWin.addLines(document.title, icon); progressWin.show(); progressWin.startCloseTimer(); - var f = function() { + // Give progress window time to appear + setTimeout(() => { var d = new Date(); var styles = Zotero.Styles.getVisible(); // XXX needs its own string really for the title! - var str = ''; + var str = '
'; for (let style of styles) { if (style.source) { continue; @@ -65,7 +68,7 @@ var Zotero_CSL_Preview = new function() { let bib; let err = false; try { - bib = generateBibliography(style); + bib = this.generateBibliography(style); } catch (e) { err = e; @@ -77,19 +80,15 @@ var Zotero_CSL_Preview = new function() { str += '
'; } } - - str += ''; - iframe.contentDocument.documentElement.innerHTML = str; - + + str += '
'; + this.updateIframe(str); + Zotero.debug(`Generated previews in ${new Date() - d} ms`); - }; - // Give progress window time to appear - setTimeout(f, 100); - } + }, 100); + }; - function generateBibliography(style) { - var iframe = document.getElementById('zotero-csl-preview-box'); - + this.generateBibliography = function (style) { var items = Zotero.getActiveZoteroPane().getSelectedItems(); if (items.length === 0) { return ''; @@ -115,7 +114,7 @@ var Zotero_CSL_Preview = new function() { // Generate bibliography var bibliography = ''; - if(style.hasBibliography) { + if (style.hasBibliography) { styleEngine.updateItems(items.map(item => item.id)); bibliography = Zotero.Cite.makeFormattedBibliography(styleEngine, "html"); } @@ -123,7 +122,23 @@ var Zotero_CSL_Preview = new function() { styleEngine.free(); return '

' + citations + '

' + bibliography; - } - - + }; + + 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}
+ `; + }; }(); diff --git a/chrome/content/zotero/tools/cslpreview.xhtml b/chrome/content/zotero/tools/cslpreview.xhtml index 3f60c1f046..126be8065e 100644 --- a/chrome/content/zotero/tools/cslpreview.xhtml +++ b/chrome/content/zotero/tools/cslpreview.xhtml @@ -25,7 +25,7 @@ Contributed by Julian Onions --> - + %cslpreviewDTD; @@ -59,7 +59,7 @@ -