Improve Add/Edit behaviour when cursor is in a non-citation field

When in bibliography, show an appropriate message. When in any other
non-citation Zotero field - just open an empty edit citation dialog.
This commit is contained in:
Adomas Venčkauskas 2026-05-29 14:28:39 +03:00
parent 8cf5c9b3d1
commit 90cc1f7c67
3 changed files with 48 additions and 3 deletions

View file

@ -1513,10 +1513,21 @@ Zotero.Integration.Session.prototype.cite = async function (field, addNote=false
if (field) {
field = await Zotero.Integration.Field.loadExisting(field);
if (field.type != INTEGRATION_TYPE_ITEM) {
throw new Zotero.Exception.Alert("integration.error.notInCitation");
if (field.type === INTEGRATION_TYPE_ITEM) {
citation = new Zotero.Integration.Citation(field, await field.unserialize(), await field.getNoteIndex());
}
else if (field.type === INTEGRATION_TYPE_BIBLIOGRAPHY) {
throw new Zotero.Exception.Alert("integration.error.inBibliography");
}
else {
// Treat any non-item and non-bibliograph field as a TEMP placeholder, that is likely
// there because previous integration command stopped prematurely (e.g. by closing Zotero).
// This could also be something else, but since the integration plugin on the word processor
// side decided to return what it thought to be a Zotero field, we should treat it as such.
newField = true;
field = new Zotero.Integration.CitationField(field._field);
citation = new Zotero.Integration.Citation(field);
}
citation = new Zotero.Integration.Citation(field, await field.unserialize(), await field.getNoteIndex());
} else {
newField = true;
field = new Zotero.Integration.CitationField(await this.addField(true));

View file

@ -894,6 +894,7 @@ integration.error.mustInsertCitation = You must insert a citation before perfor
integration.error.mustInsertBibliography = You must insert a bibliography before performing this operation.
integration.error.cannotInsertHere = Zotero fields cannot be inserted here.
integration.error.notInCitation = You must place the cursor in a Zotero citation to edit it.
integration.error.inBibliography = To insert a citation, place the cursor in the body of the text. To edit the bibliography directly, use the Add/Edit Bibliography button.
integration.error.noBibliography = The current bibliographic style does not define a bibliography. If you wish to add a bibliography, please choose another style.
integration.error.deletePipe = The pipe that Zotero uses to communicate with the word processor could not be initialized. Would you like Zotero to attempt to correct this error? You will be prompted for your password.
integration.error.invalidStyle = The style you have selected does not appear to be valid. If you have created this style yourself, please ensure that it passes validation as described at https://github.com/citation-style-language/styles/wiki/Validation. Alternatively, try selecting another style.

View file

@ -615,6 +615,39 @@ describe("Zotero.Integration", function () {
assert.equal(citation.citationItems.length, 2);
assert.equal(citation.citationItems[0].id, testItems[3].id);
});
it('should continue citation insertion if in TEMP field', async function () {
var docID = this.test.fullTitle();
if (!(docID in applications)) await initDoc(docID);
var doc = applications[docID].doc;
var field = await doc.insertField('Field', 0);
await field.setCode('TEMP');
sinon.stub(doc, 'cursorInField').resolves(field);
sinon.stub(doc, 'canInsertField').resolves(false);
setAddEditItems(testItems[0]);
await execCommand('addEditCitation', docID);
assert.equal(doc.fields.length, 1);
var citation = await (new Zotero.Integration.CitationField(field, field.code)).unserialize();
assert.equal(citation.citationItems.length, 1);
assert.equal(citation.citationItems[0].id, testItems[0].id);
});
it('should display a bibliography-specific error if in bibliography field', async function () {
await insertMultipleCitations.call(this);
var docID = this.test.fullTitle();
var doc = applications[docID].doc;
await execCommand('addEditBibliography', docID);
var bibliographyField = doc.fields[doc.fields.length - 1];
sinon.stub(doc, 'cursorInField').resolves(bibliographyField);
var displayAlertStub = sinon.stub(doc, 'displayAlert').resolves(0);
await execCommand('addEditCitation', docID);
assert.isTrue(displayAlertStub.calledOnce);
assert.equal(displayAlertStub.firstCall.args[0], Zotero.getString('integration.error.inBibliography'));
});
it('should write an implicitly updated citation into the document', async function () {
await insertMultipleCitations.call(this);