amélioration de l'éditeur
This commit is contained in:
parent
bda0cc9981
commit
5294e93989
@ -3,7 +3,8 @@ require_once '../../includes/config.php';
|
|||||||
require_once '../../includes/auth.php';
|
require_once '../../includes/auth.php';
|
||||||
require_once '../../includes/stories.php';
|
require_once '../../includes/stories.php';
|
||||||
|
|
||||||
// Vérification de l'authentification
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
if (!Auth::check()) {
|
if (!Auth::check()) {
|
||||||
http_response_code(401);
|
http_response_code(401);
|
||||||
exit(json_encode(['error' => 'Non autorisé']));
|
exit(json_encode(['error' => 'Non autorisé']));
|
||||||
@ -35,9 +36,39 @@ try {
|
|||||||
throw new Exception('Chapitre non trouvé');
|
throw new Exception('Chapitre non trouvé');
|
||||||
}
|
}
|
||||||
|
|
||||||
header('Content-Type: application/json');
|
// Vérification et correction de la structure du chapitre
|
||||||
|
if (!isset($chapter['title'])) {
|
||||||
|
$chapter['title'] = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Conversion du contenu HTML en format Delta de Quill si nécessaire
|
||||||
|
if (!isset($chapter['content'])) {
|
||||||
|
$chapter['content'] = '{"ops":[{"insert":"\n"}]}';
|
||||||
|
} else {
|
||||||
|
// Si le contenu est une chaîne HTML
|
||||||
|
if (is_string($chapter['content']) && !isJson($chapter['content'])) {
|
||||||
|
// On envoie le HTML brut, Quill le convertira côté client
|
||||||
|
$chapter['html'] = $chapter['content'];
|
||||||
|
$chapter['content'] = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
echo json_encode($chapter);
|
echo json_encode($chapter);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
error_log('Erreur dans get-chapter.php: ' . $e->getMessage());
|
||||||
http_response_code(500);
|
http_response_code(500);
|
||||||
echo json_encode(['error' => $e->getMessage()]);
|
echo json_encode([
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
'details' => [
|
||||||
|
'storyId' => $storyId,
|
||||||
|
'chapterId' => $chapterId,
|
||||||
|
'file' => __FILE__,
|
||||||
|
'line' => __LINE__
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isJson($string) {
|
||||||
|
json_decode($string);
|
||||||
|
return json_last_error() === JSON_ERROR_NONE;
|
||||||
}
|
}
|
207
admin/api/upload-image.php
Normal file
207
admin/api/upload-image.php
Normal file
@ -0,0 +1,207 @@
|
|||||||
|
<?php
|
||||||
|
require_once '../../includes/config.php';
|
||||||
|
require_once '../../includes/auth.php';
|
||||||
|
|
||||||
|
class ImageUploadHandler {
|
||||||
|
private $uploadDir;
|
||||||
|
private $allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
||||||
|
private $maxFileSize = 5242880; // 5MB
|
||||||
|
private $maxWidth = 1200;
|
||||||
|
private $maxHeight = 1200;
|
||||||
|
|
||||||
|
public function __construct($storyId) {
|
||||||
|
// Création du dossier d'upload spécifique au roman
|
||||||
|
$this->uploadDir = __DIR__ . '/../../assets/images/chapters/' . $storyId . '/';
|
||||||
|
$this->ensureUploadDirectory();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleUpload($file) {
|
||||||
|
try {
|
||||||
|
// Vérifications de base
|
||||||
|
if ($file['error'] !== UPLOAD_ERR_OK) {
|
||||||
|
throw new Exception($this->getUploadErrorMessage($file['error']));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vérification du type MIME
|
||||||
|
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
||||||
|
$mimeType = $finfo->file($file['tmp_name']);
|
||||||
|
if (!in_array($mimeType, $this->allowedTypes)) {
|
||||||
|
throw new Exception('Type de fichier non autorisé. Types acceptés : JPG, PNG, GIF, WEBP');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vérification de la taille
|
||||||
|
if ($file['size'] > $this->maxFileSize) {
|
||||||
|
throw new Exception('Fichier trop volumineux. Taille maximum : 5MB');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vérification et redimensionnement de l'image
|
||||||
|
[$width, $height, $type] = getimagesize($file['tmp_name']);
|
||||||
|
$needsResize = $width > $this->maxWidth || $height > $this->maxHeight;
|
||||||
|
|
||||||
|
// Génération d'un nom de fichier unique
|
||||||
|
$extension = $this->getExtensionFromMimeType($mimeType);
|
||||||
|
$filename = uniqid() . '.' . $extension;
|
||||||
|
$targetPath = $this->uploadDir . $filename;
|
||||||
|
|
||||||
|
if ($needsResize) {
|
||||||
|
// Calcul des nouvelles dimensions en conservant le ratio
|
||||||
|
$ratio = min($this->maxWidth / $width, $this->maxHeight / $height);
|
||||||
|
$newWidth = round($width * $ratio);
|
||||||
|
$newHeight = round($height * $ratio);
|
||||||
|
|
||||||
|
// Création de la nouvelle image
|
||||||
|
$sourceImage = $this->createImageFromFile($file['tmp_name'], $mimeType);
|
||||||
|
$newImage = imagecreatetruecolor($newWidth, $newHeight);
|
||||||
|
|
||||||
|
// Préservation de la transparence pour PNG
|
||||||
|
if ($mimeType === 'image/png') {
|
||||||
|
imagealphablending($newImage, false);
|
||||||
|
imagesavealpha($newImage, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Redimensionnement
|
||||||
|
imagecopyresampled(
|
||||||
|
$newImage, $sourceImage,
|
||||||
|
0, 0, 0, 0,
|
||||||
|
$newWidth, $newHeight,
|
||||||
|
$width, $height
|
||||||
|
);
|
||||||
|
|
||||||
|
// Sauvegarde de l'image redimensionnée
|
||||||
|
$this->saveImage($newImage, $targetPath, $mimeType);
|
||||||
|
|
||||||
|
// Libération de la mémoire
|
||||||
|
imagedestroy($sourceImage);
|
||||||
|
imagedestroy($newImage);
|
||||||
|
} else {
|
||||||
|
// Déplacement du fichier original si pas besoin de redimensionnement
|
||||||
|
if (!move_uploaded_file($file['tmp_name'], $targetPath)) {
|
||||||
|
throw new Exception('Erreur lors du déplacement du fichier uploadé');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retourner le chemin relatif pour l'éditeur
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'url' => $this->getRelativePath($targetPath),
|
||||||
|
'width' => $needsResize ? $newWidth : $width,
|
||||||
|
'height' => $needsResize ? $newHeight : $height
|
||||||
|
];
|
||||||
|
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'error' => $e->getMessage()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function ensureUploadDirectory() {
|
||||||
|
if (!file_exists($this->uploadDir)) {
|
||||||
|
if (!mkdir($this->uploadDir, 0755, true)) {
|
||||||
|
throw new Exception('Impossible de créer le dossier d\'upload');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_writable($this->uploadDir)) {
|
||||||
|
throw new Exception('Le dossier d\'upload n\'est pas accessible en écriture');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createImageFromFile($file, $mimeType) {
|
||||||
|
switch ($mimeType) {
|
||||||
|
case 'image/jpeg':
|
||||||
|
return imagecreatefromjpeg($file);
|
||||||
|
case 'image/png':
|
||||||
|
return imagecreatefrompng($file);
|
||||||
|
case 'image/gif':
|
||||||
|
return imagecreatefromgif($file);
|
||||||
|
case 'image/webp':
|
||||||
|
return imagecreatefromwebp($file);
|
||||||
|
default:
|
||||||
|
throw new Exception('Type d\'image non supporté');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function saveImage($image, $path, $mimeType) {
|
||||||
|
switch ($mimeType) {
|
||||||
|
case 'image/jpeg':
|
||||||
|
return imagejpeg($image, $path, 85); // Qualité 85%
|
||||||
|
case 'image/png':
|
||||||
|
return imagepng($image, $path, 8); // Compression 8
|
||||||
|
case 'image/gif':
|
||||||
|
return imagegif($image, $path);
|
||||||
|
case 'image/webp':
|
||||||
|
return imagewebp($image, $path, 85); // Qualité 85%
|
||||||
|
default:
|
||||||
|
throw new Exception('Type d\'image non supporté pour la sauvegarde');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getExtensionFromMimeType($mimeType) {
|
||||||
|
$map = [
|
||||||
|
'image/jpeg' => 'jpg',
|
||||||
|
'image/png' => 'png',
|
||||||
|
'image/gif' => 'gif',
|
||||||
|
'image/webp' => 'webp'
|
||||||
|
];
|
||||||
|
return $map[$mimeType] ?? 'jpg';
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getRelativePath($absolutePath) {
|
||||||
|
$relativePath = str_replace(__DIR__ . '/../../', '', $absolutePath);
|
||||||
|
// Ajout de '../' car on est dans admin/api/
|
||||||
|
return '../' . str_replace('\\', '/', $relativePath); // Pour la compatibilité Windows
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getUploadErrorMessage($error) {
|
||||||
|
$errors = [
|
||||||
|
UPLOAD_ERR_INI_SIZE => 'Le fichier dépasse la taille maximale autorisée par PHP',
|
||||||
|
UPLOAD_ERR_FORM_SIZE => 'Le fichier dépasse la taille maximale autorisée par le formulaire',
|
||||||
|
UPLOAD_ERR_PARTIAL => 'Le fichier n\'a été que partiellement uploadé',
|
||||||
|
UPLOAD_ERR_NO_FILE => 'Aucun fichier n\'a été uploadé',
|
||||||
|
UPLOAD_ERR_NO_TMP_DIR => 'Dossier temporaire manquant',
|
||||||
|
UPLOAD_ERR_CANT_WRITE => 'Échec de l\'écriture du fichier sur le disque',
|
||||||
|
UPLOAD_ERR_EXTENSION => 'Une extension PHP a arrêté l\'upload'
|
||||||
|
];
|
||||||
|
return $errors[$error] ?? 'Erreur inconnue lors de l\'upload';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Point d'entrée du script
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||||
|
http_response_code(405);
|
||||||
|
exit(json_encode(['error' => 'Méthode non autorisée']));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vérification de l'authentification
|
||||||
|
if (!Auth::check()) {
|
||||||
|
http_response_code(401);
|
||||||
|
exit(json_encode(['error' => 'Non autorisé']));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Récupération de l'ID du roman
|
||||||
|
$storyId = $_POST['storyId'] ?? null;
|
||||||
|
if (!$storyId) {
|
||||||
|
http_response_code(400);
|
||||||
|
exit(json_encode(['error' => 'ID du roman manquant']));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Traitement de l'upload
|
||||||
|
try {
|
||||||
|
$handler = new ImageUploadHandler($storyId);
|
||||||
|
$result = $handler->handleUpload($_FILES['image']);
|
||||||
|
|
||||||
|
if (!$result['success']) {
|
||||||
|
http_response_code(400);
|
||||||
|
}
|
||||||
|
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo json_encode($result);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
http_response_code(500);
|
||||||
|
echo json_encode([
|
||||||
|
'success' => false,
|
||||||
|
'error' => 'Erreur serveur : ' . $e->getMessage()
|
||||||
|
]);
|
||||||
|
}
|
@ -1,55 +1,197 @@
|
|||||||
/* Personnalisation de l'éditeur Quill */
|
/* Base de l'éditeur */
|
||||||
.ql-toolbar {
|
.ql-toolbar.ql-snow {
|
||||||
background: var(--bg-secondary) !important;
|
background: var(--bg-secondary);
|
||||||
border-color: var(--border-color) !important;
|
border-color: var(--border-color);
|
||||||
border-radius: var(--radius-sm) var(--radius-sm) 0 0;
|
border-radius: var(--radius-sm) var(--radius-sm) 0 0;
|
||||||
|
padding: var(--spacing-sm);
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.ql-container {
|
.ql-container.ql-snow {
|
||||||
background: var(--input-bg) !important;
|
background: var(--input-bg);
|
||||||
border-color: var(--border-color) !important;
|
border-color: var(--border-color);
|
||||||
color: var(--text-primary) !important;
|
|
||||||
border-radius: 0 0 var(--radius-sm) var(--radius-sm);
|
border-radius: 0 0 var(--radius-sm) var(--radius-sm);
|
||||||
|
color: var(--text-primary);
|
||||||
min-height: 300px;
|
min-height: 300px;
|
||||||
|
font-family: inherit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Contenu de l'éditeur */
|
||||||
.ql-editor {
|
.ql-editor {
|
||||||
color: var(--text-primary) !important;
|
color: var(--text-primary);
|
||||||
|
padding: var(--spacing-md);
|
||||||
|
font-size: 1rem;
|
||||||
|
line-height: 1.6;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Boutons de la barre d'outils */
|
.ql-editor.ql-blank::before {
|
||||||
|
color: var(--text-secondary);
|
||||||
|
font-style: italic;
|
||||||
|
left: var(--spacing-md);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Styles des éléments dans l'éditeur */
|
||||||
|
.ql-editor h1, .ql-editor h2, .ql-editor h3 {
|
||||||
|
color: var(--text-primary);
|
||||||
|
margin: 1em 0 0.5em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ql-editor blockquote {
|
||||||
|
border-left: 4px solid var(--accent-primary);
|
||||||
|
margin: 1.5em 0;
|
||||||
|
padding-left: 1em;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ql-editor pre.ql-syntax {
|
||||||
|
background: var(--bg-secondary);
|
||||||
|
color: var(--text-primary);
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
padding: var(--spacing-md);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Icônes de la barre d'outils */
|
||||||
.ql-snow .ql-stroke {
|
.ql-snow .ql-stroke {
|
||||||
stroke: var(--text-secondary) !important;
|
stroke: var(--text-secondary);
|
||||||
}
|
}
|
||||||
|
|
||||||
.ql-snow .ql-fill {
|
.ql-snow .ql-fill {
|
||||||
fill: var(--text-secondary) !important;
|
fill: var(--text-secondary);
|
||||||
}
|
}
|
||||||
|
|
||||||
.ql-snow .ql-picker {
|
.ql-snow .ql-picker {
|
||||||
color: var(--text-secondary) !important;
|
color: var(--text-secondary);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Menu déroulant */
|
.ql-snow.ql-toolbar button {
|
||||||
.ql-snow .ql-picker-options {
|
padding: 4px 6px;
|
||||||
background-color: var(--bg-secondary) !important;
|
height: 28px;
|
||||||
border-color: var(--border-color) !important;
|
width: 28px;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* États au survol */
|
/* États au survol et actif */
|
||||||
.ql-snow .ql-toolbar button:hover,
|
.ql-snow .ql-toolbar button:hover,
|
||||||
.ql-snow .ql-toolbar button.ql-active {
|
.ql-snow .ql-toolbar button:focus,
|
||||||
.ql-stroke {
|
.ql-snow .ql-toolbar button.ql-active,
|
||||||
stroke: var(--accent-primary) !important;
|
.ql-snow .ql-toolbar .ql-picker-label:hover,
|
||||||
}
|
.ql-snow .ql-toolbar .ql-picker-label.ql-active,
|
||||||
.ql-fill {
|
.ql-snow .ql-toolbar .ql-picker-item:hover,
|
||||||
fill: var(--accent-primary) !important;
|
.ql-snow .ql-toolbar .ql-picker-item.ql-selected {
|
||||||
}
|
color: var(--accent-primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ql-snow .ql-toolbar button:hover .ql-stroke,
|
||||||
|
.ql-snow .ql-toolbar button:focus .ql-stroke,
|
||||||
|
.ql-snow .ql-toolbar button.ql-active .ql-stroke,
|
||||||
|
.ql-snow .ql-toolbar .ql-picker-label:hover .ql-stroke,
|
||||||
|
.ql-snow .ql-toolbar .ql-picker-label.ql-active .ql-stroke {
|
||||||
|
stroke: var(--accent-primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ql-snow .ql-toolbar button:hover .ql-fill,
|
||||||
|
.ql-snow .ql-toolbar button:focus .ql-fill,
|
||||||
|
.ql-snow .ql-toolbar button.ql-active .ql-fill,
|
||||||
|
.ql-snow .ql-toolbar .ql-picker-label:hover .ql-fill,
|
||||||
|
.ql-snow .ql-toolbar .ql-picker-label.ql-active .ql-fill {
|
||||||
|
fill: var(--accent-primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Menus déroulants */
|
||||||
|
.ql-snow .ql-picker-options {
|
||||||
|
background-color: var(--bg-secondary);
|
||||||
|
border-color: var(--border-color);
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
padding: var(--spacing-sm);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ql-snow .ql-picker-options .ql-picker-item {
|
||||||
|
color: var(--text-primary);
|
||||||
|
padding: 4px 8px;
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ql-snow .ql-picker-options .ql-picker-item:hover {
|
||||||
|
color: var(--accent-primary);
|
||||||
|
background-color: var(--bg-tertiary);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Tooltip pour les liens */
|
||||||
|
.ql-snow .ql-tooltip {
|
||||||
|
background-color: var(--bg-secondary);
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
|
||||||
|
color: var(--text-primary);
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
padding: var(--spacing-sm);
|
||||||
|
z-index: 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ql-snow .ql-tooltip input[type=text] {
|
||||||
|
background-color: var(--input-bg);
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
color: var(--text-primary);
|
||||||
|
padding: 4px 8px;
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
width: 170px;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ql-snow .ql-tooltip input[type=text]:focus {
|
||||||
|
border-color: var(--accent-primary);
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ql-snow .ql-tooltip a.ql-action,
|
||||||
|
.ql-snow .ql-tooltip a.ql-remove {
|
||||||
|
color: var(--accent-primary);
|
||||||
|
padding: 4px 8px;
|
||||||
|
margin: 0 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
display: inline-block;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ql-snow .ql-tooltip a.ql-action:hover,
|
||||||
|
.ql-snow .ql-tooltip a.ql-remove:hover {
|
||||||
|
color: var(--accent-secondary);
|
||||||
|
background-color: var(--bg-tertiary);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Styles des couleurs */
|
||||||
|
.ql-snow .ql-color-picker .ql-picker-options {
|
||||||
|
padding: 5px;
|
||||||
|
width: 152px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ql-snow .ql-color-picker .ql-picker-item {
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
margin: 2px;
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Responsive */
|
/* Responsive */
|
||||||
@media (max-width: 768px) {
|
@media (max-width: 768px) {
|
||||||
.ql-container {
|
.ql-snow.ql-toolbar {
|
||||||
|
padding: var(--spacing-xs);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ql-container.ql-snow {
|
||||||
min-height: 200px;
|
min-height: 200px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.ql-snow .ql-tooltip {
|
||||||
|
left: 0 !important;
|
||||||
|
top: 100% !important;
|
||||||
|
width: 100%;
|
||||||
|
position: fixed;
|
||||||
|
transform: none !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ql-snow .ql-tooltip input[type=text] {
|
||||||
|
width: 100%;
|
||||||
|
margin: 4px 0;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,33 +1,66 @@
|
|||||||
document.addEventListener('DOMContentLoaded', function() {
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
// Initialisation de l'éditeur Quill
|
let currentChapterId = null;
|
||||||
|
const storyId = document.querySelector('input[name="id"]')?.value;
|
||||||
|
|
||||||
|
// Initialisation de l'éditeur Quill avec toutes les options
|
||||||
const quill = new Quill('#editor', {
|
const quill = new Quill('#editor', {
|
||||||
theme: 'snow',
|
theme: 'snow',
|
||||||
modules: {
|
modules: {
|
||||||
toolbar: [
|
toolbar: [
|
||||||
['bold', 'italic', 'underline'],
|
[{ 'header': [1, 2, 3, false] }],
|
||||||
|
['bold', 'italic', 'underline', 'strike'],
|
||||||
|
[{ 'color': [] }, { 'background': [] }],
|
||||||
|
[{ 'font': [] }],
|
||||||
|
[{ 'align': [] }],
|
||||||
|
['blockquote', 'code-block'],
|
||||||
[{ 'list': 'ordered'}, { 'list': 'bullet' }],
|
[{ 'list': 'ordered'}, { 'list': 'bullet' }],
|
||||||
|
[{ 'script': 'sub'}, { 'script': 'super' }],
|
||||||
|
[{ 'indent': '-1'}, { 'indent': '+1' }],
|
||||||
|
[{ 'direction': 'rtl' }],
|
||||||
|
['link', 'image', 'video'],
|
||||||
['clean']
|
['clean']
|
||||||
]
|
],
|
||||||
}
|
keyboard: {
|
||||||
|
bindings: {
|
||||||
|
tab: false,
|
||||||
|
'indent backwards': false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
placeholder: 'Commencez à écrire votre chapitre ici...',
|
||||||
|
formats: [
|
||||||
|
'header',
|
||||||
|
'bold', 'italic', 'underline', 'strike',
|
||||||
|
'color', 'background',
|
||||||
|
'font',
|
||||||
|
'align',
|
||||||
|
'blockquote', 'code-block',
|
||||||
|
'list', 'bullet',
|
||||||
|
'script',
|
||||||
|
'indent',
|
||||||
|
'direction',
|
||||||
|
'link', 'image', 'video'
|
||||||
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
// Variables globales
|
// Gestion des chapitres
|
||||||
const chaptersList = document.getElementById('chaptersList');
|
|
||||||
const modal = document.getElementById('chapterEditor');
|
const modal = document.getElementById('chapterEditor');
|
||||||
const addChapterBtn = document.getElementById('addChapter');
|
const addChapterBtn = document.getElementById('addChapter');
|
||||||
let currentChapterId = null;
|
const saveChapterBtn = document.getElementById('saveChapter');
|
||||||
const storyId = document.querySelector('input[name="id"]')?.value;
|
const cancelEditBtn = document.getElementById('cancelEdit');
|
||||||
|
const chapterTitleInput = document.getElementById('chapterTitle');
|
||||||
|
const chaptersList = document.getElementById('chaptersList');
|
||||||
|
|
||||||
// Initialisation du tri par glisser-déposer
|
// Configuration de Sortable pour la réorganisation des chapitres
|
||||||
if (chaptersList) {
|
if (chaptersList) {
|
||||||
new Sortable(chaptersList, {
|
new Sortable(chaptersList, {
|
||||||
animation: 150,
|
animation: 150,
|
||||||
|
handle: '.chapter-number', // Utiliser le numéro comme poignée de drag
|
||||||
ghostClass: 'sortable-ghost',
|
ghostClass: 'sortable-ghost',
|
||||||
onEnd: async function() {
|
onEnd: async function(evt) {
|
||||||
// Récupérer le nouvel ordre des chapitres
|
|
||||||
const chapters = Array.from(chaptersList.children).map((item, index) => ({
|
const chapters = Array.from(chaptersList.children).map((item, index) => ({
|
||||||
id: item.dataset.id,
|
id: item.dataset.id,
|
||||||
order: index
|
position: index
|
||||||
}));
|
}));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -46,160 +79,186 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
throw new Error('Erreur lors de la réorganisation');
|
throw new Error('Erreur lors de la réorganisation');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mettre à jour les numéros affichés
|
// Mise à jour des numéros de chapitres
|
||||||
chapters.forEach((chapter, index) => {
|
chapters.forEach((chapter, index) => {
|
||||||
const element = document.querySelector(`[data-id="${chapter.id}"] .chapter-number`);
|
const element = document.querySelector(`[data-id="${chapter.id}"] .chapter-number`);
|
||||||
if (element) {
|
if (element) {
|
||||||
element.textContent = index + 1;
|
element.textContent = index + 1;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Erreur:', error);
|
console.error('Erreur:', error);
|
||||||
alert('Erreur lors de la réorganisation des chapitres');
|
showNotification('Erreur lors de la réorganisation des chapitres', 'error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gestion de l'ajout d'un nouveau chapitre
|
// Gestionnaires d'événements pour l'édition des chapitres
|
||||||
if (addChapterBtn) {
|
if (addChapterBtn) {
|
||||||
addChapterBtn.addEventListener('click', () => {
|
addChapterBtn.addEventListener('click', () => {
|
||||||
currentChapterId = null;
|
currentChapterId = null;
|
||||||
document.getElementById('chapterTitle').value = '';
|
chapterTitleInput.value = '';
|
||||||
quill.setContents([{ insert: '\n' }]);
|
quill.setContents([]);
|
||||||
modal.style.display = 'block';
|
modal.style.display = 'block';
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gestion de l'édition d'un chapitre
|
if (cancelEditBtn) {
|
||||||
chaptersList?.addEventListener('click', async (e) => {
|
cancelEditBtn.addEventListener('click', () => {
|
||||||
if (e.target.matches('.edit-chapter')) {
|
if (hasUnsavedChanges()) {
|
||||||
const chapterItem = e.target.closest('.chapter-item');
|
if (!confirm('Des modifications non sauvegardées seront perdues. Voulez-vous vraiment fermer ?')) {
|
||||||
currentChapterId = chapterItem.dataset.id;
|
return;
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch(`api/get-chapter.php?storyId=${storyId}&chapterId=${currentChapterId}`);
|
|
||||||
if (!response.ok) throw new Error('Erreur lors de la récupération du chapitre');
|
|
||||||
|
|
||||||
const chapter = await response.json();
|
|
||||||
document.getElementById('chapterTitle').value = chapter.title;
|
|
||||||
try {
|
|
||||||
if (typeof chapter.content === 'string') {
|
|
||||||
quill.root.innerHTML = chapter.content;
|
|
||||||
} else {
|
|
||||||
quill.setContents(chapter.content);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Erreur lors du chargement du contenu:', error);
|
|
||||||
quill.root.innerHTML = chapter.content;
|
|
||||||
}
|
}
|
||||||
modal.style.display = 'block';
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Erreur:', error);
|
|
||||||
alert('Erreur lors de la récupération du chapitre');
|
|
||||||
}
|
}
|
||||||
|
modal.style.display = 'none';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gestion des clics sur la liste des chapitres
|
||||||
|
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;
|
||||||
|
|
||||||
|
try {
|
||||||
|
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) {
|
||||||
|
// Si on a du HTML, on le charge directement
|
||||||
|
quill.root.innerHTML = chapter.html;
|
||||||
|
} else if (chapter.content) {
|
||||||
|
// Si on a du contenu au format Delta
|
||||||
|
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 || '');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Contenu vide par défaut
|
||||||
|
quill.setContents([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
modal.style.display = 'block';
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erreur détaillée:', error);
|
||||||
|
showNotification('Erreur lors du chargement du chapitre. Veuillez réessayer.', 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Système de notification
|
||||||
|
function showNotification(message, type = 'success') {
|
||||||
|
const notification = document.createElement('div');
|
||||||
|
notification.className = `notification ${type}`;
|
||||||
|
notification.textContent = message;
|
||||||
|
|
||||||
|
document.body.appendChild(notification);
|
||||||
|
|
||||||
|
// Animation d'entrée
|
||||||
|
setTimeout(() => {
|
||||||
|
notification.style.opacity = '1';
|
||||||
|
notification.style.transform = 'translateY(0)';
|
||||||
|
}, 10);
|
||||||
|
|
||||||
|
// Auto-suppression après 3 secondes
|
||||||
|
setTimeout(() => {
|
||||||
|
notification.style.opacity = '0';
|
||||||
|
notification.style.transform = 'translateY(-100%)';
|
||||||
|
setTimeout(() => notification.remove(), 300);
|
||||||
|
}, 3000);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vérification des changements non sauvegardés
|
||||||
|
function hasUnsavedChanges() {
|
||||||
|
if (!currentChapterId) {
|
||||||
|
return chapterTitleInput.value !== '' || quill.getLength() > 1;
|
||||||
}
|
}
|
||||||
});
|
// Pour les chapitres existants, il faudrait comparer avec le contenu original
|
||||||
|
return true; // Par défaut, on suppose qu'il y a des changements
|
||||||
|
}
|
||||||
|
|
||||||
// Gestion de la suppression d'un chapitre
|
// Sauvegarde d'un chapitre
|
||||||
chaptersList?.addEventListener('click', async (e) => {
|
if (saveChapterBtn) {
|
||||||
if (e.target.matches('.delete-chapter')) {
|
saveChapterBtn.addEventListener('click', async () => {
|
||||||
if (!confirm('Voulez-vous vraiment supprimer ce chapitre ?')) return;
|
const title = chapterTitleInput.value.trim();
|
||||||
|
if (!title) {
|
||||||
const chapterItem = e.target.closest('.chapter-item');
|
showNotification('Le titre est requis', 'error');
|
||||||
const chapterId = chapterItem.dataset.id;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('api/delete-chapter.php', {
|
const response = await fetch('api/save-chapter.php', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
storyId: storyId,
|
storyId: storyId,
|
||||||
chapterId: chapterId
|
chapterId: currentChapterId,
|
||||||
|
title: title,
|
||||||
|
content: JSON.stringify(quill.getContents())
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) throw new Error('Erreur lors de la suppression');
|
if (response.ok) {
|
||||||
|
showNotification('Chapitre sauvegardé avec succès');
|
||||||
chapterItem.remove();
|
setTimeout(() => location.reload(), 500);
|
||||||
|
} else {
|
||||||
// Mettre à jour les numéros des chapitres
|
throw new Error('Erreur lors de la sauvegarde');
|
||||||
document.querySelectorAll('.chapter-number').forEach((num, index) => {
|
}
|
||||||
num.textContent = index + 1;
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Erreur:', error);
|
console.error('Erreur:', error);
|
||||||
alert('Erreur lors de la suppression du chapitre');
|
showNotification('Erreur lors de la sauvegarde du chapitre', 'error');
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
// Gestion de la sauvegarde d'un chapitre
|
// Ajouter le style des notifications
|
||||||
document.getElementById('saveChapter')?.addEventListener('click', async () => {
|
const style = document.createElement('style');
|
||||||
const title = document.getElementById('chapterTitle').value;
|
style.textContent = `
|
||||||
if (!title.trim()) {
|
.notification {
|
||||||
alert('Le titre du chapitre est requis');
|
position: fixed;
|
||||||
return;
|
top: 20px;
|
||||||
|
right: 20px;
|
||||||
|
padding: 1rem 2rem;
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
background: white;
|
||||||
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||||
|
opacity: 0;
|
||||||
|
transform: translateY(-100%);
|
||||||
|
transition: opacity 0.3s ease, transform 0.3s ease;
|
||||||
|
z-index: 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
.notification.success {
|
||||||
const response = await fetch('api/save-chapter.php', {
|
background-color: var(--success-color);
|
||||||
method: 'POST',
|
color: white;
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
storyId: storyId,
|
|
||||||
chapterId: currentChapterId,
|
|
||||||
title: title,
|
|
||||||
content: quill.root.innerHTML
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) throw new Error('Erreur lors de la sauvegarde');
|
|
||||||
|
|
||||||
const result = await response.json();
|
|
||||||
|
|
||||||
if (!currentChapterId) {
|
|
||||||
// Nouveau chapitre - ajouter à la liste
|
|
||||||
const newChapter = document.createElement('div');
|
|
||||||
newChapter.className = 'chapter-item';
|
|
||||||
newChapter.dataset.id = result.chapterId;
|
|
||||||
newChapter.innerHTML = `
|
|
||||||
<span class="chapter-number">${document.querySelectorAll('.chapter-item').length + 1}</span>
|
|
||||||
<h3 class="chapter-title">${title}</h3>
|
|
||||||
<div class="chapter-actions">
|
|
||||||
<button type="button" class="button edit-chapter">Éditer</button>
|
|
||||||
<button type="button" class="button delete-chapter">Supprimer</button>
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
chaptersList.appendChild(newChapter);
|
|
||||||
} else {
|
|
||||||
// Mise à jour du titre dans la liste
|
|
||||||
const chapterItem = document.querySelector(`[data-id="${currentChapterId}"] .chapter-title`);
|
|
||||||
if (chapterItem) {
|
|
||||||
chapterItem.textContent = title;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
modal.style.display = 'none';
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Erreur:', error);
|
|
||||||
alert('Erreur lors de la sauvegarde du chapitre');
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
// Fermeture de la modale
|
.notification.error {
|
||||||
document.getElementById('cancelEdit')?.addEventListener('click', () => {
|
background-color: var(--error-color);
|
||||||
modal.style.display = 'none';
|
color: white;
|
||||||
});
|
|
||||||
|
|
||||||
// Fermeture de la modale en cliquant en dehors
|
|
||||||
modal?.addEventListener('click', (e) => {
|
|
||||||
if (e.target === modal) {
|
|
||||||
modal.style.display = 'none';
|
|
||||||
}
|
}
|
||||||
});
|
`;
|
||||||
|
document.head.appendChild(style);
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user