text_block_event.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. use crate::document::document_event::DocumentEventTest;
  2. use crate::document::utils::{gen_id, gen_text_block_data};
  3. use flowy_document2::entities::*;
  4. use std::sync::Arc;
  5. const TEXT_BLOCK_TY: &str = "paragraph";
  6. pub struct TextBlockEventTest {
  7. doc: Arc<DocumentEventTest>,
  8. doc_id: String,
  9. }
  10. impl TextBlockEventTest {
  11. pub async fn new() -> Self {
  12. let doc = DocumentEventTest::new().await;
  13. let doc_id = doc.create_document().await.id;
  14. Self {
  15. doc: Arc::new(doc),
  16. doc_id,
  17. }
  18. }
  19. pub async fn get(&self, block_id: &str) -> Option<BlockPB> {
  20. let doc = self.doc.clone();
  21. let doc_id = self.doc_id.clone();
  22. doc.get_block(&doc_id, block_id).await
  23. }
  24. /// Insert a new text block at the index of parent's children.
  25. pub async fn insert_index(&self, text: String, index: usize, parent_id: Option<&str>) -> String {
  26. let doc = self.doc.clone();
  27. let doc_id = self.doc_id.clone();
  28. let page_id = self.doc.get_page_id(&doc_id).await;
  29. let parent_id = parent_id
  30. .map(|id| id.to_string())
  31. .unwrap_or_else(|| page_id);
  32. let parent_children = self.doc.get_block_children(&doc_id, &parent_id).await;
  33. let prev_id = {
  34. // If index is 0, then the new block will be the first child of parent.
  35. if index == 0 {
  36. None
  37. } else {
  38. parent_children.and_then(|children| {
  39. // If index is greater than the length of children, then the new block will be the last child of parent.
  40. if index >= children.len() {
  41. children.last().cloned()
  42. } else {
  43. children.get(index - 1).cloned()
  44. }
  45. })
  46. }
  47. };
  48. let new_block_id = gen_id();
  49. let data = gen_text_block_data(text);
  50. let new_block = BlockPB {
  51. id: new_block_id.clone(),
  52. ty: TEXT_BLOCK_TY.to_string(),
  53. data,
  54. parent_id: parent_id.clone(),
  55. children_id: gen_id(),
  56. };
  57. let action = BlockActionPB {
  58. action: BlockActionTypePB::Insert,
  59. payload: BlockActionPayloadPB {
  60. block: new_block,
  61. prev_id,
  62. parent_id: Some(parent_id),
  63. },
  64. };
  65. let payload = ApplyActionPayloadPB {
  66. document_id: doc_id,
  67. actions: vec![action],
  68. };
  69. doc.apply_actions(payload).await;
  70. new_block_id
  71. }
  72. pub async fn update(&self, block_id: &str, text: String) {
  73. let doc = self.doc.clone();
  74. let doc_id = self.doc_id.clone();
  75. let block = self.get(block_id).await.unwrap();
  76. let data = gen_text_block_data(text);
  77. let new_block = {
  78. let mut new_block = block.clone();
  79. new_block.data = data;
  80. new_block
  81. };
  82. let action = BlockActionPB {
  83. action: BlockActionTypePB::Update,
  84. payload: BlockActionPayloadPB {
  85. block: new_block,
  86. prev_id: None,
  87. parent_id: Some(block.parent_id.clone()),
  88. },
  89. };
  90. let payload = ApplyActionPayloadPB {
  91. document_id: doc_id,
  92. actions: vec![action],
  93. };
  94. doc.apply_actions(payload).await;
  95. }
  96. pub async fn delete(&self, block_id: &str) {
  97. let doc = self.doc.clone();
  98. let doc_id = self.doc_id.clone();
  99. let block = self.get(block_id).await.unwrap();
  100. let parent_id = block.parent_id.clone();
  101. let action = BlockActionPB {
  102. action: BlockActionTypePB::Delete,
  103. payload: BlockActionPayloadPB {
  104. block,
  105. prev_id: None,
  106. parent_id: Some(parent_id),
  107. },
  108. };
  109. let payload = ApplyActionPayloadPB {
  110. document_id: doc_id,
  111. actions: vec![action],
  112. };
  113. doc.apply_actions(payload).await;
  114. }
  115. }