Choose the Open Graph image from the current site media library
DC CMS qualification / qualify (push) Successful in 1m47s
DC CMS qualification / qualify (push) Successful in 1m47s
This commit is contained in:
+73
-26
@@ -189,7 +189,7 @@ const PAGE_SEO_FIELDS = [
|
||||
},
|
||||
{ path: "seo.siteName", label: "Site name", kind: "text", group: "Open Graph" },
|
||||
{ path: "seo.locale", label: "Locale", kind: "text", group: "Open Graph" },
|
||||
{ path: "seo.ogImage", label: "OG image", kind: "url", group: "Open Graph" },
|
||||
{ path: "seo.ogImage", label: "OG image", kind: "url", mediaKind: "image", group: "Open Graph" },
|
||||
{ path: "seo.imageAlt", label: "OG image alt", kind: "textarea", group: "Open Graph", min: 20, max: 120 },
|
||||
{ path: "seo.organizationName", label: "Organization", kind: "text", group: "Schema.org" },
|
||||
{ path: "seo.organizationUrl", label: "Organization URL", kind: "url", group: "Schema.org" },
|
||||
@@ -1190,8 +1190,9 @@ function updateSeoFieldMeter(meter, input, config) {
|
||||
}
|
||||
|
||||
function renderSeoInput({ targetModel, seoField, grid }) {
|
||||
const label = document.createElement("label");
|
||||
const labelRow = document.createElement("span");
|
||||
const hasPicker = Boolean(seoField.mediaKind);
|
||||
const label = document.createElement(hasPicker ? "div" : "label");
|
||||
const labelRow = document.createElement(hasPicker ? "label" : "span");
|
||||
const labelText = document.createElement("span");
|
||||
const kind = document.createElement("span");
|
||||
const path = document.createElement("span");
|
||||
@@ -1200,7 +1201,7 @@ function renderSeoInput({ targetModel, seoField, grid }) {
|
||||
const value = getByPath(targetModel, seoField.path) ?? "";
|
||||
const meter = seoField.min || seoField.max ? document.createElement("span") : null;
|
||||
|
||||
label.className = `field-control seo-field${isLong ? " wide" : ""}`;
|
||||
label.className = `field-control seo-field${isLong || hasPicker ? " wide" : ""}`;
|
||||
labelRow.className = "field-label-row";
|
||||
labelText.className = "field-label-text";
|
||||
labelText.textContent = seoField.label || seoField.path;
|
||||
@@ -1231,13 +1232,52 @@ function renderSeoInput({ targetModel, seoField, grid }) {
|
||||
updateSeoFieldMeter(meter, input, seoField);
|
||||
|
||||
input.addEventListener(seoField.kind === "select" ? "change" : "input", () => {
|
||||
setByPath(targetModel, seoField.path, fieldInputValue(input));
|
||||
setByPath(isStaticSelection() ? state.page : targetModel, seoField.path, fieldInputValue(input));
|
||||
updateSeoFieldMeter(meter, input, seoField);
|
||||
el.json.value = JSON.stringify(editorJsonPayload(activeBlock()), null, 2);
|
||||
markDirty("SEO-поля обновлены локально. Нажмите «Сохранить».");
|
||||
});
|
||||
|
||||
label.append(labelRow, input);
|
||||
label.append(labelRow);
|
||||
if (hasPicker) {
|
||||
input.id = "seo-og-image";
|
||||
labelRow.htmlFor = input.id;
|
||||
const row = document.createElement("div");
|
||||
row.className = "seo-media-controls";
|
||||
const button = document.createElement("button");
|
||||
button.type = "button";
|
||||
button.textContent = "Выбрать из файлов";
|
||||
const error = document.createElement("span");
|
||||
error.className = "media-upload-error";
|
||||
error.setAttribute("role", "alert");
|
||||
button.addEventListener("click", async () => {
|
||||
error.textContent = "";
|
||||
let base;
|
||||
try {
|
||||
base = new URL(state.projectConfig?.publicUrl);
|
||||
if (!["https:", "http:"].includes(base.protocol) || base.username || base.password) throw Error();
|
||||
} catch {
|
||||
error.textContent = "Укажите публичный домен в настройках сайта.";
|
||||
return;
|
||||
}
|
||||
base.search = base.hash = "";
|
||||
if (!base.pathname.endsWith("/")) base.pathname += "/";
|
||||
const assets = new URL("assets/", base).href;
|
||||
const currentUrl = input.value.startsWith(assets) ? "./assets/" + input.value.slice(assets.length) : "";
|
||||
await openMediaLibrary({
|
||||
block: targetModel, editableField: seoField, currentUrl, initialDirectory: "assets",
|
||||
requiredKind: seoField.mediaKind, error,
|
||||
onSelect(url) {
|
||||
input.value = new URL(url, base).href;
|
||||
input.dispatchEvent(new Event("input", { bubbles: true }));
|
||||
},
|
||||
}).catch(e => { error.textContent = e.message; });
|
||||
});
|
||||
row.append(input, button);
|
||||
label.append(row, error);
|
||||
} else {
|
||||
label.append(input);
|
||||
}
|
||||
if (meter) {
|
||||
meter.className = "seo-field-meter";
|
||||
label.append(meter);
|
||||
@@ -2812,14 +2852,20 @@ function updateMediaFieldValue({ block, editableField, hiddenInput, urlInput, fi
|
||||
markDirty();
|
||||
}
|
||||
|
||||
async function uploadMediaFile({ block, editableField, hiddenInput, urlInput, fileName, preview, hint, error, setSource, afterUpload, file }) {
|
||||
async function uploadMediaFile({ block, editableField, hiddenInput, urlInput, fileName, preview, hint, error, setSource, afterUpload, onSelect, requiredKind, file }) {
|
||||
if (!file) return;
|
||||
if (requiredKind && mediaKindFromValue(file.name) !== requiredKind) {
|
||||
error.textContent = "Выберите файл изображения.";
|
||||
el.mediaModalSubtitle.textContent = error.textContent;
|
||||
setStatus(error.textContent);
|
||||
return;
|
||||
}
|
||||
|
||||
state.mediaUploadActive = true;
|
||||
setFilePickerActive(true);
|
||||
recoverEditorSurface();
|
||||
error.textContent = "";
|
||||
fileName.textContent = "Сохраняем в assets...";
|
||||
if (fileName) fileName.textContent = "Сохраняем в assets...";
|
||||
|
||||
try {
|
||||
const formData = new FormData();
|
||||
@@ -2841,23 +2887,17 @@ async function uploadMediaFile({ block, editableField, hiddenInput, urlInput, fi
|
||||
throw new Error(payload.error || "Файл не удалось сохранить");
|
||||
}
|
||||
|
||||
updateMediaFieldValue({
|
||||
block,
|
||||
editableField,
|
||||
hiddenInput,
|
||||
urlInput,
|
||||
fileName,
|
||||
preview,
|
||||
hint,
|
||||
value: payload.url,
|
||||
});
|
||||
setSource("file");
|
||||
markDirty(`Файл сохранён: ${payload.url}. Нажмите «Сохранить», чтобы записать путь в content/pages/home.json.`);
|
||||
if (!onSelect) {
|
||||
updateMediaFieldValue({ block, editableField, hiddenInput, urlInput, fileName, preview, hint, value: payload.url });
|
||||
setSource("file");
|
||||
markDirty(`Файл сохранён: ${payload.url}. Нажмите «Сохранить», чтобы записать путь в content/pages/home.json.`);
|
||||
}
|
||||
if (typeof afterUpload === "function") await afterUpload(payload);
|
||||
return payload;
|
||||
} catch (uploadError) {
|
||||
error.textContent = uploadError.message;
|
||||
fileName.textContent = truncateText(fileNameFromUrl(hiddenInput.value), 28);
|
||||
if (fileName && hiddenInput) fileName.textContent = truncateText(fileNameFromUrl(hiddenInput.value), 28);
|
||||
if (requiredKind) el.mediaModalSubtitle.textContent = uploadError.message;
|
||||
setStatus(`Ошибка загрузки: ${uploadError.message}`);
|
||||
return null;
|
||||
} finally {
|
||||
@@ -2873,7 +2913,7 @@ function openMediaFilePicker(context) {
|
||||
recoverEditorSurface();
|
||||
window.clearTimeout(state.filePickerResetTimer);
|
||||
|
||||
mediaFilePicker.accept = MEDIA_ACCEPT;
|
||||
mediaFilePicker.accept = context.requiredKind === "image" ? "image/*" : MEDIA_ACCEPT;
|
||||
mediaFilePicker.value = "";
|
||||
mediaFilePicker.oncancel = () => {
|
||||
setFilePickerActive(false);
|
||||
@@ -2956,6 +2996,7 @@ function renderMediaFilterButtons() {
|
||||
const activeRoot = activeMediaRootFilter();
|
||||
|
||||
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";
|
||||
@@ -3873,7 +3914,7 @@ function renderMediaPreviewPanel() {
|
||||
|
||||
const canDelete = canDeleteMediaEntry(entry);
|
||||
|
||||
el.mediaSelect.disabled = !entry.selectable;
|
||||
el.mediaSelect.disabled = !entry.selectable || !matchesPickerKind(entry);
|
||||
el.mediaPreviewName.textContent = entry.name;
|
||||
el.mediaPreviewPath.textContent = entry.url || entry.path;
|
||||
el.mediaPreviewPath.title = entry.url || entry.path;
|
||||
@@ -3928,11 +3969,12 @@ async function toggleMediaTrash() {
|
||||
}
|
||||
|
||||
async function openMediaLibrary(context) {
|
||||
const currentValue = context.hiddenInput?.value || "";
|
||||
const currentValue = context.currentUrl ?? context.hiddenInput?.value ?? "";
|
||||
const currentValuePath = pathFromUrl(currentValue);
|
||||
const suggestedPath = `assets/uploads/${uploadBucketForField(context.block, context.editableField)}`;
|
||||
const lastPath = state.mediaLibrary.lastPath || readLastMediaDirectory();
|
||||
const startPath = lastPath || (currentValuePath ? parentMediaPath(currentValuePath) : suggestedPath);
|
||||
const startPath = (context.requiredKind && currentValuePath ? parentMediaPath(currentValuePath) : lastPath)
|
||||
|| (currentValuePath ? parentMediaPath(currentValuePath) : context.initialDirectory || suggestedPath);
|
||||
|
||||
state.mediaLibrary.context = context;
|
||||
state.mediaLibrary.trashMode = false;
|
||||
@@ -3942,7 +3984,7 @@ async function openMediaLibrary(context) {
|
||||
state.mediaLibrary.selectedPath = startPath;
|
||||
state.mediaLibrary.query = "";
|
||||
state.mediaLibrary.root = startPath;
|
||||
state.mediaLibrary.kind = "all";
|
||||
state.mediaLibrary.kind = context.requiredKind || "all";
|
||||
el.mediaSearch.value = "";
|
||||
el.mediaModalTitle.textContent = context.editableField.label || "Выбрать файл";
|
||||
el.mediaModalSubtitle.textContent = `${context.editableField.path} -> ${suggestedPath}`;
|
||||
@@ -3969,6 +4011,10 @@ function closeMediaLibrary() {
|
||||
recoverEditorSurface();
|
||||
}
|
||||
|
||||
function matchesPickerKind(entry) {
|
||||
return !state.mediaLibrary.context?.requiredKind || entry.kind === state.mediaLibrary.context.requiredKind;
|
||||
}
|
||||
|
||||
function chooseSelectedMediaFile() {
|
||||
const file = selectedMediaFile();
|
||||
const context = state.mediaLibrary.context;
|
||||
@@ -3979,6 +4025,7 @@ function chooseSelectedMediaFile() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!file.selectable || !matchesPickerKind(file)) return;
|
||||
if (typeof context.onSelect === "function") {
|
||||
context.onSelect(file.url, file);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user