feat: centraliser gestion bookmark config thème dans helper ShaaritThemeConfig partagé pour éliminer duplications lors changements mode/thème, implémenter recherche multi-tags (shaarit_config + legacy themes) avec déduplication par ID, merge intelligent config existant avec partial updates, sérialisation saves concurrents via inflight promise, cleanup automatique duplicates avec deleteBookmark(), et refactoring syncModeToBookmark/syncThemeToBookmark pour déléguer à ShaaritThemeConfig.save
This commit is contained in:
+9
-69
@@ -654,77 +654,17 @@
|
||||
|
||||
async function syncThemeToBookmark(themeId) {
|
||||
try {
|
||||
// We fetch the current config bookmark if any
|
||||
const res = await fetch(shaarli.basePath + '/?searchtags=themes');
|
||||
const text = await res.text();
|
||||
const parser = new DOMParser();
|
||||
const doc = parser.parseFromString(text, 'text/html');
|
||||
|
||||
// Get current mode from localStorage
|
||||
const currentMode = localStorage.getItem('theme') || (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
|
||||
|
||||
let config = {
|
||||
version: 2,
|
||||
if (!window.ShaaritThemeConfig) {
|
||||
console.warn('ShaaritThemeConfig helper not available');
|
||||
return;
|
||||
}
|
||||
const currentMode = localStorage.getItem('theme')
|
||||
|| (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
|
||||
await window.ShaaritThemeConfig.save({
|
||||
themes: THEMES,
|
||||
default: themeId,
|
||||
mode: currentMode
|
||||
};
|
||||
let editUrl = null;
|
||||
|
||||
// Find existing config
|
||||
const linkCard = doc.querySelector('.linklist-item');
|
||||
if (linkCard) {
|
||||
const descEl = linkCard.querySelector('.link-description p');
|
||||
if (descEl) {
|
||||
try {
|
||||
const parsed = JSON.parse(descEl.textContent.trim());
|
||||
// Preserve existing themes list if it exists and is valid
|
||||
if (parsed.themes && Array.isArray(parsed.themes)) {
|
||||
config.themes = parsed.themes;
|
||||
}
|
||||
// Update default and mode
|
||||
config.default = themeId;
|
||||
config.mode = currentMode;
|
||||
} catch(e) {}
|
||||
}
|
||||
const editBtn = linkCard.querySelector('.link-action-edit');
|
||||
if (editBtn) editUrl = editBtn.getAttribute('href');
|
||||
}
|
||||
|
||||
// Prepare form data
|
||||
const formData = new FormData();
|
||||
formData.append('lf_title', 'themes');
|
||||
formData.append('lf_url', 'https://shaarit.app/config/themes');
|
||||
formData.append('lf_tags', 'themes');
|
||||
formData.append('lf_description', JSON.stringify(config));
|
||||
formData.append('lf_private', 'on'); // Private bookmark
|
||||
formData.append('save_edit', 'Save');
|
||||
|
||||
let token = '';
|
||||
|
||||
if (editUrl) {
|
||||
// Update existing
|
||||
const editRes = await fetch(editUrl);
|
||||
const editDoc = parser.parseFromString(await editRes.text(), 'text/html');
|
||||
const tokenEl = editDoc.querySelector('input[name="token"]');
|
||||
const idEl = editDoc.querySelector('input[name="lf_id"]');
|
||||
if (tokenEl) token = tokenEl.value;
|
||||
if (idEl) formData.append('lf_id', idEl.value);
|
||||
} else {
|
||||
// Create new
|
||||
const addRes = await fetch(shaarli.basePath + '/admin/add-shaare');
|
||||
const addDoc = parser.parseFromString(await addRes.text(), 'text/html');
|
||||
const tokenEl = addDoc.querySelector('input[name="token"]');
|
||||
if (tokenEl) token = tokenEl.value;
|
||||
}
|
||||
|
||||
if (token) {
|
||||
formData.append('token', token);
|
||||
await fetch(shaarli.basePath + '/admin/shaare', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
}
|
||||
mode: currentMode,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('Failed to sync theme to bookmark', e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user