draft.rs (1800B)
1 use egui::text::LayoutJob; 2 use poll_promise::Promise; 3 4 use crate::{media_upload::Nip94Event, post::PostBuffer, ui::note::PostType, Error}; 5 use std::collections::HashMap; 6 7 #[derive(Default)] 8 pub struct Draft { 9 pub buffer: PostBuffer, 10 pub cur_layout: Option<(String, LayoutJob)>, // `PostBuffer::text_buffer` to current `LayoutJob` 11 pub cur_mention_hint: Option<MentionHint>, 12 pub uploaded_media: Vec<Nip94Event>, // media uploads to include 13 pub uploading_media: Vec<Promise<Result<Nip94Event, Error>>>, // promises that aren't ready yet 14 pub upload_errors: Vec<String>, // media upload errors to show the user 15 } 16 17 pub struct MentionHint { 18 pub index: usize, 19 pub pos: egui::Pos2, 20 pub text: String, 21 } 22 23 #[derive(Default)] 24 pub struct Drafts { 25 replies: HashMap<[u8; 32], Draft>, 26 quotes: HashMap<[u8; 32], Draft>, 27 compose: Draft, 28 } 29 30 impl Drafts { 31 pub fn compose_mut(&mut self) -> &mut Draft { 32 &mut self.compose 33 } 34 35 pub fn get_from_post_type(&mut self, post_type: &PostType) -> &mut Draft { 36 match post_type { 37 PostType::New => self.compose_mut(), 38 PostType::Quote(note_id) => self.quote_mut(note_id.bytes()), 39 PostType::Reply(note_id) => self.reply_mut(note_id.bytes()), 40 } 41 } 42 43 pub fn reply_mut(&mut self, id: &[u8; 32]) -> &mut Draft { 44 self.replies.entry(*id).or_default() 45 } 46 47 pub fn quote_mut(&mut self, id: &[u8; 32]) -> &mut Draft { 48 self.quotes.entry(*id).or_default() 49 } 50 } 51 52 impl Draft { 53 pub fn new() -> Self { 54 Draft::default() 55 } 56 57 pub fn clear(&mut self) { 58 self.buffer = PostBuffer::default(); 59 self.upload_errors = Vec::new(); 60 self.uploaded_media = Vec::new(); 61 self.uploading_media = Vec::new(); 62 } 63 }