damus.io

damus.io website
git clone git://jb55.com/damus.io
Log | Files | Refs | README

commit 7f54dbc23a444a2282bbcce00625067270ff1da7
parent 8dadb4fe69fdc5bb04549933123ae115af2a53a7
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 28 Oct 2022 09:03:22 -0700

Add post view

You can create new posts instead of just replies!

Diffstat:
Mwebv2/damus.css | 20+++++++++++++++++---
Mwebv2/damus.js | 52++++++++++++++++++++++++++++++++++++++++++++++------
Mwebv2/index.html | 8+++-----
3 files changed, 66 insertions(+), 14 deletions(-)

diff --git a/webv2/damus.css b/webv2/damus.css @@ -52,14 +52,28 @@ body { background: rgba(0,0,0,0.4); } -#reply-content { +textarea, input { background-color: rgba(255,255,255,0.2); border: 0; - width: 100%; + height: 40px; color: white; border-radius: 5px; } +#newpost { + padding: 20px; + margin: auto; + width: 70% +} + +#post-input { + width: 80%; +} + +textarea { + width: 100%; +} + button { border: 0; padding: 5px; @@ -100,7 +114,7 @@ html { } .container { margin: 0 auto; - max-width: 36em; + max-width: 48em; hyphens: auto; word-wrap: break-word; text-rendering: optimizeLegibility; diff --git a/webv2/damus.js b/webv2/damus.js @@ -68,7 +68,8 @@ async function damus_web_init() if (!model.pubkey) return model.pool = pool - model.el = document.querySelector("#posts") + model.view_el = document.querySelector("#view") + redraw_home_view(model) pool.on('open', (relay) => { //let authors = followers @@ -117,7 +118,7 @@ function handle_home_event(ids, model, relay, sub_id, ev) { if (model.realtime) { if (rerender_home_timer) clearTimeout(rerender_home_timer) - rerender_home_timer = setTimeout(render_home_view.bind(null, model), 200) + rerender_home_timer = setTimeout(redraw_events.bind(null, model), 500) } break; case ids.account: @@ -295,7 +296,8 @@ function handle_profiles_loaded(profiles_id, model, relay) { // stop asking for profiles model.pool.unsubscribe(profiles_id, relay) model.realtime = true - render_home_view(model) + + redraw_events(model) } function debounce(f, interval) { @@ -326,10 +328,49 @@ function handle_comments_loaded(profiles_id, model, relay) model.pool.subscribe(profiles_id, filter, relay) } -function render_home_view(model) { +function redraw_events(model) { log_debug("rendering home view") model.rendered = {} - model.el.innerHTML = render_events(model) + model.events_el.innerHTML = render_events(model) +} + +function redraw_home_view(model) { + model.view_el.innerHTML = render_home_view(model) + model.events_el = document.querySelector("#events") + if (model.events.length > 0) + redraw_events(model) + else + model.events_el.innerText = "Loading..." +} + +async function send_post() { + const input_el = document.querySelector("#post-input") + + const content = input_el.value + const created_at = Math.floor(new Date().getTime() / 1000) + const kind = 1 + const tags = [] + const pubkey = get_pubkey() + const privkey = get_privkey() + const {pool} = DSTATE + + let post = { pubkey, tags, content, created_at, kind } + + post.id = await nostrjs.calculate_id(post) + post.sig = await sign_id(privkey, post.id) + + pool.send(["EVENT", post]) +} + +function render_home_view(model) { + return ` + <div id="newpost"> + <input id="post-input" type="text"></input> + <button onclick="send_post(this)" id="post-button">Post</button> + </div> + <div id="events"> + </div> + ` } function render_events(model) { @@ -480,7 +521,6 @@ async function send_reply() { const privkey = get_privkey() let reply = await create_reply(privkey, pubkey, content, ev) - console.log(nostrjs.event_commitment(reply), reply) pool.send(["EVENT", reply]) close_reply() diff --git a/webv2/index.html b/webv2/index.html @@ -6,7 +6,7 @@ <title>Damus Web</title> - <link rel="stylesheet" href="damus.css?v=6"> + <link rel="stylesheet" href="damus.css?v=7"> </head> <body> <section class="header"> @@ -14,8 +14,7 @@ <img src="img/damus-nobg.svg"/> </span> </section> - <div class="container"> - <div id="posts"> + <div id="view" class="container"> </div> <div style="display: none" id="reply-modal"> @@ -39,11 +38,10 @@ </div> </div> </div> - </div> <script src="noble-secp256k1.js?v=1"></script> <script src="bech32.js?v=1"></script> <script src="nostr.js?v=3"></script> - <script src="damus.js?v=9"></script> + <script src="damus.js?v=10"></script> <script> const relay = damus_web_init() </script>