attributes.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #![allow(non_snake_case)]
  2. use crate::core::{AttributeEntry, AttributeHashMap, AttributeKey, AttributeValue};
  3. use crate::text_delta::DeltaTextOperation;
  4. use crate::{inline_attribute_entry, inline_list_attribute_entry};
  5. use lazy_static::lazy_static;
  6. use std::str::FromStr;
  7. use std::{collections::HashSet, iter::FromIterator};
  8. use strum_macros::{AsRefStr, Display, EnumString};
  9. #[inline(always)]
  10. pub fn empty_attributes() -> AttributeHashMap {
  11. AttributeHashMap::default()
  12. }
  13. pub fn attributes_except_header(op: &DeltaTextOperation) -> AttributeHashMap {
  14. let mut attributes = op.get_attributes();
  15. attributes.remove_key(BuildInTextAttributeKey::Header);
  16. attributes
  17. }
  18. #[derive(Debug, Clone)]
  19. pub struct BuildInTextAttribute();
  20. impl BuildInTextAttribute {
  21. inline_attribute_entry!(Bold, bool);
  22. inline_attribute_entry!(Italic, bool);
  23. inline_attribute_entry!(Underline, bool);
  24. inline_attribute_entry!(StrikeThrough, bool);
  25. inline_attribute_entry!(Link, &str);
  26. inline_attribute_entry!(Color, String);
  27. inline_attribute_entry!(Font, usize);
  28. inline_attribute_entry!(Size, usize);
  29. inline_attribute_entry!(Background, String);
  30. inline_attribute_entry!(InlineCode, bool);
  31. inline_attribute_entry!(Header, usize);
  32. inline_attribute_entry!(Indent, usize);
  33. inline_attribute_entry!(Align, String);
  34. inline_attribute_entry!(List, &str);
  35. inline_attribute_entry!(CodeBlock, bool);
  36. inline_attribute_entry!(BlockQuote, bool);
  37. inline_attribute_entry!(Width, usize);
  38. inline_attribute_entry!(Height, usize);
  39. // List extension
  40. inline_list_attribute_entry!(Bullet, "bullet");
  41. inline_list_attribute_entry!(Ordered, "ordered");
  42. inline_list_attribute_entry!(Checked, "checked");
  43. inline_list_attribute_entry!(UnChecked, "unchecked");
  44. }
  45. #[derive(
  46. Clone,
  47. Debug,
  48. Display,
  49. Hash,
  50. Eq,
  51. PartialEq,
  52. serde::Serialize,
  53. serde::Deserialize,
  54. AsRefStr,
  55. EnumString,
  56. )]
  57. #[strum(serialize_all = "snake_case")]
  58. pub enum BuildInTextAttributeKey {
  59. #[serde(rename = "bold")]
  60. Bold,
  61. #[serde(rename = "italic")]
  62. Italic,
  63. #[serde(rename = "underline")]
  64. Underline,
  65. #[serde(rename = "strike")]
  66. StrikeThrough,
  67. #[serde(rename = "font")]
  68. Font,
  69. #[serde(rename = "size")]
  70. Size,
  71. #[serde(rename = "link")]
  72. Link,
  73. #[serde(rename = "color")]
  74. Color,
  75. #[serde(rename = "background")]
  76. Background,
  77. #[serde(rename = "indent")]
  78. Indent,
  79. #[serde(rename = "align")]
  80. Align,
  81. #[serde(rename = "code_block")]
  82. CodeBlock,
  83. #[serde(rename = "code")]
  84. InlineCode,
  85. #[serde(rename = "list")]
  86. List,
  87. #[serde(rename = "blockquote")]
  88. BlockQuote,
  89. #[serde(rename = "width")]
  90. Width,
  91. #[serde(rename = "height")]
  92. Height,
  93. #[serde(rename = "header")]
  94. Header,
  95. }
  96. pub fn is_block(k: &AttributeKey) -> bool {
  97. if let Ok(key) = BuildInTextAttributeKey::from_str(k) {
  98. BLOCK_KEYS.contains(&key)
  99. } else {
  100. false
  101. }
  102. }
  103. pub fn is_inline(k: &AttributeKey) -> bool {
  104. if let Ok(key) = BuildInTextAttributeKey::from_str(k) {
  105. INLINE_KEYS.contains(&key)
  106. } else {
  107. false
  108. }
  109. }
  110. lazy_static! {
  111. static ref BLOCK_KEYS: HashSet<BuildInTextAttributeKey> = HashSet::from_iter(vec![
  112. BuildInTextAttributeKey::Header,
  113. BuildInTextAttributeKey::Indent,
  114. BuildInTextAttributeKey::Align,
  115. BuildInTextAttributeKey::CodeBlock,
  116. BuildInTextAttributeKey::List,
  117. BuildInTextAttributeKey::BlockQuote,
  118. ]);
  119. static ref INLINE_KEYS: HashSet<BuildInTextAttributeKey> = HashSet::from_iter(vec![
  120. BuildInTextAttributeKey::Bold,
  121. BuildInTextAttributeKey::Italic,
  122. BuildInTextAttributeKey::Underline,
  123. BuildInTextAttributeKey::StrikeThrough,
  124. BuildInTextAttributeKey::Link,
  125. BuildInTextAttributeKey::Color,
  126. BuildInTextAttributeKey::Font,
  127. BuildInTextAttributeKey::Size,
  128. BuildInTextAttributeKey::Background,
  129. BuildInTextAttributeKey::InlineCode,
  130. ]);
  131. static ref IGNORE_KEYS: HashSet<BuildInTextAttributeKey> = HashSet::from_iter(vec![
  132. BuildInTextAttributeKey::Width,
  133. BuildInTextAttributeKey::Height,
  134. ]);
  135. }
  136. #[derive(Debug, PartialEq, Eq, Clone)]
  137. pub enum AttributeScope {
  138. Inline,
  139. Block,
  140. Embeds,
  141. Ignore,
  142. }