Skip to content
Merged
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
Prev Previous commit
Next Next commit
Re-add unknown type handler; remove console logs; cleanup
  • Loading branch information
tiny-james committed May 24, 2017
commit 9a893fde01fac71f71438af229a3e9cd6fb9973a
26 changes: 10 additions & 16 deletions blocks/library/freeform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { find } from 'lodash';
* Internal dependencies
*/
import './style.scss';
import { registerBlock, query } from '../../api';
import { registerBlock, query, setUnknownTypeHandler } from '../../api';
import Editable from '../../editable';

const { children } = query;
Expand All @@ -22,31 +22,24 @@ function 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;
return expectedListType === listType;
};
}

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

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

registerBlock( 'core/freeform', {
Expand Down Expand Up @@ -113,10 +106,9 @@ registerBlock( 'core/freeform', {
} ) }
onSetup={ ( editor ) => {
editor.on( 'nodeChange', ( nodeInfo ) => {
console.log( 'nodeChange ', nodeInfo );
setAttributes( {
listType: findParentList( nodeInfo ),
inBlockQuote: findParentQuote( nodeInfo ),
listType: findListType( nodeInfo ),
inBlockQuote: findInBlockQuote( nodeInfo ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Like @nylen mentioned, we shouldn't use setAttributes for "temporary" state that is not going to be persisted in the block output markup. We should use local state for this (the edit here can be a regular React Component).

You'll be able to access the local state in the controls if you embed the controls inside the edit function (see #830)

} );
} );
setAttributes( { editor } );
Expand All @@ -138,3 +130,5 @@ registerBlock( 'core/freeform', {
return content;
},
} );

setUnknownTypeHandler( 'core/freeform' );