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 |