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/README.md +188 -0
- package/build/index.asset.php +1 -1
- package/build/index.js +3 -3
- package/package.json +1 -1
- package/src/ZipAddress.js +35 -0
- package/src/blockStore.js +44 -0
- package/src/index.js +4 -1
package/README.md
CHANGED
|
@@ -14,6 +14,12 @@ import {関数名又はコンポーネント名} from "itmar-block-packages"
|
|
|
14
14
|
npm i @wordpress/scripts@^27.6.0 --save-dev
|
|
15
15
|
|
|
16
16
|
## 更新履歴
|
|
17
|
+
= 1.6.0 =
|
|
18
|
+
- fetchZipToAddressを新設
|
|
19
|
+
|
|
20
|
+
= 1.5.0 =
|
|
21
|
+
- useTargetBlocksを新設
|
|
22
|
+
|
|
17
23
|
= 1.4.3 =
|
|
18
24
|
- UpdateAllPostsBlockAttributesコンポーネントのRestAPIによる更新の不具合を修正
|
|
19
25
|
|
|
@@ -943,3 +949,185 @@ WordPressのRestAPIで使用するrest_baseに相当する文字列
|
|
|
943
949
|
/>
|
|
944
950
|
```
|
|
945
951
|
|
|
952
|
+
## インナーブロックを取得・操作する関数
|
|
953
|
+
### `useTargetBlocks`
|
|
954
|
+
|
|
955
|
+
`useTargetBlocks` は、**Gutenberg ブロックエディタ上で同じ親ブロック内にある特定の名前・属性を持つブロックを取得する React フック**です。
|
|
956
|
+
ネストされたブロックの検索にも対応しています。
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
#### 🧩 概要
|
|
961
|
+
|
|
962
|
+
このカスタムフックは、次のような用途に使えます:
|
|
963
|
+
|
|
964
|
+
- 同じ親ブロックの中から特定のブロックを配列で取得
|
|
965
|
+
- 属性値でフィルタして一致する **1件のブロック**を取得
|
|
966
|
+
- ネストされたブロックも含めて取得(オプション)
|
|
967
|
+
|
|
968
|
+
---
|
|
969
|
+
|
|
970
|
+
#### ✅ 使い方
|
|
971
|
+
|
|
972
|
+
##### 基本構文
|
|
973
|
+
|
|
974
|
+
```js
|
|
975
|
+
const result = useTargetBlocks(clientId, blockName, attributeFilter?, includeNested?);
|
|
976
|
+
```
|
|
977
|
+
|
|
978
|
+
| 引数 | 型 | 説明 |
|
|
979
|
+
|------|----|------|
|
|
980
|
+
| `clientId` | `string` | 呼び出し元(自分自身)の `clientId`。`useBlockEditContext()` などで取得 |
|
|
981
|
+
| `blockName` | `string` | 対象ブロック名(例: `'itmar/design-text-ctrl'`) |
|
|
982
|
+
| `attributeFilter` | `object|null` | オプション:指定した属性に一致するブロックを1件だけ取得(例: `{ inputName: 'address' }`) |
|
|
983
|
+
| `includeNested` | `boolean` | オプション:`true` でネストされたブロックも対象に含める(デフォルト: `false`) |
|
|
984
|
+
|
|
985
|
+
---
|
|
986
|
+
|
|
987
|
+
#### 🧪 使用例
|
|
988
|
+
|
|
989
|
+
##### 1. 全ての `itmar/design-text-ctrl` ブロックを取得(自分を除く)
|
|
990
|
+
|
|
991
|
+
```js
|
|
992
|
+
import { useBlockEditContext } from '@wordpress/block-editor';
|
|
993
|
+
import { useTargetBlocks } from '@your-scope/use-target-blocks';
|
|
994
|
+
|
|
995
|
+
const MyComponent = () => {
|
|
996
|
+
const { clientId } = useBlockEditContext();
|
|
997
|
+
|
|
998
|
+
const blocks = useTargetBlocks(clientId, 'itmar/design-text-ctrl');
|
|
999
|
+
|
|
1000
|
+
return <div>対象ブロック数: {blocks.length}</div>;
|
|
1001
|
+
};
|
|
1002
|
+
```
|
|
1003
|
+
|
|
1004
|
+
---
|
|
1005
|
+
|
|
1006
|
+
##### 2. `inputName: 'address'` を持つブロックを1件だけ取得
|
|
1007
|
+
|
|
1008
|
+
```js
|
|
1009
|
+
const targetBlock = useTargetBlocks(clientId, 'itmar/design-text-ctrl', {
|
|
1010
|
+
inputName: 'address',
|
|
1011
|
+
});
|
|
1012
|
+
|
|
1013
|
+
if (targetBlock) {
|
|
1014
|
+
console.log('Address block found:', targetBlock.clientId);
|
|
1015
|
+
}
|
|
1016
|
+
```
|
|
1017
|
+
|
|
1018
|
+
---
|
|
1019
|
+
|
|
1020
|
+
##### 3. ネストされたブロックも含めて検索
|
|
1021
|
+
|
|
1022
|
+
```js
|
|
1023
|
+
const nestedBlock = useTargetBlocks(
|
|
1024
|
+
clientId,
|
|
1025
|
+
'itmar/design-text-ctrl',
|
|
1026
|
+
{ inputName: 'address' },
|
|
1027
|
+
true // ネスト含める
|
|
1028
|
+
);
|
|
1029
|
+
```
|
|
1030
|
+
|
|
1031
|
+
---
|
|
1032
|
+
|
|
1033
|
+
#### 📁 内部で使用しているもの
|
|
1034
|
+
|
|
1035
|
+
- `@wordpress/data`
|
|
1036
|
+
- `@wordpress/block-editor`
|
|
1037
|
+
- `useSelect`, `getBlockRootClientId`, `getBlock`, `getBlocks`
|
|
1038
|
+
|
|
1039
|
+
---
|
|
1040
|
+
|
|
1041
|
+
#### 🔁 補助関数:`flattenBlocks`
|
|
1042
|
+
|
|
1043
|
+
ネストされたブロックを平坦化するためのユーティリティも内蔵:
|
|
1044
|
+
|
|
1045
|
+
```js
|
|
1046
|
+
const flattenBlocks = (blocks) => {
|
|
1047
|
+
return blocks.reduce((acc, block) => {
|
|
1048
|
+
acc.push(block);
|
|
1049
|
+
if (block.innerBlocks?.length > 0) {
|
|
1050
|
+
acc.push(...flattenBlocks(block.innerBlocks));
|
|
1051
|
+
}
|
|
1052
|
+
return acc;
|
|
1053
|
+
}, []);
|
|
1054
|
+
};
|
|
1055
|
+
```
|
|
1056
|
+
|
|
1057
|
+
---
|
|
1058
|
+
|
|
1059
|
+
#### 🛡️ 注意事項
|
|
1060
|
+
|
|
1061
|
+
- このフックは **Gutenberg ブロックエディタ内でのみ使用可能**です。
|
|
1062
|
+
- `useTargetBlocks()` は **React フック**です。関数やイベントハンドラ内部では直接呼び出せません。
|
|
1063
|
+
|
|
1064
|
+
---
|
|
1065
|
+
|
|
1066
|
+
## 日本郵便番号から住所を取得するユーティリティ関数
|
|
1067
|
+
### `fetchZipToAddress`
|
|
1068
|
+
`fetchZipToAddress` は、[zipcloud](https://zipcloud.ibsnet.co.jp) API を使用して、日本の郵便番号から都道府県・市区町村・町域の住所を非同期で取得する JavaScript 関数です。
|
|
1069
|
+
Gutenberg ブロック開発やフロントエンドフォーム処理において、郵便番号による住所補完機能を簡単に実装できます。
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
#### 使用例(React / jQuery 共通)
|
|
1073
|
+
|
|
1074
|
+
```js
|
|
1075
|
+
const addressObj = await fetchZipToAddress("1600022");
|
|
1076
|
+
if (addressObj) {
|
|
1077
|
+
const fullAddress = addressObj.address1 + addressObj.address2 + addressObj.address3;
|
|
1078
|
+
console.log(fullAddress); // 例: 東京都新宿区新宿
|
|
1079
|
+
}
|
|
1080
|
+
```
|
|
1081
|
+
|
|
1082
|
+
---
|
|
1083
|
+
|
|
1084
|
+
#### 🔐 バリデーション仕様
|
|
1085
|
+
|
|
1086
|
+
- 郵便番号は「**ハイフンなしの7桁の数字**」形式のみ許可されます(例: `1234567`)。
|
|
1087
|
+
- 無効な形式や一致しない郵便番号、通信エラー時には `null` を返します。
|
|
1088
|
+
- エラーはすべて `alert()` によってユーザーに通知されます。
|
|
1089
|
+
|
|
1090
|
+
---
|
|
1091
|
+
|
|
1092
|
+
#### 🔁 返り値の形式(成功時)
|
|
1093
|
+
|
|
1094
|
+
```json
|
|
1095
|
+
{
|
|
1096
|
+
"zipcode": "1600022",
|
|
1097
|
+
"prefcode": "13",
|
|
1098
|
+
"address1": "東京都",
|
|
1099
|
+
"address2": "新宿区",
|
|
1100
|
+
"address3": "新宿",
|
|
1101
|
+
...
|
|
1102
|
+
}
|
|
1103
|
+
```
|
|
1104
|
+
|
|
1105
|
+
---
|
|
1106
|
+
|
|
1107
|
+
#### 🌐 使用API
|
|
1108
|
+
|
|
1109
|
+
本ライブラリは以下の外部APIを使用しています:
|
|
1110
|
+
|
|
1111
|
+
- **zipcloud(日本郵便公式APIラッパー)**
|
|
1112
|
+
- URL: [https://zipcloud.ibsnet.co.jp](https://zipcloud.ibsnet.co.jp)
|
|
1113
|
+
- ドキュメント: [https://zipcloud.ibsnet.co.jp/doc/api](https://zipcloud.ibsnet.co.jp/doc/api)
|
|
1114
|
+
|
|
1115
|
+
#### ⚠️ ご注意
|
|
1116
|
+
|
|
1117
|
+
- この API は外部サービス(zipcloud)に依存しており、アクセス過多による制限や仕様変更の可能性があります。
|
|
1118
|
+
- 本ライブラリを利用する場合は、[zipcloud利用規約](https://zipcloud.ibsnet.co.jp/doc/rule) を必ずご確認・順守してください。
|
|
1119
|
+
|
|
1120
|
+
---
|
|
1121
|
+
|
|
1122
|
+
#### 🧩 WordPress / Gutenberg との統合例
|
|
1123
|
+
|
|
1124
|
+
```js
|
|
1125
|
+
const handleZipSearch = async () => {
|
|
1126
|
+
const result = await fetchZipToAddress(zipValue);
|
|
1127
|
+
if (result) {
|
|
1128
|
+
setAttributes({ inputValue: result.address1 + result.address2 + result.address3 });
|
|
1129
|
+
}
|
|
1130
|
+
};
|
|
1131
|
+
```
|
|
1132
|
+
|
|
1133
|
+
|
package/build/index.asset.php
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => '
|
|
1
|
+
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'ed8c3612fe1e63ebdc2a');
|