itmar-block-packages 1.10.1 → 2.0.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.
Files changed (157) hide show
  1. package/README.md +78 -2
  2. package/build/cjs/AnimationBlock.js +107 -0
  3. package/build/cjs/AnimationBlock.js.map +1 -0
  4. package/build/cjs/BlockEditWrapper.js +20 -0
  5. package/build/cjs/BlockEditWrapper.js.map +1 -0
  6. package/build/cjs/BlockPlace.js +509 -0
  7. package/build/cjs/BlockPlace.js.map +1 -0
  8. package/build/cjs/BrockInserter.js +204 -0
  9. package/build/cjs/BrockInserter.js.map +1 -0
  10. package/build/cjs/DateElm.js +321 -0
  11. package/build/cjs/DateElm.js.map +1 -0
  12. package/build/cjs/DraggableBox.js +143 -0
  13. package/build/cjs/DraggableBox.js.map +1 -0
  14. package/build/cjs/GridControls.js +421 -0
  15. package/build/cjs/GridControls.js.map +1 -0
  16. package/build/cjs/IconSelectControl.js +167 -0
  17. package/build/cjs/IconSelectControl.js.map +1 -0
  18. package/build/cjs/JapaneseHolidays.js +99 -0
  19. package/build/cjs/JapaneseHolidays.js.map +1 -0
  20. package/build/cjs/MasonryControl.js +124 -0
  21. package/build/cjs/MasonryControl.js.map +1 -0
  22. package/build/cjs/PseudoElm.js +66 -0
  23. package/build/cjs/PseudoElm.js.map +1 -0
  24. package/build/cjs/ShadowStyle.js +453 -0
  25. package/build/cjs/ShadowStyle.js.map +1 -0
  26. package/build/cjs/SwiperControl.js +267 -0
  27. package/build/cjs/SwiperControl.js.map +1 -0
  28. package/build/cjs/ToggleElement.js +17 -0
  29. package/build/cjs/ToggleElement.js.map +1 -0
  30. package/build/cjs/TypographyControls.js +151 -0
  31. package/build/cjs/TypographyControls.js.map +1 -0
  32. package/build/cjs/UpdateAllPostsBlockAttributes.js +137 -0
  33. package/build/cjs/UpdateAllPostsBlockAttributes.js.map +1 -0
  34. package/build/cjs/ZipAddress.js +34 -0
  35. package/build/cjs/ZipAddress.js.map +1 -0
  36. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +117 -0
  37. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
  38. package/build/cjs/blockStore.js +65 -0
  39. package/build/cjs/blockStore.js.map +1 -0
  40. package/build/cjs/cssPropertes.js +157 -0
  41. package/build/cjs/cssPropertes.js.map +1 -0
  42. package/build/cjs/customFooks.js +300 -0
  43. package/build/cjs/customFooks.js.map +1 -0
  44. package/build/cjs/formatCreate.js +160 -0
  45. package/build/cjs/formatCreate.js.map +1 -0
  46. package/build/cjs/hslToRgb.js +133 -0
  47. package/build/cjs/hslToRgb.js.map +1 -0
  48. package/build/cjs/index.js +115 -0
  49. package/build/cjs/index.js.map +1 -0
  50. package/build/cjs/mediaUpload.js +182 -0
  51. package/build/cjs/mediaUpload.js.map +1 -0
  52. package/build/cjs/node_modules/nanoid/index.js +30 -0
  53. package/build/cjs/node_modules/nanoid/index.js.map +1 -0
  54. package/build/cjs/node_modules/nanoid/url-alphabet/index.js +7 -0
  55. package/build/cjs/node_modules/nanoid/url-alphabet/index.js.map +1 -0
  56. package/build/cjs/shopfiApi.js +188 -0
  57. package/build/cjs/shopfiApi.js.map +1 -0
  58. package/build/cjs/validationCheck.js +15 -0
  59. package/build/cjs/validationCheck.js.map +1 -0
  60. package/build/cjs/wordpressApi.js +631 -0
  61. package/build/cjs/wordpressApi.js.map +1 -0
  62. package/build/esm/AnimationBlock.js +102 -0
  63. package/build/esm/AnimationBlock.js.map +1 -0
  64. package/build/esm/BlockEditWrapper.js +16 -0
  65. package/build/esm/BlockEditWrapper.js.map +1 -0
  66. package/build/esm/BlockPlace.js +503 -0
  67. package/build/esm/BlockPlace.js.map +1 -0
  68. package/build/esm/BrockInserter.js +202 -0
  69. package/build/esm/BrockInserter.js.map +1 -0
  70. package/build/esm/DateElm.js +309 -0
  71. package/build/esm/DateElm.js.map +1 -0
  72. package/build/esm/DraggableBox.js +138 -0
  73. package/build/esm/DraggableBox.js.map +1 -0
  74. package/build/esm/GridControls.js +417 -0
  75. package/build/esm/GridControls.js.map +1 -0
  76. package/build/esm/IconSelectControl.js +163 -0
  77. package/build/esm/IconSelectControl.js.map +1 -0
  78. package/build/esm/JapaneseHolidays.js +97 -0
  79. package/build/esm/JapaneseHolidays.js.map +1 -0
  80. package/{src → build/esm}/MasonryControl.js +108 -113
  81. package/build/esm/MasonryControl.js.map +1 -0
  82. package/build/esm/PseudoElm.js +61 -0
  83. package/build/esm/PseudoElm.js.map +1 -0
  84. package/build/esm/ShadowStyle.js +448 -0
  85. package/build/esm/ShadowStyle.js.map +1 -0
  86. package/{src → build/esm}/SwiperControl.js +265 -265
  87. package/build/esm/SwiperControl.js.map +1 -0
  88. package/build/esm/ToggleElement.js +13 -0
  89. package/build/esm/ToggleElement.js.map +1 -0
  90. package/build/esm/TypographyControls.js +147 -0
  91. package/build/esm/TypographyControls.js.map +1 -0
  92. package/build/esm/UpdateAllPostsBlockAttributes.js +133 -0
  93. package/build/esm/UpdateAllPostsBlockAttributes.js.map +1 -0
  94. package/build/esm/ZipAddress.js +32 -0
  95. package/build/esm/ZipAddress.js.map +1 -0
  96. package/build/esm/_virtual/_rollupPluginBabelHelpers.js +107 -0
  97. package/build/esm/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
  98. package/build/esm/blockStore.js +60 -0
  99. package/build/esm/blockStore.js.map +1 -0
  100. package/build/esm/cssPropertes.js +144 -0
  101. package/build/esm/cssPropertes.js.map +1 -0
  102. package/{src → build/esm}/customFooks.js +290 -337
  103. package/build/esm/customFooks.js.map +1 -0
  104. package/build/esm/formatCreate.js +157 -0
  105. package/build/esm/formatCreate.js.map +1 -0
  106. package/build/esm/hslToRgb.js +129 -0
  107. package/build/esm/hslToRgb.js.map +1 -0
  108. package/build/esm/index.js +27 -0
  109. package/build/esm/index.js.map +1 -0
  110. package/build/esm/mediaUpload.js +176 -0
  111. package/build/esm/mediaUpload.js.map +1 -0
  112. package/build/esm/node_modules/nanoid/index.js +27 -0
  113. package/build/esm/node_modules/nanoid/index.js.map +1 -0
  114. package/build/esm/node_modules/nanoid/url-alphabet/index.js +5 -0
  115. package/build/esm/node_modules/nanoid/url-alphabet/index.js.map +1 -0
  116. package/build/esm/shopfiApi.js +184 -0
  117. package/build/esm/shopfiApi.js.map +1 -0
  118. package/build/esm/validationCheck.js +13 -0
  119. package/build/esm/validationCheck.js.map +1 -0
  120. package/build/esm/wordpressApi.js +618 -0
  121. package/build/esm/wordpressApi.js.map +1 -0
  122. package/package.json +22 -6
  123. package/build/index.asset.php +0 -1
  124. package/build/index.js +0 -25
  125. package/css/editor.css +0 -23
  126. package/css/editor.css.map +0 -1
  127. package/img/animation.png +0 -0
  128. package/img/blockplace.png +0 -0
  129. package/img/grid.png +0 -0
  130. package/img/iconControl.png +0 -0
  131. package/img/pseudo.png +0 -0
  132. package/img/shadow.png +0 -0
  133. package/img/typography.png +0 -0
  134. package/src/AnimationBlock.js +0 -112
  135. package/src/BlockEditWrapper.js +0 -11
  136. package/src/BlockPlace.js +0 -904
  137. package/src/BrockInserter.js +0 -247
  138. package/src/DateElm.js +0 -354
  139. package/src/DraggableBox.js +0 -143
  140. package/src/GridControls.js +0 -462
  141. package/src/IconSelectControl.js +0 -186
  142. package/src/PseudoElm.js +0 -54
  143. package/src/ShadowStyle.js +0 -520
  144. package/src/ToggleElement.js +0 -18
  145. package/src/TypographyControls.js +0 -145
  146. package/src/UpdateAllPostsBlockAttributes.js +0 -127
  147. package/src/ZipAddress.js +0 -35
  148. package/src/blockStore.js +0 -75
  149. package/src/cssPropertes.js +0 -212
  150. package/src/formatCreate.js +0 -179
  151. package/src/hslToRgb.js +0 -162
  152. package/src/index.js +0 -131
  153. package/src/mediaUpload.js +0 -178
  154. package/src/shopfiApi.js +0 -187
  155. package/src/validationCheck.js +0 -10
  156. package/src/wordpressApi.js +0 -707
  157. package/webpack.config.js +0 -10
@@ -1,707 +0,0 @@
1
- import { useState, useEffect } from "@wordpress/element";
2
- import { __ } from "@wordpress/i18n";
3
- import {
4
- ComboboxControl,
5
- CheckboxControl,
6
- ToggleControl,
7
- } from "@wordpress/components";
8
- import apiFetch from "@wordpress/api-fetch";
9
- import { addQueryArgs } from "@wordpress/url";
10
-
11
- //const _ = require("lodash");
12
-
13
- export const restFetchData = async (path) => {
14
- try {
15
- const ret_data = await apiFetch({ path: path });
16
- return ret_data;
17
- } catch (error) {
18
- console.error("Error fetching data:", error.message);
19
- }
20
- };
21
-
22
- //コンボボックスコントロールのレンダリング関数
23
- const SelectControl = (props) => {
24
- const { selectedSlug, label, homeUrl, fetchOptions } = props;
25
- const [options, setOptions] = useState([]);
26
-
27
- useEffect(() => {
28
- const fetchData = async () => {
29
- try {
30
- const fetchedOptions = await fetchOptions(homeUrl);
31
- setOptions(fetchedOptions);
32
- } catch (error) {
33
- console.error("Error fetching data:", error.message);
34
- }
35
- };
36
-
37
- fetchData();
38
- }, [fetchOptions]);
39
-
40
- const selectedInfo = options.find((info) => info.slug === selectedSlug);
41
- return (
42
- <ComboboxControl
43
- label={label}
44
- options={options}
45
- value={selectedInfo ? selectedInfo.value : -1}
46
- onChange={(newValue) => {
47
- const newInfo = options.find((info) => info.value === newValue);
48
- props.onChange(newInfo);
49
- }}
50
- />
51
- );
52
- };
53
-
54
- //選択コントロールのレンダリング関数
55
- const ChoiceControl = (props) => {
56
- const {
57
- selectedSlug,
58
- choiceItems,
59
- dispTaxonomies,
60
- type,
61
- blockMap,
62
- textDomain,
63
- fetchFunction,
64
- } = props;
65
-
66
- const [choices, setChoices] = useState([]);
67
- useEffect(() => {
68
- if (!selectedSlug) return; //ポストタイプのスラッグが選択されていないときは処理終了
69
- const fetchData = async () => {
70
- try {
71
- const fetchChoices = await fetchFunction(selectedSlug);
72
- setChoices(fetchChoices);
73
- //指定の投稿タイプに含まれないフィールドを削除する
74
- pruneChoiceItemsByObjectKeys(fetchChoices[0], choiceItems);
75
- } catch (error) {
76
- console.error("Error fetching data:", error.message);
77
- }
78
- };
79
- fetchData();
80
- }, [selectedSlug, fetchFunction]);
81
-
82
- //選択肢が変わったときに選択されている項目の配列内容を更新するハンドラ
83
- const handleChoiceChange = (checked, target, setItems) => {
84
- if (checked) {
85
- // targetが重複していない場合のみ追加
86
- if (!setItems.some((item) => _.isEqual(item, target))) {
87
- return [...setItems, target];
88
- }
89
- } else {
90
- // targetを配列から削除
91
- return setItems.filter((item) => !_.isEqual(item, target));
92
- }
93
- return setItems;
94
- };
95
- /**
96
- * dataObj のキー一覧を「choiceItems と比較する形」に変換して Set で返す
97
- * - 通常キー: そのまま
98
- * - acf / meta: 子キーに `${parent}_` を付けたもの(例: acf_relate_url, meta_footnotes)
99
- */
100
- function buildComparableKeySet(dataObj) {
101
- const keySet = new Set();
102
-
103
- if (!dataObj || typeof dataObj !== "object") return keySet;
104
-
105
- for (const [key, val] of Object.entries(dataObj)) {
106
- if (
107
- (key === "acf" || key === "meta") &&
108
- val &&
109
- typeof val === "object" &&
110
- !Array.isArray(val)
111
- ) {
112
- for (const childKey of Object.keys(val)) {
113
- keySet.add(`${key}_${childKey}`);
114
- }
115
- continue;
116
- }
117
-
118
- keySet.add(key);
119
- }
120
-
121
- return keySet;
122
- }
123
-
124
- /**
125
- * choiceItems を dataObj のキーに合わせて削除する
126
- * - choiceItems が string 配列でも、{value: "..."} の配列でも動くようにしてあります
127
- */
128
- function pruneChoiceItemsByObjectKeys(dataObj, choiceItems) {
129
- const validKeys = buildComparableKeySet(dataObj);
130
-
131
- const getItemKey = (item) => {
132
- if (typeof item === "string") return item;
133
- if (item && typeof item === "object")
134
- return item.value ?? item.key ?? item.name ?? "";
135
- return "";
136
- };
137
-
138
- const next = (choiceItems ?? []).filter((item) =>
139
- validKeys.has(getItemKey(item))
140
- );
141
-
142
- // ★ 配列の参照はそのまま、中身だけ置き換える
143
- choiceItems.splice(0, choiceItems.length, ...next);
144
-
145
- return choiceItems; // 必要なら返す
146
- }
147
-
148
- //階層化されたカスタムフィールドのフィールド名を表示する関数
149
- let groupLabel = "";
150
- const dispCustumFields = (obj, prefix = "", isImage = false, onChange) => {
151
- return Object.entries(obj).map(([key, value]) => {
152
- const fieldName = prefix ? `${prefix}.${key}` : key; //prefixはグループ名
153
-
154
- const fieldLabel = key.replace(/^(meta_|acf_)/, "");
155
- //オブジェクトであって配列でないものがグループと考える
156
- if (
157
- typeof value === "object" &&
158
- !Array.isArray(value) &&
159
- value !== null
160
- ) {
161
- groupLabel = `${fieldLabel}.`;
162
- return (
163
- <div className="group_area">
164
- <div className="group_label">{fieldLabel}</div>
165
- <div key={fieldName} className="field_group">
166
- {dispCustumFields(value, fieldName, isImage, onChange)}
167
- </div>
168
- </div>
169
- );
170
- } else {
171
- if (key === "meta__acf_changed" || key === "meta_footnotes") return; //_acf_changedは対象外
172
-
173
- //フィールドを表示するブロックの選択肢
174
- const options = [
175
- { value: "itmar/design-title", label: "itmar/design-title" },
176
- { value: "core/paragraph", label: "core/paragraph" },
177
- { value: "core/image", label: "core/image" },
178
- { value: "itmar/slide-mv", label: "itmar/slide-mv" },
179
- ];
180
- return (
181
- <div className="itmar_custom_field_set">
182
- <ToggleControl
183
- key={fieldName}
184
- className="field_choice"
185
- label={fieldLabel}
186
- checked={choiceItems.some(
187
- (choiceField) => choiceField === fieldName
188
- )}
189
- onChange={(checked) => {
190
- const newChoiceFields = handleChoiceChange(
191
- checked,
192
- fieldName,
193
- choiceItems
194
- );
195
- props.onChange(newChoiceFields);
196
- }}
197
- />
198
- {!isImage && (
199
- <ComboboxControl
200
- options={options}
201
- value={
202
- //blockMap[`${prefix ? groupLabel : ""}${key}`] || "itmar/design-title"
203
- blockMap[`${prefix ? prefix + "." : ""}${key}`] ||
204
- "itmar/design-title"
205
- }
206
- onChange={(newValue) => {
207
- //const fieldKey = prefix ? `${groupLabel}${key}` : `${key}`;
208
- const fieldKey = prefix ? `${prefix}.${key}` : `${key}`;
209
-
210
- const newBlockMap = { ...blockMap, [fieldKey]: newValue };
211
- props.onBlockMapChange(newBlockMap);
212
- }}
213
- />
214
- )}
215
- </div>
216
- );
217
- }
218
- });
219
- };
220
-
221
- return (
222
- <div className={`${type}_section`}>
223
- {type === "taxonomy" &&
224
- choices.map((choice, index) => {
225
- return (
226
- <div key={index} className="term_section">
227
- <div className="tax_label">
228
- {choice.name}
229
- <ToggleControl
230
- label={__("Display", "block-collections")}
231
- checked={dispTaxonomies.some((tax) => tax === choice.slug)}
232
- onChange={(checked) => {
233
- const newChoiceFields = handleChoiceChange(
234
- checked,
235
- choice.slug,
236
- dispTaxonomies
237
- );
238
- props.onSetDispTax(newChoiceFields);
239
- }}
240
- />
241
- </div>
242
- {choice.terms.map((term, index) => {
243
- return (
244
- <CheckboxControl
245
- className="term_check"
246
- key={index}
247
- label={term.name}
248
- checked={choiceItems.some((choiceTerm) => {
249
- return (
250
- choiceTerm.taxonomy === choice.slug &&
251
- choiceTerm.term.id === term.id
252
- );
253
- })}
254
- onChange={(checked) => {
255
- const target = {
256
- taxonomy: choice.slug,
257
- term: { id: term.id, slug: term.slug, name: term.name },
258
- };
259
- const newChoiceTerms = handleChoiceChange(
260
- checked,
261
- target,
262
- choiceItems
263
- );
264
- props.onChange(newChoiceTerms);
265
- }}
266
- />
267
- );
268
- })}
269
- </div>
270
- );
271
- })}
272
- {type === "field" &&
273
- choices.map((choice, index) => {
274
- //metaの対象カスタムフィールドが含まれるかのフラグ
275
- const metaFlg =
276
- choice.meta &&
277
- !Object.keys(choice.meta).every(
278
- (key) => key === "_acf_changed" || key === "footnotes"
279
- );
280
- //acfの対象カスタムフィールドが含まれるかのフラグ
281
- const acfFlg =
282
- choice.acf &&
283
- typeof choice.acf === "object" &&
284
- !Array.isArray(choice.acf);
285
-
286
- return (
287
- <div key={index} className="field_section">
288
- {choice.title && (
289
- <ToggleControl
290
- className="field_choice"
291
- label={__("Title", "block-collections")}
292
- checked={choiceItems.some(
293
- (choiceField) => choiceField === "title"
294
- )}
295
- onChange={(checked) => {
296
- const newChoiceFields = handleChoiceChange(
297
- checked,
298
- "title",
299
- choiceItems
300
- );
301
- props.onChange(newChoiceFields);
302
- }}
303
- />
304
- )}
305
- {choice.content && (
306
- <ToggleControl
307
- className="field_choice"
308
- label={__("Content", "block-collections")}
309
- checked={choiceItems.some(
310
- (choiceField) => choiceField === "content"
311
- )}
312
- onChange={(checked) => {
313
- const newChoiceFields = handleChoiceChange(
314
- checked,
315
- "content",
316
- choiceItems
317
- );
318
- props.onChange(newChoiceFields);
319
- }}
320
- />
321
- )}
322
- {choice.date && (
323
- <ToggleControl
324
- className="field_choice"
325
- label={__("Date", "block-collections")}
326
- checked={choiceItems.some(
327
- (choiceField) => choiceField === "date"
328
- )}
329
- onChange={(checked) => {
330
- const newChoiceFields = handleChoiceChange(
331
- checked,
332
- "date",
333
- choiceItems
334
- );
335
- props.onChange(newChoiceFields);
336
- }}
337
- />
338
- )}
339
- {choice.excerpt && (
340
- <ToggleControl
341
- className="field_choice"
342
- label={__("Excerpt", "block-collections")}
343
- checked={choiceItems.some(
344
- (choiceField) => choiceField === "excerpt"
345
- )}
346
- onChange={(checked) => {
347
- const newChoiceFields = handleChoiceChange(
348
- checked,
349
- "excerpt",
350
- choiceItems
351
- );
352
- props.onChange(newChoiceFields);
353
- }}
354
- />
355
- )}
356
- {(choice.featured_media || choice.featured_media === 0) && (
357
- <ToggleControl
358
- className="field_choice"
359
- label={__("Featured Image", "block-collections")}
360
- checked={choiceItems.some(
361
- (choiceField) => choiceField === "featured_media"
362
- )}
363
- onChange={(checked) => {
364
- const newChoiceFields = handleChoiceChange(
365
- checked,
366
- "featured_media",
367
- choiceItems
368
- );
369
- props.onChange(newChoiceFields);
370
- }}
371
- />
372
- )}
373
- {choice.link && (
374
- <div className="itmar_custom_field_set">
375
- <ToggleControl
376
- className="field_choice"
377
- label={__("Single Page Link", "block-collections")}
378
- checked={choiceItems.some(
379
- (choiceField) => choiceField === "link"
380
- )}
381
- onChange={(checked) => {
382
- const newChoiceFields = handleChoiceChange(
383
- checked,
384
- "link",
385
- choiceItems
386
- );
387
- props.onChange(newChoiceFields);
388
- }}
389
- />
390
- <ComboboxControl
391
- options={[
392
- {
393
- value: "itmar/design-button",
394
- label: "itmar/design-button",
395
- },
396
- {
397
- value: "itmar/design-title",
398
- label: "itmar/design-title",
399
- },
400
- ]}
401
- value={blockMap["link"]}
402
- onChange={(newValue) => {
403
- const newBlockMap = {
404
- ...blockMap,
405
- link: newValue,
406
- };
407
- props.onBlockMapChange(newBlockMap);
408
- }}
409
- />
410
- <p>
411
- {__(
412
- "If no block is specified, a link will be set to the parent block, Design Group.",
413
- "block-collections"
414
- )}
415
- </p>
416
- </div>
417
- )}
418
- {(metaFlg || acfFlg) && (
419
- <>
420
- <div className="custom_field_label">
421
- {__("Custom Field", "block-collections")}
422
- </div>
423
- <div className="custom_field_area">
424
- {dispCustumFields({
425
- // meta はそのまま
426
- ...Object.entries(choice.meta).reduce(
427
- (acc, [key, value]) => ({
428
- ...acc,
429
- [`meta_${key}`]: value,
430
- }),
431
- {}
432
- ),
433
- // acf は「同名で _source があるもののベース側を除く」
434
- ...Object.entries(choice.acf)
435
- .filter(([key]) => !key.endsWith("_source"))
436
- .reduce(
437
- (acc, [key, value]) => ({
438
- ...acc,
439
- [`acf_${key}`]: value,
440
- }),
441
- {}
442
- ),
443
- })}
444
- </div>
445
- </>
446
- )}
447
- </div>
448
- );
449
- })}
450
- {type === "imgField" &&
451
- choices.map((choice, index) => {
452
- //metaの対象カスタムフィールドが含まれるかのフラグ
453
- const metaFlg =
454
- choice.meta &&
455
- !Object.keys(choice.meta).every(
456
- (key) => key === "_acf_changed" || key === "footnotes"
457
- );
458
- //acfの対象カスタムフィールドが含まれるかのフラグ
459
- const acfFlg =
460
- choice.acf &&
461
- typeof choice.acf === "object" &&
462
- !Array.isArray(choice.acf);
463
-
464
- return (
465
- <div key={index} className="field_section">
466
- {choice.content && (
467
- <ToggleControl
468
- className="field_choice"
469
- label={__("Content", "block-collections")}
470
- checked={choiceItems.some(
471
- (choiceField) => choiceField === "content"
472
- )}
473
- onChange={(checked) => {
474
- const newChoiceFields = handleChoiceChange(
475
- checked,
476
- "content",
477
- choiceItems
478
- );
479
- props.onChange(newChoiceFields);
480
- }}
481
- />
482
- )}
483
- {(choice.featured_media || choice.featured_media === 0) && (
484
- <ToggleControl
485
- className="field_choice"
486
- label={__("Featured Image", "block-collections")}
487
- checked={choiceItems.some(
488
- (choiceField) => choiceField === "featured_media"
489
- )}
490
- onChange={(checked) => {
491
- const newChoiceFields = handleChoiceChange(
492
- checked,
493
- "featured_media",
494
- choiceItems
495
- );
496
- props.onChange(newChoiceFields);
497
- }}
498
- />
499
- )}
500
-
501
- {(metaFlg || acfFlg) && (
502
- <>
503
- <div className="custom_field_label">
504
- {__("Custom Field", "block-collections")}
505
- </div>
506
- <div className="custom_field_area">
507
- {dispCustumFields(
508
- {
509
- // meta はそのまま
510
- ...Object.entries(choice.meta).reduce(
511
- (acc, [key, value]) => ({
512
- ...acc,
513
- [`meta_${key}`]: value,
514
- }),
515
- {}
516
- ),
517
- // acf は「同名で _source があるもののベース側を除く」
518
- ...Object.entries(choice.acf)
519
- .filter(([key]) => !key.endsWith("_source"))
520
- .reduce(
521
- (acc, [key, value]) => ({
522
- ...acc,
523
- [`acf_${key}`]: value,
524
- }),
525
- {}
526
- ),
527
- },
528
- "",
529
- true
530
- )}
531
- </div>
532
- </>
533
- )}
534
- </div>
535
- );
536
- })}
537
- </div>
538
- );
539
- };
540
-
541
- //固定ページ取得RestAPI関数
542
- export const fetchPagesOptions = async (home_url) => {
543
- const allPages = [];
544
- let page = 1;
545
-
546
- while (true) {
547
- const items = await apiFetch({
548
- path: addQueryArgs("/wp/v2/pages", {
549
- status: "publish", // 公開のみ
550
- per_page: 100, // 最大100
551
- page,
552
- // orderby: "title",
553
- // order: "asc",
554
- }),
555
- });
556
-
557
- allPages.push(...items);
558
-
559
- // 100件未満ならこれが最後
560
- if (!items || items.length < 100) break;
561
-
562
- page++;
563
- }
564
-
565
- // ページIDが-1である要素をホーム要素として作成
566
- if (!allPages.some((p) => p.id === -1)) {
567
- allPages.unshift({
568
- id: -1,
569
- title: { rendered: "ホーム" },
570
- link: home_url,
571
- slug: "",
572
- });
573
- }
574
-
575
- const ret_pages = allPages.map((p) => ({
576
- value: p.id,
577
- slug: p.slug,
578
- label: p.title?.rendered ?? "",
579
- // 階層ページでも正しいURLになるようにRESTのlinkを優先
580
- link: p.link || (p.slug ? `${home_url}/${p.slug}` : home_url),
581
- }));
582
-
583
- return ret_pages;
584
- };
585
-
586
- //アーカイブ情報取得RestAPI関数
587
- export const fetchArchiveOptions = async (home_url) => {
588
- const response = await apiFetch({ path: "/wp/v2/types" });
589
-
590
- let idCounter = 0;
591
- return Object.keys(response).reduce((acc, key) => {
592
- const postType = response[key];
593
- if (postType.has_archive === true) {
594
- acc.push({
595
- value: idCounter++,
596
- slug: postType.slug,
597
- rest_base: postType.rest_base,
598
- link: `${home_url}/${postType.slug}`,
599
- label: postType.name,
600
- });
601
- } else if (typeof postType.has_archive === "string") {
602
- //アーカイブ名がついているとき
603
- acc.push({
604
- value: idCounter++,
605
- slug: postType.slug,
606
- rest_base: postType.rest_base,
607
- link: `${home_url}/${postType.has_archive}`,
608
- label: postType.name,
609
- });
610
- }
611
- return acc;
612
- }, []);
613
- };
614
-
615
- //タクソノミー取得RestAPI関数
616
- export const restTaxonomies = async (post_type) => {
617
- if (!post_type) return;
618
-
619
- const response = await apiFetch({
620
- path: `/wp/v2/types/${post_type}?context=edit`,
621
- });
622
-
623
- const taxonomies = response.taxonomies;
624
-
625
- const taxonomyPromises = taxonomies.map(async (slug) => {
626
- const taxonomyResponse = await apiFetch({
627
- path: `/wp/v2/taxonomies/${slug}?context=edit`,
628
- });
629
- const terms = await apiFetch({
630
- path: `/wp/v2/${taxonomyResponse.rest_base}`,
631
- });
632
-
633
- return {
634
- slug: slug,
635
- name: taxonomyResponse.name,
636
- rest_base: taxonomyResponse.rest_base,
637
- terms: terms,
638
- };
639
- });
640
-
641
- const taxonomyObjects = await Promise.all(taxonomyPromises);
642
- return taxonomyObjects;
643
- };
644
-
645
- //タームの文字列化
646
- export const termToDispObj = (terms, connectString) => {
647
- // taxonomyごとにterm.nameをまとめる
648
- const result = terms.reduce((acc, item) => {
649
- const taxonomy = item.taxonomy;
650
- const termName = item.term.name;
651
-
652
- // taxonomyがまだ存在しない場合は初期化
653
- if (!acc[taxonomy]) {
654
- acc[taxonomy] = [];
655
- }
656
-
657
- // term.nameを配列に追加
658
- acc[taxonomy].push(termName);
659
-
660
- return acc;
661
- }, {});
662
-
663
- // 各taxonomyの配列を connectString でつなげて文字列化
664
- for (const taxonomy in result) {
665
- result[taxonomy] = result[taxonomy].join(connectString);
666
- }
667
-
668
- return result;
669
- };
670
-
671
- //フィールド情報取得RestAPI関数
672
- export const restFieldes = async (rest_base) => {
673
- //投稿データに以下のフィールドが含まれているかを調べる
674
- const selectedFields = [
675
- "title",
676
- "content",
677
- "date",
678
- "excerpt",
679
- "featured_media",
680
- "link",
681
- "meta",
682
- "acf",
683
- ];
684
- const fieldsParam = selectedFields.join(",");
685
- //最新の投稿データから1件分のデータを抽出
686
- const response = await apiFetch({
687
- path: `/wp/v2/${rest_base}?_fields=${fieldsParam}&per_page=1&order=desc`,
688
- });
689
-
690
- return response;
691
- };
692
-
693
- export const PageSelectControl = (props) => (
694
- <SelectControl {...props} fetchOptions={fetchPagesOptions} />
695
- );
696
-
697
- export const ArchiveSelectControl = (props) => (
698
- <SelectControl {...props} fetchOptions={fetchArchiveOptions} />
699
- );
700
-
701
- export const TermChoiceControl = (props) => (
702
- <ChoiceControl {...props} fetchFunction={restTaxonomies} />
703
- );
704
-
705
- export const FieldChoiceControl = (props) => (
706
- <ChoiceControl {...props} fetchFunction={restFieldes} />
707
- );