PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.3
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / form-builder / blocks / src / lib / miniInserterTarget.js

miniInserterTarget.js in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 4.0.3, at form-builder/blocks/src/lib/miniInserterTarget.js

57 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Open mini-inserter destination store.
3 * Existing Fields inserts here instead of appending to the root list.
4 */
5 import { select as dataSelect } from '@wordpress/data';
6 import { useSyncExternalStore } from '@wordpress/element';
7
8 import { createPubsubStore } from './createPubsubStore';
9 import { REPEATER_BLOCK_NAME as REPEATER_BLOCK } from './repeaterBlockName';
10
11 const CLOSED = { open: false, rootClientId: undefined, clientId: undefined, isAppender: false };
12
13 const store = createPubsubStore(
14 CLOSED,
15 ( a, b ) =>
16 a.open === b.open &&
17 a.rootClientId === b.rootClientId &&
18 a.clientId === b.clientId &&
19 a.isAppender === b.isAppender,
20 );
21
22 /**
23 * Called when a mini inserter popover opens.
24 *
25 * @param {Object} destination
26 * @param {string} [destination.rootClientId] Container to insert into (undefined = root list).
27 * @param {string} [destination.clientId] Insert BEFORE this block (core's in-between semantics).
28 * @param {boolean} [destination.isAppender] Append to the end of `rootClientId`.
29 */
30 export const setMiniInserterTarget = ( { rootClientId, clientId, isAppender } = {} ) =>
31 store.set( { open: true, rootClientId, clientId, isAppender: !! isAppender } );
32
33 /** Called when the mini inserter popover closes. */
34 export const clearMiniInserterTarget = () => store.set( CLOSED );
35
36 /** Read the destination reactively (React components). */
37 export const useMiniInserterTarget = () => useSyncExternalStore( store.subscribe, store.get );
38
39 /** Read the destination imperatively (DOM sync code). */
40 export const getMiniInserterTarget = () => store.get();
41
42 /** Re-run on destination changes (DOM sync code). Returns an unsubscribe fn. */
43 export const subscribeMiniInserterTarget = ( listener ) => store.subscribe( listener );
44
45 /**
46 * False when the destination is inside a Repeater (existing fields can't be
47 * sub-fields). Driven by destination, not a per-caller flag.
48 */
49 export const miniInserterAllowsExistingFields = () => {
50 const { rootClientId } = store.get();
51 if ( ! rootClientId ) return true;
52 const editor = dataSelect( 'core/block-editor' );
53 if ( editor.getBlockName( rootClientId ) === REPEATER_BLOCK ) return false;
54 const parents = editor.getBlockParents( rootClientId ) || [];
55 return ! parents.some( ( id ) => editor.getBlockName( id ) === REPEATER_BLOCK );
56 };
57