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