-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extensibility: Define anchor behavior as filtered blocks support
- Loading branch information
Showing
14 changed files
with
131 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { assign, get } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { cloneElement } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { source } from '../api'; | ||
import InspectorControls from '../inspector-controls'; | ||
|
||
/** | ||
* Regular expression matching invalid anchor characters for replacement. | ||
* | ||
* @type {RegExp} | ||
*/ | ||
const ANCHOR_REGEX = /[\s#]/g; | ||
|
||
export default function anchor( settings ) { | ||
if ( ! get( settings.supports, 'anchor' ) ) { | ||
return settings; | ||
} | ||
|
||
// Extend attributes with anchor determined by ID on the first node. | ||
assign( settings.attributes, { | ||
anchor: { | ||
type: 'string', | ||
source: source.attr( '*', 'id' ), | ||
}, | ||
} ); | ||
|
||
// Override the default edit UI to include a new block inspector control | ||
// for assigning the anchor ID | ||
const { edit: Edit } = settings; | ||
settings.edit = function( props ) { | ||
return [ | ||
<Edit key="edit" { ...props } />, | ||
props.focus && ( | ||
<InspectorControls key="inspector"> | ||
<InspectorControls.TextControl | ||
label={ __( 'HTML Anchor' ) } | ||
help={ __( 'Anchors lets you link directly to a section on a page.' ) } | ||
value={ props.attributes.anchor || '' } | ||
onChange={ ( nextValue ) => { | ||
nextValue = nextValue.replace( ANCHOR_REGEX, '-' ); | ||
|
||
props.setAttributes( { | ||
anchor: nextValue, | ||
} ); | ||
} } /> | ||
</InspectorControls> | ||
), | ||
]; | ||
}; | ||
|
||
// Override the default block serialization to clone the returned element, | ||
// injecting the attribute ID. | ||
const { save } = settings; | ||
settings.save = function( { attributes } ) { | ||
const { anchor: id } = attributes; | ||
|
||
let result = save( ...arguments ); | ||
if ( 'string' !== typeof result && id ) { | ||
result = cloneElement( result, { id } ); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
return settings; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import createHooks from '@wordpress/hooks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import anchor from './anchor'; | ||
|
||
const { applyFilters, addFilter } = createHooks(); | ||
|
||
export { applyFilters }; | ||
export { addFilter }; | ||
|
||
addFilter( 'registerBlockType', 'supports-anchor', anchor ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.