Element.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React from 'react'
  21. import { RenderElementProps } from 'slate-react'
  22. import { ElementTypes, BlockProperties } from './constants'
  23. import ImageElement from './ImageElement'
  24. import MarqueeElement from './MarqueeElement'
  25. const ElementPropertyList = [BlockProperties.TextAlign]
  26. const Element: React.FC<RenderElementProps> = (props) => {
  27. const { attributes, children, element } = props
  28. const cssStyle: React.CSSProperties = ElementPropertyList.reduce(
  29. (obj, propertyName) => {
  30. if (element[propertyName]) {
  31. obj[propertyName] = element[propertyName]
  32. }
  33. return obj
  34. },
  35. {}
  36. )
  37. switch (element.type) {
  38. case ElementTypes.BlockQuote:
  39. return (
  40. <blockquote {...attributes} style={cssStyle}>
  41. {children}
  42. </blockquote>
  43. )
  44. case ElementTypes.Code:
  45. return (
  46. <pre style={cssStyle}>
  47. <code {...attributes}>{children}</code>
  48. </pre>
  49. )
  50. // List Elements
  51. case ElementTypes.BulletedList:
  52. return (
  53. <ul {...attributes} style={cssStyle}>
  54. {children}
  55. </ul>
  56. )
  57. case ElementTypes.NumberedList:
  58. return (
  59. <ol {...attributes} style={cssStyle}>
  60. {children}
  61. </ol>
  62. )
  63. case ElementTypes.ListItem:
  64. return (
  65. <li {...attributes} style={cssStyle}>
  66. {children}
  67. </li>
  68. )
  69. // Headings Elements
  70. case ElementTypes.HeadingOne:
  71. return (
  72. <h1 {...attributes} style={cssStyle}>
  73. {children}
  74. </h1>
  75. )
  76. case ElementTypes.HeadingTwo:
  77. return (
  78. <h2 {...attributes} style={cssStyle}>
  79. {children}
  80. </h2>
  81. )
  82. case ElementTypes.HeadingThree:
  83. return (
  84. <h3 {...attributes} style={cssStyle}>
  85. {children}
  86. </h3>
  87. )
  88. case ElementTypes.HeadingFour:
  89. return (
  90. <h4 {...attributes} style={cssStyle}>
  91. {children}
  92. </h4>
  93. )
  94. case ElementTypes.HeadingFive:
  95. return (
  96. <h5 {...attributes} style={cssStyle}>
  97. {children}
  98. </h5>
  99. )
  100. case ElementTypes.HeadingSix:
  101. return (
  102. <h6 {...attributes} style={cssStyle}>
  103. {children}
  104. </h6>
  105. )
  106. case ElementTypes.Link:
  107. return (
  108. <a href={element.url} target="_blank" {...attributes}>
  109. {children}
  110. </a>
  111. )
  112. case ElementTypes.Image:
  113. return <ImageElement {...props} />
  114. // Table Elements
  115. case ElementTypes.Table:
  116. return (
  117. <table>
  118. <tbody {...attributes}>{children}</tbody>
  119. </table>
  120. )
  121. case ElementTypes.TableRow:
  122. return (
  123. <tr {...attributes} style={cssStyle}>
  124. {children}
  125. </tr>
  126. )
  127. case ElementTypes.TableCell:
  128. return (
  129. <td {...attributes} style={cssStyle}>
  130. {children}
  131. </td>
  132. )
  133. case ElementTypes.Marquee:
  134. return <MarqueeElement {...props} />
  135. default:
  136. return (
  137. <p {...attributes} style={cssStyle}>
  138. {children}
  139. </p>
  140. )
  141. }
  142. }
  143. export default Element