delta_migration.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. use crate::editor::{DocumentNode, DocumentOperation};
  2. use flowy_error::FlowyResult;
  3. use lib_ot::core::{AttributeHashMap, DeltaOperation, Insert, Transaction};
  4. use lib_ot::text_delta::{DeltaTextOperation, DeltaTextOperations};
  5. pub struct DeltaRevisionMigration();
  6. impl DeltaRevisionMigration {
  7. pub fn run(delta: DeltaTextOperations) -> FlowyResult<Transaction> {
  8. let migrate_background_attribute = |insert: &mut Insert<AttributeHashMap>| {
  9. if let Some(Some(color)) = insert
  10. .attributes
  11. .get("background")
  12. .map(|value| value.str_value())
  13. {
  14. insert.attributes.remove_key("background");
  15. insert.attributes.insert("backgroundColor", color);
  16. }
  17. };
  18. let migrate_strike_attribute = |insert: &mut Insert<AttributeHashMap>| {
  19. if let Some(Some(_)) = insert
  20. .attributes
  21. .get("strike")
  22. .map(|value| value.str_value())
  23. {
  24. insert.attributes.remove_key("strike");
  25. insert.attributes.insert("strikethrough", true);
  26. }
  27. };
  28. let migrate_link_attribute = |insert: &mut Insert<AttributeHashMap>| {
  29. if let Some(Some(link)) = insert.attributes.get("link").map(|value| value.str_value()) {
  30. insert.attributes.remove_key("link");
  31. insert.attributes.insert("href", link);
  32. }
  33. };
  34. let migrate_list_attribute =
  35. |attribute_node: &mut DocumentNode, value: &str, number_list_number: &mut usize| {
  36. if value == "unchecked" {
  37. *number_list_number = 0;
  38. attribute_node.attributes.insert("subtype", "checkbox");
  39. attribute_node.attributes.insert("checkbox", false);
  40. }
  41. if value == "checked" {
  42. *number_list_number = 0;
  43. attribute_node.attributes.insert("subtype", "checkbox");
  44. attribute_node.attributes.insert("checkbox", true);
  45. }
  46. if value == "bullet" {
  47. *number_list_number = 0;
  48. attribute_node.attributes.insert("subtype", "bulleted-list");
  49. }
  50. if value == "ordered" {
  51. *number_list_number += 1;
  52. attribute_node.attributes.insert("subtype", "number-list");
  53. attribute_node
  54. .attributes
  55. .insert("number", *number_list_number);
  56. }
  57. };
  58. let generate_new_op_with_double_new_lines = |insert: &mut Insert<AttributeHashMap>| {
  59. let pattern = "\n\n";
  60. let mut new_ops = vec![];
  61. if insert.s.as_str().contains(pattern) {
  62. let insert_str = insert.s.clone();
  63. let insert_strings = insert_str.split(pattern).map(|s| s.to_owned());
  64. for (index, new_s) in insert_strings.enumerate() {
  65. if index == 0 {
  66. insert.s = new_s.into();
  67. } else {
  68. new_ops.push(DeltaOperation::Insert(Insert {
  69. s: new_s.into(),
  70. attributes: AttributeHashMap::default(),
  71. }));
  72. }
  73. }
  74. }
  75. new_ops
  76. };
  77. let create_text_node = |ops: Vec<DeltaTextOperation>| {
  78. let mut document_node = DocumentNode::new();
  79. document_node.node_type = "text".to_owned();
  80. ops.into_iter().for_each(|op| document_node.delta.add(op));
  81. document_node
  82. };
  83. let transform_op = |mut insert: Insert<AttributeHashMap>| {
  84. // Rename the attribute name from background to backgroundColor
  85. migrate_background_attribute(&mut insert);
  86. migrate_strike_attribute(&mut insert);
  87. migrate_link_attribute(&mut insert);
  88. let new_ops = generate_new_op_with_double_new_lines(&mut insert);
  89. (DeltaOperation::Insert(insert), new_ops)
  90. };
  91. let mut index: usize = 0;
  92. let mut number_list_number = 0;
  93. let mut editor_node = DocumentNode::new();
  94. editor_node.node_type = "editor".to_owned();
  95. let mut transaction = Transaction::new();
  96. transaction.push_operation(DocumentOperation::Insert {
  97. path: 0.into(),
  98. nodes: vec![editor_node],
  99. });
  100. let mut iter = delta.ops.into_iter().enumerate();
  101. while let Some((_, op)) = iter.next() {
  102. let mut document_node = create_text_node(vec![]);
  103. let mut split_document_nodes = vec![];
  104. match op {
  105. DeltaOperation::Delete(_) => tracing::warn!("Should not contain delete operation"),
  106. DeltaOperation::Retain(_) => tracing::warn!("Should not contain retain operation"),
  107. DeltaOperation::Insert(insert) => {
  108. if insert.s.as_str() != "\n" {
  109. let (op, new_ops) = transform_op(insert);
  110. document_node.delta.add(op);
  111. if !new_ops.is_empty() {
  112. split_document_nodes.push(create_text_node(new_ops));
  113. }
  114. }
  115. while let Some((_, DeltaOperation::Insert(insert))) = iter.next() {
  116. if insert.s.as_str() != "\n" {
  117. let (op, new_ops) = transform_op(insert);
  118. document_node.delta.add(op);
  119. if !new_ops.is_empty() {
  120. split_document_nodes.push(create_text_node(new_ops));
  121. }
  122. } else {
  123. let attribute_node = match split_document_nodes.last_mut() {
  124. None => &mut document_node,
  125. Some(split_document_node) => split_document_node,
  126. };
  127. if let Some(value) = insert.attributes.get("header") {
  128. attribute_node.attributes.insert("subtype", "heading");
  129. if let Some(v) = value.int_value() {
  130. number_list_number = 0;
  131. attribute_node
  132. .attributes
  133. .insert("heading", format!("h{}", v));
  134. }
  135. }
  136. if insert.attributes.get("blockquote").is_some() {
  137. attribute_node.attributes.insert("subtype", "quote");
  138. }
  139. if let Some(value) = insert.attributes.get("list") {
  140. if let Some(s) = value.str_value() {
  141. migrate_list_attribute(attribute_node, &s, &mut number_list_number);
  142. }
  143. }
  144. break;
  145. }
  146. }
  147. },
  148. }
  149. let mut operations = vec![document_node];
  150. operations.extend(split_document_nodes);
  151. operations.into_iter().for_each(|node| {
  152. // println!("{}", serde_json::to_string(&node).unwrap());
  153. let operation = DocumentOperation::Insert {
  154. path: vec![0, index].into(),
  155. nodes: vec![node],
  156. };
  157. transaction.push_operation(operation);
  158. index += 1;
  159. });
  160. }
  161. Ok(transaction)
  162. }
  163. }
  164. #[cfg(test)]
  165. mod tests {
  166. use crate::editor::Document;
  167. use crate::services::delta_migration::DeltaRevisionMigration;
  168. use lib_ot::text_delta::DeltaTextOperations;
  169. #[test]
  170. fn transform_delta_to_transaction_test() {
  171. let delta = DeltaTextOperations::from_json(DELTA_STR).unwrap();
  172. let transaction = DeltaRevisionMigration::run(delta).unwrap();
  173. let document = Document::from_transaction(transaction).unwrap();
  174. let s = document.get_content(true).unwrap();
  175. assert!(!s.is_empty());
  176. }
  177. const DELTA_STR: &str = r#"[
  178. {
  179. "insert": "\n👋 Welcome to AppFlowy!"
  180. },
  181. {
  182. "insert": "\n",
  183. "attributes": {
  184. "header": 1
  185. }
  186. },
  187. {
  188. "insert": "\nHere are the basics"
  189. },
  190. {
  191. "insert": "\n",
  192. "attributes": {
  193. "header": 2
  194. }
  195. },
  196. {
  197. "insert": "Click anywhere and just start typing"
  198. },
  199. {
  200. "insert": "\n",
  201. "attributes": {
  202. "list": "unchecked"
  203. }
  204. },
  205. {
  206. "insert": "Highlight",
  207. "attributes": {
  208. "background": "$fff2cd"
  209. }
  210. },
  211. {
  212. "insert": " any text, and use the menu at the bottom to "
  213. },
  214. {
  215. "insert": "style",
  216. "attributes": {
  217. "italic": true
  218. }
  219. },
  220. {
  221. "insert": " "
  222. },
  223. {
  224. "insert": "your",
  225. "attributes": {
  226. "bold": true
  227. }
  228. },
  229. {
  230. "insert": " "
  231. },
  232. {
  233. "insert": "writing",
  234. "attributes": {
  235. "underline": true
  236. }
  237. },
  238. {
  239. "insert": " "
  240. },
  241. {
  242. "insert": "however",
  243. "attributes": {
  244. "code": true
  245. }
  246. },
  247. {
  248. "insert": " "
  249. },
  250. {
  251. "insert": "you",
  252. "attributes": {
  253. "strike": true
  254. }
  255. },
  256. {
  257. "insert": " "
  258. },
  259. {
  260. "insert": "like",
  261. "attributes": {
  262. "background": "$e8e0ff"
  263. }
  264. },
  265. {
  266. "insert": "\n",
  267. "attributes": {
  268. "list": "unchecked"
  269. }
  270. },
  271. {
  272. "insert": "Click "
  273. },
  274. {
  275. "insert": "+ New Page",
  276. "attributes": {
  277. "background": "$defff1",
  278. "bold": true
  279. }
  280. },
  281. {
  282. "insert": " button at the bottom of your sidebar to add a new page"
  283. },
  284. {
  285. "insert": "\n",
  286. "attributes": {
  287. "list": "unchecked"
  288. }
  289. },
  290. {
  291. "insert": "Click the "
  292. },
  293. {
  294. "insert": "'",
  295. "attributes": {
  296. "background": "$defff1"
  297. }
  298. },
  299. {
  300. "insert": "+",
  301. "attributes": {
  302. "background": "$defff1",
  303. "bold": true
  304. }
  305. },
  306. {
  307. "insert": "'",
  308. "attributes": {
  309. "background": "$defff1"
  310. }
  311. },
  312. {
  313. "insert": " next to any page title in the sidebar to quickly add a new subpage"
  314. },
  315. {
  316. "insert": "\n",
  317. "attributes": {
  318. "list": "unchecked"
  319. }
  320. },
  321. {
  322. "insert": "\nHave a question? "
  323. },
  324. {
  325. "insert": "\n",
  326. "attributes": {
  327. "header": 2
  328. }
  329. },
  330. {
  331. "insert": "Click the "
  332. },
  333. {
  334. "insert": "'?'",
  335. "attributes": {
  336. "background": "$defff1",
  337. "bold": true
  338. }
  339. },
  340. {
  341. "insert": " at the bottom right for help and support.\n\nLike AppFlowy? Follow us:"
  342. },
  343. {
  344. "insert": "\n",
  345. "attributes": {
  346. "header": 2
  347. }
  348. },
  349. {
  350. "insert": "GitHub: https://github.com/AppFlowy-IO/appflowy"
  351. },
  352. {
  353. "insert": "\n",
  354. "attributes": {
  355. "blockquote": true
  356. }
  357. },
  358. {
  359. "insert": "Twitter: https://twitter.com/appflowy"
  360. },
  361. {
  362. "insert": "\n",
  363. "attributes": {
  364. "blockquote": true
  365. }
  366. },
  367. {
  368. "insert": "Newsletter: https://www.appflowy.io/blog"
  369. },
  370. {
  371. "insert": "\n",
  372. "attributes": {
  373. "blockquote": true
  374. }
  375. },
  376. {
  377. "insert": "item 1"
  378. },
  379. {
  380. "insert": "\n",
  381. "attributes": {
  382. "list": "ordered"
  383. }
  384. },
  385. {
  386. "insert": "item 2"
  387. },
  388. {
  389. "insert": "\n",
  390. "attributes": {
  391. "list": "ordered"
  392. }
  393. },
  394. {
  395. "insert": "item3"
  396. },
  397. {
  398. "insert": "\n",
  399. "attributes": {
  400. "list": "ordered"
  401. }
  402. },
  403. {
  404. "insert": "appflowy",
  405. "attributes": {
  406. "link": "https://www.appflowy.io/"
  407. }
  408. }
  409. ]"#;
  410. }