2025-02-14 18:15:23 +01:00
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
2025-02-15 13:03:10 +01:00
|
|
|
let currentChapterId = null;
|
|
|
|
const storyId = document.querySelector('input[name="id"]')?.value;
|
|
|
|
|
2025-02-15 14:55:27 +01:00
|
|
|
// Création de l'icône SVG pour le séparateur
|
|
|
|
const icons = {
|
|
|
|
divider: `
|
|
|
|
<svg viewBox="0 0 18 18">
|
|
|
|
<line class="ql-stroke" x1="3" x2="15" y1="9" y2="9"></line>
|
|
|
|
</svg>
|
|
|
|
`
|
|
|
|
};
|
|
|
|
|
|
|
|
// Ajout de l'icône à Quill
|
|
|
|
const Block = Quill.import('blots/block');
|
|
|
|
const icons_list = Quill.import('ui/icons');
|
|
|
|
icons_list['divider'] = icons.divider;
|
|
|
|
|
|
|
|
// Définition du format pour le séparateur
|
|
|
|
class DividerBlot extends Block {
|
|
|
|
static create() {
|
|
|
|
const node = super.create();
|
|
|
|
node.className = 'chapter-divider';
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DividerBlot.blotName = 'divider';
|
|
|
|
DividerBlot.tagName = 'hr';
|
|
|
|
Quill.register(DividerBlot);
|
|
|
|
|
|
|
|
// Configuration et initialisation de Quill
|
2025-02-14 18:15:23 +01:00
|
|
|
const quill = new Quill('#editor', {
|
|
|
|
theme: 'snow',
|
|
|
|
modules: {
|
2025-02-15 14:55:27 +01:00
|
|
|
toolbar: {
|
|
|
|
container: [
|
|
|
|
[{ 'header': [1, 2, 3, false] }],
|
|
|
|
['bold', 'italic', 'underline', 'strike'],
|
|
|
|
[{ 'color': [] }, { 'background': [] }],
|
|
|
|
[{ 'font': [] }],
|
|
|
|
[{ 'align': [] }],
|
|
|
|
['blockquote', 'code-block'],
|
|
|
|
[{ 'list': 'ordered'}, { 'list': 'bullet' }],
|
|
|
|
[{ 'script': 'sub'}, { 'script': 'super' }],
|
|
|
|
[{ 'indent': '-1'}, { 'indent': '+1' }],
|
|
|
|
[{ 'direction': 'rtl' }],
|
|
|
|
['link', 'image', 'video'],
|
|
|
|
['divider'],
|
|
|
|
['clean']
|
|
|
|
],
|
|
|
|
handlers: {
|
|
|
|
'divider': function() {
|
|
|
|
const range = quill.getSelection(true);
|
|
|
|
quill.insertText(range.index, '\n', Quill.sources.USER);
|
|
|
|
quill.insertEmbed(range.index + 1, 'divider', true, Quill.sources.USER);
|
|
|
|
quill.setSelection(range.index + 2, Quill.sources.SILENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2025-02-15 13:03:10 +01:00
|
|
|
keyboard: {
|
|
|
|
bindings: {
|
|
|
|
tab: false,
|
|
|
|
'indent backwards': false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2025-02-15 14:55:27 +01:00
|
|
|
placeholder: 'Commencez à écrire votre chapitre ici...'
|
2025-02-14 18:15:23 +01:00
|
|
|
});
|
|
|
|
|
2025-02-15 13:03:10 +01:00
|
|
|
// Gestion des chapitres
|
2025-02-14 19:57:45 +01:00
|
|
|
const modal = document.getElementById('chapterEditor');
|
|
|
|
const addChapterBtn = document.getElementById('addChapter');
|
2025-02-15 13:03:10 +01:00
|
|
|
const saveChapterBtn = document.getElementById('saveChapter');
|
|
|
|
const cancelEditBtn = document.getElementById('cancelEdit');
|
|
|
|
const chapterTitleInput = document.getElementById('chapterTitle');
|
|
|
|
const chaptersList = document.getElementById('chaptersList');
|
2025-02-14 19:57:45 +01:00
|
|
|
|
2025-02-15 13:03:10 +01:00
|
|
|
// Configuration de Sortable pour la réorganisation des chapitres
|
2025-02-14 18:15:23 +01:00
|
|
|
if (chaptersList) {
|
|
|
|
new Sortable(chaptersList, {
|
|
|
|
animation: 150,
|
2025-02-15 14:55:27 +01:00
|
|
|
handle: '.chapter-number',
|
2025-02-14 19:57:45 +01:00
|
|
|
ghostClass: 'sortable-ghost',
|
2025-02-15 13:03:10 +01:00
|
|
|
onEnd: async function(evt) {
|
2025-02-14 19:57:45 +01:00
|
|
|
const chapters = Array.from(chaptersList.children).map((item, index) => ({
|
|
|
|
id: item.dataset.id,
|
2025-02-15 13:03:10 +01:00
|
|
|
position: index
|
2025-02-14 19:57:45 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch('api/reorder-chapters.php', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
storyId: storyId,
|
|
|
|
chapters: chapters
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Erreur lors de la réorganisation');
|
|
|
|
}
|
|
|
|
|
|
|
|
chapters.forEach((chapter, index) => {
|
|
|
|
const element = document.querySelector(`[data-id="${chapter.id}"] .chapter-number`);
|
|
|
|
if (element) {
|
|
|
|
element.textContent = index + 1;
|
|
|
|
}
|
|
|
|
});
|
2025-02-15 13:03:10 +01:00
|
|
|
|
2025-02-14 19:57:45 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Erreur:', error);
|
2025-02-15 13:03:10 +01:00
|
|
|
showNotification('Erreur lors de la réorganisation des chapitres', 'error');
|
2025-02-14 19:57:45 +01:00
|
|
|
}
|
|
|
|
}
|
2025-02-14 18:15:23 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-02-15 13:03:10 +01:00
|
|
|
// Gestionnaires d'événements pour l'édition des chapitres
|
2025-02-14 18:15:23 +01:00
|
|
|
if (addChapterBtn) {
|
|
|
|
addChapterBtn.addEventListener('click', () => {
|
2025-02-14 19:57:45 +01:00
|
|
|
currentChapterId = null;
|
2025-02-15 13:03:10 +01:00
|
|
|
chapterTitleInput.value = '';
|
|
|
|
quill.setContents([]);
|
2025-02-14 18:15:23 +01:00
|
|
|
modal.style.display = 'block';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-02-15 13:03:10 +01:00
|
|
|
if (cancelEditBtn) {
|
|
|
|
cancelEditBtn.addEventListener('click', () => {
|
|
|
|
if (hasUnsavedChanges()) {
|
|
|
|
if (!confirm('Des modifications non sauvegardées seront perdues. Voulez-vous vraiment fermer ?')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
modal.style.display = 'none';
|
|
|
|
});
|
|
|
|
}
|
2025-02-14 19:57:45 +01:00
|
|
|
|
2025-02-15 14:55:27 +01:00
|
|
|
// Gestion des clics sur la liste des chapitres
|
2025-02-15 13:03:10 +01:00
|
|
|
if (chaptersList) {
|
|
|
|
chaptersList.addEventListener('click', async (e) => {
|
|
|
|
const target = e.target;
|
|
|
|
|
|
|
|
if (target.matches('.edit-chapter')) {
|
|
|
|
const chapterItem = target.closest('.chapter-item');
|
|
|
|
currentChapterId = chapterItem.dataset.id;
|
2025-02-14 19:57:45 +01:00
|
|
|
|
|
|
|
try {
|
2025-02-15 13:03:10 +01:00
|
|
|
const response = await fetch(`api/get-chapter.php?storyId=${storyId}&chapterId=${currentChapterId}`);
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Erreur réseau');
|
|
|
|
}
|
|
|
|
|
|
|
|
const chapter = await response.json();
|
|
|
|
|
|
|
|
// Mise à jour du titre
|
|
|
|
chapterTitleInput.value = chapter.title || '';
|
|
|
|
|
|
|
|
// Gestion du contenu
|
|
|
|
if (chapter.html) {
|
|
|
|
quill.root.innerHTML = chapter.html;
|
|
|
|
} else if (chapter.content) {
|
|
|
|
try {
|
|
|
|
const content = typeof chapter.content === 'string'
|
|
|
|
? JSON.parse(chapter.content)
|
|
|
|
: chapter.content;
|
|
|
|
quill.setContents(content);
|
|
|
|
} catch (contentError) {
|
|
|
|
console.error('Erreur de parsing du contenu:', contentError);
|
|
|
|
quill.setText(chapter.content || '');
|
|
|
|
}
|
2025-02-14 19:57:45 +01:00
|
|
|
} else {
|
2025-02-15 13:03:10 +01:00
|
|
|
quill.setContents([]);
|
2025-02-14 19:57:45 +01:00
|
|
|
}
|
2025-02-15 13:03:10 +01:00
|
|
|
|
|
|
|
modal.style.display = 'block';
|
2025-02-14 19:57:45 +01:00
|
|
|
} catch (error) {
|
2025-02-15 13:03:10 +01:00
|
|
|
console.error('Erreur détaillée:', error);
|
2025-02-15 14:55:27 +01:00
|
|
|
showNotification('Erreur lors du chargement du chapitre', 'error');
|
2025-02-14 19:57:45 +01:00
|
|
|
}
|
|
|
|
}
|
2025-02-15 13:03:10 +01:00
|
|
|
});
|
|
|
|
}
|
2025-02-14 19:57:45 +01:00
|
|
|
|
2025-02-15 13:03:10 +01:00
|
|
|
// Sauvegarde d'un chapitre
|
|
|
|
if (saveChapterBtn) {
|
|
|
|
saveChapterBtn.addEventListener('click', async () => {
|
|
|
|
const title = chapterTitleInput.value.trim();
|
|
|
|
if (!title) {
|
|
|
|
showNotification('Le titre est requis', 'error');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-02-14 19:57:45 +01:00
|
|
|
try {
|
2025-02-15 13:03:10 +01:00
|
|
|
const response = await fetch('api/save-chapter.php', {
|
2025-02-14 18:15:23 +01:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2025-02-14 19:57:45 +01:00
|
|
|
storyId: storyId,
|
2025-02-15 13:03:10 +01:00
|
|
|
chapterId: currentChapterId,
|
|
|
|
title: title,
|
|
|
|
content: JSON.stringify(quill.getContents())
|
2025-02-14 18:15:23 +01:00
|
|
|
})
|
|
|
|
});
|
2025-02-15 13:03:10 +01:00
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
showNotification('Chapitre sauvegardé avec succès');
|
|
|
|
setTimeout(() => location.reload(), 500);
|
|
|
|
} else {
|
|
|
|
throw new Error('Erreur lors de la sauvegarde');
|
|
|
|
}
|
2025-02-14 18:15:23 +01:00
|
|
|
} catch (error) {
|
2025-02-14 19:57:45 +01:00
|
|
|
console.error('Erreur:', error);
|
2025-02-15 13:03:10 +01:00
|
|
|
showNotification('Erreur lors de la sauvegarde du chapitre', 'error');
|
2025-02-14 18:15:23 +01:00
|
|
|
}
|
2025-02-15 13:03:10 +01:00
|
|
|
});
|
|
|
|
}
|
2025-02-14 18:15:23 +01:00
|
|
|
|
2025-02-15 14:55:27 +01:00
|
|
|
function hasUnsavedChanges() {
|
|
|
|
if (!currentChapterId) {
|
|
|
|
return chapterTitleInput.value !== '' || quill.getLength() > 1;
|
2025-02-14 18:15:23 +01:00
|
|
|
}
|
2025-02-15 14:55:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
2025-02-14 18:15:23 +01:00
|
|
|
|
2025-02-15 14:55:27 +01:00
|
|
|
function showNotification(message, type = 'success') {
|
|
|
|
const notification = document.createElement('div');
|
|
|
|
notification.className = `notification ${type}`;
|
|
|
|
notification.textContent = message;
|
|
|
|
|
|
|
|
document.body.appendChild(notification);
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
notification.style.opacity = '1';
|
|
|
|
notification.style.transform = 'translateY(0)';
|
|
|
|
}, 10);
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
notification.style.opacity = '0';
|
|
|
|
notification.style.transform = 'translateY(-100%)';
|
|
|
|
setTimeout(() => notification.remove(), 300);
|
|
|
|
}, 3000);
|
|
|
|
}
|
2025-02-14 19:57:45 +01:00
|
|
|
});
|