| 1 |
/** |
| 2 |
* WordPress Dependencies |
| 3 |
*/ |
| 4 |
import { addFilter } from '@wordpress/hooks'; |
| 5 |
|
| 6 |
/** |
| 7 |
* External dependencies |
| 8 |
*/ |
| 9 |
import getBlockMarkerAttribute from './helpers/getBlockMarkerAttribute'; |
| 10 |
|
| 11 |
/** |
| 12 |
* Register custom block attributes for BeyondWords. |
| 13 |
* |
| 14 |
* @since 4.0.4 Remove settings.attributes undefined check, to match official docs. |
| 15 |
* |
| 16 |
* @param {Object} settings Settings for the block. |
| 17 |
* |
| 18 |
* @return {Object} settings Modified settings. |
| 19 |
*/ |
| 20 |
function addAttributes( settings ) { |
| 21 |
return { |
| 22 |
...settings, |
| 23 |
attributes: { |
| 24 |
...settings.attributes, |
| 25 |
beyondwordsAudio: { |
| 26 |
type: 'boolean', |
| 27 |
default: true, |
| 28 |
}, |
| 29 |
beyondwordsMarker: { |
| 30 |
type: 'string', |
| 31 |
default: '', |
| 32 |
}, |
| 33 |
}, |
| 34 |
}; |
| 35 |
} |
| 36 |
|
| 37 |
addFilter( |
| 38 |
'blocks.registerBlockType', |
| 39 |
'beyondwords/beyondwords-block-attributes', |
| 40 |
addAttributes |
| 41 |
); |
| 42 |
|
| 43 |
/** |
| 44 |
* Set a unique BeyondWords marker for each block that doesn't already have one. |
| 45 |
* |
| 46 |
* @param {Object} attributes Attributes for the block. |
| 47 |
* |
| 48 |
* @return {Object} attributes Modified attributes. |
| 49 |
*/ |
| 50 |
function setMarkerAttribute( attributes ) { |
| 51 |
const marker = getBlockMarkerAttribute( attributes ); |
| 52 |
|
| 53 |
attributes = { |
| 54 |
...attributes, |
| 55 |
beyondwordsMarker: marker, |
| 56 |
}; |
| 57 |
|
| 58 |
return attributes; |
| 59 |
} |
| 60 |
|
| 61 |
addFilter( |
| 62 |
'blocks.getBlockAttributes', |
| 63 |
'beyondwords/set-marker-attribute', |
| 64 |
setMarkerAttribute |
| 65 |
); |
| 66 |
|