document.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. use std::{
  2. collections::HashMap,
  3. ops::{Deref, DerefMut},
  4. sync::Arc,
  5. vec,
  6. };
  7. use collab::preclude::Collab;
  8. use collab_document::{
  9. blocks::{Block, DocumentData, DocumentMeta},
  10. document::Document as InnerDocument,
  11. };
  12. use flowy_error::{ErrorCode, FlowyError, FlowyResult};
  13. use nanoid::nanoid;
  14. use parking_lot::Mutex;
  15. use crate::entities::{BlockPB, ChildrenPB, DocumentDataPB2, MetaPB};
  16. #[derive(Clone)]
  17. pub struct Document(Arc<Mutex<InnerDocument>>);
  18. impl Document {
  19. pub fn new(collab: Collab) -> FlowyResult<Self> {
  20. let inner = InnerDocument::create(collab)
  21. .map_err(|_| FlowyError::from(ErrorCode::DocumentDataInvalid))?;
  22. Ok(Self(Arc::new(Mutex::new(inner))))
  23. }
  24. pub fn create_with_data(collab: Collab, data: DocumentData) -> FlowyResult<Self> {
  25. let inner = InnerDocument::create_with_data(collab, data)
  26. .map_err(|_| FlowyError::from(ErrorCode::DocumentDataInvalid))?;
  27. Ok(Self(Arc::new(Mutex::new(inner))))
  28. }
  29. }
  30. unsafe impl Sync for Document {}
  31. unsafe impl Send for Document {}
  32. impl Deref for Document {
  33. type Target = Arc<Mutex<InnerDocument>>;
  34. fn deref(&self) -> &Self::Target {
  35. &self.0
  36. }
  37. }
  38. impl DerefMut for Document {
  39. fn deref_mut(&mut self) -> &mut Self::Target {
  40. &mut self.0
  41. }
  42. }
  43. #[derive(Clone)]
  44. pub struct DocumentDataWrapper(pub DocumentData);
  45. impl Deref for DocumentDataWrapper {
  46. type Target = DocumentData;
  47. fn deref(&self) -> &Self::Target {
  48. &self.0
  49. }
  50. }
  51. impl DerefMut for DocumentDataWrapper {
  52. fn deref_mut(&mut self) -> &mut Self::Target {
  53. &mut self.0
  54. }
  55. }
  56. impl From<DocumentDataWrapper> for DocumentDataPB2 {
  57. fn from(data: DocumentDataWrapper) -> Self {
  58. let blocks = data
  59. .0
  60. .blocks
  61. .into_iter()
  62. .map(|(id, block)| {
  63. (
  64. id,
  65. BlockPB {
  66. id: block.id,
  67. ty: block.ty,
  68. parent_id: block.parent,
  69. children_id: block.children,
  70. data: serde_json::to_string(&block.data).unwrap(),
  71. },
  72. )
  73. })
  74. .collect::<HashMap<String, BlockPB>>();
  75. let children_map = data
  76. .0
  77. .meta
  78. .children_map
  79. .into_iter()
  80. .map(|(id, children)| {
  81. (
  82. id,
  83. ChildrenPB {
  84. children: children.into_iter().collect(),
  85. },
  86. )
  87. })
  88. .collect::<HashMap<String, ChildrenPB>>();
  89. Self {
  90. page_id: data.0.page_id,
  91. blocks,
  92. meta: MetaPB { children_map },
  93. }
  94. }
  95. }
  96. impl Default for DocumentDataWrapper {
  97. fn default() -> Self {
  98. let mut blocks: HashMap<String, Block> = HashMap::new();
  99. let mut meta: HashMap<String, Vec<String>> = HashMap::new();
  100. // page block
  101. let page_id = nanoid!(10);
  102. let children_id = nanoid!(10);
  103. let root = Block {
  104. id: page_id.clone(),
  105. ty: "page".to_string(),
  106. parent: "".to_string(),
  107. children: children_id.clone(),
  108. external_id: None,
  109. external_type: None,
  110. data: HashMap::new(),
  111. };
  112. blocks.insert(page_id.clone(), root);
  113. // text block
  114. let text_block_id = nanoid!(10);
  115. let text_0_children_id = nanoid!(10);
  116. let text_block = Block {
  117. id: text_block_id.clone(),
  118. ty: "text".to_string(),
  119. parent: page_id.clone(),
  120. children: text_0_children_id.clone(),
  121. external_id: None,
  122. external_type: None,
  123. data: HashMap::new(),
  124. };
  125. blocks.insert(text_block_id.clone(), text_block);
  126. // meta
  127. meta.insert(children_id, vec![text_block_id]);
  128. meta.insert(text_0_children_id, vec![]);
  129. Self(DocumentData {
  130. page_id,
  131. blocks,
  132. meta: DocumentMeta { children_map: meta },
  133. })
  134. }
  135. }