itmar-block-packages 1.3.0 → 1.3.3

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/src/index.js CHANGED
@@ -1,6 +1,3 @@
1
- import "./editor.scss";
2
- import "./style.scss";
3
-
4
1
  //カスタムフック(一般)
5
2
  export {
6
3
  useIsIframeMobile,
@@ -20,7 +17,7 @@ export {
20
17
  PageSelectControl,
21
18
  ArchiveSelectControl,
22
19
  TermChoiceControl,
23
- //FieldChoiceControl,
20
+ FieldChoiceControl,
24
21
  } from "./wordpressApi";
25
22
 
26
23
  //styled-componet用のcssプロパティ生成関数
@@ -1,7 +1,13 @@
1
1
  import { useState, useEffect } from "@wordpress/element";
2
- import { ComboboxControl, CheckboxControl } from "@wordpress/components";
2
+ import {
3
+ ComboboxControl,
4
+ CheckboxControl,
5
+ ToggleControl,
6
+ } from "@wordpress/components";
3
7
  import apiFetch from "@wordpress/api-fetch";
4
8
 
9
+ //const _ = require("lodash");
10
+
5
11
  export const fetchData = async (rest_api) => {
6
12
  try {
7
13
  const ret_data = await rest_api();
@@ -44,12 +50,12 @@ const SelectControl = (props) => {
44
50
  );
45
51
  };
46
52
 
47
- //タームの選択コントロールのレンダリング関数
53
+ //選択コントロールのレンダリング関数
48
54
  const ChoiceControl = (props) => {
49
- const { selectedSlug, choiceTerms, type, label, fetchFunction } = props;
55
+ const { selectedSlug, choiceItems, type, fetchFunction } = props;
50
56
  const [choices, setChoices] = useState([]);
51
-
52
57
  useEffect(() => {
58
+ if (!selectedSlug) return; //ポストタイプのスラッグが選択されていないときは処理終了
53
59
  const fetchData = async () => {
54
60
  try {
55
61
  const fetchChoices = await fetchFunction(selectedSlug);
@@ -61,29 +67,55 @@ const ChoiceControl = (props) => {
61
67
  fetchData();
62
68
  }, [selectedSlug, fetchFunction]);
63
69
 
64
- const handleTermChange = (checked, target) => {
70
+ //選択肢が変わったときに選択されている項目の配列内容を更新するハンドラ
71
+ const handleChoiceChange = (checked, target) => {
65
72
  if (checked) {
66
73
  // targetが重複していない場合のみ追加
67
- if (
68
- !choiceTerms.some(
69
- (term) =>
70
- term.taxonomy === target.taxonomy && term.term === target.term
71
- )
72
- ) {
73
- return [...choiceTerms, target];
74
+ if (!choiceItems.some((item) => _.isEqual(item, target))) {
75
+ return [...choiceItems, target];
74
76
  }
75
77
  } else {
76
78
  // targetを配列から削除
77
- return choiceTerms.filter(
78
- (term) => term.taxonomy !== target.taxonomy || term.term !== target.term
79
- );
79
+ return choiceItems.filter((item) => !_.isEqual(item, target));
80
80
  }
81
- return choiceTerms;
81
+ return choiceItems;
82
82
  };
83
83
 
84
+ //階層化されたカスタムフィールドのフィールド名を表示する関数
85
+ const dispCustumFields = (obj, prefix = "", onChange) => {
86
+ return Object.entries(obj).map(([key, value]) => {
87
+ const fieldName = prefix ? `${prefix}.${key}` : key;
88
+ const fieldLabel = key;
89
+ if (typeof value === "object" && value !== null) {
90
+ return (
91
+ <div className="group_area">
92
+ <div className="group_label">{fieldLabel}</div>
93
+ <div key={fieldName} className="field_group">
94
+ {dispCustumFields(value, fieldName, onChange)}
95
+ </div>
96
+ </div>
97
+ );
98
+ } else {
99
+ if (key === "_acf_changed") return; //_acf_changedは対象外
100
+ return (
101
+ <ToggleControl
102
+ key={fieldName}
103
+ className="field_choice"
104
+ label={fieldLabel}
105
+ checked={choiceItems.some(
106
+ (choiceField) => choiceField === fieldName
107
+ )}
108
+ onChange={(checked) => {
109
+ const newChoiceFields = handleChoiceChange(checked, fieldName);
110
+ props.onChange(newChoiceFields);
111
+ }}
112
+ />
113
+ );
114
+ }
115
+ });
116
+ };
84
117
  return (
85
- <div className="tax_section">
86
- <div>{label}</div>
118
+ <div className={`${type}_section`}>
87
119
  {type === "taxonomy" &&
88
120
  choices.map((choice, index) => {
89
121
  return (
@@ -95,14 +127,21 @@ const ChoiceControl = (props) => {
95
127
  className="term_check"
96
128
  key={index}
97
129
  label={term.name}
98
- checked={choiceTerms.some(
99
- (choiceTerm) =>
130
+ checked={choiceItems.some((choiceTerm) => {
131
+ return (
100
132
  choiceTerm.taxonomy === choice.slug &&
101
- choiceTerm.term === term.slug
102
- )}
133
+ choiceTerm.term.id === term.id
134
+ );
135
+ })}
103
136
  onChange={(checked) => {
104
- const target = { taxonomy: choice.slug, term: term.slug };
105
- const newChoiceTerms = handleTermChange(checked, target);
137
+ const target = {
138
+ taxonomy: choice.slug,
139
+ term: { id: term.id, slug: term.slug },
140
+ };
141
+ const newChoiceTerms = handleChoiceChange(
142
+ checked,
143
+ target
144
+ );
106
145
  props.onChange(newChoiceTerms);
107
146
  }}
108
147
  />
@@ -111,6 +150,83 @@ const ChoiceControl = (props) => {
111
150
  </div>
112
151
  );
113
152
  })}
153
+ {type === "field" &&
154
+ choices.map((choice, index) => {
155
+ return (
156
+ <div key={index} className="field_section">
157
+ {choice.title && (
158
+ <ToggleControl
159
+ className="field_choice"
160
+ label="タイトル"
161
+ checked={choiceItems.some(
162
+ (choiceField) => choiceField === "title"
163
+ )}
164
+ onChange={(checked) => {
165
+ const newChoiceFields = handleChoiceChange(
166
+ checked,
167
+ "title"
168
+ );
169
+ props.onChange(newChoiceFields);
170
+ }}
171
+ />
172
+ )}
173
+ {choice.date && (
174
+ <ToggleControl
175
+ className="field_choice"
176
+ label="日付"
177
+ checked={choiceItems.some(
178
+ (choiceField) => choiceField === "date"
179
+ )}
180
+ onChange={(checked) => {
181
+ const newChoiceFields = handleChoiceChange(checked, "date");
182
+ props.onChange(newChoiceFields);
183
+ }}
184
+ />
185
+ )}
186
+ {choice.excerpt && (
187
+ <ToggleControl
188
+ className="field_choice"
189
+ label="抜粋"
190
+ checked={choiceItems.some(
191
+ (choiceField) => choiceField === "excerpt"
192
+ )}
193
+ onChange={(checked) => {
194
+ const newChoiceFields = handleChoiceChange(
195
+ checked,
196
+ "excerpt"
197
+ );
198
+ props.onChange(newChoiceFields);
199
+ }}
200
+ />
201
+ )}
202
+ {choice.featured_media && (
203
+ <ToggleControl
204
+ className="field_choice"
205
+ label="アイキャッチ画像"
206
+ checked={choiceItems.some(
207
+ (choiceField) => choiceField === "featured_media"
208
+ )}
209
+ onChange={(checked) => {
210
+ const newChoiceFields = handleChoiceChange(
211
+ checked,
212
+ "featured_media"
213
+ );
214
+ props.onChange(newChoiceFields);
215
+ }}
216
+ />
217
+ )}
218
+ {(choice.meta || choice.acf.length > 0) && (
219
+ <>
220
+ <div className="custom_field_label">カスタムフィールド</div>
221
+ <div className="custom_field_area">
222
+ {choice.meta && dispCustumFields(choice.meta)}
223
+ {choice.acf && dispCustumFields(choice.acf)}
224
+ </div>
225
+ </>
226
+ )}
227
+ </div>
228
+ );
229
+ })}
114
230
  </div>
115
231
  );
116
232
  };
@@ -151,6 +267,7 @@ export const fetchArchiveOptions = async (home_url) => {
151
267
  acc.push({
152
268
  value: idCounter++,
153
269
  slug: postType.slug,
270
+ rest_base: postType.rest_base,
154
271
  link: `${home_url}/${postType.slug}`,
155
272
  label: postType.name,
156
273
  });
@@ -159,6 +276,7 @@ export const fetchArchiveOptions = async (home_url) => {
159
276
  acc.push({
160
277
  value: idCounter++,
161
278
  slug: postType.slug,
279
+ rest_base: postType.rest_base,
162
280
  link: `${home_url}/${postType.has_archive}`,
163
281
  label: postType.name,
164
282
  });
@@ -169,6 +287,8 @@ export const fetchArchiveOptions = async (home_url) => {
169
287
 
170
288
  //タクソノミー取得RestAPI関数
171
289
  export const restTaxonomies = async (post_type) => {
290
+ if (!post_type) return;
291
+
172
292
  const response = await apiFetch({
173
293
  path: `/wp/v2/types/${post_type}?context=edit`,
174
294
  });
@@ -196,12 +316,24 @@ export const restTaxonomies = async (post_type) => {
196
316
  };
197
317
 
198
318
  //フィールド情報取得RestAPI関数
199
- // export const restFieldes = async (post_type) => {
200
- // const response = await apiFetch({
201
- // path: `/wp/v2/types/${post_type}?context=edit`,
202
- // });
203
- // return response;
204
- // };
319
+ export const restFieldes = async (rest_base) => {
320
+ //投稿データに以下のフィールドが含まれているかを調べる
321
+ const selectedFields = [
322
+ "title",
323
+ "date",
324
+ "excerpt",
325
+ "featured_media",
326
+ "meta",
327
+ "acf",
328
+ ];
329
+ const fieldsParam = selectedFields.join(",");
330
+ //最新の投稿データから1件分のデータを抽出
331
+ const response = await apiFetch({
332
+ path: `/wp/v2/${rest_base}?_fields=${fieldsParam}&per_page=1&order=desc`,
333
+ });
334
+
335
+ return response;
336
+ };
205
337
 
206
338
  export const PageSelectControl = (props) => (
207
339
  <SelectControl {...props} fetchOptions={fetchPagesOptions} />
@@ -215,6 +347,6 @@ export const TermChoiceControl = (props) => (
215
347
  <ChoiceControl {...props} fetchFunction={restTaxonomies} />
216
348
  );
217
349
 
218
- // export const FieldChoiceControl = (props) => (
219
- // <ChoiceControl {...props} fetchFunction={restFieldes} />
220
- // );
350
+ export const FieldChoiceControl = (props) => (
351
+ <ChoiceControl {...props} fetchFunction={restFieldes} />
352
+ );
package/build/index.css DELETED
@@ -1 +0,0 @@
1
- .block-editor-block-inspector .tax_section{margin:10px 0 20px}.block-editor-block-inspector .term_section{margin-top:20px}.block-editor-block-inspector .term_section:nth-child(2){margin-top:10px}.block-editor-block-inspector .tax_label{border-bottom:1px solid #000;border-top:1px solid #000;padding:2px 0}.block-editor-block-inspector .term_check{margin-bottom:5px}
@@ -1 +0,0 @@
1
-
package/src/editor.scss DELETED
@@ -1,25 +0,0 @@
1
- /**
2
- * The following styles get applied inside the editor only.
3
- *
4
- * Replace them with your own styles or remove the file completely.
5
- */
6
- //TermChoiceControlのスタイリング
7
- .block-editor-block-inspector{
8
- .tax_section{
9
- margin: 10px 0 20px;
10
- }
11
- .term_section{
12
- margin-top: 20px;
13
- &:nth-child(2) {
14
- margin-top: 10px;
15
- }
16
- }
17
- .tax_label{
18
- padding: 2px 0;
19
- border-top: 1px solid #000;
20
- border-bottom: 1px solid #000;
21
- }
22
- .term_check{
23
- margin-bottom: 5px;
24
- }
25
- }
package/src/style.scss DELETED
File without changes