nostr-web-components
Version:
collection of web components that provide quick access to basic nostr things
19 lines (18 loc) • 3.47 kB
JavaScript
var q=Object.defineProperty;var C=(i,a,t)=>a in i?q(i,a,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[a]=t;var f=(i,a,t)=>C(i,typeof a!="symbol"?a+"":a,t);import{verifyEvent as w}from"@nostr/tools/pure";import{decodeNostrURI as A,npubEncode as P,neventEncode as m}from"@nostr/tools/nip19";import{normalizeURL as E}from"@nostr/tools/utils";import*as b from"@nostr/tools/nip10";import{b as g,p as c,f as x}from"./nostr.js";import{d as S,s as N,n as d,a as L}from"./utils.js";import{r as T}from"./text.js";import"@nostr/tools/pool";import"@nostr/gadgets/lists";import"@nostr/gadgets/global";import"@nostr/gadgets/metadata";import"@nostr/tools/nip05";import"@nostr/tools/nip27";class k extends HTMLElement{constructor(){super();f(this,"root");f(this,"set",S(async()=>{let t=null,p=this.getAttribute("event");if(p&&(t=JSON.parse(p),t&&!w(t)))return;if(!t){let e=this.getAttribute("ref");if(e){let{type:l,data:v}=A(e);if(l!=="nevent")return;let s=v,u=(s.relays||[]).concat(N(this.getAttribute("relays")).map(E)),y={ids:[s.id]};s.author&&u.length===0&&(u=await g(s.author)),t=await c.get(u,y),!t&&s.author&&(u=await g(s.author),t=await c.get(u,y))}}if(!t)return;let n=P(t.pubkey);for(let e of this.queryPart("author-link"))e.href=d(n),e.target="_blank";for(let e of this.queryPart("author-npub"))e.textContent=n;let r=null;for(let e of this.queryPart("author-name"))r||(r=await x(t.pubkey,[])),e.textContent=r.shortName;for(let e of this.queryPart("author-npub-short"))e.textContent=L(n);let o=null;for(let e of this.queryPart("parent-link"))o||(o=b.parse(t)),o.reply?(e.href=d(m(o.reply)),e.target="_blank",e.style.display=""):e.remove();for(let e of this.queryPart("root-link"))o||(o=b.parse(t)),o.root?(e.href=d(m(o.root)),e.target="_blank"):e.remove();for(let e of this.queryPart("link"))e.href=d(m({id:t.id,relays:Array.from(c.seenOn.get(t.id)||[]).slice(0,3).map(l=>l.url),author:t.pubkey})),e.target="_blank";for(let e of this.queryPart("date")){let l=new Date(t.created_at*1e3);e.textContent=l.toLocaleString(),e.tagName==="TIME"&&e.setAttribute("time",l.toISOString().substring(0,23))}let h=null;for(let e of this.queryPart("content"))h||(h=document.createElement("div"),e.appendChild(h)),T(h,t.content)},200));this.attachShadow({mode:"open"});const{shadowRoot:t}=this;this.root=document.createElement("div"),t.appendChild(this.root)}connectedCallback(){setTimeout(()=>{let t=this.getAttribute("template")?document.getElementById(this.getAttribute("template")):null;t?this.root.appendChild(t.content.cloneNode(!0)):this.root.innerHTML=`
<div part="container">
<header part="header">
<a part="author-link">
<span part="author-name"></span>
<span part="author-npub-short"></span>
</a>
<a part="parent-link" style="display: none">
<span part="parent">parent</span>
</a>
<a part="link">
<time part="date"></time>
</a>
</header>
<div part="content"></div>
</div>
`,this.set()},1)}*queryPart(t){let p=this.querySelectorAll(`[part="${t}"]`);for(let r=0;r<p.length;r++)yield p[r];let n=this.root.querySelectorAll(`[part="${t}"]`);for(let r=0;r<n.length;r++)yield n[r]}attributeChangedCallback(){this.set()}}f(k,"observedAttributes",["ref","event","relays","template"]);window.customElements.define("nostr-note",k);
//# sourceMappingURL=nostr-note.js.map