@localazy/strapi-plugin
Version:
The official Strapi Plugin by Localazy.
318 lines (317 loc) • 12.2 kB
JavaScript
;
const set = require("./set-CmcBsVOP.js");
const index = require("./index-Q3-srfXc.js");
const _commonjsHelpers = require("./_commonjsHelpers-Bc2YnDe1.js");
function baseGet(object, path) {
path = set.castPath(path, object);
var index2 = 0, length = path.length;
while (object != null && index2 < length) {
object = object[set.toKey(path[index2++])];
}
return index2 && index2 == length ? object : void 0;
}
function get(object, path, defaultValue) {
var result = object == null ? void 0 : baseGet(object, path);
return result === void 0 ? defaultValue : result;
}
const BASE_PATH = "/strapi";
const strapiApiAxiosInstance = index.createStrapiApiAxiosInstance();
class StrapiModelService {
static async getModels() {
try {
const result = await strapiApiAxiosInstance.get(`${BASE_PATH}/models`);
return result.data;
} catch (e) {
throw e.message;
}
}
static async getLocalizableModels() {
try {
const result = await strapiApiAxiosInstance.get(`${BASE_PATH}/localizable-models`);
return result.data;
} catch (e) {
throw e.message;
}
}
}
const SUPPORTED_CONTENT_TYPE_FIELDS = [
"string",
"text",
"richtext",
"email",
"json"
];
const SUPPORTED_CUSTOM_FIELD_PLUGINS = [
"plugin::ckeditor.CKEditor",
"plugin::ckeditor5.CKEditor"
];
const getModelsTree = (allModels, localizableModels = []) => {
const modelsTree = [];
const getModelTree = (attributes, isComponentAttributes = false, isComponentAttributesObjectTranslatable = null) => {
const currentModelTree = {};
for (const attribute in attributes) {
if (attributes[attribute].type === "component") {
const nestedModel = allModels.find((model) => model.uid === attributes[attribute].component);
currentModelTree[attribute] = {
...getModelTree(
nestedModel.__schema__.attributes,
true,
isComponentAttributesObjectTranslatable === null ? attributes[attribute].pluginOptions?.i18n?.localized || false : isComponentAttributesObjectTranslatable
)
};
} else if (attributes[attribute].type === "dynamiczone") {
const components = attributes[attribute].components;
currentModelTree[attribute] = [];
components.forEach((c) => {
const nestedModel = allModels.find((model) => model.uid === c);
const modelTree = getModelTree(
nestedModel.__schema__.attributes,
true,
isComponentAttributesObjectTranslatable === null ? attributes[attribute].pluginOptions?.i18n?.localized || false : isComponentAttributesObjectTranslatable
);
currentModelTree[attribute].push({
__component__: c,
...modelTree
});
});
} else if (SUPPORTED_CONTENT_TYPE_FIELDS.includes(attributes[attribute].type) || attributes[attribute].type === "customField" && SUPPORTED_CUSTOM_FIELD_PLUGINS.includes(attributes[attribute].customField)) {
if (isComponentAttributes) {
if (isComponentAttributesObjectTranslatable) {
currentModelTree[attribute] = false;
} else {
currentModelTree[attribute] = null;
}
} else if (!isComponentAttributes && (!attributes[attribute].pluginOptions || !attributes[attribute].pluginOptions.i18n || !attributes[attribute].pluginOptions.i18n.localized)) {
currentModelTree[attribute] = null;
} else {
currentModelTree[attribute] = false;
}
} else {
currentModelTree[attribute] = null;
}
}
return currentModelTree;
};
for (const model of localizableModels) {
if (model.__schema__.collectionName) {
const modelTree = {
[model.__schema__.collectionName]: {
__model__: true,
...getModelTree(model.__schema__.attributes)
}
};
modelsTree.push(modelTree);
}
}
return modelsTree;
};
const arrayOfModelsToObject = (array) => array.reduce((obj, item) => {
obj[Object.keys(item)[0]] = item;
return obj;
}, {});
const findModelValueByKey = (allModelsTree, key) => {
const objectivizedModelsTree = arrayOfModelsToObject(allModelsTree);
return get(objectivizedModelsTree, key);
};
function baseFindIndex(array, predicate, fromIndex, fromRight) {
var length = array.length, index2 = fromIndex + -1;
while (++index2 < length) {
if (predicate(array[index2], index2, array)) {
return index2;
}
}
return -1;
}
function baseIsNaN(value) {
return value !== value;
}
function strictIndexOf(array, value, fromIndex) {
var index2 = fromIndex - 1, length = array.length;
while (++index2 < length) {
if (array[index2] === value) {
return index2;
}
}
return -1;
}
function baseIndexOf(array, value, fromIndex) {
return value === value ? strictIndexOf(array, value, fromIndex) : baseFindIndex(array, baseIsNaN, fromIndex);
}
function arrayIncludes(array, value) {
var length = array == null ? 0 : array.length;
return !!length && baseIndexOf(array, value, 0) > -1;
}
function noop() {
}
var INFINITY = 1 / 0;
var createSet = !(set.Set && 1 / set.setToArray(new set.Set([, -0]))[1] == INFINITY) ? noop : function(values) {
return new set.Set(values);
};
var LARGE_ARRAY_SIZE = 200;
function baseUniq(array, iteratee, comparator) {
var index2 = -1, includes = arrayIncludes, length = array.length, isCommon = true, result = [], seen = result;
if (length >= LARGE_ARRAY_SIZE) {
var set$1 = createSet(array);
if (set$1) {
return set.setToArray(set$1);
}
isCommon = false;
includes = set.cacheHas;
seen = new set.SetCache();
} else {
seen = result;
}
outer:
while (++index2 < length) {
var value = array[index2], computed = value;
value = value !== 0 ? value : 0;
if (isCommon && computed === computed) {
var seenIndex = seen.length;
while (seenIndex--) {
if (seen[seenIndex] === computed) {
continue outer;
}
}
result.push(value);
} else if (!includes(seen, computed, comparator)) {
if (seen !== result) {
seen.push(computed);
}
result.push(value);
}
}
return result;
}
function uniq(array) {
return array && array.length ? baseUniq(array) : [];
}
function isObject(value) {
return value !== null && typeof value === "object" && !(value instanceof Date);
}
var isArray = Array.isArray;
function deepKeys(obj, stack, parent, intermediate) {
Object.keys(obj).forEach(function(el) {
var escaped = el.replace(/\./g, "\\.");
if (isObject(obj[el]) && !isArray(obj[el])) {
var p = parent ? parent + "." + escaped : parent;
if (intermediate) stack.push(parent ? p : escaped);
deepKeys(obj[el], stack, p || escaped, intermediate);
} else if (isArray(obj[el])) {
obj[el].forEach((item, index2) => {
var p2 = parent ? parent + `.${escaped}.${index2}` : parent;
if (intermediate) stack.push(parent ? p2 : `${escaped}.${index2}`);
deepKeys(item, stack, p2 || `${escaped}.${index2}`, intermediate);
});
} else {
var key = parent ? parent + "." + escaped : escaped;
stack.push(key);
}
});
return stack;
}
var deepKeys_1 = function(obj, intermediate) {
return deepKeys(obj, [], null, intermediate);
};
const deepKeys$1 = /* @__PURE__ */ _commonjsHelpers.getDefaultExportFromCjs(deepKeys_1);
const sortByModelName = (a, b) => {
const uidA = Object.keys(a)[0].toLowerCase();
const uidB = Object.keys(b)[0].toLowerCase();
if (uidA < uidB) return -1;
if (uidA > uidB) return 1;
return 0;
};
const getContentTransferSetupKeysSets = (localizableTree = [], storedSetupSchema = []) => {
localizableTree = arrayOfModelsToObject(localizableTree.sort(sortByModelName));
storedSetupSchema = arrayOfModelsToObject(storedSetupSchema.sort(sortByModelName));
const currentModelsSchemaKeys = deepKeys$1(localizableTree);
const storedSetupSchemaKeys = deepKeys$1(storedSetupSchema);
const regex = /\.\d+\.__component__/;
let currentModelsSchemaComponentKeys = currentModelsSchemaKeys.filter((key) => key.match(regex)).map((key) => key.replace(regex, ""));
let storedSetupSchemaComponentKeys = storedSetupSchemaKeys.filter((key) => key.match(regex)).map((key) => key.replace(regex, ""));
currentModelsSchemaComponentKeys = uniq(currentModelsSchemaComponentKeys);
storedSetupSchemaComponentKeys = uniq(storedSetupSchemaComponentKeys);
const currentModelsSchemaComponentKeysComponentProp = currentModelsSchemaKeys.filter(
(key) => key.includes("__component__") && currentModelsSchemaComponentKeys.some((k) => key.includes(k))
);
const storedSetupSchemaComponentKeysComponentProp = storedSetupSchemaKeys.filter(
(key) => key.includes("__component__") && storedSetupSchemaComponentKeys.some((k) => key.includes(k))
);
let removedKeys = [];
removedKeys.push(
...storedSetupSchemaComponentKeysComponentProp.map((key) => {
const storedComponentName = get(storedSetupSchema, key);
const currentComponentName = get(localizableTree, key);
if (storedComponentName !== currentComponentName) {
const part = key.split(".").slice(0, -1).join(".");
return storedSetupSchemaKeys.filter((k) => k.includes(part));
}
return [];
}).reduce((acc, val) => acc.concat(val), [])
);
removedKeys.push(...storedSetupSchemaKeys.filter((key) => !currentModelsSchemaKeys.includes(key)));
removedKeys = uniq(removedKeys);
let newKeys = [];
newKeys.push(
...currentModelsSchemaComponentKeysComponentProp.map((key) => {
const storedComponentName = get(storedSetupSchema, key);
const currentComponentName = get(localizableTree, key);
if (storedComponentName !== currentComponentName) {
const part = key.split(".").slice(0, -1).join(".");
return currentModelsSchemaKeys.filter((k) => k.includes(part));
}
return [];
}).reduce((acc, val) => acc.concat(val), [])
);
newKeys.push(...currentModelsSchemaKeys.filter((key) => !storedSetupSchemaKeys.includes(key)));
newKeys = uniq(newKeys);
const filteredStoredSetupSchemaKeys = storedSetupSchemaKeys.filter((key) => !removedKeys.includes(key));
return {
oLocalizableTree: localizableTree,
oStoredSetupSchema: storedSetupSchema,
removedKeys,
newKeys,
filteredStoredSetupSchemaKeys
};
};
const hasModelChanged = async (localizableTree = null, storedSetupSchema = null, allModelsTree = null) => {
let models;
if (!localizableTree || !allModelsTree) {
models = await StrapiModelService.getModels();
}
if (!localizableTree) {
const localizableModels = await StrapiModelService.getLocalizableModels();
localizableTree = getModelsTree(models, localizableModels);
}
if (!storedSetupSchema) {
storedSetupSchema = (await set.PluginSettingsService.getContentTransferSetup()).setup;
}
if (!allModelsTree) {
allModelsTree = getModelsTree(models, models);
}
const { oStoredSetupSchema, removedKeys, newKeys, filteredStoredSetupSchemaKeys } = getContentTransferSetupKeysSets(
localizableTree,
storedSetupSchema || []
);
if (newKeys.length || removedKeys.length) {
return true;
}
for (const key of filteredStoredSetupSchemaKeys) {
const localizableTreeValueToSet = get(oStoredSetupSchema, key);
const allModelsTreeValueToSet = findModelValueByKey(allModelsTree, key);
if (typeof localizableTreeValueToSet === "boolean" && allModelsTreeValueToSet === null) {
return true;
}
if (localizableTreeValueToSet === null && typeof allModelsTreeValueToSet === "boolean") {
return true;
}
}
return false;
};
exports.SUPPORTED_CONTENT_TYPE_FIELDS = SUPPORTED_CONTENT_TYPE_FIELDS;
exports.SUPPORTED_CUSTOM_FIELD_PLUGINS = SUPPORTED_CUSTOM_FIELD_PLUGINS;
exports.StrapiModelService = StrapiModelService;
exports.findModelValueByKey = findModelValueByKey;
exports.get = get;
exports.getContentTransferSetupKeysSets = getContentTransferSetupKeysSets;
exports.getModelsTree = getModelsTree;
exports.hasModelChanged = hasModelChanged;