| 1 |
/** |
| 2 |
* block.js - Contains all Gutenberg functionality required by Photonic |
| 3 |
*/ |
| 4 |
let photonicBlockProperties; // Must use this, since the properties of a block are getting reset in the "waitForIFrame" call |
| 5 |
(function (wp) { |
| 6 |
const el = wp.element.createElement; |
| 7 |
const {__, _x, _n, _nx} = wp.i18n; |
| 8 |
let components = wp.components; |
| 9 |
const iconEl = el('svg', {width: 23, height: 24, viewBox: "0 0 24 24"}, |
| 10 |
el('g', {transform: "scale(0.046785)"}, |
| 11 |
el('circle', {cx: "256", cy: "192", r: "128", fill: "#0085ba"}), |
| 12 |
el('rect', {width: "64", height: "256", x: "128", y: "192", fill: "#0085ba"}), |
| 13 |
el('circle', {cx: "256", cy: "192", r: "64", fill: "white"}), |
| 14 |
el('rect', {width: "16", height: "128", x: "192", y: "192", fill: "white"}) |
| 15 |
) |
| 16 |
); |
| 17 |
|
| 18 |
const tag = Photonic_Gutenberg_JS.shortcode.toLowerCase() === 'gallery' ? 'gallery__photonic_random_314159' : Photonic_Gutenberg_JS.shortcode; |
| 19 |
wp.blocks.registerBlockType('photonic/gallery', { |
| 20 |
title: __('Photonic Gallery', 'photonic'), |
| 21 |
apiVersion: 3, |
| 22 |
category: 'widgets', |
| 23 |
keywords: ['flickr', 'smugmug', 'google'], |
| 24 |
icon: iconEl, |
| 25 |
supports: { |
| 26 |
html: false, |
| 27 |
align: ['wide', 'full'] |
| 28 |
}, |
| 29 |
|
| 30 |
transforms: { |
| 31 |
from: [ |
| 32 |
{ |
| 33 |
type: 'shortcode', |
| 34 |
tag: tag, |
| 35 |
attributes: { |
| 36 |
shortcode: { |
| 37 |
type: 'string', |
| 38 |
shortcode: function (named) { |
| 39 |
return JSON.stringify(named.named); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
}, |
| 44 |
// Works for WP >= 5.4, which is the current release. Will not work for older versions, so this will be uncommented |
| 45 |
// once WP 5.6 is out. Photonic is compatible with WP upto 2 versions old, and WP 4.9. |
| 46 |
{ |
| 47 |
type: 'shortcode', |
| 48 |
tag: 'gallery', |
| 49 |
isMatch: function (attr) { |
| 50 |
const layouts = ['square', 'circle', 'random', 'mosaic', 'masonry', 'strip-above', 'strip-below', 'strip-right', 'no-strip']; |
| 51 |
const providers = ['flickr', 'smugmug', 'google', 'zenfolio', 'instagram']; |
| 52 |
return (attr.named.style !== undefined && layouts.indexOf(attr.named.style) >= 0 && attr.named.type === undefined) || |
| 53 |
(attr.named.type !== undefined && providers.indexOf(attr.named.type) >= 0 && attr.named.layout !== undefined && layouts.indexOf(attr.named.layout) >= 0); |
| 54 |
}, |
| 55 |
attributes: { |
| 56 |
shortcode: { |
| 57 |
type: 'string', |
| 58 |
shortcode: function (named) { |
| 59 |
return JSON.stringify(named.named); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
}, |
| 64 |
{ |
| 65 |
type: 'block', |
| 66 |
blocks: ['core/gallery'], |
| 67 |
transform: function (attributes) { |
| 68 |
const images = attributes.images; |
| 69 |
let ids = ''; |
| 70 |
Array.prototype.forEach.call(images, function (image) { |
| 71 |
ids += image.id + ','; |
| 72 |
}); |
| 73 |
|
| 74 |
if (ids.length > 0) { |
| 75 |
ids = ids.slice(0, -1); |
| 76 |
} |
| 77 |
|
| 78 |
let sc = { |
| 79 |
type: 'wp', |
| 80 |
ids: ids |
| 81 |
}; |
| 82 |
|
| 83 |
return wp.blocks.createBlock('photonic/gallery', { |
| 84 |
shortcode: JSON.stringify(sc) |
| 85 |
}); |
| 86 |
} |
| 87 |
} |
| 88 |
] |
| 89 |
}, |
| 90 |
|
| 91 |
attributes: { |
| 92 |
shortcode: { |
| 93 |
type: 'string' |
| 94 |
} |
| 95 |
}, |
| 96 |
|
| 97 |
/** |
| 98 |
* Called when Gutenberg initially loads the block. |
| 99 |
*/ |
| 100 |
edit: function (props) { |
| 101 |
let nativeMediaLibrary = new PhotonicWPNativeUI(sendDataToWizard, getDataFromWizard); |
| 102 |
let markup = [], iconClass = ''; |
| 103 |
let shortcode = props.attributes.shortcode || '{}'; |
| 104 |
shortcode = JSON.parse(shortcode); |
| 105 |
|
| 106 |
const blockProps = wp.blockEditor.useBlockProps(); // Required for the "container" for the block, since apiVersion 2 & higher |
| 107 |
|
| 108 |
const providers = { |
| 109 |
'wp': 'WordPress', |
| 110 |
'flickr': 'Flickr', |
| 111 |
'smugmug': 'SmugMug', |
| 112 |
'google': 'Google Photos', |
| 113 |
'picasa': 'Picasa', |
| 114 |
'zenfolio': 'Zenfolio', |
| 115 |
'instagram': 'Instagram' |
| 116 |
}; |
| 117 |
let source; |
| 118 |
|
| 119 |
if (JSON.stringify(shortcode) !== JSON.stringify({}) && (shortcode.type === undefined || shortcode.type === 'default')) { |
| 120 |
iconClass = 'photonic-wp'; |
| 121 |
source = 'wp'; |
| 122 |
} |
| 123 |
else if (shortcode.type !== undefined && ['wp', 'flickr', 'smugmug', 'google', 'picasa', 'zenfolio', 'instagram'].indexOf(shortcode.type) > -1) { |
| 124 |
iconClass = 'photonic-' + shortcode.type; |
| 125 |
source = shortcode.type; |
| 126 |
} |
| 127 |
|
| 128 |
const title = iconClass === '' ? __('Add Photonic Gallery', 'photonic') : __('Edit Photonic Gallery', 'photonic') + ' (' + __('Source: ', 'photonic') + providers[source] + ')'; |
| 129 |
|
| 130 |
nativeMediaLibrary.waitForIFrame(); |
| 131 |
|
| 132 |
const openWizard = () => { |
| 133 |
photonicBlockProperties = props; // Must use this, since the properties of a block are getting reset in the "waitForIFrame" call |
| 134 |
tb_show(title, Photonic_Gutenberg_JS.flow_url); |
| 135 |
}; |
| 136 |
|
| 137 |
function sendDataToWizard(messageChannel) { |
| 138 |
messageChannel.port1.postMessage({ |
| 139 |
type: 'photonicBlock', |
| 140 |
object: photonicBlockProperties.attributes, // Must use this, since the properties of a block are getting reset in the "waitForIFrame" call |
| 141 |
}); |
| 142 |
} |
| 143 |
|
| 144 |
function getDataFromWizard(data) { |
| 145 |
photonicBlockProperties.setAttributes({shortcode: data.props}); |
| 146 |
} |
| 147 |
|
| 148 |
markup.push( |
| 149 |
el('div', blockProps, |
| 150 |
el('div', {key: 'photonic-placeholder', className: 'photonic-gallery'}, |
| 151 |
el('a', {className: 'photonic-placeholder-icon photonic ' + iconClass, onClick: openWizard}), |
| 152 |
title |
| 153 |
) |
| 154 |
) |
| 155 |
); |
| 156 |
|
| 157 |
return (markup); |
| 158 |
}, |
| 159 |
|
| 160 |
/** |
| 161 |
* Called when Gutenberg "saves" the block to post_content |
| 162 |
*/ |
| 163 |
save: function (props) { |
| 164 |
return null; |
| 165 |
} |
| 166 |
}); |
| 167 |
})(window.wp); |
| 168 |
|