Style editor: Simplify exposed methods, add shortcuts for refresh (#5545)

This commit is contained in:
Abe Jellinek 2025-10-08 14:51:42 -04:00 committed by GitHub
parent da0578fc39
commit e2fc3d35b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 24 deletions

View file

@ -28,10 +28,7 @@ var { FilePicker } = ChromeUtils.importESModule('chrome://zotero/content/modules
var Zotero_CSL_Editor = new function () {
let monaco, editor;
this.init = init;
this.loadCSL = loadCSL;
async function init() {
this.init = async function () {
await Zotero.Schema.schemaUpdatePromise;
const isDarkMQL = window.matchMedia('(prefers-color-scheme: dark)');
@ -74,9 +71,7 @@ var Zotero_CSL_Editor = new function () {
monaco = _monaco;
editor = _editor;
editor.getModel().onDidChangeContent(Zotero.Utilities.debounce(() => {
this.onStyleModified();
}, 250));
editor.getModel().onDidChangeContent(this.onStyleModifiedDebounced);
if (currentStyle) {
// Call asynchronously, see note in Zotero.Styles
@ -87,7 +82,7 @@ var Zotero_CSL_Editor = new function () {
monaco.editor.setTheme(ev.matches ? 'vs-dark' : 'vs-light');
this.refresh();
});
}
};
this.onStyleSelected = function (styleID) {
Zotero.Prefs.set('export.lastStyle', styleID);
@ -98,16 +93,10 @@ var Zotero_CSL_Editor = new function () {
Zotero.Prefs.get('export.lastLocale')
);
loadCSL(style.styleID);
this.loadCSL(style.styleID);
this.refresh();
};
this.refresh = function () {
this.generateBibliography(this.loadStyleFromEditor());
};
this.refreshDebounced = Zotero.Utilities.debounce(this.refresh, 250);
this.save = async function () {
var style = editor.getValue();
var fp = new FilePicker();
@ -131,11 +120,11 @@ var Zotero_CSL_Editor = new function () {
}
};
function loadCSL(cslID) {
this.loadCSL = function (cslID) {
var style = Zotero.Styles.get(cslID);
editor.setValue(style.getXML());
document.getElementById('zotero-csl-list').value = cslID;
}
};
this.loadStyleFromEditor = function () {
var styleObject;
@ -174,7 +163,9 @@ var Zotero_CSL_Editor = new function () {
Zotero_CSL_Editor.generateBibliography(styleObject);
};
this.generateBibliography = function (style) {
this.onStyleModifiedDebounced = Zotero.Utilities.debounce(this.onStyleModified.bind(this), 250);
this.generateBibliography = function (style = this.loadStyleFromEditor()) {
var items = Zotero.getActiveZoteroPane().getSelectedItems();
if (items.length == 0) {
this.updateIframe(Zotero.getString('styles.editor.warning.noItems'), 'warning');
@ -246,6 +237,8 @@ var Zotero_CSL_Editor = new function () {
styleEngine.free();
};
this.generateBibliographyDebounced = Zotero.Utilities.debounce(this.generateBibliography, 250);
this.updateMarkers = function (rawErrors) {
let model = editor.getModel();
let errors = rawErrors ? rawErrors.split('\n') : [];

View file

@ -49,18 +49,18 @@
<vbox flex="1">
<vbox class="csl-edit-toolbars">
<hbox class="csl-edit-toolbar">
<button id="preview-refresh-button" label="&zotero.general.refresh;" oncommand="Zotero_CSL_Editor.refresh()"/>
<button id="preview-refresh-button" label="&zotero.general.refresh;" command="cmd_refresh"/>
<button id="zotero-csl-save" label="&zotero.general.saveAs;" oncommand="Zotero_CSL_Editor.save()"/>
<html:div class="divider" />
<menulist id="zotero-csl-page-type" oncommand="Zotero_CSL_Editor.refresh()" native="true" data-l10n-id="styleEditor-locatorType"/>
<menulist id="zotero-csl-page-type" oncommand="Zotero_CSL_Editor.generateBibliography()" native="true" data-l10n-id="styleEditor-locatorType"/>
<label value=":" />
<label id="locator-input" data-l10n-id="styleEditor-locatorInput" hidden="true"/>
<html:input size="5" id="preview-pages" oninput="Zotero_CSL_Editor.refreshDebounced()" aria-labelledby="zotero-csl-page-type locator-input"/>
<html:input size="5" id="preview-pages" oninput="Zotero_CSL_Editor.generateBibliographyDebounced()" aria-labelledby="zotero-csl-page-type locator-input"/>
<html:div class="divider" />
<checkbox oncommand="Zotero_CSL_Editor.refresh()" id="preview-suppress-author" label="&zotero.citation.suppressAuthor.label;" native="true" />
<checkbox oncommand="Zotero_CSL_Editor.generateBibliography()" id="preview-suppress-author" label="&zotero.citation.suppressAuthor.label;" native="true" />
<html:div class="divider" />
<label value="&styles.editor.citePosition;" control="zotero-ref-position"/>
<menulist id="zotero-ref-position" oncommand="Zotero_CSL_Editor.refresh()" native="true">
<menulist id="zotero-ref-position" oncommand="Zotero_CSL_Editor.generateBibliography()" native="true">
<menupopup>
<menuitem label="first" value="0"/>
<menuitem label="subsequent" value="1"/>
@ -75,7 +75,8 @@
<menulist id="zotero-csl-list" oncommand="Zotero_CSL_Editor.onStyleSelected(this.value)" native="true"/>
<html:div class="divider" />
<label data-l10n-id="styleEditor-locale" control="locale-menu"/>
<menulist id="locale-menu" oncommand="Zotero.Prefs.set('export.lastLocale', this.value); Zotero_CSL_Editor.refresh()" native="true" />
<menulist id="locale-menu" oncommand="Zotero.Prefs.set('export.lastLocale', this.value); Zotero_CSL_Editor.generateBibliography()" native="true" />
</hbox>
</vbox>
<iframe id="zotero-csl-editor-iframe" src="chrome://scaffold/content/monaco/monaco.html" flex="1"
@ -88,5 +89,11 @@
<keyset>
<key id="key_close" key="W" modifiers="accel" oncommand="window.close()"/>
<key keycode="VK_F5" command="cmd_refresh"/>
<key key="R" modifiers="accel" command="cmd_refresh"/>
</keyset>
<commandset>
<command id="cmd_refresh" oncommand="Zotero_CSL_Editor.generateBibliography()"/>
</commandset>
</window>