Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { __ } = wp.i18n; // Import __() from wp.i18n
  2.  
  3. wp.blocks.registerBlockType('my/media-box', {
  4.     title: 'MyPlaylist',
  5.     description: 'Block inserts custom player',
  6.     icon: 'playlist-video',
  7.     category: 'common',
  8.     supports: {
  9.         multiple: false,
  10.         html: true,
  11.         reusable: false,
  12.     },
  13.     attributes: {
  14.         multiple: false,
  15.         html: true,
  16.         contentHTML: {
  17.             type: 'string',
  18.             source: 'html',
  19.             selector: 'div',
  20.         },
  21.         content: {
  22.             type: 'string',
  23.             source: 'meta',
  24.             meta: 'content',
  25.         },
  26.         thumbnail: {
  27.             type: 'string',
  28.             source: 'meta',
  29.             meta: 'thumbnail'
  30.         },
  31.         playlistId: {
  32.             type: 'number',
  33.             source: 'meta',
  34.             meta: 'playlistId'
  35.         }
  36.     },
  37.  
  38.     edit: function (props) {
  39.         const { attributes: { contentHTML, thumbnail, playlistId }, setAttributes } = props;
  40.         const postTitle = wp.data.select('core/editor').getCurrentPostAttribute('title');
  41.         const msgEventListener = (msg) => {
  42.             if (msg && msg.data && msg.data.embed) {
  43.                 doInsert({
  44.                     embedCode: msg.data.embed.embedCode,
  45.                     thumbnail: msg.data.thumbnail,
  46.                     playlistId: msg.data.playlistId
  47.                 })
  48.             }
  49.         };
  50.  
  51.         function updateContent(data) {
  52.             setAttributes({
  53.                 contentHTML: data.embedCode,
  54.                 thumbnail: data.thumbnail,
  55.                 playlistId: data.playlistId
  56.             });
  57.  
  58.         }
  59.  
  60.         function closeModal() {
  61.             setAttributes({
  62.                 isModalOpen: false,
  63.             });
  64.         }
  65.  
  66.         function openModal() {
  67.             setAttributes({
  68.                 isModalOpen: true,
  69.             });
  70.         }
  71.  
  72.         function doInsert(data) {
  73.             setAttributes({
  74.                 isModalOpen: false,
  75.             });
  76.  
  77.             window.removeEventListener("message", msgEventListener, false);
  78.  
  79.             updateContent(data);
  80.         }
  81.  
  82.         if (postTitle === 'Auto Draft' || postTitle === '') return wp.element.createElement(
  83.             "div",
  84.             null,
  85.             wp.element.createElement("div", {
  86.                 className: "components-placeholder"
  87.             },
  88.             __('Save post with title first', "my-media")
  89.             )
  90.         );
  91.        
  92.         return wp.element.createElement(
  93.             wp.element.Fragment,
  94.             null,
  95.             wp.element.createElement(
  96.                 'div',
  97.                 {
  98.                     className: "components-placeholder",
  99.                 },
  100.  
  101.                 thumbnail && thumbnail.length
  102.                 ?
  103.                 wp.element.createElement('img', {
  104.                     src: thumbnail,
  105.                 })
  106.                 :
  107.                 wp.element.createElement(wp.components.Icon, {
  108.                     icon: 'format-video',
  109.                     size: 320,
  110.                 },
  111.                     ''
  112.                 ),
  113.                 wp.element.createElement(wp.components.IconButton, {
  114.                     icon: "playlist-video",
  115.                     onClick: openModal,
  116.                     isLarge: !0,
  117.                 },
  118.                     contentHTML ? __('Edit', 'my-media') : __("My Playlist", "my-media")
  119.                 ),
  120.  
  121.                 props.attributes.isModalOpen
  122.                 ?
  123.                 window.removeEventListener("message", msgEventListener)
  124.                 ||
  125.                 window.addEventListener("message", msgEventListener, false)
  126.                 ||
  127.                 React.createElement(wp.components.Modal, {
  128.                     shouldCloseOnEsc: true,
  129.                     isDefault: true,
  130.                     shouldCloseOnClickOutside: false,
  131.                     onRequestClose: closeModal,
  132.                     title: 'Insert My Playlist',
  133.                 },
  134.                 React.createElement("iframe", {
  135.                     src: `URL_TO_EMBEDCODE`,
  136.                     className: 'my-media-iframe'
  137.                 }),
  138.                 )
  139.                 : null,
  140.  
  141.             ),
  142.         );
  143.     },
  144.     save: function ({attributes: { contentHTML }}) {
  145.         return wp.element.createElement(wp.element.RawHTML, null, contentHTML);
  146.     }
  147. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement