view.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. use crate::client_document::*;
  2. use lib_ot::core::AttributeEntry;
  3. use lib_ot::{
  4. core::{trim, Interval},
  5. errors::{ErrorBuilder, OTError, OTErrorCode},
  6. text_delta::DeltaTextOperations,
  7. };
  8. pub const RECORD_THRESHOLD: usize = 400; // in milliseconds
  9. pub struct ViewExtensions {
  10. insert_exts: Vec<InsertExtension>,
  11. format_exts: Vec<FormatExtension>,
  12. delete_exts: Vec<DeleteExtension>,
  13. }
  14. impl ViewExtensions {
  15. pub(crate) fn new() -> Self {
  16. Self {
  17. insert_exts: construct_insert_exts(),
  18. format_exts: construct_format_exts(),
  19. delete_exts: construct_delete_exts(),
  20. }
  21. }
  22. pub(crate) fn insert(
  23. &self,
  24. operations: &DeltaTextOperations,
  25. text: &str,
  26. interval: Interval,
  27. ) -> Result<DeltaTextOperations, OTError> {
  28. let mut new_operations = None;
  29. for ext in &self.insert_exts {
  30. if let Some(mut operations) = ext.apply(operations, interval.size(), text, interval.start) {
  31. trim(&mut operations);
  32. tracing::trace!("[{}] applied, delta: {}", ext.ext_name(), operations);
  33. new_operations = Some(operations);
  34. break;
  35. }
  36. }
  37. match new_operations {
  38. None => Err(ErrorBuilder::new(OTErrorCode::ApplyInsertFail).build()),
  39. Some(new_operations) => Ok(new_operations),
  40. }
  41. }
  42. pub(crate) fn delete(
  43. &self,
  44. delta: &DeltaTextOperations,
  45. interval: Interval,
  46. ) -> Result<DeltaTextOperations, OTError> {
  47. let mut new_delta = None;
  48. for ext in &self.delete_exts {
  49. if let Some(mut delta) = ext.apply(delta, interval) {
  50. trim(&mut delta);
  51. tracing::trace!("[{}] applied, delta: {}", ext.ext_name(), delta);
  52. new_delta = Some(delta);
  53. break;
  54. }
  55. }
  56. match new_delta {
  57. None => Err(ErrorBuilder::new(OTErrorCode::ApplyDeleteFail).build()),
  58. Some(new_delta) => Ok(new_delta),
  59. }
  60. }
  61. pub(crate) fn format(
  62. &self,
  63. operations: &DeltaTextOperations,
  64. attribute: AttributeEntry,
  65. interval: Interval,
  66. ) -> Result<DeltaTextOperations, OTError> {
  67. let mut new_operations = None;
  68. for ext in &self.format_exts {
  69. if let Some(mut operations) = ext.apply(operations, interval, &attribute) {
  70. trim(&mut operations);
  71. tracing::trace!("[{}] applied, delta: {}", ext.ext_name(), operations);
  72. new_operations = Some(operations);
  73. break;
  74. }
  75. }
  76. match new_operations {
  77. None => Err(ErrorBuilder::new(OTErrorCode::ApplyFormatFail).build()),
  78. Some(new_operations) => Ok(new_operations),
  79. }
  80. }
  81. }
  82. fn construct_insert_exts() -> Vec<InsertExtension> {
  83. vec![
  84. Box::new(InsertEmbedsExt {}),
  85. Box::new(ForceNewlineForInsertsAroundEmbedExt {}),
  86. Box::new(AutoExitBlock {}),
  87. Box::new(PreserveBlockFormatOnInsert {}),
  88. Box::new(PreserveLineFormatOnSplit {}),
  89. Box::new(ResetLineFormatOnNewLine {}),
  90. Box::new(AutoFormatExt {}),
  91. Box::new(PreserveInlineFormat {}),
  92. Box::new(DefaultInsertAttribute {}),
  93. ]
  94. }
  95. fn construct_format_exts() -> Vec<FormatExtension> {
  96. vec![
  97. // Box::new(FormatLinkAtCaretPositionExt {}),
  98. Box::new(ResolveBlockFormat {}),
  99. Box::new(ResolveInlineFormat {}),
  100. ]
  101. }
  102. fn construct_delete_exts() -> Vec<DeleteExtension> {
  103. vec![
  104. Box::new(PreserveLineFormatOnMerge {}),
  105. Box::new(DefaultDelete {}),
  106. ]
  107. }