document.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. use bytes::Bytes;
  2. use flowy_error::{FlowyError, FlowyResult};
  3. use flowy_revision::{RevisionObjectDeserializer, RevisionObjectSerializer};
  4. use flowy_sync::entities::revision::Revision;
  5. use lib_ot::core::{Body, Extension, Interval, NodeDataBuilder, NodeOperation, NodeTree, NodeTreeContext, Transaction};
  6. use lib_ot::text_delta::TextOperationBuilder;
  7. #[derive(Debug)]
  8. pub struct Document {
  9. tree: NodeTree,
  10. }
  11. impl Document {
  12. pub fn new(tree: NodeTree) -> Self {
  13. Self { tree }
  14. }
  15. pub fn from_transaction(transaction: Transaction) -> FlowyResult<Self> {
  16. let tree = NodeTree::from_operations(transaction.operations, make_tree_context())?;
  17. Ok(Self { tree })
  18. }
  19. pub fn get_content(&self, pretty: bool) -> FlowyResult<String> {
  20. if pretty {
  21. serde_json::to_string_pretty(self).map_err(|err| FlowyError::serde().context(err))
  22. } else {
  23. serde_json::to_string(self).map_err(|err| FlowyError::serde().context(err))
  24. }
  25. }
  26. pub fn get_tree(&self) -> &NodeTree {
  27. &self.tree
  28. }
  29. }
  30. pub(crate) fn make_tree_context() -> NodeTreeContext {
  31. NodeTreeContext {}
  32. }
  33. pub fn initial_document_content() -> String {
  34. let delta = TextOperationBuilder::new().insert("").build();
  35. let node_data = NodeDataBuilder::new("text").insert_body(Body::Delta(delta)).build();
  36. let editor_node = NodeDataBuilder::new("editor").add_node_data(node_data).build();
  37. let node_operation = NodeOperation::Insert {
  38. path: vec![0].into(),
  39. nodes: vec![editor_node],
  40. };
  41. let extension = Extension::TextSelection {
  42. before_selection: Interval::default(),
  43. after_selection: Interval::default(),
  44. };
  45. let transaction = Transaction {
  46. operations: vec![node_operation].into(),
  47. extension,
  48. };
  49. transaction.to_json().unwrap()
  50. }
  51. impl std::ops::Deref for Document {
  52. type Target = NodeTree;
  53. fn deref(&self) -> &Self::Target {
  54. &self.tree
  55. }
  56. }
  57. impl std::ops::DerefMut for Document {
  58. fn deref_mut(&mut self) -> &mut Self::Target {
  59. &mut self.tree
  60. }
  61. }
  62. pub struct DocumentRevisionSerde();
  63. impl RevisionObjectDeserializer for DocumentRevisionSerde {
  64. type Output = Document;
  65. fn deserialize_revisions(_object_id: &str, revisions: Vec<Revision>) -> FlowyResult<Self::Output> {
  66. let mut tree = NodeTree::new(make_tree_context());
  67. let transaction = make_transaction_from_revisions(revisions)?;
  68. let _ = tree.apply_transaction(transaction)?;
  69. let document = Document::new(tree);
  70. Result::<Document, FlowyError>::Ok(document)
  71. }
  72. }
  73. impl RevisionObjectSerializer for DocumentRevisionSerde {
  74. fn combine_revisions(revisions: Vec<Revision>) -> FlowyResult<Bytes> {
  75. let transaction = make_transaction_from_revisions(revisions)?;
  76. Ok(Bytes::from(transaction.to_bytes()?))
  77. }
  78. }
  79. fn make_transaction_from_revisions(revisions: Vec<Revision>) -> FlowyResult<Transaction> {
  80. let mut transaction = Transaction::new();
  81. for revision in revisions {
  82. let _ = transaction.compose(Transaction::from_bytes(&revision.bytes)?)?;
  83. }
  84. Ok(transaction)
  85. }