Skip to content
This repository has been archived by the owner on Mar 18, 2020. It is now read-only.

Commit

Permalink
Added ID to sections
Browse files Browse the repository at this point in the history
  • Loading branch information
APPLE authored and APPLE committed Mar 5, 2018
1 parent 1cc217c commit 0d94c16
Show file tree
Hide file tree
Showing 9 changed files with 4,256 additions and 41 deletions.
3,936 changes: 3,936 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
},
"dependencies": {
"cgb-scripts": "1.9.5",
"classnames": "^2.2.5"
"classnames": "^2.2.5",
"lodash": "^4.17.5",
"memize": "^1.0.5",
"npm": "^5.7.1"
}
}
Binary file added src/.DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion src/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ import "./blocks/text-and-image";
import "./blocks/features-list";
import "./blocks/team-member";
import "./blocks/section";
//import "./blocks/two-columns";
//import "./blocks/fancy-heading";
// TODO: spacer block

Binary file added src/blocks/.DS_Store
Binary file not shown.
62 changes: 23 additions & 39 deletions src/blocks/section/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import classnames from "classnames";
import icons from "../icons";
import Inspector from './inspector';

/**
* Internal block libraries
Expand All @@ -17,13 +18,9 @@ const {
InspectorControls,
BlockControls,
BlockDescription,
BlockAlignmentToolbar,
ColorPalette
BlockAlignmentToolbar
} = wp.blocks; // Import registerBlockType() from wp.blocks as well as Editable so we can use TinyMCE
const {
PanelBody,
PanelRow,
PanelColor,
TextControl,
Dashicon
} = wp.components;
Expand Down Expand Up @@ -65,6 +62,9 @@ registerBlockType("cgb/block-section", {
},
alignment: {
type: "string"
},
id: {
type: "string"
}
},

Expand All @@ -86,46 +86,29 @@ registerBlockType("cgb/block-section", {
props.setAttributes({ sectionBackgroundColor: value });
};

const updateAlignment = nextAlign =>
const updateAlignment = nextAlign => {
props.setAttributes({
alignment: nextAlign
});
};

const onChangeSectionID = value => {
props.setAttributes({
id: value
})
};

return [
!!props.focus && (
<InspectorControls key="inspector">
<PanelBody title={__("Section Spacings")}>
<PanelRow>
<TextControl
label={__("Vertical padding (rem)")}
value={props.attributes.verticalPadding}
onChange={onChangePadding}
/>
</PanelRow>
<PanelRow>
<TextControl
label={__("Top Margin (rem)")}
value={props.attributes.topMargin}
onChange={onChangeMarginTop}
/>
<TextControl
label={__("Bottom Margin (rem)")}
value={props.attributes.bottomMargin}
onChange={onChangeMarginBottom}
/>
</PanelRow>

<PanelColor
title={__("Section Background Color")}
colorValue={props.attributes.sectionBackgroundColor}
>
<ColorPalette
value={props.attributes.sectionBackgroundColor}
onChange={onChangeSectionBackgroundColor}
/>
</PanelColor>
</PanelBody>
</InspectorControls>
<Inspector
{ ...{
onChangePadding,
onChangeMarginTop,
onChangeMarginBottom,
onChangeSectionBackgroundColor,
onChangeSectionID,
...props } }
/>
),
!!props.focus && (
<BlockControls key="controls">
Expand Down Expand Up @@ -175,6 +158,7 @@ registerBlockType("cgb/block-section", {

return (
<div
id={props.attributes.id}
className={classes}
style={{
backgroundColor: props.attributes.sectionBackgroundColor,
Expand Down
73 changes: 73 additions & 0 deletions src/blocks/section/inspector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Internal block libraries
*/
const { __ } = wp.i18n;
const { Component } = wp.element;
const {
InspectorControls,
BlockDescription,
ColorPalette,
} = wp.blocks;
const {
PanelBody,
PanelRow,
PanelColor,
TextControl,
} = wp.components;

/**
* Create an Inspector Controls wrapper Component
*/
export default class Inspector extends Component {

constructor( props ) {
super( ...arguments );
}

render() {
return (
<InspectorControls key="inspector">
<PanelBody title={__("Section Spacings")}>
<PanelRow>
<TextControl
label={__("Vertical padding (rem)")}
value={this.props.attributes.verticalPadding}
onChange={this.props.onChangePadding}
/>
</PanelRow>
<PanelRow>
<TextControl
label={__("Top Margin (rem)")}
value={this.props.attributes.topMargin}
onChange={this.props.onChangeMarginTop}
/>
<TextControl
label={__("Bottom Margin (rem)")}
value={this.props.attributes.bottomMargin}
onChange={this.props.onChangeMarginBottom}
/>
</PanelRow>

<PanelColor
title={__("Section Background Color")}
colorValue={this.props.attributes.sectionBackgroundColor}
>
<ColorPalette
value={this.props.attributes.sectionBackgroundColor}
onChange={this.props.onChangeSectionBackgroundColor}
/>
</PanelColor>

<PanelRow>
<TextControl
label={__("Section ID")}
value={this.props.attributes.id}
onChange={this.props.onChangeSectionID}
/>
</PanelRow>
</PanelBody>
</InspectorControls>
);
}

}
163 changes: 163 additions & 0 deletions src/blocks/two-columns/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/**
* BLOCK: Two Columns
*
* A temporary block to show 2 columns until the native columns block improves
*/

/**
* Block Dependencies
*/
import icons from "../icons";
import Inspector from './inspector';
import { times } from 'lodash';
import memoize from 'memize';
/**
* Internal block libraries
*/

const { __, sprintf } = wp.i18n; // Import __() from wp.i18n
const {
registerBlockType,
BlockControls,
InnerBlocks,
InspectorControls
} = wp.blocks; // Import registerBlockType() from wp.blocks as well as Editable so we can use TinyMCE
const {
Button,
Toolbar,
Tooltip,
Dashicon,
PanelBody,
PanelRow,
TextControl
} = wp.components;

/**
* Returns the layouts configuration for a given number of columns.
*
* @param {number} columns Number of columns.
*
* @return {Object[]} Columns layout configuration.
*/
const getColumnLayouts = memoize( ( columns ) => {
return times( columns, ( n ) => ( {
name: `column-${ n + 1 }`,
label: sprintf( __( 'Column %d' ), n + 1 ),
icon: 'columns',
} ) );
} );

/**
* Register: aa Gutenberg Block.
*
* Registers a new block provided a unique name and an object defining its
* behavior. Once registered, the block is made editor as an option to any
* editor interface where blocks are implemented.
*
* @param {string} name Block name.
* @param {Object} settings Block settings.
* @return {?WPBlock} The block, if it has been successfully
* registered; otherwise `undefined`.
*/
registerBlockType("cgb/block-two-columns", {
// Block name. Block names must be string that contains a namespace prefix. Example: my-plugin/my-custom-block.
title: __("Two Columns", "CGB"), // Block title.
icon: "dashicons-screenoptions", // Block icon from Dashicons
category: "layout", // Block category — Group blocks together based on common traits E.g. common, formatting, layout widgets, embed.
keywords: [__("two-columns — CGB Block"), __("Two Columns")],
attributes: {
columns: {
type: 'number',
default: 2,
},
textFirstAlignment: {
type: "boolean",
default: false
},
columnWidth: {
type: "number",
default: 50,
}
},

// The "edit" property must be a valid function.
edit: props => {

const toggleTextFirstAlignment = () => {
props.setAttributes({
textFirstAlignment: !props.attributes.textFirstAlignment
});
};
const onChangeColumnWidth = value => {
props.setAttributes({
columnWidth: value
})
};
return [
!! props.focus && (
<Inspector
{ ...{ onChangeColumnWidth, ...props } }
/>
),
<div className={props.className}>
{!!props.focus && (
<BlockControls key="custom-controls">
<Toolbar className="components-toolbar">
<Tooltip text={__("Switch image/text alignment")}>
<Button
className="components-button components-icon-button components-toolbar__control"
onClick={toggleTextFirstAlignment}
>
<Dashicon icon="image-flip-horizontal" />
</Button>
</Tooltip>
</Toolbar>
</BlockControls>
)}

<div
className="flex row"
style={{
flexDirection: props.attributes.textFirstAlignment
? "row-reverse"
: "row"
}}
>
<div className="col-1" key="container" style={{
width: props.attributes.columnWidth + '%'
}}>
<InnerBlocks layouts={ getColumnLayouts( props.attributes.columns ) }/>
</div>
<div class="col-2" key="container" style={{
flex: 1,
}}>
<InnerBlocks layouts={ getColumnLayouts( props.attributes.columns ) }/>
</div>
</div>
</div>
];
},

// The "save" property must be specified and must be a valid function.
save: function(props) {
const colOrder = props.attributes.textFirstAlignment
? "row-reverse"
: "row";
return (
<div className={props.className}>
<div className="flex row" style={{ flexDirection: colOrder }}>
<div className="col-1" style={{
width: props.attributes.columnWidth + '%'
}}>
<InnerBlocks.Content />
</div>
<div className="col-2" style={{
flex: 1,
}}>
<InnerBlocks.Content />
</div>
</div>
</div>
);
}
});
Loading

0 comments on commit 0d94c16

Please sign in to comment.