itmar-block-packages 1.4.2 → 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/README.md +122 -0
- package/build/index.asset.php +1 -1
- package/build/index.js +3 -3
- package/package.json +1 -1
- package/src/UpdateAllPostsBlockAttributes.js +9 -16
- package/src/blockStore.js +44 -0
- package/src/index.js +1 -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.5.0 =
|
|
18
|
+
- useTargetBlocksを新設
|
|
19
|
+
|
|
20
|
+
= 1.4.3 =
|
|
21
|
+
- UpdateAllPostsBlockAttributesコンポーネントのRestAPIによる更新の不具合を修正
|
|
22
|
+
|
|
17
23
|
= 1.4.2 =
|
|
18
24
|
- UpdateAllPostsBlockAttributesコンポーネントにonProcessStartのコールバック関数の処理を付加
|
|
19
25
|
- parse, serializeのimport漏れを修正
|
|
@@ -940,3 +946,119 @@ WordPressのRestAPIで使用するrest_baseに相当する文字列
|
|
|
940
946
|
/>
|
|
941
947
|
```
|
|
942
948
|
|
|
949
|
+
## インナーブロックを取得・操作する関数
|
|
950
|
+
### `useTargetBlocks`
|
|
951
|
+
|
|
952
|
+
`useTargetBlocks` は、**Gutenberg ブロックエディタ上で同じ親ブロック内にある特定の名前・属性を持つブロックを取得する React フック**です。
|
|
953
|
+
ネストされたブロックの検索にも対応しています。
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
#### 🧩 概要
|
|
958
|
+
|
|
959
|
+
このカスタムフックは、次のような用途に使えます:
|
|
960
|
+
|
|
961
|
+
- 同じ親ブロックの中から特定のブロックを配列で取得
|
|
962
|
+
- 属性値でフィルタして一致する **1件のブロック**を取得
|
|
963
|
+
- ネストされたブロックも含めて取得(オプション)
|
|
964
|
+
|
|
965
|
+
---
|
|
966
|
+
|
|
967
|
+
#### ✅ 使い方
|
|
968
|
+
|
|
969
|
+
##### 基本構文
|
|
970
|
+
|
|
971
|
+
```js
|
|
972
|
+
const result = useTargetBlocks(clientId, blockName, attributeFilter?, includeNested?);
|
|
973
|
+
```
|
|
974
|
+
|
|
975
|
+
| 引数 | 型 | 説明 |
|
|
976
|
+
|------|----|------|
|
|
977
|
+
| `clientId` | `string` | 呼び出し元(自分自身)の `clientId`。`useBlockEditContext()` などで取得 |
|
|
978
|
+
| `blockName` | `string` | 対象ブロック名(例: `'itmar/design-text-ctrl'`) |
|
|
979
|
+
| `attributeFilter` | `object|null` | オプション:指定した属性に一致するブロックを1件だけ取得(例: `{ inputName: 'address' }`) |
|
|
980
|
+
| `includeNested` | `boolean` | オプション:`true` でネストされたブロックも対象に含める(デフォルト: `false`) |
|
|
981
|
+
|
|
982
|
+
---
|
|
983
|
+
|
|
984
|
+
#### 🧪 使用例
|
|
985
|
+
|
|
986
|
+
##### 1. 全ての `itmar/design-text-ctrl` ブロックを取得(自分を除く)
|
|
987
|
+
|
|
988
|
+
```js
|
|
989
|
+
import { useBlockEditContext } from '@wordpress/block-editor';
|
|
990
|
+
import { useTargetBlocks } from '@your-scope/use-target-blocks';
|
|
991
|
+
|
|
992
|
+
const MyComponent = () => {
|
|
993
|
+
const { clientId } = useBlockEditContext();
|
|
994
|
+
|
|
995
|
+
const blocks = useTargetBlocks(clientId, 'itmar/design-text-ctrl');
|
|
996
|
+
|
|
997
|
+
return <div>対象ブロック数: {blocks.length}</div>;
|
|
998
|
+
};
|
|
999
|
+
```
|
|
1000
|
+
|
|
1001
|
+
---
|
|
1002
|
+
|
|
1003
|
+
##### 2. `inputName: 'address'` を持つブロックを1件だけ取得
|
|
1004
|
+
|
|
1005
|
+
```js
|
|
1006
|
+
const targetBlock = useTargetBlocks(clientId, 'itmar/design-text-ctrl', {
|
|
1007
|
+
inputName: 'address',
|
|
1008
|
+
});
|
|
1009
|
+
|
|
1010
|
+
if (targetBlock) {
|
|
1011
|
+
console.log('Address block found:', targetBlock.clientId);
|
|
1012
|
+
}
|
|
1013
|
+
```
|
|
1014
|
+
|
|
1015
|
+
---
|
|
1016
|
+
|
|
1017
|
+
##### 3. ネストされたブロックも含めて検索
|
|
1018
|
+
|
|
1019
|
+
```js
|
|
1020
|
+
const nestedBlock = useTargetBlocks(
|
|
1021
|
+
clientId,
|
|
1022
|
+
'itmar/design-text-ctrl',
|
|
1023
|
+
{ inputName: 'address' },
|
|
1024
|
+
true // ネスト含める
|
|
1025
|
+
);
|
|
1026
|
+
```
|
|
1027
|
+
|
|
1028
|
+
---
|
|
1029
|
+
|
|
1030
|
+
#### 📁 内部で使用しているもの
|
|
1031
|
+
|
|
1032
|
+
- `@wordpress/data`
|
|
1033
|
+
- `@wordpress/block-editor`
|
|
1034
|
+
- `useSelect`, `getBlockRootClientId`, `getBlock`, `getBlocks`
|
|
1035
|
+
|
|
1036
|
+
---
|
|
1037
|
+
|
|
1038
|
+
#### 🔁 補助関数:`flattenBlocks`
|
|
1039
|
+
|
|
1040
|
+
ネストされたブロックを平坦化するためのユーティリティも内蔵:
|
|
1041
|
+
|
|
1042
|
+
```js
|
|
1043
|
+
const flattenBlocks = (blocks) => {
|
|
1044
|
+
return blocks.reduce((acc, block) => {
|
|
1045
|
+
acc.push(block);
|
|
1046
|
+
if (block.innerBlocks?.length > 0) {
|
|
1047
|
+
acc.push(...flattenBlocks(block.innerBlocks));
|
|
1048
|
+
}
|
|
1049
|
+
return acc;
|
|
1050
|
+
}, []);
|
|
1051
|
+
};
|
|
1052
|
+
```
|
|
1053
|
+
|
|
1054
|
+
---
|
|
1055
|
+
|
|
1056
|
+
#### 🛡️ 注意事項
|
|
1057
|
+
|
|
1058
|
+
- このフックは **Gutenberg ブロックエディタ内でのみ使用可能**です。
|
|
1059
|
+
- `useTargetBlocks()` は **React フック**です。関数やイベントハンドラ内部では直接呼び出せません。
|
|
1060
|
+
|
|
1061
|
+
---
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
|
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' => '3cfacb3021cb89e59e7d');
|