editor_test.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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: 4,
  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. }
  106. "#;