-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
entity-provider.js
204 lines (186 loc) · 5.83 KB
/
entity-provider.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* WordPress dependencies
*/
import {
createContext,
useContext,
useCallback,
useEffect,
} from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { parse, __unstableSerializeAndClean } from '@wordpress/blocks';
const EMPTY_ARRAY = [];
/**
* Internal dependencies
*/
import { defaultEntities, kinds } from './entities';
const entities = {
...defaultEntities.reduce( ( acc, entity ) => {
if ( ! acc[ entity.kind ] ) {
acc[ entity.kind ] = {};
}
acc[ entity.kind ][ entity.name ] = { context: createContext() };
return acc;
}, {} ),
...kinds.reduce( ( acc, kind ) => {
acc[ kind.name ] = {};
return acc;
}, {} ),
};
const getEntity = ( kind, type ) => {
if ( ! entities[ kind ] ) {
throw new Error( `Missing entity config for kind: ${ kind }.` );
}
if ( ! entities[ kind ][ type ] ) {
entities[ kind ][ type ] = { context: createContext() };
}
return entities[ kind ][ type ];
};
/**
* Context provider component for providing
* an entity for a specific entity type.
*
* @param {Object} props The component's props.
* @param {string} props.kind The entity kind.
* @param {string} props.type The entity type.
* @param {number} props.id The entity ID.
* @param {*} props.children The children to wrap.
*
* @return {Object} The provided children, wrapped with
* the entity's context provider.
*/
export default function EntityProvider( { kind, type, id, children } ) {
const Provider = getEntity( kind, type ).context.Provider;
return <Provider value={ id }>{ children }</Provider>;
}
/**
* Hook that returns the ID for the nearest
* provided entity of the specified type.
*
* @param {string} kind The entity kind.
* @param {string} type The entity type.
*/
export function useEntityId( kind, type ) {
return useContext( getEntity( kind, type ).context );
}
/**
* Hook that returns the value and a setter for the
* specified property of the nearest provided
* entity of the specified type.
*
* @param {string} kind The entity kind.
* @param {string} type The entity type.
* @param {string} prop The property name.
* @param {string} [_id] An entity ID to use instead of the context-provided one.
*
* @return {[*, Function]} A tuple where the first item is the
* property value and the second is the
* setter.
*/
export function useEntityProp( kind, type, prop, _id ) {
const providerId = useEntityId( kind, type );
const id = _id ?? providerId;
const { value, fullValue } = useSelect(
( select ) => {
const { getEntityRecord, getEditedEntityRecord } = select( 'core' );
const entity = getEntityRecord( kind, type, id ); // Trigger resolver.
const editedEntity = getEditedEntityRecord( kind, type, id );
return entity && editedEntity
? {
value: editedEntity[ prop ],
fullValue: entity[ prop ],
}
: {};
},
[ kind, type, id, prop ]
);
const { editEntityRecord } = useDispatch( 'core' );
const setValue = useCallback(
( newValue ) => {
editEntityRecord( kind, type, id, {
[ prop ]: newValue,
} );
},
[ kind, type, id, prop ]
);
return [ value, setValue, fullValue ];
}
/**
* Hook that returns block content getters and setters for
* the nearest provided entity of the specified type.
*
* The return value has the shape `[ blocks, onInput, onChange ]`.
* `onInput` is for block changes that don't create undo levels
* or dirty the post, non-persistent changes, and `onChange` is for
* peristent changes. They map directly to the props of a
* `BlockEditorProvider` and are intended to be used with it,
* or similar components or hooks.
*
* @param {string} kind The entity kind.
* @param {string} type The entity type.
* @param {Object} options
* @param {string} [options.id] An entity ID to use instead of the context-provided one.
*
* @return {[WPBlock[], Function, Function]} The block array and setters.
*/
export function useEntityBlockEditor( kind, type, { id: _id } = {} ) {
const providerId = useEntityId( kind, type );
const id = _id ?? providerId;
const { content, blocks } = useSelect(
( select ) => {
const { getEditedEntityRecord } = select( 'core' );
const editedEntity = getEditedEntityRecord( kind, type, id );
return {
blocks: editedEntity.blocks,
content: editedEntity.content,
};
},
[ kind, type, id ]
);
const { __unstableCreateUndoLevel, editEntityRecord } = useDispatch(
'core'
);
useEffect( () => {
// Load the blocks from the content if not already in state
// Guard against other instances that might have
// set content to a function already or the blocks are already in state.
if ( content && typeof content !== 'function' && ! blocks ) {
const parsedContent = parse( content );
editEntityRecord(
kind,
type,
id,
{
blocks: parsedContent,
},
{ undoIgnore: true }
);
}
}, [ content ] );
const onChange = useCallback(
( newBlocks, options ) => {
const { selection } = options;
const edits = { blocks: newBlocks, selection };
const noChange = blocks === edits.blocks;
if ( noChange ) {
return __unstableCreateUndoLevel( kind, type, id );
}
// We create a new function here on every persistent edit
// to make sure the edit makes the post dirty and creates
// a new undo level.
edits.content = ( { blocks: blocksForSerialization = [] } ) =>
__unstableSerializeAndClean( blocksForSerialization );
editEntityRecord( kind, type, id, edits );
},
[ kind, type, id, blocks ]
);
const onInput = useCallback(
( newBlocks, options ) => {
const { selection } = options;
const edits = { blocks: newBlocks, selection };
editEntityRecord( kind, type, id, edits );
},
[ kind, type, id ]
);
return [ blocks ?? EMPTY_ARRAY, onInput, onChange ];
}