Generates default data for a document. This function constructs a `DocumentData` instance that includes a page block and a text block. Each block has a unique identifier, generated using the nanoid crate. The page block is set as the parent of the text block. The `DocumentData` struct has three main components: - `page_id`: a unique identifier for the root page block. - `blocks`: a `HashMap` whe
(document_id: &str)
| 29 | /// A `DocumentData` instance populated with a single page block and a single child text block. |
| 30 | /// |
| 31 | pub fn default_document_data(document_id: &str) -> DocumentData { |
| 32 | let page_type = PAGE.to_string(); |
| 33 | let text_type = PARAGRAPH_BLOCK_TYPE.to_string(); |
| 34 | |
| 35 | let mut blocks: HashMap<String, Block> = HashMap::new(); |
| 36 | let mut children_map: HashMap<String, Vec<String>> = HashMap::new(); |
| 37 | let mut text_map: HashMap<String, String> = HashMap::new(); |
| 38 | |
| 39 | // page block |
| 40 | let page_id = page_id_from_document_id(document_id).unwrap_or_else(generate_id); |
| 41 | let children_id = page_id.clone(); |
| 42 | let root = Block { |
| 43 | id: page_id.clone(), |
| 44 | ty: page_type, |
| 45 | parent: "".to_string(), |
| 46 | children: children_id.clone(), |
| 47 | external_id: None, |
| 48 | external_type: None, |
| 49 | data: HashMap::new(), |
| 50 | }; |
| 51 | blocks.insert(page_id.clone(), root); |
| 52 | |
| 53 | // text block |
| 54 | let text_block_id = generate_id(); |
| 55 | let text_block_children_id = generate_id(); |
| 56 | let text_external_id = generate_id(); |
| 57 | let text_block = Block { |
| 58 | id: text_block_id.clone(), |
| 59 | ty: text_type, |
| 60 | parent: page_id.clone(), |
| 61 | children: text_block_children_id.clone(), |
| 62 | external_id: Some(text_external_id.clone()), |
| 63 | external_type: Some("text".to_string()), |
| 64 | data: HashMap::new(), |
| 65 | }; |
| 66 | blocks.insert(text_block_id.clone(), text_block); |
| 67 | |
| 68 | // children_map |
| 69 | children_map.insert(children_id, vec![text_block_id]); |
| 70 | children_map.insert(text_block_children_id, vec![]); |
| 71 | |
| 72 | // text_map |
| 73 | text_map.insert(text_external_id, "[]".to_string()); |
| 74 | |
| 75 | DocumentData { |
| 76 | page_id, |
| 77 | blocks, |
| 78 | meta: DocumentMeta { |
| 79 | children_map, |
| 80 | text_map: Some(text_map), |
| 81 | }, |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// Generates default collab data for a document. This document only contains the initial state |
| 86 | /// of the document. |
no test coverage detected