File size: 3,305 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { select } from '@wordpress/data';
import { debounce, get } from 'lodash';
import tracksRecordEvent from './track-record-event';

/**
 * Handles search tracking from "Autocomplete / Block" component.
 *
 * Depends on a valid target, which was previously handled by the `selectorHandler()` function.
 * The most important checks are performed there.
 */
const trackInserterInlineSearchTerm = () => {
	// Pick up the search term from the `content` block attributes.
	const search_term = get(
		select( 'core/block-editor' ).getSelectedBlock(),
		[ 'attributes', 'content' ],
		''
	).substr( 1 );

	if ( search_term.length < 3 ) {
		return;
	}

	const context = 'inserter_inline';

	tracksRecordEvent( 'wpcom_block_picker_search_term', {
		search_term,
		context,
	} );

	/*
	 * Check if there are results by looking for the popover autocomplete in the DOM
	 * which will only be present
	 * if there are blocks that match the search term.
	 * Also, there is only a single popover Slot
	 * and so only 1 popover can render at a time.
	 */
	const hasResults = !! document.querySelectorAll( '.components-autocomplete__popover' ).length;
	if ( hasResults ) {
		return;
	}

	tracksRecordEvent( 'wpcom_block_picker_no_results', {
		search_term,
		context,
	} );
};

/**
 * This function takes over of returning a valid
 * target element, which will used to bind the handler function
 * to record the events.
 * It looks if there is a selected block in the editor,
 * checks the block type, if it's the active element in the
 * document, etc.
 */
function selectorHandler() {
	const selectedBlock = select( 'core/block-editor' ).getSelectedBlock();

	// Skip If there isn't a selected block.
	if ( ! selectedBlock ) {
		return;
	}

	const { name, attributes } = selectedBlock;

	// Skip if it is not a core/paragraph block.
	if ( name !== 'core/paragraph' ) {
		return;
	}

	/*
	 * Skip if content doesn't start with the slash `/` (as this is the shortcut used to trigger the block inserter).
	 * Also, check if the block search term is a valid block name
	 * ie: only lowercase alphanumeric characters and dashes,
	 * and must begin with a letter.
	 */
	const { content } = attributes;
	if ( ! /^\/[a-z][a-z0-9-]+$/.test( content ) ) {
		return;
	}

	const blockDOMElement = document.getElementById( `block-${ selectedBlock.clientId }` );
	// Skip if there is not a Block DOM element.
	if ( ! blockDOMElement ) {
		return;
	}

	// Skip if the block Element is not the active one.
	if ( blockDOMElement !== document.activeElement ) {
		return;
	}

	const blockClassName = blockDOMElement.className;

	// Skip if the block is not marked as "selected" via a class attribute.
	if ( ! blockClassName || ! blockDOMElement.classList.contains( 'is-selected' ) ) {
		return;
	}

	return blockDOMElement;
}
/**
 * Return the event definition object to track `wpcom_block_picker_search_term`,
 * adding the context event property with the `inserter_inline` value.
 * It also tracks `wpcom_block_picker_no_results` if the search term doesn't return any results.
 * @returns {import('./types').DelegateEventHandler} event object definition.
 */
export default () => ( {
	id: 'wpcom-inserter-inline-search-term',
	selector: selectorHandler,
	type: 'keyup',
	handler: debounce( trackInserterInlineSearchTerm, 500 ),
} );