/
post-format.js
79 lines (70 loc) · 1.76 KB
/
post-format.js
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
/**
* External dependencies.
*/
import of from 'callbag-of';
import startWith from 'callbag-start-with';
import fromDelegatedEvent from 'callbag-from-delegated-event';
import distinctUntilChanged from 'callbag-distinct-until-changed';
import { addFilter } from '@wordpress/hooks';
import { select } from '@wordpress/data';
import {
pipe,
map,
filter
} from 'callbag-basics';
/**
* Internal dependencies.
*/
import fromSelector from '../../../utils/from-selector';
/**
* The default state.
*
* @type {Object}
*/
const INITIAL_STATE = {
post_format: 'standard'
};
/**
* Extracts `post_format` from the input.
*
* @param {Object} input
* @return {Object}
*/
function getPostFormatFromRadioInput( input ) {
let value = input.value;
// Normalize the value of "Standard" input.
if ( value === '0' ) {
value = 'standard';
}
return {
post_format: value
};
}
/**
* Defines the side effects for Classic Editor.
*/
addFilter( 'carbon-fields.conditional-display-post-format.classic', 'carbon-fields/metaboxes', () => {
const node = document.querySelector( 'div#post-formats-select' );
if ( ! node ) {
return of( INITIAL_STATE );
}
return pipe(
fromDelegatedEvent( node, 'input.post-format', 'change' ),
map( ( { target } ) => getPostFormatFromRadioInput( target ) ),
startWith( getPostFormatFromRadioInput( node.querySelector( 'input.post-format:checked' ) ) )
);
} );
/**
* Defines the side effects for Gutenberg.
*/
addFilter( 'carbon-fields.conditional-display-post-format.gutenberg', 'carbon-fields/metaboxes', ( ) => {
return pipe(
fromSelector( select( 'core/editor' ).getEditedPostAttribute, 'format' ),
distinctUntilChanged(),
filter( Boolean ),
map( ( postFormat ) => ( {
post_format: postFormat
} ) ),
startWith( INITIAL_STATE )
);
} );