Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API to add custom formats to Editable #3060

Merged
merged 5 commits into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 19 additions & 16 deletions blocks/editable/format-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ const FORMATTING_CONTROLS = [
title: __( 'Strikethrough' ),
format: 'strikethrough',
},
{
icon: 'admin-links',
title: __( 'Link' ),
format: 'link',
},
];

// Default controls shown if no `enabledControls` prop provided
Expand Down Expand Up @@ -131,20 +136,27 @@ class FormatToolbar extends Component {
}
}

isFormatActive( format ) {
return this.props.formats[ format ] && this.props.formats[ format ].isActive;
}

render() {
const { formats, focusPosition, enabledControls = DEFAULT_CONTROLS } = this.props;
const { formats, focusPosition, enabledControls = DEFAULT_CONTROLS, customControls = [] } = this.props;
const { isAddingLink, isEditingLink, newLinkValue, settingsVisible, opensInNewWindow } = this.state;
const linkStyle = focusPosition
? { position: 'absolute', ...focusPosition }
: null;

const toolbarControls = FORMATTING_CONTROLS
const toolbarControls = FORMATTING_CONTROLS.concat( customControls )
.filter( control => enabledControls.indexOf( control.format ) !== -1 )
.map( ( control ) => ( {
...control,
onClick: this.toggleFormat( control.format ),
isActive: !! formats[ control.format ],
} ) );
.map( ( control ) => {
const isLink = control.format === 'link';
return {
...control,
onClick: isLink ? this.addLink : this.toggleFormat( control.format ),
isActive: this.isFormatActive( control.format ) || ( isLink && isAddingLink ),
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice refactoring to merge the "link" control to the same loop 👍

} );

const linkSettings = settingsVisible && (
<fieldset className="blocks-format-toolbar__link-settings">
Expand All @@ -155,15 +167,6 @@ class FormatToolbar extends Component {
</fieldset>
);

if ( enabledControls.indexOf( 'link' ) !== -1 ) {
toolbarControls.push( {
icon: 'admin-links',
title: __( 'Link' ),
onClick: this.addLink,
isActive: isAddingLink || !! formats.link,
} );
}

return (
<div className="blocks-format-toolbar">
<Toolbar controls={ toolbarControls } />
Expand Down
65 changes: 57 additions & 8 deletions blocks/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ function isLinkBoundary( fragment ) {
fragment.childNodes[ 0 ].text[ 0 ] === '\uFEFF';
}

function getFormatProperties( formatName, parents ) {
switch ( formatName ) {
case 'link' : {
const anchor = find( parents, node => node.nodeName.toLowerCase() === 'a' );
return !! anchor ? { value: anchor.getAttribute( 'href' ) || '', target: anchor.getAttribute( 'target' ) || '', node: anchor } : {};
}
default:
return {};
}
}

const DEFAULT_FORMATS = [ 'bold', 'italic', 'strikethrough', 'link' ];

export default class Editable extends Component {
constructor( props ) {
super( ...arguments );
Expand Down Expand Up @@ -150,6 +163,24 @@ export default class Editable extends Component {

onInit() {
this.updateFocus();
this.registerCustomFormatters();
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if the formatters prop change, should we resync the custom formatters in TinyMCE? If it's not possible, maybe we should just guard against updating this prop in componentWillReceiveProps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed the prop from formatters to initialFormatters and added a console.error into componentWillReceiveProps, I forsee passing functions through this in future so it will be hard to determine if props have changed...

}

adaptFormatter( options ) {
switch ( options.type ) {
case 'inline-style': {
return {
inline: 'span',
styles: { ...options.style },
};
}
}
}

registerCustomFormatters() {
forEach( this.props.formatters, ( formatter ) => {
this.editor.formatter.register( formatter.format, this.adaptFormatter( formatter ) );
} );
}

onFocus() {
Expand Down Expand Up @@ -480,13 +511,15 @@ export default class Editable extends Component {
}

onNodeChange( { parents } ) {
const formats = {};
const link = find( parents, ( node ) => node.nodeName.toLowerCase() === 'a' );
if ( link ) {
formats.link = { value: link.getAttribute( 'href' ) || '', target: link.getAttribute( 'target' ) || '', node: link };
}
const activeFormats = this.editor.formatter.matchAll( [ 'bold', 'italic', 'strikethrough' ] );
activeFormats.forEach( ( activeFormat ) => formats[ activeFormat ] = true );
const formatNames = this.props.formattingControls;
const formats = this.editor.formatter.matchAll( formatNames ).reduce( ( accFormats, activeFormat ) => {
accFormats[ activeFormat ] = {
isActive: true,
...getFormatProperties( activeFormat, parents ),
};

return accFormats;
}, {} );

const focusPosition = this.getFocusPosition();
this.setState( { formats, focusPosition, selectedNodeId: this.state.selectedNodeId + 1 } );
Expand Down Expand Up @@ -556,8 +589,17 @@ export default class Editable extends Component {
}
}

componentWillReceiveProps( nextProps ) {
if ( 'development' === process.env.NODE_ENV ) {
if ( ! isEqual( this.props.formatters, nextProps.formatters ) ) {
// eslint-disable-next-line no-console
console.error( 'Formatters passed via `formatters` prop will only be registered once. Formatters can be enabled/disabled via the `formattingControls` prop.' );
}
}
}

isFormatActive( format ) {
return !! this.state.formats[ format ];
return this.state.formats[ format ] && this.state.formats[ format ].isActive;
}

removeFormat( format ) {
Expand Down Expand Up @@ -611,6 +653,7 @@ export default class Editable extends Component {
placeholder,
multiline: MultilineTag,
keepPlaceholderOnFocus = false,
formatters,
} = this.props;

// Generating a key that includes `tagName` ensures that if the tag
Expand All @@ -627,6 +670,7 @@ export default class Editable extends Component {
formats={ this.state.formats }
onChange={ this.changeFormats }
enabledControls={ formattingControls }
customControls={ formatters }
/>
);

Expand Down Expand Up @@ -669,3 +713,8 @@ export default class Editable extends Component {
Editable.contextTypes = {
onUndo: noop,
};

Editable.defaultProps = {
formattingControls: DEFAULT_FORMATS,
formatters: [],
};