editor_test.rs 4.3 KB

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