diff --git a/chrome/content/zotero/tools/csledit.js b/chrome/content/zotero/tools/csledit.js index d44ebb773f..4b846c76e4 100644 --- a/chrome/content/zotero/tools/csledit.js +++ b/chrome/content/zotero/tools/csledit.js @@ -25,7 +25,7 @@ import FilePicker from 'zotero/modules/filePicker'; -var Zotero_CSL_Editor = new function() { +var Zotero_CSL_Editor = new function () { let monaco, editor; this.init = init; @@ -86,7 +86,7 @@ var Zotero_CSL_Editor = new function() { }); } - this.onStyleSelected = function(styleID) { + this.onStyleSelected = function (styleID) { Zotero.Prefs.set('export.lastStyle', styleID); let style = Zotero.Styles.get(styleID); Zotero.Styles.updateLocaleList( @@ -97,11 +97,11 @@ var Zotero_CSL_Editor = new function() { loadCSL(style.styleID); this.refresh(); - } + }; - this.refresh = function() { + this.refresh = function () { this.generateBibliography(this.loadStyleFromEditor()); - } + }; this.refreshDebounced = Zotero.Utilities.debounce(this.refresh, 250); @@ -110,7 +110,7 @@ var Zotero_CSL_Editor = new function() { var fp = new FilePicker(); fp.init(window, Zotero.getString('styles.editor.save'), fp.modeSave); fp.appendFilter("Citation Style Language", "*.csl"); - //get the filename from the id; we could consider doing even more here like creating the id from filename. + //get the filename from the id; we could consider doing even more here like creating the id from filename. var parser = new DOMParser(); var doc = parser.parseFromString(style, 'text/xml'); var filename = doc.getElementsByTagName("id"); @@ -134,22 +134,20 @@ var Zotero_CSL_Editor = new function() { document.getElementById('zotero-csl-list').value = cslID; } - this.loadStyleFromEditor = function() { + this.loadStyleFromEditor = function () { var styleObject; try { styleObject = new Zotero.Style( editor.getValue() ); - } catch(e) { - document.getElementById('zotero-csl-preview-box') - .contentDocument.documentElement.innerHTML = '
' - + Zotero.getString('styles.editor.warning.parseError') - + '
' + e + '
'; + } + catch (e) { + this.updateIframe(Zotero.getString('styles.editor.warning.parseError') + '
' + e + '
', 'error'); throw e; } return styleObject; - } + }; this.onStyleModified = function () { let xml = editor.getValue(); @@ -171,17 +169,12 @@ var Zotero_CSL_Editor = new function() { Zotero.Prefs.get('export.lastLocale') ); Zotero_CSL_Editor.generateBibliography(styleObject); - } + }; - this.generateBibliography = function(style) { - var iframe = document.getElementById('zotero-csl-preview-box'); - + this.generateBibliography = function (style) { 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; } @@ -189,8 +182,9 @@ var Zotero_CSL_Editor = new function() { var styleEngine; try { styleEngine = style.getCiteProc(style.locale || selectedLocale, 'html'); - } catch(e) { - iframe.contentDocument.documentElement.innerHTML = '
' + Zotero.getString('styles.editor.warning.parseError') + '
'+e+'
'; + } + catch (e) { + this.updateIframe(Zotero.getString('styles.editor.warning.parseError') + '
' + e + '
'); throw e; } @@ -203,8 +197,8 @@ var Zotero_CSL_Editor = new function() { citation.citationItems = []; citation.properties = {}; citation.properties.noteIndex = 1; - for (var i = 0, ilen = items.length; i < ilen; i += 1) { - citation.citationItems.push({id:itemIds[i]}); + for (let i = 0, ilen = items.length; i < ilen; i += 1) { + citation.citationItems.push({ id: itemIds[i] }); } // Generate single citations @@ -213,7 +207,7 @@ var Zotero_CSL_Editor = new function() { var loc = document.getElementById('zotero-csl-page-type'); var pos = document.getElementById('zotero-ref-position').selectedItem.value; var citations = '

' + Zotero.getString('styles.editor.output.individualCitations') + '

'; - for (var i=0; i'; } try { - var multCitations = '

' + Zotero.getString('styles.editor.output.singleCitation') + '

' + - styleEngine.previewCitationCluster(citation, [], [], "html"); + var multCitations = '

' + Zotero.getString('styles.editor.output.singleCitation') + '

' + + styleEngine.previewCitationCluster(citation, [], [], "html"); // Generate bibliography styleEngine.updateItems(itemIds); - var bibliography = '

' + Zotero.getString('styles.bibliography') + '

' + - Zotero.Cite.makeFormattedBibliography(styleEngine, "html"); - - iframe.contentDocument.documentElement.innerHTML = - '
' + citations + multCitations + bibliography + '
'; - } catch(e) { - iframe.contentDocument.documentElement.innerHTML = '
' + Zotero.getString('styles.editor.warning.renderError') + '
'+e+'
'; - throw e; + var bibliography = '

' + Zotero.getString('styles.bibliography') + '

' + + Zotero.Cite.makeFormattedBibliography(styleEngine, "html"); + + this.updateIframe(citations + multCitations + bibliography); + } + catch (e) { + this.updateIframe(Zotero.getString('styles.editor.warning.renderError') + '
' + e + '
', 'error'); + throw e; } styleEngine.free(); - } + }; this.updateMarkers = function (rawErrors) { let model = editor.getModel(); @@ -270,4 +264,15 @@ var Zotero_CSL_Editor = new function() { }).filter(Boolean); monaco.editor.setModelMarkers(model, 'csl-validator', markers); }; + + this.updateIframe = function (content, containerClass = 'preview') { + let iframe = document.getElementById('zotero-csl-preview-box'); + iframe.contentDocument.documentElement.innerHTML = ` + + + + +
${content}
+ `; + }; }(); diff --git a/chrome/content/zotero/tools/csledit.xhtml b/chrome/content/zotero/tools/csledit.xhtml index ae83a45870..91668ed802 100644 --- a/chrome/content/zotero/tools/csledit.xhtml +++ b/chrome/content/zotero/tools/csledit.xhtml @@ -24,7 +24,7 @@ ***** END LICENSE BLOCK ***** --> - + %csleditDTD; @@ -43,34 +43,39 @@