editor_test.rs 4.2 KB

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