index.js
182 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { __ } from '@wordpress/i18n'; |
| 5 | import { transformStyles } from '@wordpress/block-editor'; |
| 6 | import { useState, useRef, useEffect } from '@wordpress/element'; |
| 7 | import { Tooltip, Icon } from '@wordpress/components'; |
| 8 | import { info } from '@wordpress/icons'; |
| 9 | |
| 10 | /** |
| 11 | * External dependencies |
| 12 | */ |
| 13 | import classnames from 'classnames'; |
| 14 | import CodeMirror from '@uiw/react-codemirror'; |
| 15 | import { css } from '@codemirror/lang-css'; |
| 16 | import { EditorView } from '@codemirror/view'; |
| 17 | |
| 18 | /** |
| 19 | * Internal dependencies |
| 20 | */ |
| 21 | import { stripHTML } from '@vkblocks/utils/strip-html'; |
| 22 | |
| 23 | export const CodeMirrorCss = (props) => { |
| 24 | const { |
| 25 | id = 'vk-custom-css-code-mirror', |
| 26 | className, |
| 27 | value, |
| 28 | onChange, |
| 29 | style = { |
| 30 | ...style, |
| 31 | marginTop: '0.5em', |
| 32 | border: '1px solid #ccc', |
| 33 | height: '200px', |
| 34 | resize: 'vertical', |
| 35 | overflow: 'hidden', |
| 36 | }, |
| 37 | onBlur = (event) => { |
| 38 | if (!event?.target?.textContent) { |
| 39 | setCSSError(null); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | const [transformed] = transformStyles( |
| 44 | [{ css: event.target.textContent }], |
| 45 | '.editor-styles-wrapper' |
| 46 | ); |
| 47 | |
| 48 | setCSSError( |
| 49 | transformed === null |
| 50 | ? __( |
| 51 | 'There is an error with your CSS structure.', |
| 52 | 'vk-blocks' |
| 53 | ) |
| 54 | : null |
| 55 | ); |
| 56 | }, |
| 57 | } = props; |
| 58 | const [cssError, setCSSError] = useState(null); |
| 59 | const wrapperRef = useRef(null); |
| 60 | const [isInitialLoad, setIsInitialLoad] = useState(true); |
| 61 | |
| 62 | // リサイズ検知して高さを動的に調整 |
| 63 | useEffect(() => { |
| 64 | if (wrapperRef.current) { |
| 65 | const observer = new window.ResizeObserver(() => { |
| 66 | if (isInitialLoad) { |
| 67 | wrapperRef.current.style.setProperty( |
| 68 | 'height', |
| 69 | 'auto', |
| 70 | 'important' |
| 71 | ); |
| 72 | setIsInitialLoad(false); |
| 73 | } |
| 74 | }); |
| 75 | observer.observe(wrapperRef.current); |
| 76 | return () => observer.disconnect(); |
| 77 | } |
| 78 | }, [isInitialLoad]); |
| 79 | |
| 80 | // gutterの高さ検知して動的に調整 |
| 81 | useEffect(() => { |
| 82 | if (wrapperRef.current) { |
| 83 | const gutters = wrapperRef.current.querySelector('.cm-gutters'); |
| 84 | if (gutters) { |
| 85 | const observer = new window.ResizeObserver(() => { |
| 86 | const guttersHeight = gutters.offsetHeight; |
| 87 | if (guttersHeight < 200) { |
| 88 | gutters.style.setProperty( |
| 89 | 'minHeight', |
| 90 | '200px', |
| 91 | 'important' |
| 92 | ); |
| 93 | } else { |
| 94 | gutters.style.minHeight = ''; |
| 95 | } |
| 96 | }); |
| 97 | observer.observe(gutters); |
| 98 | return () => observer.disconnect(); |
| 99 | } |
| 100 | } |
| 101 | }, [value]); |
| 102 | |
| 103 | const customStyleExtension = EditorView.theme({ |
| 104 | '.cm-editor': { |
| 105 | minHeight: '200px', |
| 106 | height: '100%', |
| 107 | overflowY: 'auto', |
| 108 | resize: 'vertical', |
| 109 | }, |
| 110 | '.cm-scroller': { |
| 111 | minHeight: '200px', |
| 112 | overflow: 'auto', |
| 113 | }, |
| 114 | }); |
| 115 | |
| 116 | return ( |
| 117 | <div |
| 118 | className="vk_custom-css-editor-wrapper" |
| 119 | style={{ position: 'relative' }} |
| 120 | > |
| 121 | <CodeMirror |
| 122 | id={id} |
| 123 | className={classnames(`vk_custom-css-editor`, className)} |
| 124 | height="100%" // � |
| 125 | 部のエディタをラッパーの高さに合わせる |
| 126 | // https://uiwjs.github.io/react-codemirror/#/extensions/color |
| 127 | extensions={[ |
| 128 | css(), |
| 129 | EditorView.lineWrapping, |
| 130 | customStyleExtension, |
| 131 | ]} |
| 132 | value={value} |
| 133 | onChange={(newValue) => { |
| 134 | onChange(stripHTML(newValue)); |
| 135 | |
| 136 | const [transformed] = transformStyles( |
| 137 | [{ css: newValue }], |
| 138 | '.editor-styles-wrapper' |
| 139 | ); |
| 140 | if (transformed) { |
| 141 | setCSSError(null); |
| 142 | } |
| 143 | }} |
| 144 | style={{ |
| 145 | ...style, |
| 146 | height: '200px', |
| 147 | minHeight: '200px', |
| 148 | resize: 'vertical', |
| 149 | }} |
| 150 | onBlur={(event) => { |
| 151 | onBlur(event); |
| 152 | }} |
| 153 | /> |
| 154 | {cssError && ( |
| 155 | <Tooltip text={cssError}> |
| 156 | <div |
| 157 | style={{ |
| 158 | position: 'absolute', |
| 159 | bottom: '0px', |
| 160 | right: '5px', |
| 161 | }} |
| 162 | > |
| 163 | <Icon icon={info} style={{ fill: '#cc1818' }} /> |
| 164 | </div> |
| 165 | </Tooltip> |
| 166 | )} |
| 167 | {(() => { |
| 168 | if (value && value.indexOf(' ') !== -1) { |
| 169 | return ( |
| 170 | <p> |
| 171 | {__( |
| 172 | 'Note : Contains double-byte spaces; CSS may not work.', |
| 173 | 'vk-blocks' |
| 174 | )} |
| 175 | </p> |
| 176 | ); |
| 177 | } |
| 178 | })()} |
| 179 | </div> |
| 180 | ); |
| 181 | }; |
| 182 |