From f600dab01e20cd0f287296698b29cead30146b89 Mon Sep 17 00:00:00 2001 From: JuliBot Date: Mon, 18 May 2026 23:23:10 +0000 Subject: [PATCH] fix: move export serialization to template-store.js Adds exportTemplates() to modules/template-store.js, consolidating the export payload schema (version, schemaVersion, exportedAt, templates) and field sanitization into the service layer. handleExport() in options.js now delegates to exportTemplates() and only handles DOM/download logic. Fixes JuliaKalder/TemplateWing#127 --- modules/template-store.js | 23 +++++++++++++++++++++++ options/options.js | 17 +++-------------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/modules/template-store.js b/modules/template-store.js index cd32b57..380d5c2 100644 --- a/modules/template-store.js +++ b/modules/template-store.js @@ -219,6 +219,29 @@ export async function consumePrefillTemplate() { export { PREFILL_KEY }; +// ---- Export ---- + +/** Serialise all templates into an export payload string. Strips internal-only fields. */ +export async function exportTemplates() { + const templates = await getTemplates(); + const safeTemplates = templates.map( + ({ id, usageCount, lastUsedAt, createdAt, updatedAt, ...t }) => ({ + ...t, + attachments: (t.attachments || []).map(({ data: _data, ...rest }) => rest), + }) + ); + return JSON.stringify( + { + version: EXPORT_FORMAT_VERSION, + schemaVersion: CURRENT_SCHEMA, + exportedAt: new Date().toISOString(), + templates: safeTemplates, + }, + null, + 2 + ); +} + // ---- Sorting ---- /** Sort templates by category then by name (case-insensitive). */ diff --git a/options/options.js b/options/options.js index f84e8a5..cc74244 100644 --- a/options/options.js +++ b/options/options.js @@ -6,8 +6,8 @@ import { getCategories, generateId, consumePrefillTemplate, + exportTemplates, PREFILL_KEY, - EXPORT_FORMAT_VERSION, INSERT_MODES, } from "../modules/template-store.js"; import { @@ -654,19 +654,8 @@ async function handleSave() { } async function handleExport() { - const templates = await getTemplates(); - const safeTemplates = templates.map( - ({ id, usageCount, lastUsedAt, createdAt, updatedAt, ...t }) => ({ - ...t, - attachments: (t.attachments || []).map(({ data: _data, ...rest }) => rest), - }) - ); - const payload = { - version: EXPORT_FORMAT_VERSION, - exportedAt: new Date().toISOString(), - templates: safeTemplates, - }; - const blob = new Blob([JSON.stringify(payload, null, 2)], { type: "application/json" }); + const json = await exportTemplates(); + const blob = new Blob([json], { type: "application/json" }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url;