add-item.js
2 months ago
delete-item-button.js
2 months ago
index.js
2 months ago
preview.js
2 months ago
preview.js
99 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { __ } from '@wordpress/i18n'; |
| 5 | |
| 6 | /** |
| 7 | * Internal dependencies |
| 8 | */ |
| 9 | import { colorSlugToColorCode } from '@vkblocks/admin/utils/color-slug-to-color-code'; |
| 10 | import hex2rgba from '@vkblocks/utils/hex-to-rgba'; |
| 11 | /*globals vkBlocksObject */ |
| 12 | |
| 13 | const getPreviewClassName = (obj) => |
| 14 | obj.is_active_highlighter |
| 15 | ? obj.class_name + '--vk-highlighter' |
| 16 | : obj.class_name; |
| 17 | |
| 18 | export const TextStylePreview = (props) => { |
| 19 | const { textStyleListObj } = props; |
| 20 | |
| 21 | const generateCustomFormatCss = () => { |
| 22 | let declarations = ''; |
| 23 | if (textStyleListObj.font_weight_bold) { |
| 24 | declarations += `font-weight:bold;`; |
| 25 | } |
| 26 | if (textStyleListObj.font_italic) { |
| 27 | declarations += `font-style:italic;`; |
| 28 | } |
| 29 | if (textStyleListObj.font_strikethrough) { |
| 30 | declarations += `text-decoration:line-through;`; |
| 31 | } |
| 32 | if (textStyleListObj.nowrap) { |
| 33 | declarations += `white-space:nowrap;`; |
| 34 | } |
| 35 | if (!!textStyleListObj.font_size) { |
| 36 | declarations += `font-size: ${textStyleListObj.font_size};`; |
| 37 | } |
| 38 | if (!!textStyleListObj.color) { |
| 39 | declarations += `color: ${colorSlugToColorCode( |
| 40 | textStyleListObj.color |
| 41 | )};`; |
| 42 | } |
| 43 | |
| 44 | const highlighterColor = !!textStyleListObj.highlighter |
| 45 | ? textStyleListObj.highlighter |
| 46 | : vkBlocksObject.highlighterColor; |
| 47 | if (textStyleListObj.is_active_highlighter) { |
| 48 | declarations += `--vk-highlighter-color: ${hex2rgba(highlighterColor, 0.7)};`; |
| 49 | declarations += `background: linear-gradient(transparent 60%, var(--vk-highlighter-color) 0);`; |
| 50 | if (!!textStyleListObj.background_color) { |
| 51 | declarations += `background-color: ${colorSlugToColorCode(textStyleListObj.background_color)};`; |
| 52 | } |
| 53 | } else if (!!textStyleListObj.background_color) { |
| 54 | declarations += `background-image: ${colorSlugToColorCode(textStyleListObj.background_color)};`; |
| 55 | } |
| 56 | |
| 57 | let dynamic_css = ''; |
| 58 | if (declarations) { |
| 59 | dynamic_css += `.${getPreviewClassName(textStyleListObj)} { ${declarations} }`; |
| 60 | } |
| 61 | |
| 62 | if (textStyleListObj.custom_css) { |
| 63 | // selectorをクラスに変換する |
| 64 | dynamic_css += textStyleListObj.custom_css.replace( |
| 65 | /selector/g, |
| 66 | '.' + getPreviewClassName(textStyleListObj) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | return dynamic_css; |
| 71 | }; |
| 72 | |
| 73 | return ( |
| 74 | <div className="custom_format_item_preview"> |
| 75 | <p |
| 76 | className={ |
| 77 | textStyleListObj.title ? 'active-custom-format' : null |
| 78 | } |
| 79 | > |
| 80 | {(() => { |
| 81 | const cssTag = generateCustomFormatCss(textStyleListObj); |
| 82 | if (cssTag) { |
| 83 | return <style>{cssTag}</style>; |
| 84 | } |
| 85 | })()} |
| 86 | <span |
| 87 | className={ |
| 88 | textStyleListObj.is_active_highlighter |
| 89 | ? getPreviewClassName(textStyleListObj) |
| 90 | : getPreviewClassName(textStyleListObj) |
| 91 | } |
| 92 | > |
| 93 | {__('Preview Text', 'vk-blocks')} |
| 94 | </span> |
| 95 | </p> |
| 96 | </div> |
| 97 | ); |
| 98 | }; |
| 99 |