Документация: разделы и статьи в docs.html
- Добавлены 12 разделов документации: Журнал, Отчёты о занятиях, Ученики, Группы, Обратная связь, Короткие сообщения, Чаты, Фото, Файлы, Ссылки, Закладки, Заметки (каждый со вступительной статьёй) - Сид разделов в lib/docs.js (ensureDocsTables) и db/migration.sql -doc_images добавлен в sweepOrphanedUploads и BROKEN_IMAGE_SOURCES - Исправлен дубль docSectionIds в backup-restore.js - Роуты /api/docs/* в server.js - Страницы public/docs.html, public/docs-public.html, скрипты public/js/docs.js, public/js/docs-public.js, public/js/md.js
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
(function () {
|
||||
const treeEl = document.getElementById('dpTree');
|
||||
const articleEl = document.getElementById('dpArticle');
|
||||
const headSub = document.getElementById('dpHeadSub');
|
||||
const lightbox = document.getElementById('dpLightbox');
|
||||
const lightboxImg = document.getElementById('dpLightboxImg');
|
||||
const COLLAPSE_KEY = 'whatido.docsPublic.collapsed';
|
||||
const ICON_CHEVRON = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m6 9 6 6 6-6"/></svg>';
|
||||
const ICON_FOLDER = '<svg class="dp-folder" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z"/></svg>';
|
||||
let tree = [];
|
||||
let currentSlug = '';
|
||||
let collapsed = new Set();
|
||||
|
||||
try {
|
||||
const saved = JSON.parse(localStorage.getItem(COLLAPSE_KEY) || '[]');
|
||||
if (Array.isArray(saved)) collapsed = new Set(saved.map(Number));
|
||||
} catch (e) {}
|
||||
|
||||
function esc(value) {
|
||||
return String(value == null ? '' : value)
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"');
|
||||
}
|
||||
|
||||
function saveCollapsed() {
|
||||
localStorage.setItem(COLLAPSE_KEY, JSON.stringify([...collapsed]));
|
||||
}
|
||||
|
||||
async function getJson(url) {
|
||||
const res = await fetch(url);
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) throw new Error(data.error || 'Ошибка загрузки');
|
||||
return data;
|
||||
}
|
||||
|
||||
function articleRow(article, depth) {
|
||||
return `<div class="dp-article-row"><a href="?slug=${encodeURIComponent(article.slug)}" data-slug="${esc(article.slug)}"
|
||||
style="padding-left:${26 + depth * 14}px" class="${currentSlug === article.slug ? 'active' : ''}">${esc(article.title)}</a></div>`;
|
||||
}
|
||||
|
||||
function sectionNode(node, depth) {
|
||||
const closed = collapsed.has(Number(node.id));
|
||||
const hasItems = (node.children && node.children.length) || (node.articles && node.articles.length);
|
||||
const chev = hasItems
|
||||
? `<button type="button" class="dp-chev${closed ? ' closed' : ''}" data-toggle="${node.id}">${ICON_CHEVRON}</button>`
|
||||
: '<span style="width:14px;flex:none"></span>';
|
||||
const children = closed ? '' : [
|
||||
...(node.children || []).map(child => sectionNode(child, depth + 1)),
|
||||
...(node.articles || []).map(article => articleRow(article, depth + 1)),
|
||||
].join('');
|
||||
return `<div>
|
||||
<div class="dp-section-row" data-section="${node.id}" style="padding-left:${8 + depth * 14}px">
|
||||
${chev}${ICON_FOLDER}<span class="dp-section-title">${esc(node.title)}</span>
|
||||
</div>
|
||||
<div${closed ? ' hidden' : ''}>${children}</div>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function renderTree() {
|
||||
if (!tree.length) {
|
||||
treeEl.innerHTML = '<div class="dp-empty">Документация пока пуста</div>';
|
||||
return;
|
||||
}
|
||||
treeEl.innerHTML = tree.map(node => sectionNode(node, 0)).join('');
|
||||
}
|
||||
|
||||
async function open(slug, updateUrl) {
|
||||
const data = await getJson('/api/docs/public/articles/' + encodeURIComponent(slug));
|
||||
const article = data.item;
|
||||
currentSlug = article.slug;
|
||||
articleEl.innerHTML = `<div class="dp-crumbs">${esc(article.section_title || '')}</div>
|
||||
<h1>${esc(article.title)}</h1>
|
||||
<div class="dp-body">${WhatIDoMD.render(article.body)}</div>`;
|
||||
document.title = article.title + ' — Документация';
|
||||
if (headSub) headSub.textContent = article.section_title || '';
|
||||
if (updateUrl !== false) history.replaceState(null, '', '?slug=' + encodeURIComponent(article.slug));
|
||||
renderTree();
|
||||
}
|
||||
|
||||
function bind() {
|
||||
treeEl.addEventListener('click', e => {
|
||||
const toggle = e.target.closest('[data-toggle]');
|
||||
if (toggle) {
|
||||
e.preventDefault();
|
||||
const id = Number(toggle.dataset.toggle);
|
||||
if (collapsed.has(id)) collapsed.delete(id); else collapsed.add(id);
|
||||
saveCollapsed();
|
||||
renderTree();
|
||||
return;
|
||||
}
|
||||
const link = e.target.closest('[data-slug]');
|
||||
if (link) {
|
||||
e.preventDefault();
|
||||
open(link.dataset.slug).catch(err => { articleEl.textContent = err.message; });
|
||||
return;
|
||||
}
|
||||
const row = e.target.closest('.dp-section-row');
|
||||
if (row) {
|
||||
const id = Number(row.dataset.section);
|
||||
if (collapsed.has(id)) collapsed.delete(id); else collapsed.add(id);
|
||||
saveCollapsed();
|
||||
renderTree();
|
||||
}
|
||||
});
|
||||
articleEl.addEventListener('click', e => {
|
||||
const img = e.target.closest('[data-img]');
|
||||
if (!img) return;
|
||||
e.preventDefault();
|
||||
lightboxImg.src = img.dataset.img;
|
||||
lightbox.classList.add('open');
|
||||
});
|
||||
lightbox.addEventListener('click', () => {
|
||||
lightbox.classList.remove('open');
|
||||
lightboxImg.removeAttribute('src');
|
||||
});
|
||||
document.addEventListener('keydown', e => {
|
||||
if (e.key === 'Escape' && lightbox.classList.contains('open')) {
|
||||
lightbox.classList.remove('open');
|
||||
lightboxImg.removeAttribute('src');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function firstSlug(nodes) {
|
||||
for (const node of nodes) {
|
||||
if (node.articles && node.articles.length) return node.articles[0].slug;
|
||||
const nested = firstSlug(node.children || []);
|
||||
if (nested) return nested;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
async function init() {
|
||||
bind();
|
||||
try {
|
||||
const data = await getJson('/api/docs/public/tree');
|
||||
tree = data.sections || [];
|
||||
renderTree();
|
||||
const params = new URLSearchParams(location.search);
|
||||
const slug = params.get('slug') || firstSlug(tree);
|
||||
if (slug) await open(slug, !params.get('slug'));
|
||||
else articleEl.innerHTML = '<div class="dp-empty" style="padding:40px 0">Статьи не опубликованы</div>';
|
||||
} catch (e) {
|
||||
articleEl.textContent = e.message;
|
||||
treeEl.innerHTML = '<div class="dp-empty">Не удалось загрузить документацию</div>';
|
||||
}
|
||||
}
|
||||
|
||||
init();
|
||||
})();
|
||||
Reference in New Issue
Block a user