-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.ts
33 lines (28 loc) · 1.21 KB
/
index.ts
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
import { useSelect, useDispatch } from '@wordpress/data';
// @ts-ignore-next-line - The type definitions for the block-editor package are incomplete.
import { store as blockEditorStore, useBlockEditContext } from '@wordpress/block-editor';
/*
* useBlockParentAttributes
*
* allows you to easily interface with the attributes of the direct
* parent of the current block
*/
export function useBlockParentAttributes() {
const { clientId } = useBlockEditContext();
const parentBlocks = useSelect(
// @ts-ignore-next-line - The type definitions for the core store are incomplete.
(select) => select(blockEditorStore).getBlockParents(clientId),
[clientId],
);
const parentBlockClientId = parentBlocks[parentBlocks.length - 1];
const parentBlock = useSelect(
// @ts-ignore-next-line - The type definitions for the core store are incomplete.
(select) => select(blockEditorStore).getBlock(parentBlockClientId),
[parentBlockClientId],
);
const { updateBlockAttributes } = useDispatch(blockEditorStore);
const setParentAttributes = (attributes: { [key: string]: unknown }) => {
updateBlockAttributes(parentBlockClientId, attributes);
};
return [(parentBlock?.attributes as Object) ?? {}, setParentAttributes] as const;
}