itmar-block-packages 1.4.3 → 1.6.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.6.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": {
@@ -0,0 +1,35 @@
1
+ import { __ } from "@wordpress/i18n";
2
+
3
+ //郵便番号検索のデータと関数
4
+ export const fetchZipToAddress = async (zipNum) => {
5
+ if (!/^\d{7}$/.test(zipNum)) {
6
+ alert(
7
+ __(
8
+ "Please enter your postal code as 7 digits without hyphens.",
9
+ "block-collections"
10
+ )
11
+ );
12
+ return null;
13
+ }
14
+
15
+ try {
16
+ const response = await fetch(
17
+ `https://zipcloud.ibsnet.co.jp/api/search?zipcode=${zipNum.replace(
18
+ "-",
19
+ ""
20
+ )}`
21
+ );
22
+ const data = await response.json();
23
+
24
+ if (data.results && data.results.length > 0) {
25
+ const result = data.results[0];
26
+ return result;
27
+ } else {
28
+ alert(__("No matching address found", "block-collections"));
29
+ return null;
30
+ }
31
+ } catch (error) {
32
+ alert(__("Communication failed", "block-collections"));
33
+ return null;
34
+ }
35
+ };
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,10 @@ 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";
96
+
97
+ //住所変換関連の関数
98
+ export { fetchZipToAddress } from "./ZipAddress";