From 6e64f44629496ff1813942a5e818c21a0e157c47 Mon Sep 17 00:00:00 2001 From: Lensors Date: Sun, 27 Apr 2025 09:43:24 +0200 Subject: [PATCH] Ajout_modifs_markdown --- Projet/.classpath | 1 + Projet/.project | 11 +++ Projet/src/main/java/projet/Bloc.java | 16 ++- Projet/src/main/java/projet/NouveauBloc.java | 6 +- Projet/src/main/java/projet/NouvellePage.java | 2 +- Projet/src/main/java/projet/Page.java | 5 +- .../src/main/java/projet/SupprimerBloc.java | 1 - .../src/main/java/projet/SupprimerPage.java | 1 - Projet/src/main/webapp/script.js | 98 +++++++++---------- 9 files changed, 70 insertions(+), 71 deletions(-) diff --git a/Projet/.classpath b/Projet/.classpath index be1e92f..b2da0f7 100644 --- a/Projet/.classpath +++ b/Projet/.classpath @@ -13,5 +13,6 @@ + diff --git a/Projet/.project b/Projet/.project index 08e98c5..43237c8 100644 --- a/Projet/.project +++ b/Projet/.project @@ -28,4 +28,15 @@ org.eclipse.jdt.core.javanature org.eclipse.wst.jsdt.core.jsNature + + + 1745735846816 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/Projet/src/main/java/projet/Bloc.java b/Projet/src/main/java/projet/Bloc.java index 290e9a0..8410a51 100644 --- a/Projet/src/main/java/projet/Bloc.java +++ b/Projet/src/main/java/projet/Bloc.java @@ -8,8 +8,6 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.time.LocalDate; -import java.util.HashMap; -import java.util.Map; public class Bloc extends ParamBD { private int id; @@ -18,7 +16,7 @@ public class Bloc extends ParamBD { private LocalDate dateCreation; private LocalDate dateModification; private int ordre; - private Map metadata; + private String metadata; public enum Type { TEXTE, @@ -32,14 +30,14 @@ public class Bloc extends ParamBD { } - public Bloc(int id, Type type, String contenu, int ordre) { + public Bloc(int id, Type type, String contenu, int ordre, String metadata) { this.id = id; this.type = type; this.contenu = contenu; this.ordre = ordre; this.dateCreation = LocalDate.now(); this.dateModification = LocalDate.now(); - metadata = new HashMap<>(); + this.metadata = metadata; } public int getId() { @@ -90,11 +88,11 @@ public class Bloc extends ParamBD { this.ordre = ordre; } - public Map getMetadata() { + public String getMetadata() { return metadata; } - public void setMetadata(Map metadata) { + public void setMetadata(String metadata) { this.metadata = metadata; } @@ -102,7 +100,7 @@ public class Bloc extends ParamBD { return "Bloc{id=" + id + ", type=" + type + ", contenu='" + contenu + "', dateCreation=" + dateCreation + ", dateModification=" + dateModification + ", ordre=" + ordre + "}"; } - protected static int ajouterBloc(int idU, int idP, String contenu, int ordre, Type type, LocalDate dl, Map metadata) { + protected static int ajouterBloc(int idU, int idP, String contenu, int ordre, Type type, LocalDate dl, String metadata) { int idGenere = -1 ; try { Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); @@ -117,7 +115,7 @@ public class Bloc extends ParamBD { pst.setInt(5, idP); pst.setInt(6, ordre); pst.setInt(7, idU); - pst.setString(8, metadata.toString()); + pst.setString(8, metadata); pst.executeUpdate(); ResultSet rs = pst.getGeneratedKeys(); diff --git a/Projet/src/main/java/projet/NouveauBloc.java b/Projet/src/main/java/projet/NouveauBloc.java index 6ea9a1a..58771d2 100644 --- a/Projet/src/main/java/projet/NouveauBloc.java +++ b/Projet/src/main/java/projet/NouveauBloc.java @@ -9,8 +9,6 @@ import jakarta.servlet.http.HttpSession; import java.io.IOException; import java.time.LocalDate; -import java.util.HashMap; -import java.util.Map; @WebServlet("/NouveauBloc") public class NouveauBloc extends HttpServlet { @@ -42,13 +40,12 @@ public class NouveauBloc extends HttpServlet { String typeStr = request.getParameter("type"); String ordreStr = request.getParameter("ordre"); String pageIdStr = request.getParameter("pageId"); - + String metadata = request.getParameter("metadata"); int ordre = Integer.parseInt(ordreStr); int pageId = Integer.parseInt(pageIdStr); Bloc.Type type = Bloc.Type.valueOf(typeStr); - Map metadata = new HashMap<>(); int idGenere = Bloc.ajouterBloc(u.getId(), pageId, contenu, ordre, type, LocalDate.now(), metadata); response.setContentType("application/json"); @@ -59,5 +56,4 @@ public class NouveauBloc extends HttpServlet { response.sendRedirect("/Projet/"); } } - } diff --git a/Projet/src/main/java/projet/NouvellePage.java b/Projet/src/main/java/projet/NouvellePage.java index 47aa579..861867f 100644 --- a/Projet/src/main/java/projet/NouvellePage.java +++ b/Projet/src/main/java/projet/NouvellePage.java @@ -38,7 +38,7 @@ public class NouvellePage extends HttpServlet { String titre = request.getParameter("titre"); - if (titre == null || titre.isEmpty()) { + if (titre == null || titre.trim().isEmpty()) { response.sendRedirect("AfficherPage"); } else { int id = Page.ajouterPage(u.getId(), titre, LocalDate.now()); diff --git a/Projet/src/main/java/projet/Page.java b/Projet/src/main/java/projet/Page.java index 072124b..d3edf9e 100644 --- a/Projet/src/main/java/projet/Page.java +++ b/Projet/src/main/java/projet/Page.java @@ -172,7 +172,7 @@ public class Page extends ParamBD { page.id = id; } if(titre != null) { - sql = " SELECT id, contenu, type, ordre" + sql = " SELECT id, contenu, type, ordre, metadata" + " FROM bloc" + " WHERE page_id=?" + " ORDER BY ordre" @@ -185,10 +185,11 @@ public class Page extends ParamBD { int idB = rs.getInt("id"); String contenu = rs.getString("contenu"); String type = rs.getString("type"); + String metadata = rs.getString("metadata"); Type typeEnum = Type.valueOf(type.toUpperCase()); int ordre = rs.getInt("ordre"); - Bloc bloc = new Bloc(idB, typeEnum, contenu, ordre); + Bloc bloc = new Bloc(idB, typeEnum, contenu, ordre, metadata); page.listeBlocs.add(bloc); } } diff --git a/Projet/src/main/java/projet/SupprimerBloc.java b/Projet/src/main/java/projet/SupprimerBloc.java index 232b0b9..7cd53e1 100644 --- a/Projet/src/main/java/projet/SupprimerBloc.java +++ b/Projet/src/main/java/projet/SupprimerBloc.java @@ -1,6 +1,5 @@ package projet; -import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; diff --git a/Projet/src/main/java/projet/SupprimerPage.java b/Projet/src/main/java/projet/SupprimerPage.java index 261e6fb..a676138 100644 --- a/Projet/src/main/java/projet/SupprimerPage.java +++ b/Projet/src/main/java/projet/SupprimerPage.java @@ -1,6 +1,5 @@ package projet; -import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; diff --git a/Projet/src/main/webapp/script.js b/Projet/src/main/webapp/script.js index e84205c..4dd9b46 100644 --- a/Projet/src/main/webapp/script.js +++ b/Projet/src/main/webapp/script.js @@ -1,6 +1,4 @@ - - // focus sur le dernier textarea quand la page s'ouvre window.addEventListener('DOMContentLoaded', () => { const blocs = document.querySelectorAll('#md [contenteditable="true"]'); if (blocs.length > 0) { @@ -33,7 +31,7 @@ ajouterBlocVideSiBesoin(); }); - // Fonction pour ajouter un nouvel événement à chaque textarea + // Fonction pour ajouter un nouvel événement à chaque bloc function addBlocEvent(bloc) { bloc.addEventListener('keydown', function(event) { @@ -47,48 +45,48 @@ if (texte.startsWith("/")) { // Gérer les commandes handleSlashCommand(currentBloc, texte); - } else { - const blocId = currentBloc.getAttribute('data-id'); // Récupère l'ID du bloc - const type = currentBloc.getAttribute('data-type'); - const metadata = currentBloc.getAttribute('metadata'); - const params = new URLSearchParams(); - - let contenuTexte = currentBloc.innerHTML - .replace(//gi, '\n') // Remplace les
par \n - .replace(//gi, '**') // Remplace par ** - .replace(/<\/b>/gi, '**') // Remplace par ** - .replace(//gi, '*') // Remplace par * - .replace(/<\/i>/gi, '*'); // Remplace par * - params.append("contenu", contenuTexte); - params.append("blocId", blocId); - params.append("metadata", metadata); - params.append("type", type); - - fetch("/Projet/ModifBloc", { - method: "POST", - headers: { - "Content-Type": "application/x-www-form-urlencoded" - }, - body: params - }).then(response => { - if (!response.ok) { - console.error("Erreur lors de la mise à jour du bloc."); - } - }); - - ajouterBlocVideSiBesoin(); } - } - }); + + const blocId = currentBloc.getAttribute('data-id'); // Récupère l'ID du bloc + const type = currentBloc.getAttribute('data-type'); + const metadata = currentBloc.getAttribute('metadata'); + const params = new URLSearchParams(); + + let contenuTexte = currentBloc.innerHTML + .replace(//gi, '\n') // Remplace les
par \n + .replace(//gi, '**') // Remplace par ** + .replace(/<\/b>/gi, '**') // Remplace par ** + .replace(//gi, '*') // Remplace par * + .replace(/<\/i>/gi, '*'); // Remplace par * + params.append("contenu", contenuTexte); + params.append("blocId", blocId); + params.append("metadata", metadata); + params.append("type", type); + + fetch("/Projet/ModifBloc", { + method: "POST", + headers: { + "Content-Type": "application/x-www-form-urlencoded" + }, + body: params + }).then(response => { + if (!response.ok) { + console.error("Erreur lors de la mise à jour du bloc."); + } + }); + + ajouterBlocVideSiBesoin(); + } + }); - bloc.addEventListener('input', function () { + bloc.addEventListener('input', function () { autoResize(bloc); - }); + }); } - function autoResize(textarea) { - textarea.style.height = 'auto'; - textarea.style.height = textarea.scrollHeight + 'px'; + function autoResize(bloc) { + bloc.style.height = 'auto'; + bloc.style.height = bloc.scrollHeight + 'px'; } document.querySelectorAll('#md [contenteditable="true"]').forEach(bloc => { @@ -137,7 +135,7 @@ newBloc.setAttribute('rows', '1'); newBloc.setAttribute('data-type', 'TEXTE'); newBloc.setAttribute('data-id', idGenere); - newBloc.setAttribute('metadata', 'TEXTE'); + newBloc.setAttribute('metadata', '{}'); control.appendChild(newBloc); container.appendChild(control); @@ -179,7 +177,7 @@ const bloc = blocContainer.querySelector('[contenteditable="true"]'); if (bloc.innerText.trim() === "") { - return; // Empêche la suppression si le textarea est vide + return; // Empêche la suppression si le bloc est vide } if (confirm("Voulez-vous vraiment supprimer ce bloc ?")) { @@ -285,13 +283,15 @@ bloc.addEventListener('input', (event) => { const blocId = event.target.getAttribute('data-id'); const content = event.target.textContent; // Récupère le contenu modifié du bloc + const type = event.target.getAttribute('data-type'); const metadata = event.target.getAttribute('metadata'); // Récupère le metadata du bloc const modif = { action: "update", blocId: blocId, content: content, - metadata: metadata + metadata: metadata, + type: type }; socketBloc.send(JSON.stringify(modif)); // Envoi de la modification via le WebSocket @@ -360,7 +360,6 @@ // Nettoyage des anciennes classes bloc.classList.remove('is-title', 'is-title-1', 'is-title-2', 'is-title-3'); bloc.classList.remove('is-code-block', 'is-list', 'is-toggle'); - bloc.setAttribute('placeholder', 'Tapez ici...'); bloc.style.whiteSpace = "normal"; // reset si code const type = bloc.dataset.type || 'TEXTE'; @@ -371,31 +370,26 @@ break; case 'TITRE': - const level = bloc.dataset.level || '2'; + const level = bloc.dataset.level || '1'; bloc.classList.add('is-title', `is-title-${level}`); - bloc.setAttribute('placeholder', `Titre niveau ${level}`); break; case 'LISTE': bloc.classList.add('is-list'); - bloc.setAttribute('placeholder', '• Élément de liste'); break; case 'CODE': bloc.classList.add('is-code-block'); bloc.style.whiteSpace = "pre"; - const lang = bloc.dataset.language || 'plaintext'; - bloc.setAttribute('placeholder', `Code (${lang})`); + bloc.dataset.language || 'plaintext'; break; case 'PAGE': bloc.classList.add('is-page-block'); - bloc.setAttribute('placeholder', 'Nouvelle page...'); break; case 'TOGGLE': bloc.classList.add('is-toggle'); - bloc.setAttribute('placeholder', 'Cliquez pour développer...'); break; default: @@ -421,7 +415,7 @@ case "h2": case "h3": applyBlocType(bloc, "TITRE", { level: parseInt(command[1] || "1") }); - bloc.textContent = ''; // On vide le bloc après transformation + bloc.textContent = param; // On vide le bloc après transformation autoResize(bloc); break;