-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathhooks.tsx
415 lines (378 loc) · 11.4 KB
/
hooks.tsx
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable react-hooks/exhaustive-deps */
/**
* External dependencies
*/
import {
h as createElement,
options,
createContext,
cloneElement,
type ComponentChildren,
} from 'preact';
import { useRef, useCallback, useContext } from 'preact/hooks';
import type { VNode, Context, RefObject } from 'preact';
/**
* Internal dependencies
*/
import { store, stores, universalUnlock } from './store';
import { warn } from './utils';
export interface DirectiveEntry {
value: string | object;
namespace: string;
suffix: string;
}
type DirectiveEntries = Record< string, DirectiveEntry[] >;
interface DirectiveArgs {
/**
* Object map with the defined directives of the element being evaluated.
*/
directives: DirectiveEntries;
/**
* Props present in the current element.
*/
props: { children?: ComponentChildren };
/**
* Virtual node representing the element.
*/
element: VNode< {
class?: string;
style?: string | Record< string, string | number >;
content?: ComponentChildren;
} >;
/**
* The inherited context.
*/
context: Context< any >;
/**
* Function that resolves a given path to a value either in the store or the
* context.
*/
evaluate: Evaluate;
}
interface DirectiveCallback {
( args: DirectiveArgs ): VNode | null | void;
}
interface DirectiveOptions {
/**
* Value that specifies the priority to evaluate directives of this type.
* Lower numbers correspond with earlier execution.
*
* @default 10
*/
priority?: number;
}
interface Scope {
evaluate: Evaluate;
context: object;
ref: RefObject< HTMLElement >;
attributes: createElement.JSX.HTMLAttributes;
}
interface Evaluate {
( entry: DirectiveEntry, ...args: any[] ): any;
}
interface GetEvaluate {
( args: { scope: Scope } ): Evaluate;
}
type PriorityLevel = string[];
interface GetPriorityLevels {
( directives: DirectiveEntries ): PriorityLevel[];
}
interface DirectivesProps {
directives: DirectiveEntries;
priorityLevels: PriorityLevel[];
element: VNode;
originalProps: any;
previousScope?: Scope;
}
// Main context.
const context = createContext< any >( {} );
// Wrap the element props to prevent modifications.
const immutableMap = new WeakMap();
const immutableError = () => {
throw new Error(
'Please use `data-wp-bind` to modify the attributes of an element.'
);
};
const immutableHandlers: ProxyHandler< object > = {
get( target, key, receiver ) {
const value = Reflect.get( target, key, receiver );
return !! value && typeof value === 'object'
? deepImmutable( value )
: value;
},
set: immutableError,
deleteProperty: immutableError,
};
const deepImmutable = < T extends object = {} >( target: T ): T => {
if ( ! immutableMap.has( target ) ) {
immutableMap.set( target, new Proxy( target, immutableHandlers ) );
}
return immutableMap.get( target );
};
// Store stacks for the current scope and the default namespaces and export APIs
// to interact with them.
const scopeStack: Scope[] = [];
const namespaceStack: string[] = [];
/**
* Retrieves the context inherited by the element evaluating a function from the
* store. The returned value depends on the element and the namespace where the
* function calling `getContext()` exists.
*
* @param namespace Store namespace. By default, the namespace where the calling
* function exists is used.
* @return The context content.
*/
export const getContext = < T extends object >( namespace?: string ): T =>
getScope()?.context[ namespace || getNamespace() ];
/**
* Retrieves a representation of the element where a function from the store
* is being evalutated. Such representation is read-only, and contains a
* reference to the DOM element, its props and a local reactive state.
*
* @return Element representation.
*/
export const getElement = () => {
if ( ! getScope() ) {
throw Error(
'Cannot call `getElement()` outside getters and actions used by directives.'
);
}
const { ref, attributes } = getScope();
return Object.freeze( {
ref: ref.current,
attributes: deepImmutable( attributes ),
} );
};
export const getScope = () => scopeStack.slice( -1 )[ 0 ];
export const setScope = ( scope: Scope ) => {
scopeStack.push( scope );
};
export const resetScope = () => {
scopeStack.pop();
};
export const getNamespace = () => namespaceStack.slice( -1 )[ 0 ];
export const setNamespace = ( namespace: string ) => {
namespaceStack.push( namespace );
};
export const resetNamespace = () => {
namespaceStack.pop();
};
// WordPress Directives.
const directiveCallbacks: Record< string, DirectiveCallback > = {};
const directivePriorities: Record< string, number > = {};
/**
* Register a new directive type in the Interactivity API runtime.
*
* @example
* ```js
* directive(
* 'alert', // Name without the `data-wp-` prefix.
* ( { directives: { alert }, element, evaluate } ) => {
* const defaultEntry = alert.find( entry => entry.suffix === 'default' );
* element.props.onclick = () => { alert( evaluate( defaultEntry ) ); }
* }
* )
* ```
*
* The previous code registers a custom directive type for displaying an alert
* message whenever an element using it is clicked. The message text is obtained
* from the store under the inherited namespace, using `evaluate`.
*
* When the HTML is processed by the Interactivity API, any element containing
* the `data-wp-alert` directive will have the `onclick` event handler, e.g.,
*
* ```html
* <div data-wp-interactive="messages">
* <button data-wp-alert="state.alert">Click me!</button>
* </div>
* ```
* Note that, in the previous example, the directive callback gets the path
* value (`state.alert`) from the directive entry with suffix `default`. A
* custom suffix can also be specified by appending `--` to the directive
* attribute, followed by the suffix, like in the following HTML snippet:
*
* ```html
* <div data-wp-interactive="myblock">
* <button
* data-wp-color--text="state.text"
* data-wp-color--background="state.background"
* >Click me!</button>
* </div>
* ```
*
* This could be an hypothetical implementation of the custom directive used in
* the snippet above.
*
* @example
* ```js
* directive(
* 'color', // Name without prefix and suffix.
* ( { directives: { color }, ref, evaluate } ) =>
* colors.forEach( ( color ) => {
* if ( color.suffix = 'text' ) {
* ref.style.setProperty(
* 'color',
* evaluate( color.text )
* );
* }
* if ( color.suffix = 'background' ) {
* ref.style.setProperty(
* 'background-color',
* evaluate( color.background )
* );
* }
* } );
* }
* )
* ```
*
* @param name Directive name, without the `data-wp-` prefix.
* @param callback Function that runs the directive logic.
* @param options Options object.
* @param options.priority Option to control the directive execution order. The
* lesser, the highest priority. Default is `10`.
*/
export const directive = (
name: string,
callback: DirectiveCallback,
{ priority = 10 }: DirectiveOptions = {}
) => {
directiveCallbacks[ name ] = callback;
directivePriorities[ name ] = priority;
};
// Resolve the path to some property of the store object.
const resolve = ( path: string, namespace: string ) => {
if ( ! namespace ) {
warn(
`Namespace missing for "${ path }". The value for that path won't be resolved.`
);
return;
}
let resolvedStore = stores.get( namespace );
if ( typeof resolvedStore === 'undefined' ) {
resolvedStore = store( namespace, undefined, {
lock: universalUnlock,
} );
}
const current = {
...resolvedStore,
context: getScope().context[ namespace ],
};
try {
// TODO: Support lazy/dynamically initialized stores
return path.split( '.' ).reduce( ( acc, key ) => acc[ key ], current );
} catch ( e ) {}
};
// Generate the evaluate function.
export const getEvaluate: GetEvaluate =
( { scope } ) =>
( entry, ...args ) => {
let { value: path, namespace } = entry;
if ( typeof path !== 'string' ) {
throw new Error( 'The `value` prop should be a string path' );
}
// If path starts with !, remove it and save a flag.
const hasNegationOperator =
path[ 0 ] === '!' && !! ( path = path.slice( 1 ) );
setScope( scope );
const value = resolve( path, namespace );
const result = typeof value === 'function' ? value( ...args ) : value;
resetScope();
return hasNegationOperator ? ! result : result;
};
// Separate directives by priority. The resulting array contains objects
// of directives grouped by same priority, and sorted in ascending order.
const getPriorityLevels: GetPriorityLevels = ( directives ) => {
const byPriority = Object.keys( directives ).reduce<
Record< number, string[] >
>( ( obj, name ) => {
if ( directiveCallbacks[ name ] ) {
const priority = directivePriorities[ name ];
( obj[ priority ] = obj[ priority ] || [] ).push( name );
}
return obj;
}, {} );
return Object.entries( byPriority )
.sort( ( [ p1 ], [ p2 ] ) => parseInt( p1 ) - parseInt( p2 ) )
.map( ( [ , arr ] ) => arr );
};
// Component that wraps each priority level of directives of an element.
const Directives = ( {
directives,
priorityLevels: [ currentPriorityLevel, ...nextPriorityLevels ],
element,
originalProps,
previousScope,
}: DirectivesProps ) => {
// Initialize the scope of this element. These scopes are different per each
// level because each level has a different context, but they share the same
// element ref, state and props.
const scope = useRef< Scope >( {} as Scope ).current;
scope.evaluate = useCallback( getEvaluate( { scope } ), [] );
scope.context = useContext( context );
/* eslint-disable react-hooks/rules-of-hooks */
scope.ref = previousScope?.ref || useRef( null );
/* eslint-enable react-hooks/rules-of-hooks */
// Create a fresh copy of the vnode element and add the props to the scope,
// named as attributes (HTML Attributes).
element = cloneElement( element, { ref: scope.ref } );
scope.attributes = element.props;
// Recursively render the wrapper for the next priority level.
const children =
nextPriorityLevels.length > 0
? createElement( Directives, {
directives,
priorityLevels: nextPriorityLevels,
element,
originalProps,
previousScope: scope,
} )
: element;
const props = { ...originalProps, children };
const directiveArgs = {
directives,
props,
element,
context,
evaluate: scope.evaluate,
};
setScope( scope );
for ( const directiveName of currentPriorityLevel ) {
const wrapper = directiveCallbacks[ directiveName ]?.( directiveArgs );
if ( wrapper !== undefined ) {
props.children = wrapper;
}
}
resetScope();
return props.children;
};
// Preact Options Hook called each time a vnode is created.
const old = options.vnode;
options.vnode = ( vnode: VNode< any > ) => {
if ( vnode.props.__directives ) {
const props = vnode.props;
const directives = props.__directives;
if ( directives.key ) {
vnode.key = directives.key.find(
( { suffix } ) => suffix === 'default'
).value;
}
delete props.__directives;
const priorityLevels = getPriorityLevels( directives );
if ( priorityLevels.length > 0 ) {
vnode.props = {
directives,
priorityLevels,
originalProps: props,
type: vnode.type,
element: createElement( vnode.type as any, props ),
top: true,
};
vnode.type = Directives;
}
}
if ( old ) {
old( vnode );
}
};