feat(attachments): 新增附件管理页面
This commit is contained in:
@@ -367,6 +367,8 @@ input:focus {
|
||||
}
|
||||
|
||||
.tab {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
height: 60px;
|
||||
padding: 0 20px;
|
||||
border: 0;
|
||||
@@ -376,6 +378,7 @@ input:focus {
|
||||
font: inherit;
|
||||
font-weight: 600;
|
||||
border-bottom: 2px solid transparent;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.tab.active {
|
||||
@@ -889,6 +892,23 @@ input:focus {
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.attachment-manager-link {
|
||||
display: inline-grid;
|
||||
place-items: center;
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 999px;
|
||||
color: var(--accent);
|
||||
text-decoration: none;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.attachment-manager-link:hover {
|
||||
border-color: var(--accent);
|
||||
background: #eaf2ff;
|
||||
}
|
||||
|
||||
.upload-status {
|
||||
margin: 0;
|
||||
line-height: 1.5;
|
||||
@@ -1177,6 +1197,215 @@ input:focus {
|
||||
}
|
||||
}
|
||||
|
||||
.attachment-manager-page {
|
||||
display: grid;
|
||||
align-content: start;
|
||||
gap: 12px;
|
||||
min-height: 0;
|
||||
height: calc(100vh - 60px);
|
||||
overflow-y: auto;
|
||||
padding: 16px 24px 20px;
|
||||
background: var(--bg);
|
||||
}
|
||||
|
||||
.attachment-manager-hero,
|
||||
.attachment-manager-panel,
|
||||
.attachment-manager-content {
|
||||
width: min(1440px, 100%);
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.attachment-manager-hero {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.attachment-manager-hero h1 {
|
||||
margin: 2px 0;
|
||||
font-size: 22px;
|
||||
}
|
||||
|
||||
.attachment-manager-hero p {
|
||||
margin: 0;
|
||||
color: var(--muted);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.attachment-manager-toolbar {
|
||||
min-height: 66px;
|
||||
}
|
||||
|
||||
.attachment-manager-selectbar {
|
||||
display: grid;
|
||||
grid-template-columns: auto minmax(420px, 680px) auto;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
min-width: min(900px, 60vw);
|
||||
}
|
||||
|
||||
.attachment-manager-selectbar label {
|
||||
color: var(--muted);
|
||||
font-size: 13px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.return-chat-link {
|
||||
padding: 8px 12px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 8px;
|
||||
color: var(--accent);
|
||||
text-decoration: none;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.attachment-manager-panel {
|
||||
display: grid;
|
||||
gap: 10px;
|
||||
padding: 12px 14px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 8px;
|
||||
background: #ffffff;
|
||||
}
|
||||
|
||||
.attachment-manager-panel label {
|
||||
color: var(--muted);
|
||||
font-size: 13px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.attachment-manager-panel select,
|
||||
.attachment-search {
|
||||
min-height: 34px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 8px;
|
||||
background: #ffffff;
|
||||
color: var(--text);
|
||||
font: inherit;
|
||||
}
|
||||
|
||||
.attachment-manager-panel select,
|
||||
.attachment-manager-select-control {
|
||||
width: 100%;
|
||||
height: 38px;
|
||||
padding: 0 12px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 8px;
|
||||
background: #ffffff;
|
||||
color: var(--text);
|
||||
font: inherit;
|
||||
}
|
||||
|
||||
.attachment-manager-select-control:focus {
|
||||
border-color: var(--accent);
|
||||
box-shadow: 0 0 0 3px rgba(58, 114, 216, 0.14);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.attachment-manager-content {
|
||||
display: grid;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.attachment-manager-split {
|
||||
grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
|
||||
align-items: start;
|
||||
}
|
||||
|
||||
.attachment-search {
|
||||
width: 220px;
|
||||
padding: 0 10px;
|
||||
}
|
||||
|
||||
.manager-upload-dropzone {
|
||||
min-height: 132px;
|
||||
padding: 14px;
|
||||
}
|
||||
|
||||
.upload-manager-panel .summary-subheading span {
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.attachment-table-wrap {
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
.attachment-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.attachment-table th,
|
||||
.attachment-table td {
|
||||
padding: 10px 8px;
|
||||
border-bottom: 1px solid var(--line);
|
||||
text-align: left;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.attachment-table th {
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.attachment-name {
|
||||
max-width: 360px;
|
||||
overflow-wrap: anywhere;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.attachment-actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.attachment-actions a,
|
||||
.attachment-actions button {
|
||||
min-height: 28px;
|
||||
padding: 4px 8px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 6px;
|
||||
background: #ffffff;
|
||||
color: var(--accent);
|
||||
cursor: pointer;
|
||||
font: inherit;
|
||||
font-size: 12px;
|
||||
font-weight: 700;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.attachment-actions a:hover,
|
||||
.attachment-actions button:hover {
|
||||
border-color: var(--accent);
|
||||
background: #eaf2ff;
|
||||
}
|
||||
|
||||
.table-empty,
|
||||
.attachment-manager-empty {
|
||||
color: var(--muted);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.attachment-manager-empty {
|
||||
min-height: 150px;
|
||||
place-content: center;
|
||||
}
|
||||
|
||||
.attachment-manager-empty h2 {
|
||||
margin: 0;
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
.attachment-manager-empty p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.tabbar {
|
||||
overflow-x: auto;
|
||||
@@ -1244,6 +1473,30 @@ input:focus {
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
}
|
||||
|
||||
.attachment-manager-page {
|
||||
height: auto;
|
||||
min-height: calc(100vh - 60px);
|
||||
padding: 12px;
|
||||
}
|
||||
|
||||
.attachment-manager-hero {
|
||||
align-items: stretch;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.attachment-manager-selectbar {
|
||||
grid-template-columns: 1fr;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.attachment-manager-split {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.attachment-search {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes pulse-caret {
|
||||
|
||||
147
static/js/attachment_manager.js
Normal file
147
static/js/attachment_manager.js
Normal file
@@ -0,0 +1,147 @@
|
||||
(function () {
|
||||
var page = document.querySelector(".attachment-manager-page");
|
||||
if (!page) {
|
||||
return;
|
||||
}
|
||||
|
||||
var conversationSelect = document.getElementById("attachmentConversationSelect");
|
||||
var uploadDropzone = document.getElementById("managerUploadDropzone");
|
||||
var attachmentInput = document.getElementById("managerAttachmentInput");
|
||||
var uploadStatus = document.getElementById("managerUploadStatus");
|
||||
var searchInput = document.getElementById("attachmentSearch");
|
||||
var table = document.getElementById("attachmentManagerTable");
|
||||
|
||||
function csrfToken() {
|
||||
var cookie = document.cookie.split("; ").find(function (item) {
|
||||
return item.indexOf("csrftoken=") === 0;
|
||||
});
|
||||
return cookie ? decodeURIComponent(cookie.split("=")[1]) : "";
|
||||
}
|
||||
|
||||
function selectedConversationUrl(id) {
|
||||
return id ? "/attachments/?conversation=" + encodeURIComponent(id) : "/attachments/";
|
||||
}
|
||||
|
||||
async function patchAttachment(row, payload) {
|
||||
var response = await fetch(row.getAttribute("data-update-url"), {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-CSRFToken": csrfToken(),
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("附件更新失败。");
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function deleteAttachment(row) {
|
||||
var response = await fetch(row.getAttribute("data-update-url"), {
|
||||
method: "DELETE",
|
||||
headers: { "X-CSRFToken": csrfToken() },
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("附件删除失败。");
|
||||
}
|
||||
}
|
||||
|
||||
async function uploadFiles(files) {
|
||||
if (!uploadDropzone || !files || !files.length) {
|
||||
return;
|
||||
}
|
||||
var formData = new FormData();
|
||||
Array.prototype.forEach.call(files, function (file) {
|
||||
formData.append("files", file);
|
||||
});
|
||||
if (uploadStatus) {
|
||||
uploadStatus.textContent = "上传中...";
|
||||
}
|
||||
try {
|
||||
var response = await fetch(uploadDropzone.getAttribute("data-upload-url"), {
|
||||
method: "POST",
|
||||
headers: { "X-CSRFToken": csrfToken() },
|
||||
body: formData,
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("上传失败。");
|
||||
}
|
||||
window.location.reload();
|
||||
} catch (error) {
|
||||
if (uploadStatus) {
|
||||
uploadStatus.textContent = "上传失败,请重试。";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (conversationSelect) {
|
||||
conversationSelect.addEventListener("change", function () {
|
||||
window.location.href = selectedConversationUrl(conversationSelect.value);
|
||||
});
|
||||
}
|
||||
|
||||
if (uploadDropzone && attachmentInput) {
|
||||
uploadDropzone.addEventListener("click", function () {
|
||||
attachmentInput.click();
|
||||
});
|
||||
uploadDropzone.addEventListener("dragover", function (event) {
|
||||
event.preventDefault();
|
||||
uploadDropzone.classList.add("dragging");
|
||||
});
|
||||
uploadDropzone.addEventListener("dragleave", function () {
|
||||
uploadDropzone.classList.remove("dragging");
|
||||
});
|
||||
uploadDropzone.addEventListener("drop", function (event) {
|
||||
event.preventDefault();
|
||||
uploadDropzone.classList.remove("dragging");
|
||||
uploadFiles(event.dataTransfer.files);
|
||||
});
|
||||
attachmentInput.addEventListener("change", function () {
|
||||
uploadFiles(attachmentInput.files);
|
||||
attachmentInput.value = "";
|
||||
});
|
||||
}
|
||||
|
||||
if (searchInput && table) {
|
||||
searchInput.addEventListener("input", function () {
|
||||
var keyword = searchInput.value.trim().toLowerCase();
|
||||
table.querySelectorAll("tbody tr[data-attachment-id]").forEach(function (row) {
|
||||
var name = (row.querySelector(".attachment-name") || row).textContent.toLowerCase();
|
||||
row.hidden = keyword && name.indexOf(keyword) === -1;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (table) {
|
||||
table.addEventListener("click", async function (event) {
|
||||
var actionButton = event.target.closest("[data-attachment-action]");
|
||||
if (!actionButton) {
|
||||
return;
|
||||
}
|
||||
var row = actionButton.closest("tr[data-attachment-id]");
|
||||
if (!row) {
|
||||
return;
|
||||
}
|
||||
var action = actionButton.getAttribute("data-attachment-action");
|
||||
try {
|
||||
if (action === "edit") {
|
||||
var nameCell = row.querySelector(".attachment-name");
|
||||
var nextName = window.prompt("请输入新的附件展示名", nameCell ? nameCell.textContent.trim() : "");
|
||||
if (nextName) {
|
||||
await patchAttachment(row, { original_name: nextName });
|
||||
window.location.reload();
|
||||
}
|
||||
} else if (action === "toggle") {
|
||||
await patchAttachment(row, { is_active: actionButton.textContent.trim() === "启用" });
|
||||
window.location.reload();
|
||||
} else if (action === "delete" && window.confirm("确认删除该附件?")) {
|
||||
await deleteAttachment(row);
|
||||
window.location.reload();
|
||||
}
|
||||
} catch (error) {
|
||||
window.alert(error.message || "附件操作失败。");
|
||||
}
|
||||
});
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user