Guest User

Untitled

a guest
Apr 20th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. import min from 'lodash/min'
  2.  
  3. import { BodyComponent } from 'mjml-core'
  4.  
  5. import widthParser from 'mjml-core/lib/helpers/widthParser'
  6.  
  7. export default class McImage extends BodyComponent {
  8. static tagOmission = true
  9.  
  10. static allowedAttributes = {
  11. 'mc:edit': 'string',
  12. 'alt': 'string',
  13. 'href': 'string',
  14. 'src': 'string',
  15. 'srcset': 'string',
  16. 'title': 'string',
  17. align: 'enum(left,center,right)',
  18. border: 'string',
  19. 'border-bottom': 'string',
  20. 'border-left': 'string',
  21. 'border-right': 'string',
  22. 'border-top': 'string',
  23. 'border-radius': 'unit(px,%)',
  24. 'container-background-color': 'string',
  25. padding: 'unit(px,%){1,4}',
  26. 'padding-bottom': 'unit(px,%)',
  27. 'padding-left': 'unit(px,%)',
  28. 'padding-right': 'unit(px,%)',
  29. 'padding-top': 'unit(px,%)',
  30. height: 'unit(px,%)',
  31. width: 'unit(px,%)',
  32. }
  33.  
  34. static defaultAttributes = {
  35. align: 'center',
  36. border: '0',
  37. height: 'auto',
  38. padding: '10px 25px',
  39. target: '_blank',
  40. }
  41.  
  42. getStyles() {
  43. const width = this.getContentWidth()
  44. const fullWidth = this.getAttribute('full-width') === 'full-width'
  45.  
  46. const { parsedWidth, unit } = widthParser(width)
  47.  
  48. return {
  49. img: {
  50. border: this.getAttribute('border'),
  51. 'border-radius': this.getAttribute('border-radius'),
  52. display: 'block',
  53. outline: 'none',
  54. 'text-decoration': 'none',
  55. 'min-width': fullWidth ? '100%' : null,
  56. width: fullWidth ? `${parsedWidth}${unit}` : '100%',
  57. 'max-width': fullWidth ? '100%' : null,
  58. },
  59. td: {
  60. width: fullWidth ? null : `${parsedWidth}${unit}`,
  61. },
  62. table: {
  63. 'min-width': fullWidth ? '100%' : null,
  64. 'max-width': fullWidth ? '100%' : null,
  65. width: fullWidth ? `${parsedWidth}${unit}` : null,
  66. 'border-collapse': 'collapse',
  67. 'border-spacing': '0px',
  68. },
  69. }
  70. }
  71.  
  72. getContentWidth() {
  73. const { containerWidth } = this.context
  74.  
  75. const width = this.getAttribute('width')
  76. ? min([
  77. parseInt(this.getAttribute('width'), 10),
  78. parseInt(containerWidth, 10),
  79. ])
  80. : parseInt(containerWidth, 10)
  81.  
  82. const paddingRight = this.getShorthandAttrValue('padding', 'right')
  83. const paddingLeft = this.getShorthandAttrValue('padding', 'left')
  84.  
  85. const widthOverflow =
  86. paddingLeft +
  87. paddingRight +
  88. parseFloat(width) -
  89. parseInt(containerWidth, 10)
  90.  
  91. return widthOverflow > 0
  92. ? parseFloat(width - widthOverflow)
  93. : parseFloat(width)
  94. }
  95.  
  96. renderImage() {
  97. const img = `
  98. <img
  99. ${this.htmlAttributes({
  100. alt: this.getAttribute('alt'),
  101. height: this.getAttribute('height'),
  102. src: this.getAttribute('src'),
  103. srcset: this.getAttribute('srcset'),
  104. style: 'img',
  105. title: this.getAttribute('title'),
  106. width: this.getContentWidth(),
  107. 'mc:edit': this.getAttribute('mc:edit'),
  108. })}
  109. />
  110. `
  111.  
  112. if (this.getAttribute('href')) {
  113. return `
  114. <a
  115. ${this.htmlAttributes({
  116. href: this.getAttribute('href'),
  117. target: this.getAttribute('target'),
  118. })}
  119. >
  120. ${img}
  121. </a>
  122. `
  123. }
  124.  
  125. return img
  126. }
  127.  
  128. render() {
  129. return `
  130. <table
  131. ${this.htmlAttributes({
  132. align: this.getAttribute('align'),
  133. border: '0',
  134. cellpadding: '0',
  135. cellspacing: '0',
  136. role: 'presentation',
  137. style: 'table',
  138. })}
  139. >
  140. <tbody>
  141. <tr>
  142. <td ${this.htmlAttributes({ style: 'td' })}>
  143. ${this.renderImage()}
  144. </td>
  145. </tr>
  146. </tbody>
  147. </table>
  148. `
  149. }
  150. }
Add Comment
Please, Sign In to add comment