editor_test.rs 4.3 KB

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