text_document/
text_list.rs1use std::sync::Arc;
4
5use parking_lot::Mutex;
6
7use frontend::commands::{block_commands, list_commands};
8use frontend::common::types::EntityId;
9
10use crate::inner::TextDocumentInner;
11use crate::text_block::{TextBlock, format_list_marker};
12use crate::{ListFormat, ListStyle};
13
14#[derive(Clone)]
18pub struct TextList {
19 pub(crate) doc: Arc<Mutex<TextDocumentInner>>,
20 pub(crate) list_id: usize,
21}
22
23impl TextList {
24 pub fn id(&self) -> usize {
26 self.list_id
27 }
28
29 pub fn style(&self) -> ListStyle {
31 let inner = self.doc.lock();
32 list_commands::get_list(&inner.ctx, &(self.list_id as u64))
33 .ok()
34 .flatten()
35 .map(|l| l.style)
36 .unwrap_or(ListStyle::Disc)
37 }
38
39 pub fn indent(&self) -> u8 {
41 let inner = self.doc.lock();
42 list_commands::get_list(&inner.ctx, &(self.list_id as u64))
43 .ok()
44 .flatten()
45 .map(|l| l.indent as u8)
46 .unwrap_or(0)
47 }
48
49 pub fn prefix(&self) -> String {
51 let inner = self.doc.lock();
52 list_commands::get_list(&inner.ctx, &(self.list_id as u64))
53 .ok()
54 .flatten()
55 .map(|l| l.prefix)
56 .unwrap_or_default()
57 }
58
59 pub fn suffix(&self) -> String {
61 let inner = self.doc.lock();
62 list_commands::get_list(&inner.ctx, &(self.list_id as u64))
63 .ok()
64 .flatten()
65 .map(|l| l.suffix)
66 .unwrap_or_default()
67 }
68
69 pub fn count(&self) -> usize {
71 let inner = self.doc.lock();
72 let list_entity_id = self.list_id as EntityId;
73 let all_blocks = block_commands::get_all_block(&inner.ctx).unwrap_or_default();
74 all_blocks
75 .iter()
76 .filter(|b| b.list == Some(list_entity_id))
77 .count()
78 }
79
80 pub fn item(&self, index: usize) -> Option<TextBlock> {
82 let inner = self.doc.lock();
83 let list_entity_id = self.list_id as EntityId;
84 let mut all_blocks = block_commands::get_all_block(&inner.ctx).unwrap_or_default();
85 let store = inner.ctx.db_context.get_store();
86 crate::inner::refresh_block_positions(&mut all_blocks, store);
87 let mut list_blocks: Vec<_> = all_blocks
88 .iter()
89 .filter(|b| b.list == Some(list_entity_id))
90 .collect();
91 list_blocks.sort_by_key(|b| b.document_position);
92
93 list_blocks.get(index).map(|b| TextBlock {
94 doc: Arc::clone(&self.doc),
95 block_id: b.id as usize,
96 })
97 }
98
99 pub fn format(&self) -> ListFormat {
101 let inner = self.doc.lock();
102 list_commands::get_list(&inner.ctx, &(self.list_id as u64))
103 .ok()
104 .flatten()
105 .map(|l| ListFormat {
106 style: Some(l.style),
107 indent: Some(l.indent as u8),
108 prefix: Some(l.prefix),
109 suffix: Some(l.suffix),
110 })
111 .unwrap_or_default()
112 }
113
114 pub fn item_marker(&self, index: usize) -> String {
116 let inner = self.doc.lock();
117 match list_commands::get_list(&inner.ctx, &(self.list_id as u64))
118 .ok()
119 .flatten()
120 {
121 Some(list_dto) => format_list_marker(&list_dto, index),
122 None => String::new(),
123 }
124 }
125}