UX-Überarbeitung, Signatur-Bausteine, QoL-Verbesserungen

- Neues Layout: Inline-Editor, aufklappbarer Import, ⚙-Tab
- Signatur Header/Footer Baustein-System (Footer pro Abteilung)
- Signatur-Quelle Dropdown (Eigene / = andere@)
- "Vorlage laden" mit Platzhalter-Ersetzung (Name, Email, Abteilung, Tel, Fax)
- "Signatur speichern" pusht automatisch zum Server
- Footer-Editor mit auto-load beim Aufklappen
- Abteilungswechsel synct Footer + Templates neu
- "Aktualisieren" Button = Pull + Push in einem Schritt
- Vorlagen: Checkbox "Für alle Abteilungen"
- Löschen vom Server für alle möglich
- Toolbar für Signaturen gleichwertig mit Vorlagen-Toolbar
- Base64 whitespace-Fix für Gitea API
- Offline-resilient (Cache-Fallback, graceful error handling)
This commit is contained in:
Kendrick Bollens
2026-04-20 22:46:54 +02:00
parent f87258498d
commit 78a6310424
5 changed files with 1117 additions and 536 deletions

View File

@@ -46,7 +46,8 @@ class GiteaClient {
}
static fromBase64(b64) {
return decodeURIComponent(escape(atob(b64)));
if (!b64) return '';
return decodeURIComponent(escape(atob(b64.replace(/\s/g, ''))));
}
encodePath(p) {
@@ -206,6 +207,28 @@ class SyncManager {
return { success: true, departments };
}
/**
* Lightweight check: get remote file SHAs without downloading content
*/
async checkRemoteShas() {
if (!this.isConfigured) throw new Error('Sync nicht konfiguriert');
if (!this.department) throw new Error('Keine Abteilung ausgewählt');
const remoteShas = {}; // "folder/filename" -> sha
const folders = [SHARED_FOLDER, this.department];
for (const folder of folders) {
const files = await this.client.listDir(folder);
for (const file of files) {
if (file.name.endsWith('.html')) {
remoteShas[`${folder}/${file.name}`] = file.sha;
}
}
}
return { success: true, remoteShas };
}
/**
* Pull templates from repo: department folder + shared folder
*/
@@ -328,93 +351,203 @@ class SyncManager {
return { success: true, pushed };
}
/**
* Pull signatures from repo signatures/ folder and apply to matching Thunderbird identities
* Filename = email address (e.g. info@hotel.de.html)
*/
/**
* Get the personal filename slug from author name
*/
get authorSlug() {
return SyncManager.toFilename(this.config.authorName || '');
}
static get FOOTER_SEPARATOR() {
return '<!-- SIG_FOOTER_START -->';
}
static extractHeader(fullSignature) {
const idx = fullSignature.indexOf(SyncManager.FOOTER_SEPARATOR);
if (idx === -1) return fullSignature;
return fullSignature.substring(0, idx).trim();
}
static combineSignature(header, footer) {
if (!footer) return header;
return header + '\n' + SyncManager.FOOTER_SEPARATOR + '\n' + footer;
}
/**
* Pull signatures from repo.
* For each identity: if personal sig enabled AND personal file exists → use it.
* Otherwise use the shared file (email.html).
* Load the signature header template from signatures/headers/_vorlage.html
*/
async loadSignatureTemplate() {
if (!this.isConfigured) throw new Error('Sync nicht konfiguriert');
const fileData = await this.client.getFile('signatures/headers/_vorlage.html');
if (!fileData || !fileData.content) {
throw new Error('Vorlage nicht gefunden unter signatures/headers/_vorlage.html');
}
const html = GiteaClient.fromBase64(fileData.content);
return { success: true, html };
}
/**
* Pull footer for current department.
* Tries signatures/footers/{department}.html first, falls back to signatures/footers/_default.html
*/
async pullFooter() {
let fileData = null;
// Try department-specific footer first
if (this.department) {
fileData = await this.client.getFile(`signatures/footers/${this.department}.html`);
}
// Fall back to default
if (!fileData || !fileData.content) {
fileData = await this.client.getFile('signatures/footers/_default.html');
}
// Legacy fallback: old single-file location
if (!fileData || !fileData.content) {
fileData = await this.client.getFile('signatures/_footer.html');
}
if (!fileData || !fileData.content) return '';
const footer = GiteaClient.fromBase64(fileData.content);
await browser.storage.local.set({ 'sig_footer_cache': footer });
return footer;
}
/**
* Load footer for editing (returns HTML)
*/
async loadFooter() {
if (!this.isConfigured) throw new Error('Sync nicht konfiguriert');
const footer = await this.pullFooter();
return { success: true, html: footer };
}
/**
* Push footer for current department to signatures/footers/{department}.html
*/
async pushFooter(html) {
if (!this.isConfigured) throw new Error('Sync nicht konfiguriert');
if (!this.config.authorName) throw new Error('Bitte Name eintragen');
if (!this.department) throw new Error('Keine Abteilung ausgewählt');
const filepath = `signatures/footers/${this.department}.html`;
const commitMsg = `Signatur-Footer ${this.department} - von ${this.config.authorName}`;
const existing = await this.client.getFile(filepath);
if (existing && existing.content) {
const existingContent = GiteaClient.fromBase64(existing.content);
if (existingContent !== html) {
await this.client.updateFile(filepath, html, existing.sha, commitMsg);
}
} else {
await this.client.createFile(filepath, html, commitMsg);
}
await browser.storage.local.set({ 'sig_footer_cache': html });
return { success: true };
}
/**
* Pull signatures from repo using header/footer baustein model.
* - Loads shared footer from signatures/_footer.html
* - Loads personal headers from signatures/headers/email.authorslug.html
* - Combines header + footer and applies to Thunderbird identity
* - Resolves "=other@" references in second pass
*/
async pullSignatures() {
if (!this.isConfigured) throw new Error('Sync nicht konfiguriert');
const files = await this.client.listDir('signatures');
if (files.length === 0) {
return { success: true, updated: 0 };
}
// Pull shared footer
const footer = await this.pullFooter();
// Build lookup: filename → file entry
const fileMap = {};
for (const f of files) {
if (f.name.endsWith('.html')) {
fileMap[f.name.toLowerCase()] = f;
// List header files
const headerFiles = await this.client.listDir('signatures/headers');
const headerMap = {};
for (const f of headerFiles) {
if (f.name.endsWith('.html') && !f.name.startsWith('_')) {
headerMap[f.name.toLowerCase()] = f;
}
}
// Get personal email list
const personalResult = await browser.storage.local.get('sig_personal_emails');
const personalEmails = new Set((personalResult.sig_personal_emails || []).map(e => e.toLowerCase()));
// Get source map
const sourceResult = await browser.storage.local.get('sig_source_map');
const sourceMap = sourceResult.sig_source_map || {};
// Get all Thunderbird identities
const accounts = await browser.accounts.list();
let updated = 0;
const loadedHeaders = {}; // email → header html
const allIdentityList = [];
// First pass: load headers for "own" identities
for (const account of accounts) {
const identities = await browser.identities.list(account.id);
for (const identity of identities) {
const email = identity.email.toLowerCase();
const isPersonal = personalEmails.has(email);
const source = sourceMap[email] || 'own';
allIdentityList.push({ id: identity.id, email });
if (source.startsWith('=')) continue;
// Try personal header: email.authorslug.html
let targetFile = null;
if (isPersonal && this.authorSlug) {
// Try personal file first: email.authorslug.html
if (this.authorSlug) {
const personalName = `${email}.${this.authorSlug}.html`;
targetFile = fileMap[personalName] || null;
targetFile = headerMap[personalName] || null;
}
// Fall back to shared file: email.html
if (!targetFile) {
targetFile = fileMap[`${email}.html`] || null;
}
if (!targetFile) continue;
if (!targetFile) continue; // No header file yet — user hasn't set up signature
const fileData = await this.client.getFile(targetFile.path);
if (!fileData) continue;
const signature = GiteaClient.fromBase64(fileData.content);
const header = GiteaClient.fromBase64(fileData.content);
loadedHeaders[email] = header;
const fullSig = SyncManager.combineSignature(header, footer);
await browser.identities.update(identity.id, {
signature: signature,
signature: fullSig,
signatureIsPlainText: false
});
updated++;
}
}
// Second pass: resolve "=other@" references
for (const { id, email } of allIdentityList) {
const source = sourceMap[email] || 'own';
if (!source.startsWith('=')) continue;
const srcEmail = source.substring(1).toLowerCase();
const srcHeader = loadedHeaders[srcEmail];
if (srcHeader !== undefined) {
const fullSig = SyncManager.combineSignature(srcHeader, footer);
await browser.identities.update(id, {
signature: fullSig,
signatureIsPlainText: false
});
loadedHeaders[email] = srcHeader;
updated++;
}
}
return { success: true, updated };
}
/**
* Push signatures to repo.
* Personal: saves as email.authorslug.html
* Shared: saves as email.html
* Push signature headers to repo.
* Only the header part is pushed as signatures/headers/email.authorslug.html
* Skips "=other@" identities.
*/
async pushSignatures() {
if (!this.isConfigured) throw new Error('Sync nicht konfiguriert');
if (!this.config.authorName) throw new Error('Bitte Name eintragen (für Commit-Zuordnung)');
if (!this.authorSlug) throw new Error('Name fehlt für Dateiname');
const personalResult = await browser.storage.local.get('sig_personal_emails');
const personalEmails = new Set((personalResult.sig_personal_emails || []).map(e => e.toLowerCase()));
const sourceResult = await browser.storage.local.get('sig_source_map');
const sourceMap = sourceResult.sig_source_map || {};
const accounts = await browser.accounts.list();
let pushed = 0;
@@ -425,29 +558,27 @@ class SyncManager {
if (!identity.signature) continue;
const email = identity.email.toLowerCase();
const isPersonal = personalEmails.has(email);
const source = sourceMap[email] || 'own';
if (source.startsWith('=')) continue;
let filename;
if (isPersonal && this.authorSlug) {
filename = `${email}.${this.authorSlug}.html`;
} else {
filename = `${email}.html`;
}
// Extract just the header from the full signature
const header = SyncManager.extractHeader(identity.signature);
if (!header.trim()) continue;
const filepath = `signatures/${filename}`;
const label = isPersonal ? `(persönlich)` : `(gemeinsam)`;
const commitMsg = `Signatur ${identity.email} ${label} - von ${this.config.authorName}`;
const filename = `${email}.${this.authorSlug}.html`;
const filepath = `signatures/headers/${filename}`;
const commitMsg = `Signatur-Header ${identity.email} - von ${this.config.authorName}`;
const existing = await this.client.getFile(filepath);
if (existing) {
const existingContent = GiteaClient.fromBase64(existing.content);
if (existingContent !== identity.signature) {
await this.client.updateFile(filepath, identity.signature, existing.sha, commitMsg);
if (existingContent !== header) {
await this.client.updateFile(filepath, header, existing.sha, commitMsg);
pushed++;
}
} else {
await this.client.createFile(filepath, identity.signature, commitMsg);
await this.client.createFile(filepath, header, commitMsg);
pushed++;
}
}
@@ -501,6 +632,22 @@ class SyncManager {
return { success: true };
}
/**
* Delete a template from the remote repo by its remote path
*/
async deleteRemoteTemplate(remotePath) {
if (!this.isConfigured) throw new Error('Sync nicht konfiguriert');
if (!this.config.authorName) throw new Error('Bitte Name eintragen (für Commit-Zuordnung)');
const fileData = await this.client.getFile(remotePath);
if (!fileData) throw new Error('Datei nicht im Repository gefunden');
const commitMsg = `${remotePath.split('/').pop()} gelöscht von ${this.config.authorName}`;
await this.client.deleteFile(remotePath, fileData.sha, commitMsg);
return { success: true };
}
async testConnection() {
if (!this.isConfigured) throw new Error('Sync nicht konfiguriert');
const repoInfo = await this.client.testConnection();
@@ -515,7 +662,7 @@ const syncManager = new SyncManager();
// ── Background message handler for sync ──
browser.runtime.onMessage.addListener(async (msg, sender) => {
const syncActions = ['testConnection', 'pullTemplates', 'pushTemplates', 'pullSingleTemplate', 'pushSingleTemplate', 'listDepartments', 'pullSignatures', 'pushSignatures'];
const syncActions = ['testConnection', 'pullTemplates', 'pushTemplates', 'pullSingleTemplate', 'pushSingleTemplate', 'deleteRemoteTemplate', 'listDepartments', 'checkRemoteShas', 'pullSignatures', 'pushSignatures', 'loadSignatureTemplate', 'loadFooter', 'pushFooter'];
if (!syncActions.includes(msg.action)) return;
try {
@@ -533,6 +680,9 @@ browser.runtime.onMessage.addListener(async (msg, sender) => {
case 'listDepartments':
return await syncManager.listDepartments();
case 'checkRemoteShas':
return await syncManager.checkRemoteShas();
case 'pullTemplates':
return await syncManager.pullTemplates();
@@ -545,12 +695,24 @@ browser.runtime.onMessage.addListener(async (msg, sender) => {
case 'pushSingleTemplate':
return await syncManager.pushSingleTemplate(msg.templateId);
case 'deleteRemoteTemplate':
return await syncManager.deleteRemoteTemplate(msg.remotePath);
case 'pullSignatures':
return await syncManager.pullSignatures();
case 'pushSignatures':
return await syncManager.pushSignatures();
case 'loadSignatureTemplate':
return await syncManager.loadSignatureTemplate();
case 'loadFooter':
return await syncManager.loadFooter();
case 'pushFooter':
return await syncManager.pushFooter(msg.html);
default:
return { success: false, error: 'Unbekannte Aktion' };
}