Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Basic freeform block
  • Loading branch information
tiny-james committed May 24, 2017
commit 3e9d958fa633b9703d8c9484fa9920bbb7adec9b
131 changes: 118 additions & 13 deletions blocks/library/freeform/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,53 @@
/**
* WordPress dependencies
*/
import { find } from 'lodash';

/**
* Internal dependencies
*/
import { registerBlock, query, setUnknownTypeHandler } from '../../api';
import './style.scss';
import { registerBlock, query } from '../../api';
import Editable from '../../editable';

const { children } = query;

function execCommand( command ) {
return ( { editor } ) => {
if ( editor ) {
editor.execCommand( command );
}
};
}

function blockQuoteIsActive() {
return ( { inBlockQuote } ) => {
console.log( 'inQuote:', inBlockQuote );
return inBlockQuote;
};
}

function listIsActive( expectedListType ) {
return ( { listType } ) => {
const inList = expectedListType === listType;
console.log( 'inList:', inList );
return inList;
};
}

const { html } = query;
function findParentList( { parents } ) {
const list = find( parents, ( node ) => node.nodeName === 'UL' || node.nodeName === 'OL' );
const listType = list ? list.nodeName : null;
console.log( 'listType:', listType );
return listType;
}

function findParentQuote( { parents } ) {
const quote = find( parents, ( node ) => node.nodeName === 'BLOCKQUOTE' );
const inBlockQuote = ! ! quote;
console.log( 'inBlockQuote:', inBlockQuote );
return inBlockQuote;
}

registerBlock( 'core/freeform', {
title: wp.i18n.__( 'Freeform' ),
Expand All @@ -13,23 +57,84 @@ registerBlock( 'core/freeform', {
category: 'common',

attributes: {
html: html(),
content: children(),
},

defaultAttributes: {
content: <p />,
listType: null,
inBlockQuote: false,
},

edit( { attributes } ) {
controls: [
{
icon: 'editor-quote',
title: wp.i18n.__( 'Quote' ),
isActive: blockQuoteIsActive(),
onClick: execCommand( 'mceBlockQuote' ),
},
{
icon: 'editor-ul',
title: wp.i18n.__( 'Convert to unordered' ),
isActive: listIsActive( 'UL' ),
onClick: execCommand( 'InsertUnorderedList' ),
},
{
icon: 'editor-ol',
title: wp.i18n.__( 'Convert to ordered' ),
isActive: listIsActive( 'OL' ),
onClick: execCommand( 'InsertOrderedList' ),
},
],

advControls: [
{
icon: 'editor-outdent',
title: wp.i18n.__( 'Outdent list item' ),
onClick: execCommand( 'Outdent' ),
},
{
icon: 'editor-indent',
title: wp.i18n.__( 'Indent list item' ),
onClick: execCommand( 'Indent' ),
},
],

edit( { attributes, setAttributes, focus, setFocus } ) {
const { content } = attributes;

return (
<div
contentEditable
suppressContentEditableWarning
>
{ attributes.html }
</div>
<Editable
Copy link
Contributor

@youknowriad youknowriad May 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use Editable for this block? I don't know the answer here because we may think this block would be way more complex than the regular Editable usage. At the same time having access to the onSetup callback allows us to enhance the behaviour, so maybe we could try for now and reconsider later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

className="blocks-freeform"
value={ content }
getSettings={ ( settings ) => ( {
...settings,
plugins: ( settings.plugins || [] ).concat( 'lists' ),
} ) }
onSetup={ ( editor ) => {
editor.on( 'nodeChange', ( nodeInfo ) => {
console.log( 'nodeChange ', nodeInfo );
setAttributes( {
listType: findParentList( nodeInfo ),
inBlockQuote: findParentQuote( nodeInfo ),
} );
} );
setAttributes( { editor } );
} }
onChange={ ( nextContent ) => {
setAttributes( {
content: nextContent,
} );
} }
focus={ focus }
onFocus={ setFocus }
showAlignments
/>
);
},

save( { attributes } ) {
return attributes.html;
const { content } = attributes;
return content;
},
} );

setUnknownTypeHandler( 'core/freeform' );
12 changes: 12 additions & 0 deletions blocks/library/freeform/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.blocks-freeform .blocks-editable__tinymce {
ul, ol {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like spaces instead of tabs here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I have my editor in spaces mode by default - I've changed the workspace settings so this hopefully won't happen in future.

padding-left: 2.5em;
margin-left: 0;
}
blockquote {
margin: 0;
box-shadow: inset 0px 0px 0px 0px $light-gray-500;
border-left: 4px solid $black;
padding-left: 1em;
}
}