Update typeSchemaData and builder script (#2576)

This commit is contained in:
Abe Jellinek 2022-04-27 23:12:29 -07:00 committed by GitHub
parent fe14ee79a3
commit 5196ba379b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 90 deletions

View file

@ -6,94 +6,6 @@
<p>This script builds typeSchemaData.js, which contains Zotero schema information for the connector.</p>
<p id="result"></p>
<script src="../include.js"></script>
<script type="text/javascript">
import FilePicker from 'zotero/modules/filePicker';
(async function () {
// Create schema
var schema = {"itemTypes":{}, "creatorTypes":{}, "fields":{}};
var types = Zotero.ItemTypes.getTypes();
var fieldIDs = await Zotero.DB.columnQueryAsync("SELECT fieldID FROM fieldsCombined");
var baseMappedFields = Zotero.ItemFields.getBaseMappedFields();
for (let fieldID of fieldIDs) {
var fieldObj = [/* name */Zotero.ItemFields.getName(fieldID)];
try {
// localizedString
let str = Zotero.getString("itemFields." + fieldObj.name);
if (str == "itemFields." + fieldObj.name) {
// Use name for localizedString
str = fieldObj[0];
}
fieldObj.push(str);
} catch(e) {
fieldObj.push(/* name -> localizedString */fieldObj[0]);
}
fieldObj.push(/* isBaseField */ !baseMappedFields.includes(fieldID));
schema.fields[fieldID] = fieldObj;
}
// names, localizedStrings, creatorTypes, and fields for each item type
for (let type of types) {
var fieldIDs = Zotero.ItemFields.getItemTypeFields(type.id);
var baseFields = {};
for (let fieldID of fieldIDs) {
if (baseMappedFields.includes(fieldID)) {
baseFields[fieldID] = Zotero.ItemFields.getBaseIDFromTypeAndField(type.id, fieldID);
}
}
var icon = Zotero.ItemTypes.getImageSrc(type.name);
icon = icon.substr(icon.lastIndexOf("/")+1);
try {
var creatorTypes = Zotero.CreatorTypes.getTypesForItemType(type.id).map((creatorType) => creatorType.id);
} catch (e) {
creatorTypes = [];
}
var primaryCreatorType = Zotero.CreatorTypes.getPrimaryIDForType(type.id);
if(creatorTypes[0] != primaryCreatorType) {
creatorTypes.splice(creatorTypes.indexOf(primaryCreatorType), 1);
creatorTypes.unshift(primaryCreatorType);
}
schema.itemTypes[type.id] = [
/* name */type.name,
/* localizedString */Zotero.ItemTypes.getLocalizedString(type.name),
/* creatorTypes */creatorTypes,
/* fields */ fieldIDs,
/* baseFields */baseFields,
/* icon */icon
];
}
var types = Zotero.CreatorTypes.getTypes();
for (let type of types) {
schema.creatorTypes[type.id] = [
/* name */type.name,
/* localizedString */Zotero.CreatorTypes.getLocalizedString(type.name)
];
}
// Write to file
var fp = new FilePicker();
fp.init(window, Zotero.getString('dataDir.selectDir'), fp.modeGetFolder);
let resultElem = document.getElementById('result');
if (await fp.show() != fp.returnOK) {
result.innerHTML = '<p>Failed.</p>';
} else {
let schemaFile = fp.file;
schemaFile.append("connectorTypeSchemaData.js");
await Zotero.File.putContentsAsync(
schemaFile,
`Zotero.Connector_Types.schema = ${JSON.stringify(schema)}`
);
result.innerHTML = `<p>Wrote ${schemaFile} successfully.</p>`;
}
})();
</script>
<script src="build_typeSchemaData.js"></script>
</body>
</html>

View file

@ -0,0 +1,87 @@
import FilePicker from 'zotero/modules/filePicker';
(async function () {
// Create schema
var schema = {"itemTypes":{}, "creatorTypes":{}, "fields":{}};
var types = Zotero.ItemTypes.getTypes();
var fieldIDs = await Zotero.DB.columnQueryAsync("SELECT fieldID FROM fieldsCombined");
var baseMappedFields = Zotero.ItemFields.getBaseMappedFields();
for (let fieldID of fieldIDs) {
var fieldObj = [/* name */Zotero.ItemFields.getName(fieldID)];
try {
// localizedString
let str = Zotero.getString("itemFields." + fieldObj.name);
if (str == "itemFields." + fieldObj.name) {
// Use name for localizedString
str = fieldObj[0];
}
fieldObj.push(str);
} catch(e) {
fieldObj.push(/* name -> localizedString */fieldObj[0]);
}
fieldObj.push(/* isBaseField */ !baseMappedFields.includes(fieldID));
schema.fields[fieldID] = fieldObj;
}
// names, localizedStrings, creatorTypes, and fields for each item type
for (let type of types) {
var fieldIDs = Zotero.ItemFields.getItemTypeFields(type.id);
var baseFields = {};
for (let fieldID of fieldIDs) {
if (baseMappedFields.includes(fieldID)) {
baseFields[fieldID] = Zotero.ItemFields.getBaseIDFromTypeAndField(type.id, fieldID);
}
}
var icon = Zotero.ItemTypes.getImageSrc(type.name);
icon = icon.substr(icon.lastIndexOf("/")+1);
try {
var creatorTypes = Zotero.CreatorTypes.getTypesForItemType(type.id).map((creatorType) => creatorType.id);
} catch (e) {
creatorTypes = [];
}
var primaryCreatorType = Zotero.CreatorTypes.getPrimaryIDForType(type.id);
if(creatorTypes[0] != primaryCreatorType) {
creatorTypes.splice(creatorTypes.indexOf(primaryCreatorType), 1);
creatorTypes.unshift(primaryCreatorType);
}
schema.itemTypes[type.id] = [
/* name */type.name,
/* localizedString */Zotero.ItemTypes.getLocalizedString(type.name),
/* creatorTypes */creatorTypes,
/* fields */ fieldIDs,
/* baseFields */baseFields,
/* icon */icon
];
}
var types = Zotero.CreatorTypes.getTypes();
for (let type of types) {
schema.creatorTypes[type.id] = [
/* name */type.name,
/* localizedString */Zotero.CreatorTypes.getLocalizedString(type.name)
];
}
// Write to file
var fp = new FilePicker();
fp.init(window, Zotero.getString('dataDir.selectDir'), fp.modeGetFolder);
let resultElem = document.getElementById('result');
if (await fp.show() != fp.returnOK) {
resultElem.innerHTML = '<p>Failed.</p>';
} else {
let schemaFile = Zotero.File.pathToFile(fp.file);
schemaFile.append("connectorTypeSchemaData.js");
await Zotero.File.putContentsAsync(
schemaFile,
`Zotero.Connector_Types.schema = ${JSON.stringify(schema)}`
);
resultElem.innerHTML = `<p>Wrote ${schemaFile} successfully.</p>`;
}
})();

File diff suppressed because one or more lines are too long