itmar-block-packages 1.2.2 → 1.3.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.
@@ -0,0 +1 @@
1
+
package/css/editor.css ADDED
@@ -0,0 +1,23 @@
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
+ .block-editor-block-inspector .tax_section {
7
+ margin: 10px 0 20px;
8
+ }
9
+ .block-editor-block-inspector .term_section {
10
+ margin-top: 20px;
11
+ }
12
+ .block-editor-block-inspector .term_section:nth-child(2) {
13
+ margin-top: 10px;
14
+ }
15
+ .block-editor-block-inspector .tax_label {
16
+ padding: 2px 0;
17
+ border-top: 1px solid #000;
18
+ border-bottom: 1px solid #000;
19
+ }
20
+ .block-editor-block-inspector .term_check {
21
+ margin-bottom: 5px;
22
+ }
23
+ /*# sourceMappingURL=editor.css.map */
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/editor.scss","editor.css"],"names":[],"mappings":"AAAA;;;;EAAA;AAOC;EACC,mBAAA;ACDF;ADGC;EACC,gBAAA;ACDF;ADEE;EACC,gBAAA;ACAH;ADGC;EACC,cAAA;EACA,0BAAA;EACA,6BAAA;ACDF;ADGC;EACC,kBAAA;ACDF","file":"editor.css"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "itmar-block-packages",
3
- "version": "1.2.2",
3
+ "version": "1.3.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,25 @@
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/index.js CHANGED
@@ -1,3 +1,6 @@
1
+ import "./editor.scss";
2
+ import "./style.scss";
3
+
1
4
  //カスタムフック(一般)
2
5
  export {
3
6
  useIsIframeMobile,
@@ -12,8 +15,12 @@ export {
12
15
  export {
13
16
  fetchPagesOptions,
14
17
  fetchArchiveOptions,
18
+ restTaxonomies,
19
+ //restFieldes,
15
20
  PageSelectControl,
16
21
  ArchiveSelectControl,
22
+ TermChoiceControl,
23
+ //FieldChoiceControl,
17
24
  } from "./wordpressApi";
18
25
 
19
26
  //styled-componet用のcssプロパティ生成関数
package/src/style.scss ADDED
File without changes
@@ -1,10 +1,19 @@
1
- import { useState, useEffect } from '@wordpress/element';
2
- import { ComboboxControl } from '@wordpress/components';
3
- import apiFetch from '@wordpress/api-fetch';
1
+ import { useState, useEffect } from "@wordpress/element";
2
+ import { ComboboxControl, CheckboxControl } from "@wordpress/components";
3
+ import apiFetch from "@wordpress/api-fetch";
4
4
 
5
- //コントロールのレンダリング関数
6
- const SelectControl = ({ setAttributes, attributes, label, homeUrl, fetchOptions }) => {
7
- const { selectedPageId } = attributes;
5
+ export const fetchData = async (rest_api) => {
6
+ try {
7
+ const ret_data = await rest_api();
8
+ return ret_data;
9
+ } catch (error) {
10
+ console.error("Error fetching data:", error.message);
11
+ }
12
+ };
13
+
14
+ //コンボボックスコントロールのレンダリング関数
15
+ const SelectControl = (props) => {
16
+ const { selectedSlug, label, homeUrl, fetchOptions } = props;
8
17
  const [options, setOptions] = useState([]);
9
18
 
10
19
  useEffect(() => {
@@ -13,57 +22,187 @@ const SelectControl = ({ setAttributes, attributes, label, homeUrl, fetchOptions
13
22
  const fetchedOptions = await fetchOptions(homeUrl);
14
23
  setOptions(fetchedOptions);
15
24
  } catch (error) {
16
- console.error('Error fetching data:', error.message);
25
+ console.error("Error fetching data:", error.message);
17
26
  }
18
27
  };
19
28
 
20
29
  fetchData();
21
30
  }, [fetchOptions]);
22
31
 
23
- const handleChange = (selectedId) => {
24
- const selectedPage = options.find(page => page.value === selectedId);
25
- setAttributes({
26
- selectedPageId: selectedId,
27
- selectedPageUrl: selectedPage ? selectedPage.link : homeUrl
28
- });
29
- };
32
+ const selectedInfo = options.find((info) => info.slug === selectedSlug);
30
33
 
31
34
  return (
32
35
  <ComboboxControl
33
36
  label={label}
34
37
  options={options}
35
- value={selectedPageId}
36
- onChange={handleChange}
38
+ value={selectedInfo ? selectedInfo.value : -1}
39
+ onChange={(newValue) => {
40
+ const newInfo = options.find((info) => info.value === newValue);
41
+ props.onChange(newInfo);
42
+ }}
37
43
  />
38
44
  );
39
45
  };
40
46
 
47
+ //タームの選択コントロールのレンダリング関数
48
+ const ChoiceControl = (props) => {
49
+ const { selectedSlug, choiceTerms, type, label, fetchFunction } = props;
50
+ const [choices, setChoices] = useState([]);
51
+
52
+ useEffect(() => {
53
+ const fetchData = async () => {
54
+ try {
55
+ const fetchChoices = await fetchFunction(selectedSlug);
56
+ setChoices(fetchChoices);
57
+ } catch (error) {
58
+ console.error("Error fetching data:", error.message);
59
+ }
60
+ };
61
+ fetchData();
62
+ }, [selectedSlug, fetchFunction]);
63
+
64
+ const handleTermChange = (checked, target) => {
65
+ if (checked) {
66
+ // targetが重複していない場合のみ追加
67
+ if (
68
+ !choiceTerms.some(
69
+ (term) =>
70
+ term.taxonomy === target.taxonomy && term.term === target.term
71
+ )
72
+ ) {
73
+ return [...choiceTerms, target];
74
+ }
75
+ } else {
76
+ // targetを配列から削除
77
+ return choiceTerms.filter(
78
+ (term) => term.taxonomy !== target.taxonomy || term.term !== target.term
79
+ );
80
+ }
81
+ return choiceTerms;
82
+ };
83
+
84
+ return (
85
+ <div className="tax_section">
86
+ <div>{label}</div>
87
+ {type === "taxonomy" &&
88
+ choices.map((choice, index) => {
89
+ return (
90
+ <div key={index} className="term_section">
91
+ <div className="tax_label">{choice.name}</div>
92
+ {choice.terms.map((term, index) => {
93
+ return (
94
+ <CheckboxControl
95
+ className="term_check"
96
+ key={index}
97
+ label={term.name}
98
+ checked={choiceTerms.some(
99
+ (choiceTerm) =>
100
+ choiceTerm.taxonomy === choice.slug &&
101
+ choiceTerm.term === term.slug
102
+ )}
103
+ onChange={(checked) => {
104
+ const target = { taxonomy: choice.slug, term: term.slug };
105
+ const newChoiceTerms = handleTermChange(checked, target);
106
+ props.onChange(newChoiceTerms);
107
+ }}
108
+ />
109
+ );
110
+ })}
111
+ </div>
112
+ );
113
+ })}
114
+ </div>
115
+ );
116
+ };
117
+
118
+ //固定ページ取得RestAPI関数
41
119
  export const fetchPagesOptions = async (home_url) => {
42
- const pages = await apiFetch({ path: '/wp/v2/pages' });
43
- if (pages && !pages.some(page => page.id === -1)) {
44
- pages.unshift({ id: -1, title: { rendered: 'ホーム' }, link: home_url });
120
+ const pages = await apiFetch({ path: "/wp/v2/pages" });
121
+ //ページIDが-1である要素をホーム要素として作成
122
+ if (pages && !pages.some((page) => page.id === -1)) {
123
+ pages.unshift({
124
+ id: -1,
125
+ title: { rendered: "ホーム" },
126
+ link: home_url,
127
+ slug: "home",
128
+ });
45
129
  }
46
- return pages ? pages.map(page => ({
47
- value: page.id,
48
- label: page.title.rendered,
49
- link: page.link
50
- })) : [];
130
+
131
+ const ret_pages = pages
132
+ ? pages.map((page) => ({
133
+ value: page.id,
134
+ slug: page.slug,
135
+ label: page.title.rendered,
136
+ link: page.link,
137
+ }))
138
+ : [];
139
+
140
+ return ret_pages;
51
141
  };
52
142
 
143
+ //アーカイブ情報取得RestAPI関数
53
144
  export const fetchArchiveOptions = async (home_url) => {
54
- const response = await apiFetch({ path: '/wp/v2/types' });
145
+ const response = await apiFetch({ path: "/wp/v2/types" });
146
+
55
147
  let idCounter = 0;
56
148
  return Object.keys(response).reduce((acc, key) => {
57
149
  const postType = response[key];
58
150
  if (postType.has_archive === true) {
59
- acc.push({ value: idCounter++, link: `${home_url}/${postType.slug}`, label: postType.name });
60
- } else if (typeof postType.has_archive === 'string') {
61
- acc.push({ value: idCounter++, link: `${home_url}/${postType.has_archive}`, label: postType.name });
151
+ acc.push({
152
+ value: idCounter++,
153
+ slug: postType.slug,
154
+ link: `${home_url}/${postType.slug}`,
155
+ label: postType.name,
156
+ });
157
+ } else if (typeof postType.has_archive === "string") {
158
+ //アーカイブ名がついているとき
159
+ acc.push({
160
+ value: idCounter++,
161
+ slug: postType.slug,
162
+ link: `${home_url}/${postType.has_archive}`,
163
+ label: postType.name,
164
+ });
62
165
  }
63
166
  return acc;
64
167
  }, []);
65
168
  };
66
169
 
170
+ //タクソノミー取得RestAPI関数
171
+ export const restTaxonomies = async (post_type) => {
172
+ const response = await apiFetch({
173
+ path: `/wp/v2/types/${post_type}?context=edit`,
174
+ });
175
+
176
+ const taxonomies = response.taxonomies;
177
+
178
+ const taxonomyPromises = taxonomies.map(async (slug) => {
179
+ const taxonomyResponse = await apiFetch({
180
+ path: `/wp/v2/taxonomies/${slug}?context=edit`,
181
+ });
182
+ const terms = await apiFetch({
183
+ path: `/wp/v2/${taxonomyResponse.rest_base}`,
184
+ });
185
+
186
+ return {
187
+ slug: slug,
188
+ name: taxonomyResponse.name,
189
+ rest_base: taxonomyResponse.rest_base,
190
+ terms: terms,
191
+ };
192
+ });
193
+
194
+ const taxonomyObjects = await Promise.all(taxonomyPromises);
195
+ return taxonomyObjects;
196
+ };
197
+
198
+ //フィールド情報取得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
+ // };
205
+
67
206
  export const PageSelectControl = (props) => (
68
207
  <SelectControl {...props} fetchOptions={fetchPagesOptions} />
69
208
  );
@@ -71,3 +210,11 @@ export const PageSelectControl = (props) => (
71
210
  export const ArchiveSelectControl = (props) => (
72
211
  <SelectControl {...props} fetchOptions={fetchArchiveOptions} />
73
212
  );
213
+
214
+ export const TermChoiceControl = (props) => (
215
+ <ChoiceControl {...props} fetchFunction={restTaxonomies} />
216
+ );
217
+
218
+ // export const FieldChoiceControl = (props) => (
219
+ // <ChoiceControl {...props} fetchFunction={restFieldes} />
220
+ // );
package/babel.config.js DELETED
@@ -1,10 +0,0 @@
1
- module.exports = {
2
- presets: [
3
- [
4
- require.resolve("@wordpress/babel-preset-default"),
5
- {
6
- modules: "commonjs",
7
- },
8
- ],
9
- ],
10
- };