Add site-wide media grid with combined filters and shared path bar
DC CMS qualification / qualify (push) Successful in 1m46s
DC CMS qualification / qualify (push) Successful in 1m46s
This commit is contained in:
+89
-40
@@ -1,3 +1,4 @@
|
||||
import { renderPathBar } from '@nodedc/ui-dom';
|
||||
const sitePrefix = window.location.pathname.match(/^\/s\/[a-z][a-z0-9-]*/)?.[0] || "";
|
||||
const siteUrl = path => `${sitePrefix}/${String(path).replace(/^\.?\//, "")}`;
|
||||
function siteFetch(url, options) { return fetch(siteUrl(url), options); }
|
||||
@@ -139,7 +140,9 @@ const state = {
|
||||
selectedEntry: null,
|
||||
selectedUrl: "",
|
||||
root: "",
|
||||
kind: "all",
|
||||
kinds: [],
|
||||
view: "flat",
|
||||
loadId: 0,
|
||||
query: "",
|
||||
loaded: false,
|
||||
dragEntryPath: "",
|
||||
@@ -275,6 +278,8 @@ const el = {
|
||||
mediaTrashToggle: document.querySelector("#media-trash-toggle"),
|
||||
mediaSearch: document.querySelector("#media-search"),
|
||||
mediaKindFilters: document.querySelector("#media-kind-filters"),
|
||||
mediaFlatToggle: document.querySelector("#media-flat-toggle"),
|
||||
mediaResults: document.querySelector("#media-results"),
|
||||
mediaRootFilters: document.querySelector("#media-root-filters"),
|
||||
mediaUpload: document.querySelector("#media-upload"),
|
||||
mediaRefresh: document.querySelector("#media-refresh"),
|
||||
@@ -2998,16 +3003,28 @@ function renderMediaFilterButtons() {
|
||||
el.mediaRootFilters.innerHTML = "";
|
||||
const activeRoot = activeMediaRootFilter();
|
||||
|
||||
const flat = state.mediaLibrary.view === "flat" && !state.mediaLibrary.trashMode;
|
||||
el.mediaRootFilters.hidden = flat;
|
||||
el.mediaFlatToggle.disabled = state.mediaLibrary.trashMode;
|
||||
el.mediaFlatToggle.setAttribute("aria-pressed", String(flat));
|
||||
el.mediaFlatToggle.title = flat ? "Показать папки" : "Все файлы без папок";
|
||||
el.mediaFlatToggle.setAttribute("aria-label", el.mediaFlatToggle.title);
|
||||
el.mediaFlatToggle.classList.add("nodedc-icon-button");
|
||||
el.mediaFlatToggle.replaceChildren(document.querySelector(`#ds-icon-${flat ? "folder" : "grid"}`).content.firstElementChild.cloneNode(true));
|
||||
for (const [kind, label] of MEDIA_KIND_FILTERS) {
|
||||
if (state.mediaLibrary.context?.requiredKind && kind !== state.mediaLibrary.context.requiredKind) continue;
|
||||
const button = document.createElement("button");
|
||||
button.type = "button";
|
||||
button.className = "media-filter-button";
|
||||
button.dataset.active = String(state.mediaLibrary.kind === kind);
|
||||
const button = document.querySelector('#ds-iconButton').content.firstElementChild.cloneNode(true);
|
||||
button.classList.add("media-filter-button");
|
||||
const active = kind === "all" ? state.mediaLibrary.kinds.length === 0 : state.mediaLibrary.kinds.includes(kind);
|
||||
button.dataset.active = String(active); button.dataset.kind = kind;
|
||||
button.setAttribute("aria-pressed", String(active)); button.setAttribute("aria-label", label); button.title = label;
|
||||
button.disabled = state.mediaLibrary.trashMode;
|
||||
button.textContent = label;
|
||||
const icon = {all:"file",image:"image",video:"video",model:"layers"}[kind];
|
||||
button.replaceChildren(document.querySelector(`#ds-icon-${icon}`).content.firstElementChild.cloneNode(true));
|
||||
button.addEventListener("click", () => {
|
||||
state.mediaLibrary.kind = kind;
|
||||
if (state.mediaLibrary.context?.requiredKind) return;
|
||||
const kinds = state.mediaLibrary.kinds;
|
||||
state.mediaLibrary.kinds = kind === "all" ? [] : kinds.includes(kind) ? kinds.filter(value => value !== kind) : [...kinds,kind];
|
||||
renderMediaLibrary();
|
||||
});
|
||||
el.mediaKindFilters.append(button);
|
||||
@@ -3380,8 +3397,7 @@ function entryMatchesMediaFilters(entry) {
|
||||
}
|
||||
|
||||
if (entry.type === "directory") return true;
|
||||
if (state.mediaLibrary.kind === "all") return true;
|
||||
return entry.kind === state.mediaLibrary.kind;
|
||||
return state.mediaLibrary.kinds.length === 0 || state.mediaLibrary.kinds.includes(entry.kind);
|
||||
}
|
||||
|
||||
function findEntryByUrl(url) {
|
||||
@@ -3481,6 +3497,7 @@ function openMediaContextMenu(event, { entry = null, columnPath = state.mediaLib
|
||||
}
|
||||
|
||||
function scrollMediaBrowserToSelection() {
|
||||
if (state.mediaLibrary.view === "flat" && !state.mediaLibrary.trashMode) return;
|
||||
window.requestAnimationFrame(() => {
|
||||
const columns = [...el.mediaList.querySelectorAll(".media-column")];
|
||||
const targetColumn = columns.at(-1);
|
||||
@@ -3529,7 +3546,23 @@ function scrollMediaBrowserToSelection() {
|
||||
});
|
||||
}
|
||||
|
||||
async function loadFlatMedia({ selectedUrl = state.mediaLibrary.selectedUrl } = {}) {
|
||||
const loadId = ++state.mediaLibrary.loadId;
|
||||
state.mediaLibrary.trashMode = false;
|
||||
el.mediaResults.textContent = "Загрузка файлов…";
|
||||
const response = await siteFetch('/api/media/assets');
|
||||
const payload = await readApiJson(response, "Не удалось загрузить файлы сайта");
|
||||
if (loadId !== state.mediaLibrary.loadId || !state.mediaLibrary.context) return;
|
||||
state.mediaLibrary.columns = [{path: "assets", entries: payload.files || []}];
|
||||
state.mediaLibrary.loaded = true;
|
||||
state.mediaLibrary.selectedUrl = selectedUrl || "";
|
||||
state.mediaLibrary.selectedEntry = selectedUrl ? findEntryByUrl(selectedUrl) : null;
|
||||
renderMediaLibrary();
|
||||
}
|
||||
|
||||
async function loadMediaColumns(targetPath, { selectedUrl = state.mediaLibrary.selectedUrl } = {}) {
|
||||
if (state.mediaLibrary.view === "flat") return loadFlatMedia({selectedUrl});
|
||||
const loadId = ++state.mediaLibrary.loadId;
|
||||
const path = String(targetPath || "").replace(/^\/+/, "");
|
||||
const chain = mediaPathChain(path);
|
||||
const columns = [];
|
||||
@@ -3541,6 +3574,7 @@ async function loadMediaColumns(targetPath, { selectedUrl = state.mediaLibrary.s
|
||||
columns.push(await browseMediaPath(columnPath));
|
||||
}
|
||||
|
||||
if (loadId !== state.mediaLibrary.loadId || !state.mediaLibrary.context) return;
|
||||
state.mediaLibrary.columns = columns;
|
||||
state.mediaLibrary.selectedPath = path;
|
||||
state.mediaLibrary.loaded = true;
|
||||
@@ -3552,9 +3586,11 @@ async function loadMediaColumns(targetPath, { selectedUrl = state.mediaLibrary.s
|
||||
}
|
||||
|
||||
async function loadMediaTrash({ selectedTrashPath = "" } = {}) {
|
||||
const loadId = ++state.mediaLibrary.loadId;
|
||||
setStatus("Открываем удалённые файлы...");
|
||||
const column = await browseMediaTrash();
|
||||
|
||||
if (loadId !== state.mediaLibrary.loadId || !state.mediaLibrary.context) return;
|
||||
state.mediaLibrary.trashMode = true;
|
||||
state.mediaLibrary.columns = [column];
|
||||
state.mediaLibrary.selectedPath = "__trash__";
|
||||
@@ -3566,23 +3602,17 @@ async function loadMediaTrash({ selectedTrashPath = "" } = {}) {
|
||||
}
|
||||
|
||||
function renderMediaBreadcrumbs() {
|
||||
el.mediaBreadcrumbs.innerHTML = "";
|
||||
const breadcrumbs = state.mediaLibrary.columns.at(-1)?.breadcrumbs || [{ name: "NODEDC_SITE", path: "" }];
|
||||
|
||||
breadcrumbs.forEach((crumb, index) => {
|
||||
const button = document.createElement("button");
|
||||
button.type = "button";
|
||||
button.className = "media-breadcrumb-button";
|
||||
button.textContent = crumb.name;
|
||||
button.dataset.active = String(index === breadcrumbs.length - 1);
|
||||
button.addEventListener("click", () => {
|
||||
state.mediaLibrary.root = crumb.path;
|
||||
state.mediaLibrary.selectedEntry = null;
|
||||
state.mediaLibrary.selectedUrl = "";
|
||||
loadMediaColumns(crumb.path).catch((error) => setStatus(`Ошибка медиатеки: ${error.message}`));
|
||||
});
|
||||
el.mediaBreadcrumbs.append(button);
|
||||
});
|
||||
const flat = state.mediaLibrary.view === "flat" && !state.mediaLibrary.trashMode;
|
||||
const selected = state.mediaLibrary.selectedEntry;
|
||||
const path = selected?.path || (flat ? "" : state.mediaLibrary.selectedPath);
|
||||
const parts = state.mediaLibrary.trashMode ? [] : mediaPathParts(path);
|
||||
const crumbs = [{label: state.mediaLibrary.trashMode ? "Удалённые файлы" : state.projectConfig?.name || "Файлы сайта",path:"",kind:"folder"}];
|
||||
parts.forEach((part,index) => crumbs.push({label:part,path:parts.slice(0,index+1).join('/'),kind:index === parts.length-1 && selected?.type === 'file' ? 'file' : 'folder',current:index === parts.length-1}));
|
||||
renderPathBar(el.mediaBreadcrumbs, crumbs, {icon: kind => document.querySelector(`#ds-icon-${kind}`).content.firstElementChild.cloneNode(true), onNavigate: path => {
|
||||
state.mediaLibrary.view = "folders"; state.mediaLibrary.root = path;
|
||||
state.mediaLibrary.selectedEntry = null; state.mediaLibrary.selectedUrl = "";
|
||||
loadMediaColumns(path).catch(error => setStatus(`Ошибка медиатеки: ${error.message}`));
|
||||
}});
|
||||
}
|
||||
|
||||
function renderMediaEntry(entry, columnPath) {
|
||||
@@ -3616,7 +3646,7 @@ function renderMediaEntry(entry, columnPath) {
|
||||
image.loading = "lazy";
|
||||
image.decoding = "async";
|
||||
icon.append(image);
|
||||
} else if (entry.type === "file" && entry.url && entry.kind === "video") {
|
||||
} else if (entry.type === "file" && entry.url && entry.kind === "video" && state.mediaLibrary.view !== "flat") {
|
||||
const video = document.createElement("video");
|
||||
icon.classList.add("is-preview");
|
||||
video.src = adminPreviewUrl(entry.url);
|
||||
@@ -3635,6 +3665,10 @@ function renderMediaEntry(entry, columnPath) {
|
||||
: entry.extension?.toUpperCase() || "FILE";
|
||||
}
|
||||
|
||||
if (state.mediaLibrary.view === "flat" && entry.kind !== "image") {
|
||||
const iconName = entry.kind === "video" ? "video" : entry.kind === "model" ? "layers" : "file";
|
||||
icon.replaceChildren(document.querySelector(`#ds-icon-${iconName}`).content.firstElementChild.cloneNode(true));
|
||||
}
|
||||
copy.className = "media-entry-copy";
|
||||
name.className = "media-entry-name";
|
||||
name.textContent = entry.name;
|
||||
@@ -3643,6 +3677,7 @@ function renderMediaEntry(entry, columnPath) {
|
||||
entry.type === "directory"
|
||||
? entry.path || "Корень проекта"
|
||||
: [mediaUsageLabel(entry), entry.extension, formatFileSize(entry.size)].filter(Boolean).join(" · ");
|
||||
if (state.mediaLibrary.view === "flat") { const path = document.createElement("span"); path.className = "media-entry-meta media-entry-path"; path.textContent = parentMediaPath(entry.path); path.title = path.textContent; copy.append(path); }
|
||||
copy.append(name, meta);
|
||||
|
||||
statusDot.className = "media-status-dot media-entry-status";
|
||||
@@ -3884,7 +3919,7 @@ function renderMediaPreviewPanel() {
|
||||
if (!entry) {
|
||||
el.mediaPreviewName.textContent = "Выберите файл или папку";
|
||||
el.mediaPreviewPath.textContent = state.mediaLibrary.selectedPath || "NODEDC_SITE";
|
||||
el.mediaPreviewMeta.textContent = "Column view проекта";
|
||||
el.mediaPreviewMeta.textContent = state.mediaLibrary.view === "flat" ? "Все файлы выбранного сайта" : "Файлы выбранной папки";
|
||||
el.mediaPreviewUsage.dataset.status = "idle";
|
||||
if (usageText) usageText.textContent = "Нет выбора";
|
||||
return;
|
||||
@@ -3933,21 +3968,26 @@ function renderMediaPreviewPanel() {
|
||||
}
|
||||
|
||||
function renderMediaLibrary() {
|
||||
const flat = state.mediaLibrary.view === "flat" && !state.mediaLibrary.trashMode;
|
||||
const scrollTop = el.mediaList.scrollTop;
|
||||
el.mediaTrashToggle.dataset.active = String(state.mediaLibrary.trashMode);
|
||||
el.mediaUpload.textContent = state.mediaLibrary.trashMode ? "Очистить корзину" : "+ Загрузить";
|
||||
renderMediaFilterButtons();
|
||||
renderMediaBreadcrumbs();
|
||||
el.mediaList.innerHTML = "";
|
||||
el.mediaList.classList.add("media-columns");
|
||||
|
||||
renderMediaFilterButtons(); renderMediaBreadcrumbs();
|
||||
el.mediaList.replaceChildren();
|
||||
el.mediaList.classList.toggle("media-gallery", flat);
|
||||
el.mediaList.classList.toggle("media-columns", !flat);
|
||||
const columns = state.mediaLibrary.columns || [];
|
||||
el.mediaEmpty.classList.toggle("hidden", columns.length > 0);
|
||||
for (const column of columns) {
|
||||
el.mediaList.append(renderMediaColumn(column));
|
||||
const entries = columns.flatMap(column => column.entries || []).filter(entryMatchesMediaFilters);
|
||||
el.mediaResults.textContent = flat ? `Файлов: ${entries.length}` : "";
|
||||
el.mediaEmpty.classList.add("hidden");
|
||||
if (flat) {
|
||||
for (const entry of entries) el.mediaList.append(renderMediaEntry(entry, parentMediaPath(entry.path)));
|
||||
if (!entries.length) { const empty = document.createElement('div'); empty.className = 'media-column-empty'; empty.textContent = 'По выбранным фильтрам файлы не найдены.'; el.mediaList.append(empty); }
|
||||
el.mediaList.scrollTop = scrollTop;
|
||||
} else {
|
||||
for (const column of columns) el.mediaList.append(renderMediaColumn(column));
|
||||
}
|
||||
|
||||
renderMediaPreviewPanel();
|
||||
scrollMediaBrowserToSelection();
|
||||
renderMediaPreviewPanel(); scrollMediaBrowserToSelection();
|
||||
}
|
||||
|
||||
async function refreshMediaLibrary() {
|
||||
@@ -3990,7 +4030,9 @@ async function openMediaLibrary(context) {
|
||||
state.mediaLibrary.selectedPath = startPath;
|
||||
state.mediaLibrary.query = "";
|
||||
state.mediaLibrary.root = startPath;
|
||||
state.mediaLibrary.kind = context.requiredKind || "all";
|
||||
state.mediaLibrary.kinds = context.requiredKind ? [context.requiredKind] : [];
|
||||
state.mediaLibrary.view = managing ? "flat" : "folders";
|
||||
state.mediaLibrary.columns = [];
|
||||
el.mediaSearch.value = "";
|
||||
el.mediaModalTitle.textContent = managing ? `Файлы — ${state.projectConfig?.name || state.projectConfig?.id || "текущий сайт"}` : context.editableField.label || "Выбрать файл";
|
||||
el.mediaModalSubtitle.textContent = managing ? "Изображения, видео и другие файлы выбранного сайта." : `${context.editableField.path} -> ${suggestedPath}`;
|
||||
@@ -4008,6 +4050,7 @@ async function openMediaLibrary(context) {
|
||||
}
|
||||
|
||||
function closeMediaLibrary() {
|
||||
state.mediaLibrary.loadId++;
|
||||
const managing = state.mediaLibrary.context?.mode === "manage";
|
||||
closeMediaContextMenu();
|
||||
el.mediaModal.classList.add("hidden");
|
||||
@@ -6117,6 +6160,12 @@ el.mediaSearch.addEventListener("input", () => {
|
||||
state.mediaLibrary.query = el.mediaSearch.value;
|
||||
renderMediaLibrary();
|
||||
});
|
||||
el.mediaFlatToggle.addEventListener("click", async () => {
|
||||
state.mediaLibrary.view = state.mediaLibrary.view === "flat" ? "folders" : "flat";
|
||||
closeMediaContextMenu();
|
||||
try { await loadMediaColumns(state.mediaLibrary.selectedEntry ? parentMediaPath(state.mediaLibrary.selectedEntry.path) : state.mediaLibrary.selectedPath || 'assets'); }
|
||||
catch (error) { setStatus(`Ошибка медиатеки: ${error.message}`); }
|
||||
});
|
||||
el.mediaRefresh.addEventListener("click", () => refreshMediaLibrary().catch((error) => setStatus(`Ошибка медиатеки: ${error.message}`)));
|
||||
el.mediaUpload.addEventListener("click", () => {
|
||||
if (state.mediaLibrary.trashMode) {
|
||||
|
||||
Reference in New Issue
Block a user