sanity-plugin-spreadsheet-wizard-x
Version:
Spreadsheet Wizard is a magnificient Sanity plugin that help you EXPORT & IMPORT spreadsheets. A fantastic bridge between Sanity & your spreadsheet software of choice. (Google Sheets... Excel... Numbers etc.)
137 lines (113 loc) • 4.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.csvFileFromObjects = csvFileFromObjects;
exports.download = download;
exports.fieldsAndDataProcessed = fieldsAndDataProcessed;
exports.fieldsOfParticularSchema = fieldsOfParticularSchema;
exports.getEmptyValueFor = getEmptyValueFor;
exports.getValue = getValue;
exports.isJsonString = isJsonString;
exports.makeClipboardCompatible = makeClipboardCompatible;
exports.schemeNames = schemeNames;
exports.transformMe = transformMe;
var _constants = require("./constants.js");
var _schema = _interopRequireDefault(require("part:@sanity/base/schema"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
//
// Taken from stackOverflow
// stackoverflow.com/questions/13405129/create-and-save-a-file-with-javascript/
//
function download(data, filename, type) {
var file = new Blob([data], {
type: type
});
if (window.navigator.msSaveOrOpenBlob) {
// IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
console.log("a. file created!");
} else {
// Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
console.log("b. file created!");
}
}
function schemeNames(schemas) {
return schemas._original.types.filter(item => item.type === "document").map(item => item.name);
}
function fieldsOfParticularSchema(type) {
return _schema.default._original.types.filter(item => item.name === type)[0].fields.map(x => ({
name: x.name,
type: x.type
}));
}
function transformMe(value, dataType) {
switch (dataType) {
case "number":
return Number(value);
case "boolean":
return !!value;
default:
return value;
}
}
function headingsSorted(metaTypes, fields, allFields) {
var SANITY_META_TYPES_NAMES = _constants.SANITY_META_TYPES.map(thing => thing.name);
var sortedMetaNames = SANITY_META_TYPES_NAMES.filter(thing => metaTypes.includes(thing));
var unsortedFieldNames = fields.filter(field => _constants.ACCEPTED_TYPES.map(x => x.name).includes(field.type)).map(field => field.name);
var sortedFieldNames = allFields.map(thing => thing.name).filter(thing => unsortedFieldNames.includes(thing));
return [...sortedMetaNames, ...sortedFieldNames];
}
function csvFileFromObjects(data, fields, metaTypes, fileName, allFields) {
var headings = headingsSorted(metaTypes, fields, allFields);
var myFileData = [headings.join(_constants.CSV_TOOLS_DELIMETER)];
data.forEach(dataItem => myFileData.push(headings.map(heading => getValue(dataItem[heading], heading) || "").join(_constants.CSV_TOOLS_DELIMETER)));
myFileData = myFileData.join("\n");
download(myFileData, "".concat(fileName, "-CSVTools.tsv"), "text/plain");
}
function fieldsAndDataProcessed(data, fields, metaTypes, allFields) {
var headings = headingsSorted(metaTypes, fields, allFields);
var myFileData = [headings];
data.forEach((dataItem, index) => myFileData.push(headings.map(heading => {
var _allFields$find;
return getValue(dataItem[heading], heading, ((_allFields$find = allFields.find(x => x.name === heading)) === null || _allFields$find === void 0 ? void 0 : _allFields$find.type) || "string") || "";
})));
return myFileData;
}
function getValue(item, heading, myType) {
var displayAsIs = ["string", "boolean", "number"];
if (!item || displayAsIs.includes(typeof item)) {
var _ACCEPTED_TYPES$find;
return item ? _constants.ACCEPTED_TYPES !== null && _constants.ACCEPTED_TYPES !== void 0 && (_ACCEPTED_TYPES$find = _constants.ACCEPTED_TYPES.find(x => x.name === myType)) !== null && _ACCEPTED_TYPES$find !== void 0 && _ACCEPTED_TYPES$find.asJSON ? JSON.stringify(item) : item : "";
} else {
var _ACCEPTED_TYPES$find2;
return item[_constants.ACCEPTED_TYPES === null || _constants.ACCEPTED_TYPES === void 0 ? void 0 : (_ACCEPTED_TYPES$find2 = _constants.ACCEPTED_TYPES.find(x => x.name === heading)) === null || _ACCEPTED_TYPES$find2 === void 0 ? void 0 : _ACCEPTED_TYPES$find2.query] || JSON.stringify(item);
}
}
function makeClipboardCompatible(data) {
return data.map(thing => thing.join("\t")).join("\n");
} //
// Taken from https://stackoverflow.com/questions/3710204/how-to-check-if-a-string-is-a-valid-json-string
//
function isJsonString(str) {
try {
var val = JSON.parse(str);
return typeof val !== "number" ? true : false;
} catch (e) {
return false;
}
}
function getEmptyValueFor(type) {
return null;
}
//# sourceMappingURL=utils.js.map