From deedbae563713742e6eb93762e88704077979953 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Sat, 30 Dec 2023 22:10:40 -0800 Subject: [PATCH] wip blocks renderer --- src/render.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/src/render.rs b/src/render.rs index b925cfa..0ebd049 100644 --- a/src/render.rs +++ b/src/render.rs @@ -7,7 +7,7 @@ use egui::{ use log::{debug, info, warn}; use nostr_sdk::nips::nip19::Nip19; use nostr_sdk::prelude::*; -use nostrdb::{Note, Transaction}; +use nostrdb::{BlockType, Blocks, Note, Transaction}; use std::f32::consts::PI; impl ProfileRenderData { @@ -23,6 +23,7 @@ impl ProfileRenderData { #[derive(Debug, Clone)] pub struct NoteData { + pub id: Option<[u8; 32]>, pub content: String, } @@ -90,7 +91,7 @@ impl From for EventSource { impl NoteData { fn default() -> Self { let content = "".to_string(); - NoteData { content } + NoteData { content, id: None } } } @@ -192,12 +193,16 @@ fn get_profile_render_data( fn ndb_note_to_data(note: &Note) -> NoteData { let content = note.content().to_string(); - NoteData { content } + let id = Some(*note.id()); + NoteData { content, id } } fn sdk_note_to_note_data(note: &Event) -> NoteData { let content = note.content.clone(); - NoteData { content } + NoteData { + content, + id: Some(note.id.to_bytes()), + } } fn get_note_render_data( @@ -341,6 +346,31 @@ fn note_frame_align() -> egui::Layout { } } +fn note_blocks_ui(ui: &mut egui::Ui, note: &Note, blocks: &Blocks) { + let size = 30.0; + ui.horizontal_wrapped(|ui| { + for block in blocks.iter(note) { + match block.blocktype() { + BlockType::Text => ui.label(RichText::new(block.as_str()).size(30.0)), + + BlockType::Url => ui.label( + RichText::new(block.as_str()) + .color(Color32::BLUE) + .size(size), + ), + + BlockType::Hashtag => ui.label( + RichText::new(format!("#{}", block.as_str())) + .color(Color32::RED) + .size(size), + ), + + _ => ui.label(block.as_str()), + }; + } + }); +} + fn note_ui(app: &Notecrumbs, ctx: &egui::Context, note: &NoteRenderData) { setup_visuals(&app.font_data, ctx); @@ -395,12 +425,29 @@ fn note_ui(app: &Notecrumbs, ctx: &egui::Context, note: &NoteRenderData) { }); }); - ui.vertical(|ui| { + ui.horizontal_wrapped(|ui| { let desired = Vec2::new(desired_width, desired_height / 2.2); ui.set_max_size(desired); ui.set_min_size(desired); - // only one widget is allowed in here - wrapped_body(ui, ¬e.note.content); + + let mut rendered = false; + + let ok = (|| -> Result<(), nostrdb::Error> { + let txn = Transaction::new(&app.ndb)?; + let note_id = note.note.id.ok_or(nostrdb::Error::NotFound)?; + let note = app.ndb.get_note_by_id(&txn, ¬e_id)?; + let blocks = + app.ndb.get_blocks_by_key(&txn, note.key().unwrap())?; + + note_blocks_ui(ui, ¬e, &blocks); + + Ok(()) + })(); + + if let Err(_) = ok { + ui.label(¬e.note.content); + } + //wrapped_body(ui, ¬e.note.content); }); ui.horizontal(|ui| {