dnd5-scripts/locations.js
2026-06-17 22:32:48 +03:00

81 lines
2.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Функция для получения ссылок на изображения
function getImageLinks(p, basePath) {
let avatar = p["avatar-image"];
if (avatar && avatar.path) {
let imagePath = `${basePath}/${avatar.path}`; // Используем базовый путь хранилища
return `<div style="width: 70px; height: 70px; display: flex; align-items: center; justify-content: center; overflow: hidden;">
<img src="file:///${imagePath}" style="width: 100%; height: 100%; object-fit: cover;">
</div>`;
}
return ""; // Возвращаем пустую строку, если изображения нет
}
// Рекурсивная функция для проверки, является ли страница потомком любой из целевых локаций
function isDescendant(dv, page, targetNames, visited = new Set()) {
if (!page || visited.has(page.file.path)) {
return false;
}
visited.add(page.file.path);
// Если страница совпадает с любой из целевых, она не считается своим потомком
if (targetNames.includes(page.file.name)) {
return true;
}
// Получаем родительские регионы
let parents = page["parent-region"];
if (!parents) {
return false;
}
if (!Array.isArray(parents)) {
parents = [parents];
}
// Рекурсивно проверяем каждого родителя
for (let parent of parents) {
let parentPage = null;
if (typeof parent === "object" && parent.path) {
parentPage = dv.page(parent.path);
} else if (typeof parent === "string") {
parentPage = dv.page(parent);
} else {
parentPage = null;
}
if (parentPage && isDescendant(dv, parentPage, targetNames, visited)) {
return true;
}
}
return false;
}
function regionMatches(dv, regionsList, pageRegions) {
if (pageRegions !== null) {
if (!Array.isArray(pageRegions)) {
pageRegions = [pageRegions];
}
return pageRegions.some(pageRegion => {
let regionPage = null;
if (typeof pageRegion === 'object' && pageRegion.hasOwnProperty('path')) {
regionPage = dv.page(pageRegion.path);
} else if (typeof pageRegion === 'string') {
regionPage = dv.page(pageRegion);
} else {
return regionsList.includes(pageRegion);
}
return isDescendant(dv, regionPage, regionsList);
});
} else {
return false;
}
}
dv.locations = {
getImageLinks,
isDescendant,
regionMatches,
};