This commit is contained in:
Tykayn 2025-03-04 22:59:49 +01:00 committed by tykayn
parent 9f1b265a21
commit 499ec2154a
5 changed files with 102 additions and 39 deletions

View file

@ -19,6 +19,8 @@ const editor = CodeMirror.fromTextArea(document.getElementById('editor-content')
}
});
let fileToSave = 'livre';
// Gestion du thème
const themeSwitch = document.getElementById('theme-switch');
const htmlElement = document.documentElement;
@ -368,6 +370,14 @@ function orgToHtml(text) {
return `<a href="#" class="character-link" data-type="character" data-name="${name}">${name}</a>`;
} else if (type === 'plot') {
return `<a href="#" class="plot-link" data-type="plot" data-name="${name}">${name}</a>`;
} else if (content.startsWith('http')) {
// Si c'est une URL, on vérifie si c'est une image
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg'];
if (imageExtensions.some(ext => content.toLowerCase().endsWith(ext))) {
return `<img src="${content}" alt="Image" class="org-image" style="max-width: 100%; height: auto;">`;
}
// Sinon c'est un lien normal
return `<a href="${content}" target="_blank">${content}</a>`;
}
return match;
});
@ -531,6 +541,25 @@ if (enable_auto_update) {
let body = `content=${encodeURIComponent(content)}&editor_title=${encodeURIComponent(editorTitle)}`;
// Déterminer le bon endpoint en fonction du titre
// Utiliser le type de fichier actuel pour déterminer l'endpoint
if (currentFileType === 'character') {
const name = editorTitle.split(':')[1].trim();
endpoint = '/update_character';
body = `name=${encodeURIComponent(name)}&content=${encodeURIComponent(content)}`;
} else if (currentFileType === 'plot') {
const name = editorTitle.split(':')[1].trim();
endpoint = '/update_plot';
body = `name=${encodeURIComponent(name)}&content=${encodeURIComponent(content)}`;
} else if (currentFileType === 'characters_file') {
endpoint = '/update_characters_file';
body = `content=${encodeURIComponent(content)}`;
} else if (currentFileType === 'plots_file') {
endpoint = '/update_plots_file';
body = `content=${encodeURIComponent(content)}`;
} else if (currentFileType === 'book') {
endpoint = '/update';
body = `content=${encodeURIComponent(content)}&editor_title=${encodeURIComponent(editorTitle)}`;
}
if (editorTitle.startsWith('Éditeur de Personnage:')) {
const name = editorTitle.split(':')[1].trim();
endpoint = '/update_character';
@ -541,8 +570,10 @@ if (enable_auto_update) {
body = `name=${encodeURIComponent(name)}&content=${encodeURIComponent(content)}`;
} else if (editorTitle === 'Personnages') {
endpoint = '/update_characters_file';
body = `content=${encodeURIComponent(content)}`;
} else if (editorTitle === 'Intrigues') {
endpoint = '/update_plots_file';
body = `content=${encodeURIComponent(content)}`;
}
try {
@ -571,17 +602,37 @@ if (enable_auto_update) {
document.getElementById('update-btn').addEventListener('click', async () => {
const content = editor.getValue();
const editorTitle = document.querySelector('.editor h2').textContent;
let endpoint = '/update';
let body = `content=${encodeURIComponent(content)}&editor_title=${encodeURIComponent(editorTitle)}`;
// Déterminer le bon endpoint en fonction du titre
if (editorTitle.startsWith('Éditeur de Personnage:')) {
const name = editorTitle.split(':')[1].trim();
endpoint = '/update_character';
body = `name=${encodeURIComponent(name)}&content=${encodeURIComponent(content)}`;
} else if (editorTitle.startsWith('Éditeur d\'Intrigue:')) {
const name = editorTitle.split(':')[1].trim();
endpoint = '/update_plot';
body = `name=${encodeURIComponent(name)}&content=${encodeURIComponent(content)}`;
} else if (editorTitle === 'Personnages') {
endpoint = '/update_characters_file';
body = `content=${encodeURIComponent(content)}`;
} else if (editorTitle === 'Intrigues') {
endpoint = '/update_plots_file';
body = `content=${encodeURIComponent(content)}`;
}
try {
const response = await fetch('/update', {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `content=${encodeURIComponent(content)}&editor_title=${encodeURIComponent(editorTitle)}`
body: body
});
const data = await response.json();
if (response.ok) {
if (editorTitle === 'Livre') {
if (endpoint === '/update') {
document.getElementById('words-today').textContent = data.words_today;
updateProgress(data.words_today, parseInt(wordGoalInput.value));
}
@ -803,8 +854,9 @@ document.addEventListener('DOMContentLoaded', function () {
const data = await response.json();
editor.setValue(data.content);
// Mettre à jour le titre de l'éditeur
document.querySelector('.editor h2').textContent = `Éditeur de ${type === 'character' ? 'Personnage' : 'Intrigue'}: ${name}`;
updateSaveButtonText();
const editorTitle = `Éditeur de ${type === 'character' ? 'Personnage' : 'Intrigue'}: ${name}`;
document.querySelector('.editor h2').textContent = editorTitle;
updateSaveButtonText(editorTitle);
} else {
alert(`Erreur lors du chargement du ${type}`);
}
@ -881,7 +933,7 @@ document.addEventListener('DOMContentLoaded', function () {
bookLink.addEventListener('click', (e) => {
e.preventDefault();
loadBook();
updateSaveButtonText();
updateSaveButtonText('livre');
});
}
@ -891,7 +943,7 @@ document.addEventListener('DOMContentLoaded', function () {
charactersFileLink.addEventListener('click', (e) => {
e.preventDefault();
loadCharactersFile();
updateSaveButtonText();
updateSaveButtonText('personnages');
});
}
@ -901,7 +953,7 @@ document.addEventListener('DOMContentLoaded', function () {
plotsFileLink.addEventListener('click', (e) => {
e.preventDefault();
loadPlotsFile();
updateSaveButtonText();
updateSaveButtonText('intrigues');
});
}
@ -941,8 +993,7 @@ document.addEventListener('DOMContentLoaded', function () {
editor.setSize(null, 'calc(100vh - 100px)');
// Gérer le changement de thème
const themeToggle = document.getElementById('theme-toggle');
themeToggle.addEventListener('click', function () {
themeSwitch.addEventListener('change', function () {
const isDark = document.body.getAttribute('data-theme') === 'dark';
editor.setOption('theme', isDark ? 'monokai' : 'default');
});
@ -954,7 +1005,7 @@ document.addEventListener('DOMContentLoaded', function () {
if (editorTitle === 'Livre') {
updateWordCount();
}
updateSaveButtonText();
updateSaveButtonText(editorTitle);
});
// Gérer le focus
@ -1110,24 +1161,9 @@ function initializeEditor() {
}
// Fonction pour mettre à jour le texte du bouton de sauvegarde
function updateSaveButtonText() {
const editorTitle = document.querySelector('.editor h2').textContent;
function updateSaveButtonText(fileName) {
const updateBtn = document.getElementById('update-btn');
let fileText = '';
fileToSave = fileName;
updateBtn.textContent = `Sauvegarder ${fileName}.org`;
if (editorTitle.startsWith('Éditeur de Personnage:')) {
const name = editorTitle.split(':')[1].trim();
fileText = `Sauvegarder ${name}`;
} else if (editorTitle.startsWith('Éditeur d\'Intrigue:')) {
const name = editorTitle.split(':')[1].trim();
fileText = `Sauvegarder ${name}`;
} else if (editorTitle === 'Personnages') {
fileText = 'Sauvegarder personnages.org';
} else if (editorTitle === 'Intrigues') {
fileText = 'Sauvegarder intrigues.org';
} else {
fileText = 'Sauvegarder livre.org';
}
updateBtn.textContent = fileText;
}