text_block.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. use crate::{
  2. entities::revision::{RepeatedRevision, Revision},
  3. errors::CollaborateError,
  4. };
  5. use flowy_derive::ProtoBuf;
  6. use lib_ot::{errors::OTError, rich_text::RichTextDelta};
  7. #[derive(ProtoBuf, Default, Debug, Clone)]
  8. pub struct CreateTextBlockParams {
  9. #[pb(index = 1)]
  10. pub id: String,
  11. #[pb(index = 2)]
  12. pub revisions: RepeatedRevision,
  13. }
  14. #[derive(ProtoBuf, Default, Debug, Clone, Eq, PartialEq)]
  15. pub struct DocumentPB {
  16. #[pb(index = 1)]
  17. pub block_id: String,
  18. #[pb(index = 2)]
  19. pub text: String,
  20. #[pb(index = 3)]
  21. pub rev_id: i64,
  22. #[pb(index = 4)]
  23. pub base_rev_id: i64,
  24. }
  25. impl DocumentPB {
  26. pub fn delta(&self) -> Result<RichTextDelta, OTError> {
  27. let delta = RichTextDelta::from_bytes(&self.text)?;
  28. Ok(delta)
  29. }
  30. }
  31. impl std::convert::TryFrom<Revision> for DocumentPB {
  32. type Error = CollaborateError;
  33. fn try_from(revision: Revision) -> Result<Self, Self::Error> {
  34. if !revision.is_initial() {
  35. return Err(CollaborateError::revision_conflict()
  36. .context("Revision's rev_id should be 0 when creating the document"));
  37. }
  38. let delta = RichTextDelta::from_bytes(&revision.delta_data)?;
  39. let doc_json = delta.to_delta_str();
  40. Ok(DocumentPB {
  41. block_id: revision.object_id,
  42. text: doc_json,
  43. rev_id: revision.rev_id,
  44. base_rev_id: revision.base_rev_id,
  45. })
  46. }
  47. }
  48. #[derive(ProtoBuf, Default, Debug, Clone)]
  49. pub struct ResetTextBlockParams {
  50. #[pb(index = 1)]
  51. pub block_id: String,
  52. #[pb(index = 2)]
  53. pub revisions: RepeatedRevision,
  54. }
  55. #[derive(ProtoBuf, Default, Debug, Clone)]
  56. pub struct TextBlockDeltaPB {
  57. #[pb(index = 1)]
  58. pub block_id: String,
  59. #[pb(index = 2)]
  60. pub delta_str: String,
  61. }
  62. #[derive(ProtoBuf, Default, Debug, Clone)]
  63. pub struct NewDocUserPB {
  64. #[pb(index = 1)]
  65. pub user_id: String,
  66. #[pb(index = 2)]
  67. pub rev_id: i64,
  68. #[pb(index = 3)]
  69. pub doc_id: String,
  70. }
  71. #[derive(ProtoBuf, Default, Debug, Clone)]
  72. pub struct TextBlockIdPB {
  73. #[pb(index = 1)]
  74. pub value: String,
  75. }
  76. impl AsRef<str> for TextBlockIdPB {
  77. fn as_ref(&self) -> &str {
  78. &self.value
  79. }
  80. }
  81. impl std::convert::From<String> for TextBlockIdPB {
  82. fn from(value: String) -> Self {
  83. TextBlockIdPB { value }
  84. }
  85. }
  86. impl std::convert::From<TextBlockIdPB> for String {
  87. fn from(block_id: TextBlockIdPB) -> Self {
  88. block_id.value
  89. }
  90. }
  91. impl std::convert::From<&String> for TextBlockIdPB {
  92. fn from(s: &String) -> Self {
  93. TextBlockIdPB { value: s.to_owned() }
  94. }
  95. }