itmar-block-packages 1.7.1 → 1.8.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 +104 -0
- package/build/index.asset.php +1 -1
- package/build/index.js +3 -3
- package/package.json +1 -1
- package/src/formatCreate.js +179 -0
- package/src/index.js +3 -0
package/package.json
CHANGED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { __ } from "@wordpress/i18n";
|
|
2
|
+
import {
|
|
3
|
+
PanelBody,
|
|
4
|
+
PanelRow,
|
|
5
|
+
RangeControl,
|
|
6
|
+
TextControl,
|
|
7
|
+
SelectControl,
|
|
8
|
+
} from "@wordpress/components";
|
|
9
|
+
import { format, getSettings } from "@wordpress/date";
|
|
10
|
+
|
|
11
|
+
//日付のフォーマット
|
|
12
|
+
const dateFormats = [
|
|
13
|
+
{ label: "YYYY-MM-DD HH:mm:ss", value: "Y-m-d H:i:s" },
|
|
14
|
+
{ label: "MM/DD/YYYY", value: "m/d/Y" },
|
|
15
|
+
{ label: "DD/MM/YYYY", value: "d/m/Y" },
|
|
16
|
+
{ label: "MMMM D, YYYY", value: "F j, Y" },
|
|
17
|
+
{ label: "HH:mm:ss", value: "H:i:s" },
|
|
18
|
+
{ label: "YYYY.M.D", value: "Y.n.j" },
|
|
19
|
+
{ label: "Day, MMMM D, YYYY", value: "l, F j, Y" },
|
|
20
|
+
{ label: "ddd, MMM D, YYYY", value: "D, M j, Y" },
|
|
21
|
+
{ label: "YYYY年M月D日 (曜日)", value: "Y年n月j日 (l)" },
|
|
22
|
+
];
|
|
23
|
+
//プレーンのフォーマット
|
|
24
|
+
const plaineFormats = [
|
|
25
|
+
{
|
|
26
|
+
key: "str_free",
|
|
27
|
+
label: __("Free String", "block-collections"),
|
|
28
|
+
value: "%s",
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
key: "num_comma",
|
|
32
|
+
label: __("Numbers (comma separated)", "block-collections"),
|
|
33
|
+
value: {
|
|
34
|
+
style: "decimal",
|
|
35
|
+
useGrouping: true, // カンマ区切り
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
key: "num_no_comma",
|
|
40
|
+
label: __("Numbers (no commas)", "block-collections"),
|
|
41
|
+
value: {
|
|
42
|
+
style: "decimal",
|
|
43
|
+
useGrouping: false,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
key: "num_amount",
|
|
48
|
+
label: __("Amount", "block-collections"),
|
|
49
|
+
value: {
|
|
50
|
+
style: "currency",
|
|
51
|
+
currency: "JPY",
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
export const FormatSelectControl = ({
|
|
57
|
+
titleType,
|
|
58
|
+
userFormat,
|
|
59
|
+
freeStrFormat,
|
|
60
|
+
decimal,
|
|
61
|
+
onFormatChange,
|
|
62
|
+
}) => {
|
|
63
|
+
const isPlaine = titleType === "plaine";
|
|
64
|
+
const isDate = titleType === "date";
|
|
65
|
+
const isUser = titleType === "user";
|
|
66
|
+
|
|
67
|
+
//SelectControlのオプションを生成
|
|
68
|
+
const options = isDate
|
|
69
|
+
? dateFormats
|
|
70
|
+
: plaineFormats.map((f) => ({ label: f.label, value: f.key }));
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
<PanelBody title={__("Display Format Setting", "block-collections")}>
|
|
74
|
+
{(isPlaine || isDate) && (
|
|
75
|
+
<>
|
|
76
|
+
<SelectControl
|
|
77
|
+
label={__("Select Format", "block-collections")}
|
|
78
|
+
value={userFormat}
|
|
79
|
+
options={options}
|
|
80
|
+
onChange={(newFormat) =>
|
|
81
|
+
onFormatChange({
|
|
82
|
+
userFormat: newFormat,
|
|
83
|
+
freeStrFormat,
|
|
84
|
+
decimal,
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
/>
|
|
88
|
+
|
|
89
|
+
{userFormat?.startsWith("str_") && (
|
|
90
|
+
<TextControl
|
|
91
|
+
label={__("String Format", "block-collections")}
|
|
92
|
+
value={freeStrFormat}
|
|
93
|
+
onChange={(newFormat) =>
|
|
94
|
+
onFormatChange({
|
|
95
|
+
userFormat,
|
|
96
|
+
freeStrFormat: newFormat,
|
|
97
|
+
decimal,
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
/>
|
|
101
|
+
)}
|
|
102
|
+
{userFormat?.startsWith("num_") && (
|
|
103
|
+
<PanelRow className="itmar_post_blocks_pannel">
|
|
104
|
+
<RangeControl
|
|
105
|
+
value={decimal}
|
|
106
|
+
label={__("Decimal Num", "query-blocks")}
|
|
107
|
+
max={5}
|
|
108
|
+
min={0}
|
|
109
|
+
onChange={(val) =>
|
|
110
|
+
onFormatChange({
|
|
111
|
+
userFormat,
|
|
112
|
+
freeStrFormat,
|
|
113
|
+
decimal: val,
|
|
114
|
+
})
|
|
115
|
+
}
|
|
116
|
+
/>
|
|
117
|
+
</PanelRow>
|
|
118
|
+
)}
|
|
119
|
+
</>
|
|
120
|
+
)}
|
|
121
|
+
|
|
122
|
+
{isUser && (
|
|
123
|
+
<TextControl
|
|
124
|
+
label={__("User Format", "block-collections")}
|
|
125
|
+
value={freeStrFormat}
|
|
126
|
+
onChange={(newFormat) =>
|
|
127
|
+
onFormatChange({
|
|
128
|
+
userFormat: "str_free",
|
|
129
|
+
freeStrFormat: newFormat,
|
|
130
|
+
decimal,
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
/>
|
|
134
|
+
)}
|
|
135
|
+
</PanelBody>
|
|
136
|
+
);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export const displayFormated = (
|
|
140
|
+
content,
|
|
141
|
+
userFormat,
|
|
142
|
+
freeStrFormat,
|
|
143
|
+
decimal
|
|
144
|
+
) => {
|
|
145
|
+
// 内部で使用するロケール
|
|
146
|
+
const locale = getSettings().l10n?.locale || "en";
|
|
147
|
+
|
|
148
|
+
//日付にフォーマットがあれば、それで書式設定してリターン
|
|
149
|
+
const isDateFormat = dateFormats.find((f) => f.value === userFormat);
|
|
150
|
+
if (isDateFormat) {
|
|
151
|
+
const ret_val = format(userFormat, content, getSettings());
|
|
152
|
+
return ret_val;
|
|
153
|
+
}
|
|
154
|
+
//数値や文字列のフォーマット
|
|
155
|
+
const selectedFormat = plaineFormats.find((f) => f.key === userFormat)?.value;
|
|
156
|
+
if (typeof selectedFormat === "object") {
|
|
157
|
+
// Intl.NumberFormat オプション
|
|
158
|
+
try {
|
|
159
|
+
const numeric = parseFloat(content);
|
|
160
|
+
// `selectedFormat` を元に新しいフォーマット設定を生成(mutateしない)
|
|
161
|
+
const options = { ...selectedFormat };
|
|
162
|
+
|
|
163
|
+
if (typeof decimal === "number" && decimal > 0) {
|
|
164
|
+
options.minimumFractionDigits = decimal;
|
|
165
|
+
options.maximumFractionDigits = decimal;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const formatter = new Intl.NumberFormat(locale, options);
|
|
169
|
+
return formatter.format(numeric);
|
|
170
|
+
} catch (e) {
|
|
171
|
+
console.warn("Number format failed:", e);
|
|
172
|
+
return content;
|
|
173
|
+
}
|
|
174
|
+
} else if (typeof selectedFormat === "string") {
|
|
175
|
+
return freeStrFormat.replace("%s", content);
|
|
176
|
+
}
|
|
177
|
+
//フォーマットが見つからないときはそのまま返す
|
|
178
|
+
return content;
|
|
179
|
+
};
|
package/src/index.js
CHANGED