# speechkit/4.0.2/src/Component/Post/BlockAttributes/helpers/getBlockMarkerAttribute.js

BeyondWords – AI audio for publishers, version 4.0.2. 80 lines.

- Page: https://pluginprobe.com/plugins/speechkit/4.0.2/code/src/Component/Post/BlockAttributes/helpers/getBlockMarkerAttribute.js
- Raw: https://pluginprobe.com/plugins/speechkit/4.0.2/raw/src/Component/Post/BlockAttributes/helpers/getBlockMarkerAttribute.js
- Modified: 2023-08-03T19:23:38+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/speechkit/4.0.2/code/src/Component/Post/BlockAttributes/helpers/getBlockMarkerAttribute.js#L10-L20`.

```javascript
/**
 * WordPress Dependencies
 */
import { select } from '@wordpress/data';

/**
 * External dependencies
 */
import { v4 as uuidv4 } from 'uuid';

/**
 * Get a beyondwordsMarker attribute for a block.
 *
 * Using the "Duplicate" button in the Block toolbar duplicates the marker
 * attribute too, so we attempt to handle this by getting all the markers in the
 * current Post and assinging new UUIDs to markers that already exist.
 *
 * @since 4.0.0
 *
 * @param {Object} attributes Attributes for the block.
 *
 * @return {String} marker The block marker (segment marker in BeyondWords API).
 */
const getBlockMarkerAttribute = ( attributes ) => {
	const { beyondwordsMarker } = attributes;

	if ( ! beyondwordsMarker ) return uuidv4();

	const existingMarkers = getExistingBlockMarkers()

	if ( countInArray( existingMarkers, beyondwordsMarker ) > 1 ) {
		// Return a new UUID if this marker is a duplicate
		return uuidv4();
	}

	// Return the existing marker only if it is not a duplicate
	return beyondwordsMarker;
}

/**
 * Get all existing Block markers for the currently-edited post.
 *
 * If using `getBlocks()` proves to be too respource-intensive then further work
 * will be required to optimise this.
 *
 * @since 4.0.0
 *
 * @return {String[]} markers The block markers for the current Post.
 */
const getExistingBlockMarkers = () => {
	// Get all Blocks in current Post
	const blocks = select( 'core/block-editor' )
		.getBlocks();

	// Return all non-empty markers of the Blocks
	return blocks
		.map( block => block?.attributes?.beyondwordsMarker )
		.filter( block => block );
}

/**
 * Count the number of times an item is in an array.
 *
 * @since 4.0.0
 *
 * @return {Number} count The number of times the item occurs.
 */
function countInArray(array, item) {
    var count = 0;

    for ( var i = 0; i < array.length; i++ ) {
        if ( array[ i ] === item ) {
            count++;
        }
    }

    return count;
}

export default getBlockMarkerAttribute;
```
