taglib-wasm
Version:
TagLib-Wasm is the universal tagging library for TypeScript/JavaScript platforms: Browsers, Node.js, Deno, Bun, Cloudflare Workers, and Electron apps
54 lines (53 loc) • 1.85 kB
JavaScript
import { readPictures } from "../simple/index.js";
function ensureArrayBufferBacked(data) {
if (data.buffer instanceof ArrayBuffer) {
return data;
}
const copy = new Uint8Array(data.length);
copy.set(data);
return copy;
}
function displayPicture(picture, imgElement) {
if (imgElement.src.startsWith("blob:")) {
URL.revokeObjectURL(imgElement.src);
}
const data = ensureArrayBufferBacked(picture.data);
const blob = new Blob([data], { type: picture.mimeType });
const objectURL = URL.createObjectURL(blob);
imgElement.src = objectURL;
imgElement.addEventListener("load", () => {
setTimeout(() => URL.revokeObjectURL(objectURL), 100);
}, { once: true });
}
function createPictureDownloadURL(picture, filename = "cover") {
const data = ensureArrayBufferBacked(picture.data);
const blob = new Blob([data], { type: picture.mimeType });
return URL.createObjectURL(blob);
}
async function createPictureGallery(file, container, options = {}) {
const pictures = await readPictures(file);
container.innerHTML = "";
pictures.forEach((picture, index) => {
const wrapper = document.createElement("div");
wrapper.className = options.className ?? "picture-item";
const img = document.createElement("img");
displayPicture(picture, img);
img.alt = picture.description ?? `Picture ${index + 1}`;
if (options.onClick) {
img.style.cursor = "pointer";
img.addEventListener("click", () => options.onClick(picture, index));
}
wrapper.appendChild(img);
if (options.includeDescription && picture.description) {
const caption = document.createElement("p");
caption.textContent = picture.description;
wrapper.appendChild(caption);
}
container.appendChild(wrapper);
});
}
export {
createPictureDownloadURL,
createPictureGallery,
displayPicture
};