itmar-block-packages 1.4.3 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "itmar-block-packages",
3
- "version": "1.4.3",
3
+ "version": "1.5.0",
4
4
  "description": "We have put together a package of common React components used for WordPress custom blocks.",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
package/src/blockStore.js CHANGED
@@ -1,3 +1,47 @@
1
+ import { useSelect } from "@wordpress/data";
2
+ import { store as blockEditorStore } from "@wordpress/block-editor";
3
+
4
+ export const useTargetBlocks = (
5
+ clientId,
6
+ blockName,
7
+ attributeFilter = null,
8
+ includeNested = false
9
+ ) => {
10
+ const targetblocks = useSelect(
11
+ (select) => {
12
+ const { getBlockRootClientId, getBlock } = select(blockEditorStore);
13
+
14
+ const parentId = getBlockRootClientId(clientId); // 自分の親の clientId を取得
15
+ if (!parentId) return attributeFilter ? null : [];
16
+
17
+ const parentBlock = getBlock(parentId);
18
+ if (!parentBlock) return attributeFilter ? null : [];
19
+
20
+ const siblingBlocks = includeNested
21
+ ? flattenBlocks(parentBlock.innerBlocks || []) //ネストされたブロックも含んで検索
22
+ : parentBlock.innerBlocks || []; //兄弟ブロックのみ
23
+
24
+ const filtered = siblingBlocks.filter(
25
+ (block) => block.name === blockName && block.clientId !== clientId
26
+ );
27
+
28
+ if (attributeFilter) {
29
+ return (
30
+ filtered.find((block) => {
31
+ return Object.entries(attributeFilter).every(
32
+ ([key, value]) => block.attributes[key] === value
33
+ );
34
+ }) || null
35
+ );
36
+ }
37
+
38
+ return filtered;
39
+ },
40
+ [clientId, blockName, JSON.stringify(attributeFilter), includeNested]
41
+ );
42
+ return targetblocks;
43
+ };
44
+
1
45
  //ネストしたブロックを平坦化
2
46
  export const flattenBlocks = (blocks) => {
3
47
  return blocks.reduce((acc, block) => {
package/src/index.js CHANGED
@@ -89,7 +89,7 @@ export {
89
89
  } from "./DateElm";
90
90
 
91
91
  //インナーブロック関連の関数
92
- export { flattenBlocks } from "./blockStore";
92
+ export { flattenBlocks, useTargetBlocks } from "./blockStore";
93
93
 
94
94
  //特定の投稿タイプの投稿に含まれる本ブロックの属性を書き換える
95
95
  export { default as UpdateAllPostsBlockAttributes } from "./UpdateAllPostsBlockAttributes";