Commit 22513c25 authored by tao355667's avatar tao355667

fix: keep article page after publishing

parent e5f96e5f
......@@ -16,6 +16,26 @@ let consultationTeachers = [];
let staffAccounts = [];
let activeConsultationUser = null;
const consultationStatusLabels = { unprocessed: "未处理", added: "已添加", not_added: "未添加" };
const ADMIN_STATE_KEY = "qiyouxue-admin-state";
function readAdminState() {
try {
const value = JSON.parse(sessionStorage.getItem(ADMIN_STATE_KEY) || "null");
return value && typeof value === "object" ? value : {};
} catch {
return {};
}
}
function updateAdminState(patch) {
try {
const next = { ...readAdminState(), ...patch };
if (next.view !== "articles") delete next.editorSlug;
sessionStorage.setItem(ADMIN_STATE_KEY, JSON.stringify(next));
} catch {
// Session storage may be unavailable in privacy-restricted browsers.
}
}
async function api(path, options = {}) {
const endpoint = path.startsWith("/") ? path : `/${path}`;
......@@ -101,10 +121,21 @@ async function enterApp(user) {
document.querySelectorAll(".content-role").forEach((element) => element.classList.toggle("hidden", customerService));
$("#account-role").textContent = admin ? "管理员账号" : operator ? "运营账号" : "客服账号";
$("#account-name").textContent = user?.name || "";
await showAdminView(operator ? "articles" : "consultations");
const rememberedState = readAdminState();
const initialView = operator ? "articles" : customerService ? "consultations" : rememberedState.view || "consultations";
await showAdminView(initialView);
if (admin || operator) {
await loadCategories();
loadArticles();
if (initialView === "articles" && rememberedState.editorSlug) {
try {
await openEditor(rememberedState.editorSlug);
} catch {
updateAdminState({ editorSlug: null });
await loadArticles();
}
} else {
await loadArticles();
}
}
}
......@@ -118,8 +149,10 @@ const adminViews = {
async function showAdminView(name) {
Object.values(adminViews).forEach(([selector]) => hide(selector));
hide("#edit-view");
const allowedName = currentUser?.role === "operator" ? "articles" : name;
const view = adminViews[allowedName] || adminViews.consultations;
const requestedName = currentUser?.role === "operator" ? "articles" : name;
const allowedName = adminViews[requestedName] ? requestedName : "consultations";
const view = adminViews[allowedName];
updateAdminState({ view: allowedName });
show(view[0]);
$("#page-title").textContent = view[1];
$("#page-subtitle").textContent = view[2];
......@@ -884,6 +917,7 @@ function setEditorStatus(status) {
async function openEditor(slug) {
editingSlug = slug;
updateAdminState({ view: "articles", editorSlug: slug || null });
dirty = false;
fillCategories();
$("#preview").innerHTML = "";
......@@ -962,6 +996,7 @@ $("#article-file").addEventListener("change", async (event) => {
});
$("#back").addEventListener("click", () => {
if (dirty && !confirm("还有未保存的内容,确定返回文章列表吗?")) return;
updateAdminState({ view: "articles", editorSlug: null });
setView(false);
loadArticles();
});
......@@ -979,6 +1014,7 @@ async function save() {
? await api(`/articles/${editingSlug}`, { method: "PUT", body: payload() })
: await api("/articles", { method: "POST", body: payload() });
editingSlug = result.slug;
updateAdminState({ view: "articles", editorSlug: editingSlug });
dirty = false;
setEditorStatus(result.publishStatus);
return result.slug;
......@@ -993,7 +1029,9 @@ $("#edit-form").addEventListener("submit", async (event) => {
} catch (error) { $("#message").textContent = error.message; }
});
$("#publish").addEventListener("click", async () => {
$("#publish").addEventListener("click", async (event) => {
event.preventDefault();
event.stopPropagation();
$("#message").textContent = "正在保存草稿…";
try {
const slug = await save();
......@@ -1009,7 +1047,11 @@ $("#discard").addEventListener("click", async () => {
try {
const result = await api(`/articles/${editingSlug}/draft`, { method: "DELETE" });
dirty = false;
if (result.removed) { setView(false); loadArticles(); }
if (result.removed) {
updateAdminState({ view: "articles", editorSlug: null });
setView(false);
loadArticles();
}
else await openEditor(editingSlug);
} catch (error) { $("#message").textContent = error.message; }
});
......@@ -1039,6 +1081,7 @@ $("#delete-article").addEventListener("click", async () => {
await runContentAction(`/articles/${editingSlug}`, { method: "DELETE" });
dirty = false;
editingSlug = null;
updateAdminState({ view: "articles", editorSlug: null });
setView(false);
await loadArticles();
} catch (error) { $("#message").textContent = error.message; }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment