-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathview.js
122 lines (114 loc) · 2.98 KB
/
view.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
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
115
116
117
118
119
120
121
122
/**
* WordPress dependencies
*/
import {
store,
getContext,
useEffect,
privateApis,
} from '@wordpress/interactivity';
const { directive, proxifyState, h } = privateApis(
'I acknowledge that using private APIs means my theme or plugin will inevitably break in the next version of WordPress.'
);
/**
* Namespace used in custom directives and store.
*/
const namespace = 'directive-priorities';
/**
* Util to check that render calls happen in order.
*
* @param {string} n Name passed from the directive being executed.
*/
const executionProof = ( n ) => {
const el = document.querySelector( '[data-testid="execution order"]' );
if ( ! el.textContent ) {
el.textContent = n;
} else {
el.textContent += `, ${ n }`;
}
};
/**
* Simple context directive, just for testing purposes. It provides a deep
* signal with these two properties:
* - attribute: 'from context'
* - text: 'from context'
*/
directive(
'test-context',
( { context: { Provider }, props: { children } } ) => {
executionProof( 'context' );
const value = {
[ namespace ]: proxifyState( namespace, {
attribute: 'from context',
text: 'from context',
} ),
};
return h( Provider, { value }, children );
},
{ priority: 8 }
);
/**
* Simple attribute directive, for testing purposes. It reads the value of
* `attribute` from context and populates `data-attribute` with it.
*/
directive( 'test-attribute', ( { evaluate, element } ) => {
executionProof( 'attribute' );
const attributeValue = evaluate( {
namespace,
value: 'context.attribute',
} );
useEffect( () => {
element.ref.current.setAttribute( 'data-attribute', attributeValue );
}, [] );
element.props[ 'data-attribute' ] = attributeValue;
} );
/**
* Simple text directive, for testing purposes. It reads the value of
* `text` from context and populates `children` with it.
*/
directive(
'test-text',
( { evaluate, element } ) => {
executionProof( 'text' );
const textValue = evaluate( { namespace, value: 'context.text' } );
element.props.children = h( 'p', { 'data-testid': 'text' }, textValue );
},
{ priority: 12 }
);
/**
* Children directive, for testing purposes. It adds a wrapper around
* `children`, including two buttons to modify `text` and `attribute` values
* from the received context.
*/
directive(
'test-children',
( { evaluate, element } ) => {
executionProof( 'children' );
const updateAttribute = () => {
evaluate( { namespace, value: 'actions.updateAttribute' } );
};
const updateText = () => {
evaluate( { namespace, value: 'actions.updateText' } );
};
element.props.children = h(
'div',
{},
element.props.children,
h( 'button', { onClick: updateAttribute }, 'Update attribute' ),
h( 'button', { onClick: updateText }, 'Update text' )
);
},
{ priority: 14 }
);
store( 'directive-priorities', {
actions: {
updateText() {
const context = getContext();
context.text = 'updated';
},
updateAttribute() {
const context = getContext();
context.attribute = 'updated';
},
},
} );