PluginProbe ʕ •ᴥ•ʔ
VK Blocks / 1.123.0
VK Blocks v1.123.0
1.123.0 1.122.0 1.121.0 1.120.0 1.119.2 1.119.1 1.119.0 1.118.7 1.82.0.0 1.82.0.1 1.83.0.0 1.83.0.1 1.84.0.0 1.84.0.1 1.85.0.2 1.85.0.3 1.85.1.0 1.85.1.1 1.86.0.0 1.86.0.1 1.86.1.0 1.87.0.0 1.87.0.1 1.88.0.1 1.88.0.2 1.89.0.0 1.9.2 1.90.0.0 1.90.0.1 1.90.1.1 1.91.0.0 1.91.1.1 1.92.0.0 1.92.0.1 1.92.1.0 1.92.1.1 1.93.0.0 trunk 1.93.0.1 0.1.1 1.93.1.0 0.10.3 1.93.1.1 0.11.0 1.94.0.0 0.12.6 1.94.0.1 0.15.1 1.94.1.0 0.16.2 1.94.2.1 0.17.6 1.94.2.2 0.2.2 1.95.0.3 0.22.4 1.95.0.4 0.26.7 1.96.2.0 0.3.2 1.96.2.1 0.31.0 1.97.0.1 0.35.5 1.97.0.2 0.37.5 1.99.0.0 0.38.8 1.99.0.1 0.39.4 0.4.7 0.41.0 0.42.0 0.44.13 0.45.2 0.46.1 0.47.0 0.48.0 0.49.8 0.5.2 0.50.3 0.51.0 0.52.2 0.53.2 0.54.0 0.55.0 0.56.2 0.56.3 0.57.6 0.58.10 0.59.0 0.6.0 0.60.1 0.61.2 0.7.1 0.8.2 1.0.16 1.10.0 1.100.0.0 1.100.0.1 1.102.0.0 1.102.0.1 1.103.0.0 1.104.0.0 1.104.0.1 1.105.1.0 1.105.1.1 1.106.0.0 1.106.0.1 1.107.0.0 1.107.0.1 1.107.0.2 1.108.0.0 1.108.0.1 1.109.0.0 1.109.0.1 1.11.4 1.110.0.0 1.110.0.1 1.111.0.0 1.111.0.1 1.111.0.2 1.112.0.0 1.112.0.1 1.113.0.0 1.113.0.1 1.114.0.0 1.114.0.1 1.114.2.0 1.114.2.1 1.115.0.0 1.115.0.1 1.115.1.0 1.115.1.1 1.115.2.0 1.115.2.1 1.116.0.0 1.116.0.1 1.116.1.0 1.116.1.1 1.116.2.0 1.117.0.0 1.117.0.1 1.117.1.0 1.118.2 1.118.5 1.12.0 1.13.2 1.14.1 1.15.1 1.16.11 1.17.0 1.18.6 1.19.0 1.19.1 1.2.3 1.20.7 1.21.0 1.22.4 1.23.0 1.24.5 1.25.1 1.26.2 1.27.7.2 1.28.0.1 1.29.2.0 1.3.9 1.30.0.1 1.31.0.1 1.32.0.2 1.33.2.1 1.36.1.5 1.37.0.0 1.39.2.1 1.4.6 1.40.0.1 1.40.1.0 1.40.1.1 1.41.0.0 1.41.0.1 1.41.2.2 1.41.2.3 1.43.0.0 1.43.0.1 1.43.0.2 1.44.0.0 1.44.0.1 1.45.0.0 1.45.0.1 1.46.0.0 1.46.0.1 1.47.0.0 1.47.0.1 1.47.1.0 1.48.0.0 1.48.0.1 1.48.0.2 1.48.1.0 1.48.1.1 1.5.0 1.50.0.0 1.50.0.1 1.51.0.0 1.51.0.1 1.52.0.0 1.52.0.1 1.53.0.0 1.53.0.1 1.54.0.0 1.54.0.1 1.55.0.0 1.55.0.1 1.56.0.0 1.56.0.1 1.57.0.0 1.57.0.2 1.57.0.3 1.57.0.4 1.57.0.5 1.57.1.0 1.57.1.1 1.57.1.2 1.58.0.0 1.58.0.1 1.59.0.0 1.59.0.1 1.6.0 1.60.0.0 1.60.0.1 1.63.0.0 1.63.0.1 1.64.0.0 1.64.0.1 1.64.1.0 1.64.1.2 1.67.0.0 1.67.0.1 1.68.0.0 1.68.0.1 1.69.0.0 1.69.0.1 1.69.1.1 1.69.1.2 1.7.1 1.70.0.0 1.70.0.1 1.71.0.0 1.71.0.1 1.72.0.0 1.72.1.0 1.72.1.1 1.73.0.0 1.73.0.1 1.74.0.0 1.74.0.1 1.75.0.0 1.75.1.0 1.75.1.1 1.76.0.0 1.76.0.1 1.76.1.0 1.76.1.1 1.76.2.0 1.76.2.1 1.77.0.0 1.77.0.1 1.78.0.0 1.78.0.1 1.79.0.0 1.79.0.1 1.79.0.2 1.79.0.3 1.79.1.0 1.79.1.1 1.8.2 1.80.1.0 1.80.1.1 1.80.1.2 1.81.0.1 1.81.0.2
vk-blocks / src / extensions / core / list / style.js
vk-blocks / src / extensions / core / list Last commit date
style.js 2 months ago
style.js
342 lines
1 /**
2 * WordPress dependencies
3 */
4 import { __ } from '@wordpress/i18n';
5 import { addFilter } from '@wordpress/hooks';
6 import { PanelBody } from '@wordpress/components';
7 import {
8 InspectorControls,
9 ColorPalette,
10 getColorObjectByColorValue,
11 getColorObjectByAttributeValues,
12 } from '@wordpress/block-editor';
13 import { select, dispatch } from '@wordpress/data';
14 import { createHigherOrderComponent } from '@wordpress/compose';
15 import { useEffect, useRef } from '@wordpress/element';
16
17 /**
18 * Internal dependencies
19 */
20 import { colorSlugToColorCode } from '@vkblocks/utils/color-slug-to-color-code';
21 import { sanitizeSlug } from '@vkblocks/utils/sanitizeSlug';
22
23 /**
24 * External dependencies
25 */
26 import { compareVersions } from 'compare-versions';
27
28 const isValidBlockType = (name) => {
29 const validBlockTypes = ['core/list'];
30 return validBlockTypes.includes(name);
31 };
32
33 // WP6.2以上か NOTE: WP6.1以下をサポートしなくなったら削除すること
34 const isLagerThanWp62 = () => {
35 if (
36 window.wpVersion !== undefined &&
37 window.wpVersion !== null &&
38 compareVersions(window.wpVersion, '6.2') < 0
39 ) {
40 return false;
41 }
42 return true;
43 };
44
45 // サポートしているクラス名かどうか
46 const isVKColorPaletteManager = (colorSet) => {
47 // 設定されているカラーパレットのスラッグ�
48 �列
49 const colorSetSlugArray = [];
50 colorSet.forEach((item) => {
51 colorSetSlugArray.push(item.slug);
52 });
53
54 const supportColorSlugArray = [
55 'black',
56 'cyan-bluish-gray',
57 'white',
58 'pale-pink',
59 'vivid-red',
60 'luminous-vivid-orange',
61 'luminous-vivid-amber',
62 'light-green-cyan',
63 'pale-cyan-blue',
64 'vivid-cyan-blue',
65 'vivid-purple',
66 ];
67 if (
68 supportColorSlugArray.every(
69 (element) => colorSetSlugArray.indexOf(element) !== -1
70 )
71 ) {
72 return true;
73 }
74 return false;
75 };
76
77 const convertPresetToCssVar = (value = '') => {
78 const matches = value.match(/^var:preset\|([^|]+)\|([^|]+)$/);
79 if (matches) {
80 return `var(--wp--preset--${matches[1]}--${matches[2]})`;
81 }
82 return value;
83 };
84
85 const getNumberedListLineHeightLength = (attributes = {}) => {
86 const rawLineHeight = attributes?.style?.typography?.lineHeight;
87 if (rawLineHeight === undefined || rawLineHeight === null) {
88 return '';
89 }
90
91 const lineHeight = rawLineHeight.toString().trim();
92 if (!lineHeight || lineHeight.toLowerCase() === 'normal') {
93 return '';
94 }
95
96 let value = lineHeight;
97 if (value.startsWith('var:preset|')) {
98 value = convertPresetToCssVar(value);
99 }
100
101 if (value.toLowerCase().startsWith('calc(')) {
102 return value;
103 }
104
105 if (value.startsWith('var(')) {
106 return `calc( ${value} * 1em )`;
107 }
108
109 if (value.endsWith('%')) {
110 const numeric = parseFloat(value.slice(0, -1));
111 if (!Number.isNaN(numeric)) {
112 return `calc( ${(numeric / 100).toString()} * 1em )`;
113 }
114 }
115
116 const numeric = Number(value);
117 if (!Number.isNaN(numeric)) {
118 return `calc( ${value} * 1em )`;
119 }
120
121 return value;
122 };
123
124 export const addAttribute = (settings) => {
125 if (isValidBlockType(settings.name)) {
126 settings.attributes = {
127 ...settings.attributes,
128 color: {
129 type: 'string',
130 },
131 };
132 }
133 return settings;
134 };
135 addFilter('blocks.registerBlockType', 'vk-blocks/list-style', addAttribute);
136
137 export const addBlockControl = createHigherOrderComponent(
138 (BlockEdit) => (props) => {
139 const { name, attributes, setAttributes } = props;
140 const { color, className, ordered } = attributes;
141 if (!isValidBlockType(name)) {
142 return <BlockEdit {...props} />;
143 }
144
145 const colorSet = select('core/block-editor').getSettings().colors;
146
147 // 6.2未満かつサポートしているクラス名以外のカラーパレットの時
148 if (!isLagerThanWp62() && !isVKColorPaletteManager(colorSet)) {
149 return <BlockEdit {...props} />;
150 }
151
152 // 順序付きリストでスタイルが「デフォルト」以外の場合の注意書きを表示するかどうか
153 const nowClassArray = className ? className.split(' ') : [];
154 const hasNonDefaultStyle = nowClassArray.find(
155 (item) => item.match(/is-style-/) && !item.match(/is-style-default/)
156 );
157
158 const showNotice = ordered && hasNonDefaultStyle;
159 const noticeShownRef = useRef(false);
160
161 // 通知での表示
162 useEffect(() => {
163 if (showNotice && !noticeShownRef.current) {
164 // 通知がまだ表示されていない場合のみ作成
165 dispatch('core/notices').createNotice(
166 'warning',
167 __(
168 'When this style is selected for ordered lists, the "List Style", "Initial Value", and "Reverse Order" features cannot be used.',
169 'vk-blocks'
170 ),
171 {
172 id: 'vk-blocks-list-notice',
173 isDismissible: true,
174 autoDismiss: false,
175 }
176 );
177 noticeShownRef.current = true;
178 } else if (!showNotice && noticeShownRef.current) {
179 // 条件が変わった時のみ通知を削除
180 dispatch('core/notices').removeNotice('vk-blocks-list-notice');
181 noticeShownRef.current = false;
182 }
183 }, [showNotice]);
184
185 return (
186 <>
187 <BlockEdit {...props} />
188 <InspectorControls>
189 <PanelBody
190 title={__('List Icon Color', 'vk-blocks')}
191 initialOpen={false}
192 >
193 <ColorPalette
194 value={colorSlugToColorCode(color)}
195 disableCustomColors={
196 isLagerThanWp62() ? false : true
197 }
198 onChange={(newColor) => {
199 // 色コードを colorSet から探して色データを取得
200 const ColorValue = getColorObjectByColorValue(
201 colorSet,
202 newColor
203 );
204
205 // 現在のクラス名を�
206 �列化
207 const nowClassArray =
208 className && className.split(' ');
209
210 // 新しいクラス名の�
211 �列
212 let newClassNameArray = nowClassArray
213 ? nowClassArray
214 : [];
215
216 // 互換処理:設定されていたクラス名vk-has-〇〇-colorを削除する
217 if (nowClassArray) {
218 newClassNameArray = nowClassArray.filter(
219 (item) => {
220 return !item.match(
221 /vk-has-(.*)-color/
222 );
223 }
224 );
225 }
226
227 // 6.2未満の場合
228 if (!isLagerThanWp62()) {
229 // newColorがあれば新しいクラス名を追加する
230 if (newColor !== undefined) {
231 // コアのテキストカラーと被らないようにvk-has-〇〇-colorを追加する
232 newClassNameArray.push(
233 `vk-has-${ColorValue.slug}-color`
234 );
235 }
236 }
237
238 const newClassName =
239 newClassNameArray.join(' ');
240
241 setAttributes({
242 className: newClassName,
243 color: ColorValue?.slug
244 ? ColorValue?.slug
245 : newColor,
246 });
247 }}
248 />
249 </PanelBody>
250 </InspectorControls>
251 </>
252 );
253 },
254 'addMyCustomBlockControls'
255 );
256 addFilter('editor.BlockEdit', 'vk-blocks/list-style', addBlockControl);
257
258 /**
259 * Override the default block element to include elements styles.
260 */
261 const withElementsStyles = createHigherOrderComponent(
262 (BlockListBlock) => (props) => {
263 const { name, attributes, clientId } = props;
264 const { color, className } = attributes;
265 if (!isValidBlockType(name)) {
266 return <BlockListBlock {...props} />;
267 }
268
269 const nowClassArray = className ? className.split(' ') : [];
270
271 // 以前の形式 vk-has-(.*)-colorで保存されている場合
272 const hasDeprecatedClassName = nowClassArray.find((item) =>
273 item.match(/vk-has-(.*)-color/)
274 );
275 if (hasDeprecatedClassName) {
276 return <BlockListBlock {...props} />;
277 }
278
279 // 6.2未満の場合
280 if (!isLagerThanWp62()) {
281 return <BlockListBlock {...props} />;
282 }
283
284 const colorSet = select('core/block-editor').getSettings().colors;
285 let colorValue;
286 if (color) {
287 const ColorValue =
288 getColorObjectByAttributeValues(colorSet, color) || {};
289 if (ColorValue.slug !== undefined) {
290 const safeSlug = sanitizeSlug(ColorValue.slug);
291 colorValue = `var(--wp--preset--color--${safeSlug})`;
292 } else if (ColorValue.color) {
293 colorValue = ColorValue.color;
294 } else {
295 colorValue = color;
296 }
297 }
298
299 const bgStyle = nowClassArray.find((item) =>
300 item.match(/is-style-vk-numbered-(circle|square)-mark/)
301 );
302 const lineHeightLength = getNumberedListLineHeightLength(attributes);
303 const cssRules = [];
304 const blockSelector = `#block-${clientId}`;
305
306 if (bgStyle && lineHeightLength) {
307 cssRules.push(
308 `${blockSelector} { --vk-numbered-line-height-length: ${lineHeightLength}; }`
309 );
310 }
311
312 if (colorValue) {
313 if (bgStyle) {
314 cssRules.push(`${blockSelector} li::before {
315 color: #fff;
316 background-color: ${colorValue};
317 }`);
318 } else {
319 cssRules.push(`${blockSelector} li::marker, ${blockSelector} li::before {
320 color: ${colorValue};
321 }`);
322 }
323 }
324
325 if (!cssRules.length) {
326 return <BlockListBlock {...props} />;
327 }
328
329 return (
330 <>
331 <style>{cssRules.join('\n')}</style>
332 <BlockListBlock {...props} />
333 </>
334 );
335 }
336 );
337 addFilter(
338 'editor.BlockListBlock',
339 'vk-blocks/list-style/with-block-controls',
340 withElementsStyles
341 );
342