config.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { BlockData, BlockType } from '$app/interfaces/document';
  2. export enum SplitRelationship {
  3. NextSibling,
  4. FirstChild,
  5. }
  6. /**
  7. * If the block type is not in the config, it will be thrown an error in development env
  8. */
  9. export const blockConfig: Record<
  10. string,
  11. {
  12. /**
  13. * Whether the block can have children
  14. */
  15. canAddChild: boolean;
  16. /**
  17. * The regexps that will be used to match the markdown flag
  18. */
  19. markdownRegexps?: RegExp[];
  20. /**
  21. * The default data of the block
  22. */
  23. defaultData?: BlockData<any>;
  24. /**
  25. * The props that will be passed to the text split function
  26. */
  27. splitProps?: {
  28. /**
  29. * The relationship between the next line block and the current block
  30. */
  31. nextLineRelationShip: SplitRelationship;
  32. /**
  33. * The type of the next line block
  34. */
  35. nextLineBlockType: BlockType;
  36. };
  37. }
  38. > = {
  39. [BlockType.TextBlock]: {
  40. canAddChild: true,
  41. defaultData: {
  42. delta: [],
  43. },
  44. splitProps: {
  45. nextLineRelationShip: SplitRelationship.NextSibling,
  46. nextLineBlockType: BlockType.TextBlock,
  47. },
  48. },
  49. [BlockType.HeadingBlock]: {
  50. canAddChild: false,
  51. splitProps: {
  52. nextLineRelationShip: SplitRelationship.NextSibling,
  53. nextLineBlockType: BlockType.TextBlock,
  54. },
  55. /**
  56. * # or ## or ###
  57. */
  58. markdownRegexps: [/^(#{1,3})$/],
  59. },
  60. [BlockType.TodoListBlock]: {
  61. canAddChild: true,
  62. defaultData: {
  63. delta: [],
  64. checked: false,
  65. },
  66. splitProps: {
  67. nextLineRelationShip: SplitRelationship.NextSibling,
  68. nextLineBlockType: BlockType.TodoListBlock,
  69. },
  70. /**
  71. * -[] or -[x] or -[ ] or [] or [x] or [ ]
  72. */
  73. markdownRegexps: [/^((-)?\[(x|\s)?\])$/],
  74. },
  75. [BlockType.BulletedListBlock]: {
  76. canAddChild: true,
  77. defaultData: {
  78. delta: [],
  79. format: 'default',
  80. },
  81. splitProps: {
  82. nextLineRelationShip: SplitRelationship.NextSibling,
  83. nextLineBlockType: BlockType.BulletedListBlock,
  84. },
  85. /**
  86. * - or + or *
  87. */
  88. markdownRegexps: [/^(\s*[-+*])$/],
  89. },
  90. [BlockType.NumberedListBlock]: {
  91. canAddChild: true,
  92. defaultData: {
  93. delta: [],
  94. format: 'default',
  95. },
  96. splitProps: {
  97. nextLineRelationShip: SplitRelationship.NextSibling,
  98. nextLineBlockType: BlockType.NumberedListBlock,
  99. },
  100. /**
  101. * 1. or 2. or 3.
  102. * a. or b. or c.
  103. */
  104. markdownRegexps: [/^(\s*[\d|a-zA-Z]+\.)$/],
  105. },
  106. [BlockType.QuoteBlock]: {
  107. canAddChild: true,
  108. defaultData: {
  109. delta: [],
  110. size: 'default',
  111. },
  112. splitProps: {
  113. nextLineRelationShip: SplitRelationship.NextSibling,
  114. nextLineBlockType: BlockType.TextBlock,
  115. },
  116. /**
  117. * " or “ or ”
  118. */
  119. markdownRegexps: [/^("|“|”)$/],
  120. },
  121. [BlockType.ToggleListBlock]: {
  122. canAddChild: true,
  123. defaultData: {
  124. delta: [],
  125. collapsed: false,
  126. },
  127. splitProps: {
  128. nextLineRelationShip: SplitRelationship.FirstChild,
  129. nextLineBlockType: BlockType.TextBlock,
  130. },
  131. /**
  132. * >
  133. */
  134. markdownRegexps: [/^(>)$/],
  135. },
  136. [BlockType.CodeBlock]: {
  137. canAddChild: false,
  138. /**
  139. * ```
  140. */
  141. markdownRegexps: [/^(```)$/],
  142. },
  143. };