-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
bde767c
80ab769
3d213e0
0501aa5
e76eb2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ); | ||
|
@@ -150,6 +163,24 @@ export default class Editable extends Component { | |
|
||
onInit() { | ||
this.updateFocus(); | ||
this.registerCustomFormatters(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
|
@@ -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 } ); | ||
|
@@ -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 ) { | ||
|
@@ -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 | ||
|
@@ -627,6 +670,7 @@ export default class Editable extends Component { | |
formats={ this.state.formats } | ||
onChange={ this.changeFormats } | ||
enabledControls={ formattingControls } | ||
customControls={ formatters } | ||
/> | ||
); | ||
|
||
|
@@ -669,3 +713,8 @@ export default class Editable extends Component { | |
Editable.contextTypes = { | ||
onUndo: noop, | ||
}; | ||
|
||
Editable.defaultProps = { | ||
formattingControls: DEFAULT_FORMATS, | ||
formatters: [], | ||
}; |
There was a problem hiding this comment.
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 👍