refactoring

This commit is contained in:
Artur
2025-11-27 17:54:20 +03:00
parent 03071a5fdd
commit 8aba277baf
6 changed files with 729 additions and 941 deletions

View File

@ -226,7 +226,7 @@
</div>
</div>
</div>
<div class="notesStyle display-none">
<div id="notesStyle" class="notesStyle display-none">
<span class="i18n">Display citations as:</span>
<div class="radioHolder">
<input type="radio" id="footNotes" class="form-control" value="footnotes" name="notesAs" checked/>

File diff suppressed because it is too large Load Diff

View File

@ -4,29 +4,13 @@
/// <reference path="../csl/citation/citation.js" />
/// <reference path="../csl/styles/style-parser.js" />
/**
* @typedef {Object} CustomField
* @property {string} Value
* @property {string} Content
* @property {string} [FieldId]
*/
/**
* @param {string} citPrefix
* @param {string} citSuffix
* @param {string} bibPrefix
* @param {string} bibSuffix
* @param {StyleFormat} styleFormat
* @param {"footnotes" | "endnotes"} notesStyle
*/
function CitationDocService(
citPrefix,
citSuffix,
bibPrefix,
bibSuffix,
styleFormat,
notesStyle
) {
function CitationDocService(citPrefix, citSuffix, bibPrefix, bibSuffix) {
this._citPrefixOld = "ZOTERO_CITATION";
this._bibPrefixOld = "ZOTERO_BIBLIOGRAPHY";
@ -34,8 +18,6 @@ function CitationDocService(
this._citSuffix = citSuffix;
this._bibPrefix = bibPrefix;
this._bibSuffix = bibSuffix;
this._styleFormat = styleFormat;
this._notesStyle = notesStyle;
/** @type {number} */
this._repeatTimeout;
@ -78,9 +60,10 @@ CitationDocService.prototype.addBibliography = function (text, value) {
/**
* @param {string} text
* @param {string} value
* @param {"footnotes" | "endnotes" | null} notesStyle
* @returns
*/
CitationDocService.prototype.addCitation = function (text, value) {
CitationDocService.prototype.addCitation = function (text, value, notesStyle) {
const self = this;
const supSubPositions = this._removeSuperSubTagsWithPositions(text);
/** @type {CustomField} */
@ -88,22 +71,18 @@ CitationDocService.prototype.addCitation = function (text, value) {
Value: this._citPrefix + " " + this._citSuffix + value,
Content: supSubPositions.text,
};
if ("note" === this._styleFormat) {
switch (this._notesStyle) {
case "footnotes":
window.Asc.plugin.callCommand(function () {
const oDocument = Api.GetDocument();
oDocument.AddFootnote();
});
break;
case "endnotes":
window.Asc.plugin.callCommand(function () {
const oDocument = Api.GetDocument();
oDocument.AddEndnote();
});
break;
}
if ("footnotes" === notesStyle) {
window.Asc.plugin.callCommand(function () {
const oDocument = Api.GetDocument();
oDocument.AddFootnote();
});
} else if ("endnotes" === notesStyle) {
window.Asc.plugin.callCommand(function () {
const oDocument = Api.GetDocument();
oDocument.AddEndnote();
});
}
return this._addAddinField(field).then(function () {
if (!supSubPositions.positions.length) return;
return self._setSuperSubByPositions(supSubPositions.positions);
@ -258,20 +237,6 @@ CitationDocService.prototype.saveAsText = function () {
});
};
/**
* @param {"footnotes" | "endnotes"} notesStyle
*/
CitationDocService.prototype.setNotesStyle = function (notesStyle) {
this._notesStyle = notesStyle;
};
/**
* @param {StyleFormat} styleFormat
*/
CitationDocService.prototype.setStyleFormat = function (styleFormat) {
this._styleFormat = styleFormat;
};
/**
* @param {Array<SupSubPositions>} positions
* @returns {Promise<void>}

View File

@ -10,34 +10,38 @@
/// <reference path="../csl/locales/locales-manager.js" />
/**
* @param {CitationDocService} citationDocService
* @param {LocalesManager} localesManager
* @param {CslStylesManager} cslStylesManager
* @param {ZoteroSdk} sdk
*/
function CitationService(
citationDocService,
localesManager,
cslStylesManager,
sdk
) {
function CitationService(localesManager, cslStylesManager, sdk) {
this.bibPlaceholder = "Please insert some citation into the document.";
this.citPrefixNew = "ZOTERO_ITEM";
this.citSuffixNew = "CSL_CITATION";
this.citPrefix = "ZOTERO_CITATION";
this.bibPrefixNew = "ZOTERO_BIBL";
this.bibSuffixNew = "CSL_BIBLIOGRAPHY";
this.bibPrefix = "ZOTERO_BIBLIOGRAPHY";
this.sdk = sdk;
this.localesManager = localesManager;
this.cslStylesManager = cslStylesManager;
/** @type {CSL.Engine} */
this.formatter;
this.citationDocService = citationDocService;
this.citationDocService = new CitationDocService(
this.citPrefixNew,
this.citSuffixNew,
this.bibPrefixNew,
"CSL_BIBLIOGRAPHY"
);
/** @type {"footnotes" | "endnotes"} */
this._notesStyle;
/** @type {StyleFormat} */
this._styleFormat;
}
CitationService.prototype = {
/**
* @param {{id: string, uris: string[]}} item
* @param {SearchResultItem} item
* @returns
*/
fillUrisFromId: function (item) {
@ -45,7 +49,7 @@ CitationService.prototype = {
const slashLastIndex = item.id.lastIndexOf("/") + 1;
const httpIndex = item.id.indexOf("http");
if (slashFirstIndex !== slashLastIndex && httpIndex === 0) {
if (!Object.hasOwnProperty.call(item, "uris")) {
if (!item.uris) {
item.uris = [];
}
item.uris.push(item.id);
@ -99,9 +103,14 @@ CitationService.prototype = {
tempElement.innerHTML =
self.formatter.makeCitationCluster(keysL);
cslCitation.addPlainCitation(tempElement.innerText);
let notesStyle = null;
if ("note" === self._styleFormat) {
notesStyle = self._notesStyle;
}
return self.citationDocService.addCitation(
tempElement.innerText,
JSON.stringify(cslCitation.toJSON())
JSON.stringify(cslCitation.toJSON()),
notesStyle
);
})
.then(function () {
@ -233,76 +242,25 @@ CitationService.prototype = {
},
/**
* @param {{id: string, uris: string[]}} item
* @returns
* @returns {Promise<boolean>}
*/
/*synchronizeCSLItem: function (item) {
this.fillUrisFromId(item);
saveAsText: function () {
return this.citationDocService.saveAsText();
},
var cslItem = CSLCitationStorage.get(item.id);
if (!cslItem) {
return;
}
cslItem.fillFromObject(item);
},*/
/**
* @param {"footnotes" | "endnotes"} notesStyle
*/
setNotesStyle: function (notesStyle) {
this._notesStyle = notesStyle;
},
/*synchronizeData: function () {
const self = this;
// form an array for request (one array for user and other for groups)
// todo now we should make full update (because when we make refresh, we check fields into the document). Fix it in new version (when we change refreshing and updating processes)
if (!CSLCitationStorage.size) return;
showLoader(true);
var bHasGroupsItems = false;
var arrUsrItems = [];
var arrGroupsItems = {};
CSLCitationStorage.forEach(function (citationItem, id) {
let index = CSLCitationStorage.getIndex(id);
let item = citationItem.toFlatJSON(index);
var userID = citationItem.getProperty("userID");
var groupID = citationItem.getProperty("groupID");
if (userID) {
arrUsrItems.push(citationItem.id);
} else if (groupID) {
if (!arrGroupsItems[groupID]) arrGroupsItems[groupID] = [];
arrGroupsItems[groupID].push(item.id);
}
});
const promises = [];
if (arrUsrItems.length) {
promises.push(this.sdk
.getItems(null, arrUsrItems)
.then(function (res) {
var items = (res.items ? res.items.items : []) || [];
items.forEach(function (item) {
self.synchronizeCSLItem(item);
});
}));
}
for (var groupID in arrGroupsItems) {
if (Object.hasOwnProperty.call(arrGroupsItems, groupID)) {
bHasGroupsItems = true;
promises.push(this.sdk
.getGroupItems(null, groupID, arrGroupsItems[groupID])
.then(function (res) {
var items = (res.items ? res.items.items : []) || [];
items.forEach(function (item) {
self.synchronizeCSLItem(item);
});
}));
}
}
Promise.all(promises).catch(function (err) {
console.error(err);
}).then(function () {
return self._updateAfterSync();
});
},*/
/**
* @param {StyleFormat} styleFormat
*/
setStyleFormat: function (styleFormat) {
this._styleFormat = styleFormat;
},
/**
* @param {boolean} bUpdateAll
@ -451,10 +409,6 @@ CitationService.prototype = {
}
});
},
/*_updateAfterSync: function () {
// todo now we should make full update (because when we make refresh, we check fields into the document). Fix it in new version (when we change refreshing and updating processes)
this._updateFormatter(true, false, true);
},*/
// onInit (1,0,0)
// Insert Citation (1,0,0)

View File

@ -75,6 +75,57 @@
/** ------------------------------------------------ */
/**
* @typedef {Object} SearchResult
* @property {Array<SearchResultItem>} items
* @property {number|string} id
* @property {function(): Promise<SearchResult>} [next]
*/
/**
* @typedef {Object} SearchResultItem
* @property {string} [abstract]
* @property {string} [archive]
* @property {string} [archive_location]
* @property {Array<{family: string, given: string}>} [author]
* @property {string} [event-place]
* @property {number|string} [groupID]
* @property {string} [call-number]
* @property {string} [collection-number]
* @property {string} [collection-title]
* @property {string} [container-title]
* @property {string} [edition]
* @property {string} [event-place]
* @property {string} id
* @property {string} [ISBN]
* @property {{'date-parts': number[][]}} [issued]
* @property {string} [language]
* @property {string} [note]
* @property {string} [number-of-pages]
* @property {string} [number-of-volumes]
* @property {number} [page]
* @property {string} [publisher]
* @property {string} [publisher-place]
* @property {string} [shortTitle]
* @property {string} title
* @property {string} type
* @property {Array<string>} [uris]
* @property {string} [URL]
* @property {number|string} [userID]
* @property {string} [volume]
*/
/** ------------------------------------------------ */
/**
* @typedef {Object} CustomField
* @property {string} Value
* @property {string} Content
* @property {string} [FieldId]
*/
/** ------------------------------------------------ */
/**
* @typedef {Object} AscSimpleRequestParams
* @property {string} url
@ -155,7 +206,7 @@ var AscSimpleRequest = window.AscSimpleRequest;
* @property {function(string): void} sendToPlugin
* @property {function} onTranslate
* @property {function(string, function): void} attachEvent
* @property {string} onThemeChanged
* @property {function(string): void} onThemeChanged
* @property {function(string): void} onThemeChangedBase
* @property {AscPluginTheme} theme
* @property {function(string): string} tr
@ -186,7 +237,7 @@ var Api = window.Api;
* @typedef {Object} FetchResponse
* @property {function(): Promise<ArrayBuffer>} arrayBuffer
* @property {function(): Promise<Blob>} blob
* @property {function(): Promise<Object>} json
* @property {function(): Promise<any>} json
* @property {function(): Promise<string>} text
* @property {boolean} ok
* @property {number} status

View File

@ -188,11 +188,10 @@ ZoteroSdk.prototype._parseDesktopItemsResponse = function (
reject,
id
) {
var self = this;
return promise
.then(function (response) {
return {
items: { items: JSON.parse(response.responseText) },
items: JSON.parse(response.responseText),
id: id,
};
})
@ -225,11 +224,14 @@ ZoteroSdk.prototype._parseItemsResponse = function (
var links = self._parseLinkHeader(
response.headers.get("Link") || ""
);
/** @type {{items: any, id: number|string, next?: function(): Promise<void>}} */
/** @type {SearchResult} */
var result = {
items: json,
id: id,
};
if (typeof json === "object" && json.items) {
result.items = json.items;
}
if (links.next) {
result.next = function () {
@ -276,8 +278,9 @@ ZoteroSdk.prototype._parseResponse = function (promise, resolve, reject, id) {
/**
* Get items from user library
* @param {string|null} search
* @param {string[]} itemsID
* @param {string[]} [itemsID]
* @param {"csljson"|"json"} [format]
* @returns {Promise<SearchResult>}
*/
ZoteroSdk.prototype.getItems = function (search, itemsID, format) {
var self = this;
@ -311,9 +314,9 @@ ZoteroSdk.prototype.getItems = function (search, itemsID, format) {
* Get items from group library
* @param {string | null} search
* @param {number|string} groupId
* @param {string[]} itemsID
* @param {string[]} [itemsID]
* @param {"csljson"|"json"} [format]
*
* @returns {Promise<SearchResult>}
*/
ZoteroSdk.prototype.getGroupItems = function (
search,
@ -322,7 +325,6 @@ ZoteroSdk.prototype.getGroupItems = function (
format
) {
var self = this;
format = format || self.DEFAULT_FORMAT;
return new Promise(function (resolve, reject) {
@ -340,7 +342,6 @@ ZoteroSdk.prototype.getGroupItems = function (
var path =
self.API_PATHS.GROUPS + "/" + groupId + "/" + self.API_PATHS.ITEMS;
var request = self._buildGetRequest(path, queryParams);
self._parseResponse(request, resolve, reject, groupId);
});
};