import { CustomHTMLElement } from "./lib/custom_html_element.mjs"; export class N2File extends CustomHTMLElement { static { this.tmpl = document.createElement('template') this.tmpl.innerHTML = `
` } constructor() { super(true) this.addEventListener('click', event => { event.preventDefault() event.stopPropagation() window.open( URL.createObjectURL(this.file), (event.ctrlKey || event.shiftKey) ? '_blank' : '_self', ) }) this.render() } async render() { const src = this.getAttribute('src') // N2's db:// URLs are fetched from IndexedDB. if (src.toLowerCase().match('^db://[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$')) { // image population has to happen asynchronously, // while Marked lib has to be returned a string when exiting this function. // populateImg makes sure this returned img element exists and then populates it // with the image from IndexedDB. const file = await globalThis.nodeStore.files.get(src.slice(5)) if (!file) return this.file = file.file if (file.file.type.startsWith('image/')) this.elImage.src = URL.createObjectURL(file.file) else { // Check for and use an existing MIME type icon. // Place them in static/images/file_icons/ and replace the slash with an underscore. const url = `/images/${_VERSION}/file_icons/${file.file.type.replaceAll('/', '_')}.svg` const res = await fetch(url) if (res.ok) this.elImage.src = url this.elFilename.innerText = file.file.name this.elFilename.style.display = 'block' } } else this.elImage.src = src } } customElements.define('n2-file', N2File)