Skip to content

Commit

Permalink
Issue #34 - refactor, ready to add post_parent and number_posts
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Feb 2, 2020
1 parent 1aa3df7 commit defa815
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 172 deletions.
45 changes: 45 additions & 0 deletions blocks/oik-content/attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const shortcode_attributes =
{
shortcode: {
type: 'string',
default: '',
},

content: {
type: 'string',
default: '',
},

parameters: {
type: 'string',
default: '',
},

post_type: {
type: 'string',
default: '',
},

post_parent: {
type: 'string',
default: '.',
},

number_posts: {
type: 'string',
default: 10,
}

// post_parent = ., 0, ID null
// categories
// number_posts = -1, 1->100
// offset = null,
// orderby =
// order = ASC / DESC
// per_page
//


};

export { shortcode_attributes};
139 changes: 139 additions & 0 deletions blocks/oik-content/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/**
* Implements the edit part of oik/content-block
*
* @copyright (C) Copyright Bobbing Wide 2020
* @author Herb Miller @bobbingwide
*/
const { __ } = wp.i18n;
const {
Editable,

AlignmentToolbar,
BlockControls,
ServerSideRender,
} = wp.editor;
const {
PlainText,
InspectorControls,
} = wp.blockEditor;

const {
withInstanceId,
} = wp.compose;

const {
PanelBody,
PanelRow,
FormToggle,
TextControl,
TextareaControl,
SelectControl,
} = wp.components;
const Fragment = wp.element.Fragment;
import { map, partial, has } from 'lodash';




import {bw_shortcodes, getAttributes} from "./bw_shortcodes";
import {PostTypes} from "./post_type";

const edit= withInstanceId(
( { attributes, setAttributes, instanceId, isSelected } ) => {
const inputId = `blocks-shortcode-input-${ instanceId }`;


const onChangeContent = ( value ) => {
setAttributes( { content: value } );
};

const onChangeParameters = ( value ) => {
setAttributes( { parameters: value } );
}

const onChangeShortcode = ( value ) => {

attributes = getAttributes( value );
setAttributes( { shortcode: value } );
};


function onChangeAttr( key, value ) {
//var nextAttributes = {};
//nextAttributes[ key ] = value;
//setAttributes( nextAttributes );
setAttributes( { [key] : value } );
};

const onChangePostType = ( value ) => {
attributes = getAttributes( value );
setAttributes( { post_type: value });
}

const onChangePostParent = ( value ) => {
setAttributes( { post_parent: value });
}

const onChangeNumberPosts = ( value ) => {
setAttributes( { number_posts: value } );
}


/*
<GenericAttrs value={attributes.shortcode} />
*/
return (
<Fragment>

<InspectorControls>
<PanelBody>
<SelectControl label="Display" value={attributes.shortcode}
options={ map( bw_shortcodes, ( key, label ) => ( { value: label, label: label + ' - ' + key } ) ) }
onChange={partial( onChangeAttr, 'shortcode' )}
/>

<PostTypes value={ attributes.post_type } onChange={onChangePostType } />

<TextareaControl label="Parameters"
value={ attributes.parameters }
placeholder={ __( 'Enter your shortcode parameters' ) }
onChange={onChangeParameters}
rows="1"
/>
<TextareaControl label="Content"
id={ inputId }
value={ attributes.content }
placeholder={ __( 'Enter your shortcode content' ) }
onChange={onChangeContent}
/>






</PanelBody>
</InspectorControls>


<div className="wp-block-oik-block-shortcode wp-block-shortcode">
<SelectControl label="Display" value={attributes.shortcode}
options={ map( bw_shortcodes, ( key, label ) => ( { value: label, label: label + ' - ' + key } ) ) }
onChange={partial( onChangeAttr, 'shortcode' )}
/>



</div>
<div>
<ServerSideRender
block="oik/content-block" attributes={ attributes }
/>
</div>
</Fragment>

);
}
)

export { edit };
136 changes: 7 additions & 129 deletions blocks/oik-content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ const {
registerBlockType,
} = wp.blocks;

const {
Editable,

AlignmentToolbar,
BlockControls,
ServerSideRender,
} = wp.editor;
const {
PlainText,
InspectorControls,
} = wp.blockEditor;

const {
Toolbar,
Expand All @@ -38,46 +27,22 @@ const {
SelectControl,
} = wp.components;

const {
withInstanceId,
} = wp.compose;

const Fragment = wp.element.Fragment;
const RawHTML = wp.element.RawHTML;

//var TextControl = wp.blocks.InspectorControls.TextControl;

import { bw_shortcodes, getAttributes, bw_shortcodes_attrs } from './bw_shortcodes';
import { BwQueryControls } from './query_controls';

//import GenericAttrs from './GenericAttrs';
import { bwPostTypes, bw_post_types, PostTypes } from './post_type';
import { PostTypes } from './post_type';
import { edit } from './edit';

import { map, partial, has } from 'lodash';

const shortcode_attributes =
{
shortcode: {
type: 'string',
default: '',
},

content: {
type: 'string',
default: '',
},

parameters: {
type: 'string',
default: '',
},

post_type: {
type: 'string',
default: '',
}


};

import { shortcode_attributes} from './attributes';



Expand Down Expand Up @@ -123,100 +88,13 @@ export default registerBlockType(
customClassName: false,
className: false,
html: false,
alignWide: true,
},

edit: withInstanceId(
( { attributes, setAttributes, instanceId, isSelected } ) => {
const inputId = `blocks-shortcode-input-${ instanceId }`;


const onChangeContent = ( value ) => {
setAttributes( { content: value } );
};

const onChangeParameters = ( value ) => {
setAttributes( { parameters: value } );
}

const onChangeShortcode = ( value ) => {

attributes = getAttributes( value );
setAttributes( { shortcode: value } );
};


function onChangeAttr( key, value ) {
//var nextAttributes = {};
//nextAttributes[ key ] = value;
//setAttributes( nextAttributes );
setAttributes( { [key] : value } );
};

const onChangePostType = ( value ) => {
attributes = getAttributes( value );
setAttributes( { post_type: value });
}


/*
<GenericAttrs value={attributes.shortcode} />
*/
return (
<Fragment>

<InspectorControls>
<PanelBody>
<SelectControl label="Shortcode" value={attributes.shortcode}
options={ map( bw_shortcodes, ( key, label ) => ( { value: label, label: label + ' - ' + key } ) ) }
onChange={partial( onChangeAttr, 'shortcode' )}
/>
<TextareaControl label="Parameters"
value={ attributes.parameters }
placeholder={ __( 'Enter your shortcode parameters' ) }
onChange={onChangeParameters}
rows="1"
/>
<TextareaControl label="Content"
id={ inputId }
value={ attributes.content }
placeholder={ __( 'Enter your shortcode content' ) }
onChange={onChangeContent}
/>
<PostTypes value={ attributes.post_type } onChange={onChangePostType } />




</PanelBody>
</InspectorControls>


<div className="wp-block-oik-block-shortcode wp-block-shortcode">
<SelectControl label="Shortcode" value={attributes.shortcode}
options={ map( bw_shortcodes, ( key, label ) => ( { value: label, label: label + ' - ' + key } ) ) }
onChange={partial( onChangeAttr, 'shortcode' )}
/>



</div>
<div>
<ServerSideRender
block="oik/content-block" attributes={ attributes }
/>
</div>
</Fragment>
edit,

);
}
),


/**
* We intend to render this dynamically. The content created by the user
* is stored in the content attribute.
*
*/
save( { attributes } ) {
return null;
},
Expand Down
4 changes: 4 additions & 0 deletions blocks/oik-content/number_posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Usage:
* <NumberPosts value={ attributes.number_posts } onChange={onChangeNumberPosts} />
*/
Loading

0 comments on commit defa815

Please sign in to comment.