Skip to content

Commit

Permalink
Issue #39 - Add support for block variations
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Oct 26, 2020
1 parent 7f4beae commit b0831b5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 31 deletions.
11 changes: 7 additions & 4 deletions blocks/oik-blockicon/blockicons.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,13 @@ function getBlockorVariation( blocknamebarvariation ) {
var variation_name = undefined;
if ( parts.length > 1 ) {
variation_name = parts[1];
var variations = getBlockVariations( blockname, 'block');
var variation = variations.find( variation => variation.name === variation_name);
if ( variation ) {
block = cloneVariation( variation, block );
//var variations = getBlockVariations( blockname, 'block');
if ( undefined !== block.variations ) {
var variations = block.variations;
var variation = variations.find(variation => variation.name === variation_name);
if (variation) {
block = cloneVariation(variation, block);
}
}

}
Expand Down
70 changes: 46 additions & 24 deletions blocks/oik-blockicon/blockvariations.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function BlockVariations( blockname ) {
if ( blockname.variations === undefined ) {
return null;
}
const blockVariations = getBlockVariations( blockname.name, 'block');
//const blockVariations = getBlockVariations( blockname.name, 'block');
var blockVariations = blockname.variations;
//console.log( blockVariations );
return( <dl>
{ blockVariations.map(( variation ) => blockVariationLink( variation, blockname)) }
Expand All @@ -46,20 +47,6 @@ function blockVariationLink( variation, block_type ) {
return BlockListItem( variationasblock, true );
}

function unused_blockVariation( variation ) {
var blocklink = getBlockLink( variation );
return(<Fragment key={variation.name}>
<dt >
<BlockIcon icon={variation.icon} />
</dt>
<dd>{variation.title} - { variation.name }</dd>

<dd>{variation.description}</dd>
<dd>{variation.block_name}</dd>
</Fragment> );

}

/**
* We want to return an array of variations.
* @param block_types
Expand All @@ -81,7 +68,8 @@ function getAllBlockVariations( block_types ) {
*/

function getPrefixedBlockVariations( block_type ) {
var variations = getBlockVariations( block_type.name );
// var variations = getBlockVariations( block_type.name );
var variations = block_type.variations;
var prefixed_variations = variations.map(( variation ) => cloneVariation( variation, block_type ));
return prefixed_variations;
}
Expand Down Expand Up @@ -112,6 +100,34 @@ function cloneVariation( variation, block_type ) {

}


function title_to_permalink_part( title ) {
var blockTitle = title;
blockTitle = blockTitle.replace(/ /g, '-');
blockTitle = blockTitle.replace( /\//g, '' );
blockTitle = blockTitle.replace( /--/g, '-' );
blockTitle = blockTitle.toLowerCase();
return blockTitle;
}


/**
* Returns the site's home URL.
*
* This should be similar to the value we'd get from the WordPress home URL logic
* returning the base URL from which to construct a link, including a trailing slash
* e.g. https://s.b/wp56/ for the installation in the wp56 folder
* or https://blocks.wp-a2z.org/ for a subdomain install in a WPMS network.
*
* @returns {*}
*/
function getHomeUrl() {
var windowLocationHref = window.location.href;
var originPathName = window.location.origin + window.location.pathname;
var homeUrl = originPathName.replace( 'wp-admin/post.php', '');
return homeUrl;
}

/**
* Gets a link to the variation of the block
*
Expand All @@ -120,18 +136,23 @@ function cloneVariation( variation, block_type ) {
*/

function getVariationLink( block ) {
var blockTitle = block.block_title.replace(/ /g, '-');
blockTitle = blockTitle.replace( /\//g, '' );
blockTitle = blockTitle.replace( /--/g, '-' );
blockTitle = blockTitle.toLowerCase();
var blockTitle = title_to_permalink_part( block.block_title );
//.replace(/ /g, '-');
//blockTitle = blockTitle.replace( /\//g, '' );
//blockTitle = blockTitle.replace( /--/g, '-' );
//blockTitle = blockTitle.toLowerCase();
//var blockName = block.name.replace('/', '-');
var blockLink = null;
var prefix = null;
var siteurl = select('core/editor').getPermalinkParts();
var variationName = block.name;
//var prefix = null;
var prefix = getHomeUrl() + 'block/';
//var siteurl = select('core/editor').getPermalinkParts();
//console.log( block);
var variationTitle = title_to_permalink_part( block.title );
var blockName = block.block_name.replace( '/', '-' );

/*
if (siteurl !== null) {
//console.log(siteurl);
var postType = select('core/editor').getCurrentPostType();
Expand All @@ -141,7 +162,8 @@ function getVariationLink( block ) {
} else {
console.log("SiteURL's null");
}
blockLink = `${prefix}${blockTitle}-${blockName}/${variationName}`;
*/
blockLink = `${prefix}${blockTitle}-${blockName}/${variationTitle}-${blockName}`;
return blockLink;
}

Expand Down
8 changes: 6 additions & 2 deletions blocks/oik-blockinfo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,15 @@ export default registerBlockType(

save: props => {
return BlockinfoStyled( props.attributes.blockicon,
props.attributes.showBlockLink,
props.attributes.showBlockIcon,
props.attributes.showBlockLink, props.attributes.showBlockTypeName, props.attributes.showTitle, props.attributes.showDescription,
props.attributes.showBlockTypeName,
props.attributes.showTitle,
props.attributes.showDescription,
props.attributes.showCategory,
props.attributes.showKeywords,
props.attributes.showVariations,
props );
},
},
);
);
10 changes: 9 additions & 1 deletion blocks/oik-blocklist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,18 @@ export default registerBlockType(


},

example: {
attributes: {
showBlockLink: false,
prefix: 'oik',
}
},





edit: props => {


Expand Down Expand Up @@ -206,7 +213,8 @@ export default registerBlockType(
/>
*/
save: props => {
return BlockListStyled( props.attributes.prefix, props.attributes.showBlockLink, props.attributes.showCreateBlockLink, props.attributes.showDescription, props.attributes.showBatch, props.attributes.component, props );
console.log( "BlockListStyled - save");
return BlockListStyled( props.attributes.prefix, props.attributes.showBlockLink, props.attributes.showDescription, props.attributes.showBatch, props.attributes.component, props );
},
},
);

0 comments on commit b0831b5

Please sign in to comment.