document_pad.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. use crate::{client_document::InitialDocumentText, errors::CollaborateError, synchronizer::RevisionSyncObject};
  2. use lib_ot::{
  3. core::*,
  4. rich_text::{RichTextAttributes, RichTextDelta},
  5. };
  6. pub struct ServerDocument {
  7. doc_id: String,
  8. delta: RichTextDelta,
  9. }
  10. impl ServerDocument {
  11. #[allow(dead_code)]
  12. pub fn new<C: InitialDocumentText>(doc_id: &str) -> Self {
  13. Self::from_delta(doc_id, C::initial_delta())
  14. }
  15. pub fn from_delta(doc_id: &str, delta: RichTextDelta) -> Self {
  16. let doc_id = doc_id.to_owned();
  17. ServerDocument { doc_id, delta }
  18. }
  19. }
  20. impl RevisionSyncObject<RichTextAttributes> for ServerDocument {
  21. fn id(&self) -> &str {
  22. &self.doc_id
  23. }
  24. fn compose(&mut self, other: &RichTextDelta) -> Result<(), CollaborateError> {
  25. // tracing::trace!("{} compose {}", &self.delta.to_json(), other.to_json());
  26. let new_delta = self.delta.compose(other)?;
  27. self.delta = new_delta;
  28. Ok(())
  29. }
  30. fn transform(&self, other: &RichTextDelta) -> Result<(RichTextDelta, RichTextDelta), CollaborateError> {
  31. let value = self.delta.transform(other)?;
  32. Ok(value)
  33. }
  34. fn to_json(&self) -> String {
  35. self.delta.json_str()
  36. }
  37. fn set_delta(&mut self, new_delta: Delta<RichTextAttributes>) {
  38. self.delta = new_delta;
  39. }
  40. }