editor_test.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. use super::script::{NodeScript::*, *};
  2. use lib_ot::core::AttributeBuilder;
  3. use lib_ot::{
  4. core::{NodeData, Path},
  5. text_delta::TextOperationBuilder,
  6. };
  7. #[test]
  8. fn editor_deserialize_node_test() {
  9. let mut test = NodeTest::new();
  10. let node: NodeData = serde_json::from_str(EXAMPLE_JSON).unwrap();
  11. let path: Path = 0.into();
  12. let expected_delta = TextOperationBuilder::new()
  13. .insert("👋 ")
  14. .insert_with_attributes(
  15. "Welcome to ",
  16. AttributeBuilder::new().insert("href", "appflowy.io").build(),
  17. )
  18. .insert_with_attributes(
  19. "AppFlowy Editor",
  20. AttributeBuilder::new().insert("italic", true).build(),
  21. )
  22. .build();
  23. test.run_scripts(vec![
  24. InsertNode {
  25. path,
  26. node_data: node.clone(),
  27. rev_id: 1,
  28. },
  29. AssertNumberOfNodesAtPath { path: None, len: 1 },
  30. AssertNumberOfNodesAtPath {
  31. path: Some(0.into()),
  32. len: 14,
  33. },
  34. AssertNumberOfNodesAtPath {
  35. path: Some(0.into()),
  36. len: 14,
  37. },
  38. AssertNodeDelta {
  39. path: vec![0, 1].into(),
  40. expected: expected_delta,
  41. },
  42. AssertNodeData {
  43. path: vec![0, 0].into(),
  44. expected: Some(node.children[0].clone()),
  45. },
  46. AssertNodeData {
  47. path: vec![0, 3].into(),
  48. expected: Some(node.children[3].clone()),
  49. },
  50. ]);
  51. }
  52. #[allow(dead_code)]
  53. const EXAMPLE_JSON: &str = r#"
  54. {
  55. "type": "editor",
  56. "children": [
  57. {
  58. "type": "image",
  59. "attributes": {
  60. "image_src": "https://s1.ax1x.com/2022/08/26/v2sSbR.jpg",
  61. "align": "center"
  62. }
  63. },
  64. {
  65. "type": "text",
  66. "attributes": {
  67. "subtype": "heading",
  68. "heading": "h1"
  69. },
  70. "body": {
  71. "delta": [
  72. {
  73. "insert": "👋 "
  74. },
  75. {
  76. "insert": "Welcome to ",
  77. "attributes": {
  78. "href": "appflowy.io"
  79. }
  80. },
  81. {
  82. "insert": "AppFlowy Editor",
  83. "attributes": {
  84. "italic": true
  85. }
  86. }
  87. ]
  88. }
  89. },
  90. { "type": "text", "delta": [] },
  91. {
  92. "type": "text",
  93. "body": {
  94. "delta": [
  95. { "insert": "AppFlowy Editor is a " },
  96. { "insert": "highly customizable", "attributes": { "bold": true } },
  97. { "insert": " " },
  98. { "insert": "rich-text editor", "attributes": { "italic": true } },
  99. { "insert": " for " },
  100. { "insert": "Flutter", "attributes": { "underline": true } }
  101. ]
  102. }
  103. },
  104. {
  105. "type": "text",
  106. "attributes": { "checkbox": true, "subtype": "checkbox" },
  107. "body": {
  108. "delta": [{ "insert": "Customizable" }]
  109. }
  110. },
  111. {
  112. "type": "text",
  113. "attributes": { "checkbox": true, "subtype": "checkbox" },
  114. "delta": [{ "insert": "Test-covered" }]
  115. },
  116. {
  117. "type": "text",
  118. "attributes": { "checkbox": false, "subtype": "checkbox" },
  119. "delta": [{ "insert": "more to come!" }]
  120. },
  121. { "type": "text", "delta": [] },
  122. {
  123. "type": "text",
  124. "attributes": { "subtype": "quote" },
  125. "delta": [{ "insert": "Here is an exmaple you can give it a try" }]
  126. },
  127. { "type": "text", "delta": [] },
  128. {
  129. "type": "text",
  130. "delta": [
  131. { "insert": "You can also use " },
  132. {
  133. "insert": "AppFlowy Editor",
  134. "attributes": {
  135. "italic": true,
  136. "bold": true,
  137. "backgroundColor": "0x6000BCF0"
  138. }
  139. },
  140. { "insert": " as a component to build your own app." }
  141. ]
  142. },
  143. { "type": "text", "delta": [] },
  144. {
  145. "type": "text",
  146. "attributes": { "subtype": "bulleted-list" },
  147. "delta": [{ "insert": "Use / to insert blocks" }]
  148. },
  149. {
  150. "type": "text",
  151. "attributes": { "subtype": "bulleted-list" },
  152. "delta": [
  153. {
  154. "insert": "Select text to trigger to the toolbar to format your notes."
  155. }
  156. ]
  157. }
  158. ]
  159. }
  160. "#;