Smart-Sync, Scope-Fix, Auto-Refresh, defaults.local.json

- Background-Sync: SHA-Check alle 5s, voller Pull nur bei Änderung
- Sync-Hashes werden nach Pull im Storage geschrieben → grüne Dots
- UI refreshed automatisch bei Background-Sync (storage.onChanged)
- Scope-Badge-Bug gefixt (folderToScope normalisiert den Vergleich)
- defaults.local.json: optionale vorkonfigurierte Verbindungsdaten
- README: Doku für defaults.local.json und XPI-Build mit/ohne Defaults
This commit is contained in:
Kendrick Bollens
2026-05-07 10:50:36 +02:00
parent 533d5a34f2
commit bc82e33bf2
6 changed files with 118 additions and 38 deletions

View File

@@ -786,6 +786,12 @@ async function handlePushSingle(e) {
// ── Inline Scope Change ──
function folderToScope(folder) {
if (folder === '_gemeinsam') return 'shared';
if (folder?.startsWith('_benutzer/')) return 'private';
return 'department';
}
async function handleScopeChange(e) {
const id = e.target.dataset.id;
const newScope = e.target.value;
@@ -793,36 +799,26 @@ async function handleScopeChange(e) {
const template = templates.find(t => t.id === id);
if (!template) return;
const oldScope = folderToScope(template.folder);
if (oldScope === newScope) return;
const authorEmail = await getAuthorEmail();
const oldFolder = template.folder;
let newFolder;
if (newScope === 'shared') newFolder = '_gemeinsam';
else if (newScope === 'private') newFolder = `_benutzer/${authorEmail}`;
else newFolder = undefined;
if (oldFolder === newFolder) return;
if (newScope === 'private' && !authorEmail) {
showToast('Bitte E-Mail in den Einstellungen eintragen.', 'error');
// Reset
e.target.value = oldFolder === '_gemeinsam' ? 'shared'
: oldFolder?.startsWith('_benutzer/') ? 'private' : 'department';
e.target.value = oldScope;
return;
}
// Warn on downgrade
const isDowngrade = (oldFolder === '_gemeinsam' && newFolder !== '_gemeinsam') ||
(!oldFolder?.startsWith('_benutzer/') && newFolder?.startsWith('_benutzer/'));
if (isDowngrade && template.remotePath) {
const scopeRank = { shared: 2, department: 1, private: 0 };
if (scopeRank[newScope] < scopeRank[oldScope] && template.remotePath) {
const confirmed = await showConfirmDialog(
'Sichtbarkeit verringern?',
'Die Vorlage wird am bisherigen Ort gelöscht. Andere Nutzer verlieren ggf. den Zugriff.',
'Trotzdem ändern'
);
if (!confirmed) {
e.target.value = oldFolder === '_gemeinsam' ? 'shared'
: oldFolder?.startsWith('_benutzer/') ? 'private' : 'department';
e.target.value = oldScope;
return;
}
}
@@ -838,13 +834,18 @@ async function handleScopeChange(e) {
template.remotePath = undefined;
}
template.folder = newFolder;
// Set new folder
if (newScope === 'shared') template.folder = '_gemeinsam';
else if (newScope === 'private') template.folder = `_benutzer/${authorEmail}`;
else template.folder = undefined;
await saveTemplates(templates);
// Auto-push to new location
try {
await browser.runtime.sendMessage({ action: 'pushTemplates' });
templates = await getTemplates();
storeTplHashes(templates);
} catch (_) {}
renderTemplates(templates);
@@ -1388,10 +1389,15 @@ async function loadSyncConfig() {
const result = await browser.storage.local.get(SYNC_CONFIG_KEY);
let config = result[SYNC_CONFIG_KEY];
// First launch: apply defaults from defaults.local.js if available
if (!config && typeof DEFAULT_SYNC_CONFIG !== 'undefined') {
config = { ...DEFAULT_SYNC_CONFIG };
await browser.storage.local.set({ [SYNC_CONFIG_KEY]: config });
// First launch: load defaults from defaults.local.json if available
if (!config) {
try {
const res = await fetch(browser.runtime.getURL('defaults.local.json'));
if (res.ok) {
config = await res.json();
await browser.storage.local.set({ [SYNC_CONFIG_KEY]: config });
}
} catch (_) {}
}
if (config) {
@@ -1670,4 +1676,15 @@ window.addEventListener('load', async () => {
checkForServerUpdates();
setInterval(checkForServerUpdates, 30000);
// Auto-refresh UI when background sync updates hashes (fires after templates + hashes are both written)
browser.storage.onChanged.addListener(async (changes, area) => {
if (area !== 'local') return;
if (changes[HASH_STORAGE_KEY]) {
await loadSyncHashes();
const templates = await getTemplates();
renderTemplates(templates);
updateTplSyncIndicator();
}
});
});