import { currentUser } from '../components/auth.js';
// Import editors
import { loadVditor, initVditorEditor } from '../editors/vditorLoader.js';
// Import course-builder modules
import { courseStructure, currentCourseId, isEditMode, isDraftMode, setVditorInstance, getVditorInstance } from '../course-builder/State.js';
import { saveDraft, previewCourse, publishCourse, updateDraftsCount, showDraftsModal, loadDraftIntoEditor } from '../course-builder/DraftManager.js';
import { initCourseStructure, loadCourseForEditing, renderCourseStructure } from '../course-builder/StructureManager.js';
import { showMessage, updateBuilderTitle } from '../course-builder/CourseUI.js';
export { courseStructure, currentCourseId, isEditMode, isDraftMode };
export async function render() {
if (!currentUser) {
return `
Конструктор курса
Создайте новый курс с помощью редактора Markdown с поддержкой LaTeX
`;
}
export function afterRender() {
if (!currentUser) return;
initVditor();
initCourseStructure();
document.getElementById('save-draft').addEventListener('click', saveDraft);
document.getElementById('preview-course').addEventListener('click', previewCourse);
document.getElementById('publish-course').addEventListener('click', publishCourse);
document.getElementById('manage-drafts').addEventListener('click', showDraftsModal);
updateDraftsCount();
setTimeout(() => {
const vditor = getVditorInstance();
if (vditor) {
let debounceTimeout;
vditor.vditor.element.addEventListener('keyup', () => {
clearTimeout(debounceTimeout);
debounceTimeout = setTimeout(() => { if (currentCourseId) saveDraft(); }, 2000);
});
}
}, 2000);
}
function initVditor() {
const initialContent = getInitialContent();
if (typeof Vditor === 'undefined') loadVditor().then(() => { const inst = initVditorEditor('vditor-container', initialContent); if (inst) setVditorInstance(inst); });
else { const inst = initVditorEditor('vditor-container', initialContent); if (inst) setVditorInstance(inst); }
}
function getInitialContent() {
const draft = localStorage.getItem('course_draft');
let initialContent = '# Заголовок курса\n\n## Введение\nНапишите введение к вашему курсу здесь...\n\n## Основная часть\n- Пункт 1\n- Пункт 2\n\n## Формулы LaTeX\nВведите формулы в формате LaTeX:\n$$E = mc^2$$\n$$\\int_{a}^{b} f(x) dx$$\n\n## Заключение\nПодведите итоги курса...';
if (draft) {
try {
const data = JSON.parse(draft);
if (data.content) initialContent = data.content;
} catch (e) { console.error('Error loading draft for editor:', e); }
}
return initialContent;
}
window.addEventListener('load', () => {
const draft = localStorage.getItem('course_draft');
if (draft) {
try {
const data = JSON.parse(draft);
const titleEl = document.getElementById('course-title');
const descEl = document.getElementById('course-description');
const levelEl = document.getElementById('course-level');
const messageEl = document.getElementById('builder-message');
if (titleEl) titleEl.value = data.title || '';
if (descEl) descEl.value = data.description || '';
if (levelEl) levelEl.value = data.level || 'beginner';
if (messageEl && data.savedAt) {
messageEl.textContent = `📝 Загружен черновик от ${new Date(data.savedAt).toLocaleString()}`;
messageEl.className = 'message info';
}
} catch (e) { console.error('Error loading draft:', e); }
}
});