Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions modules/template-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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). */
Expand Down
17 changes: 3 additions & 14 deletions options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
getCategories,
generateId,
consumePrefillTemplate,
exportTemplates,
PREFILL_KEY,
EXPORT_FORMAT_VERSION,
INSERT_MODES,
} from "../modules/template-store.js";
import {
Expand Down Expand Up @@ -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;
Expand Down
Loading