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