chore(icon_pack): map woff2 attachment

This commit is contained in:
Elian Doran 2025-12-26 17:28:19 +02:00
parent 183020a4e3
commit 5ad7323d03
No known key found for this signature in database
4 changed files with 72 additions and 11 deletions

View File

@ -1,15 +1,16 @@
"use strict";
import utils from "../../services/utils.js";
import dateUtils from "../../services/date_utils.js";
import AbstractBeccaEntity from "./abstract_becca_entity.js";
import sql from "../../services/sql.js";
import protectedSessionService from "../../services/protected_session.js";
import log from "../../services/log.js";
import type { AttachmentRow } from "@triliumnext/commons";
import type BNote from "./bnote.js";
import type BBranch from "./bbranch.js";
import dateUtils from "../../services/date_utils.js";
import log from "../../services/log.js";
import noteService from "../../services/notes.js";
import protectedSessionService from "../../services/protected_session.js";
import sql from "../../services/sql.js";
import utils from "../../services/utils.js";
import AbstractBeccaEntity from "./abstract_becca_entity.js";
import type BBranch from "./bbranch.js";
import type BNote from "./bnote.js";
const attachmentRoleToNoteTypeMapping = {
image: "image",

View File

@ -1,5 +1,5 @@
import { buildNote } from "../test/becca_easy_mocking";
import { IconPackManifest, processIconPack } from "./icon_packs";
import { determineBestFontAttachment, IconPackManifest, processIconPack } from "./icon_packs";
describe("Processing icon packs", () => {
it("doesn't crash if icon pack is incorrect type", () => {
@ -29,3 +29,20 @@ describe("Processing icon packs", () => {
});
});
});
describe("Mapping attachments", () => {
it("handles woff2", () => {
const iconPackNote = buildNote({
type: "text",
attachments: [
{
role: "file",
title: "Font",
mime: "font/woff2"
}
]
});
const attachment = determineBestFontAttachment(iconPackNote);
expect(attachment?.mime).toStrictEqual("font/woff2");
});
});

View File

@ -1,6 +1,11 @@
import type BAttachment from "../becca/entities/battachment";
import type BNote from "../becca/entities/bnote";
import log from "./log";
const PREFERRED_MIME_TYPE = [
"font/woff2"
];
export interface IconPackManifest {
name: string;
prefix: string;
@ -18,8 +23,23 @@ export function processIconPack(iconPackNote: BNote): ProcessResult | undefined
return;
}
return {
iconMappings: manifest.icons
};
}
export function determineBestFontAttachment(iconPackNote: BNote) {
// Map all the attachments by their MIME.
const mappings = new Map<string, BAttachment>();
for (const attachment of iconPackNote.getAttachmentsByRole("file")) {
mappings.set(attachment.mime, attachment);
}
// Return the icon formats in order of preference.
for (const preferredMimeType of PREFERRED_MIME_TYPE) {
const correspondingAttachment = mappings.get(preferredMimeType);
if (correspondingAttachment) return correspondingAttachment;
}
return null;
}

View File

@ -1,5 +1,6 @@
import { NoteType } from "@triliumnext/commons";
import BAttachment from "../becca/entities/battachment.js";
import BAttribute from "../becca/entities/battribute.js";
import BBranch from "../becca/entities/bbranch.js";
import BNote from "../becca/entities/bnote.js";
@ -15,6 +16,11 @@ interface NoteDefinition extends AttributeDefinitions, RelationDefinitions {
type?: NoteType;
mime?: string;
children?: NoteDefinition[];
attachments?: {
title: string;
role: string;
mime: string;
}[];
}
/**
@ -106,5 +112,22 @@ export function buildNote(noteDef: NoteDefinition) {
position++;
}
// Handle attachments.
if (noteDef.attachments) {
const allAttachments: BAttachment[] = [];
for (const { title, role, mime } of noteDef.attachments) {
const attachment = new BAttachment({
ownerId: note.noteId,
title,
role,
mime
});
allAttachments.push(attachment);
}
note.getAttachmentsByRole = (role) => allAttachments.filter(a => a.role === role);
}
return note;
}