Guest User

Untitled

a guest
Jan 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. const { RichText, MediaUpload, PlainText } = wp.editor;
  2. const { registerBlockType } = wp.blocks;
  3. const { Button } = wp.components;
  4.  
  5. import './style.scss';
  6. import './editor.scss';
  7.  
  8. registerBlockType('stackoverflow/image-card', {
  9. title: "Image Card",
  10. icon: 'feedback',
  11. category: 'common',
  12. attributes: {
  13. title: {
  14. source: 'text',
  15. selector: '.imageCard__title'
  16. },
  17. body: {
  18. type: 'string',
  19. source: 'children',
  20. selector: '.imageCard__body'
  21. },
  22. imageAlt: {
  23. attribute: 'alt',
  24. selector: '.imageCard__image'
  25. },
  26. imageUrl: {
  27. attribute: 'src',
  28. selector: '.imageCard__image'
  29. }
  30. },
  31.  
  32.  
  33.  
  34. edit({ attributes, className, setAttributes }) {
  35.  
  36. const getImageButton = (openEvent) => {
  37. if(attributes.imageUrl) {
  38. return (
  39. <img
  40. src={ attributes.imageUrl }
  41. onClick={ openEvent }
  42. className="image"
  43. />
  44. );
  45. }
  46. else {
  47. return (
  48. <div className="button-container">
  49. <Button
  50. onClick={ openEvent }
  51. className="button button-large"
  52. >
  53. Pick an image
  54. </Button>
  55. </div>
  56. );
  57. }
  58. };
  59.  
  60. return (
  61. <div className="container">
  62. <MediaUpload
  63. onSelect={ media => { setAttributes({ imageAlt: media.alt, imageUrl: media.url }); } }
  64. type="image"
  65. value={ attributes.imageID }
  66. render={ ({ open }) => getImageButton(open) }
  67. />
  68. <h3>
  69. <PlainText
  70. onChange={ content => setAttributes({ title: content }) }
  71. value={ attributes.title }
  72. placeholder="Your card title"
  73. className="heading"
  74. />
  75. </h3>
  76. <div className={className}>
  77. <RichText
  78. onChange={ content => setAttributes({ body: content }) }
  79. value={ attributes.body }
  80. multiline="p"
  81. placeholder="Your card text"
  82. />
  83. </div>
  84. </div>
  85. );
  86. },
  87.  
  88. save({ attributes }) {
  89.  
  90. const cardImage = (src, alt) => {
  91. if(!src) return null;
  92.  
  93. if(alt) {
  94. return (
  95. <img
  96. className="card__image"
  97. src={ src }
  98. alt={ alt }
  99. />
  100. );
  101. }
  102.  
  103. // No alt set, so let's hide it from screen readers
  104. return (
  105. <img
  106. className="card__image"
  107. src={ src }
  108. alt=""
  109. aria-hidden="true"
  110. />
  111. );
  112. };
  113.  
  114. return (
  115. <div className="container">
  116. <img
  117. className="card__image"
  118. src={ attributes.imageUrl }
  119. alt={ attributes.imageAlt }
  120. />
  121. <h3 className="card__title">{ attributes.title }</h3>
  122. <div className="card__body">
  123. { attributes.body }
  124. </div>
  125. </div>
  126. );
  127. }
  128. });
Add Comment
Please, Sign In to add comment