| 1 |
import { addCompleter } from 'ace-builds/src-noconflict/ext-language_tools'; |
| 2 |
import classnames from 'classnames/dedupe'; |
| 3 |
|
| 4 |
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks'; |
| 5 |
import { |
| 6 |
__experimentalToolsPanelItem as ExperimentalToolsPanelItem, |
| 7 |
__stableToolsPanelItem as StableToolsPanelItem, |
| 8 |
BaseControl, |
| 9 |
Button, |
| 10 |
Dropdown, |
| 11 |
} from '@wordpress/components'; |
| 12 |
import { useState } from '@wordpress/element'; |
| 13 |
import { addFilter } from '@wordpress/hooks'; |
| 14 |
import { __ } from '@wordpress/i18n'; |
| 15 |
|
| 16 |
import CodeEditor from '../../../components/code-editor'; |
| 17 |
import ResponsiveToggle from '../../../components/responsive-toggle'; |
| 18 |
import useResponsive from '../../../hooks/use-responsive'; |
| 19 |
import useStyles from '../../../hooks/use-styles'; |
| 20 |
|
| 21 |
const ToolsPanelItem = StableToolsPanelItem || ExperimentalToolsPanelItem; |
| 22 |
|
| 23 |
const placeholder = 'selector {\n\n}'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Autocomplete for `selector`. |
| 27 |
*/ |
| 28 |
addCompleter({ |
| 29 |
getCompletions(editor, session, pos, prefix, callback) { |
| 30 |
if (editor.id === 'gkt-custom-css-editor') { |
| 31 |
callback(null, [ |
| 32 |
{ |
| 33 |
caption: 'selector', |
| 34 |
value: 'selector', |
| 35 |
meta: __('Block Selector', 'ghostkit'), |
| 36 |
}, |
| 37 |
]); |
| 38 |
} |
| 39 |
}, |
| 40 |
identifierRegexps: [/selector/], |
| 41 |
}); |
| 42 |
|
| 43 |
function CustomCSSCustomTools(props) { |
| 44 |
const [defaultPlaceholder, setDefaultPlaceholder] = useState(placeholder); |
| 45 |
|
| 46 |
const { getStyle, hasStyle, setStyles, resetStyles } = useStyles(props); |
| 47 |
|
| 48 |
const { device, allDevices } = useResponsive(); |
| 49 |
|
| 50 |
let hasCustom = false; |
| 51 |
|
| 52 |
['', ...Object.keys(allDevices)].forEach((thisDevice) => { |
| 53 |
hasCustom = hasCustom || hasStyle('custom', thisDevice); |
| 54 |
}); |
| 55 |
|
| 56 |
const baseControlLabel = ( |
| 57 |
<> |
| 58 |
{__('Custom', 'ghostkit')} |
| 59 |
<ResponsiveToggle |
| 60 |
checkActive={(checkMedia) => { |
| 61 |
return hasStyle('custom', checkMedia); |
| 62 |
}} |
| 63 |
/> |
| 64 |
</> |
| 65 |
); |
| 66 |
|
| 67 |
return ( |
| 68 |
<ToolsPanelItem |
| 69 |
label={__('Custom', 'ghostkit')} |
| 70 |
hasValue={() => !!hasCustom} |
| 71 |
onSelect={() => { |
| 72 |
if (!hasStyle('custom')) { |
| 73 |
setStyles({ custom: '' }); |
| 74 |
} |
| 75 |
}} |
| 76 |
onDeselect={() => { |
| 77 |
resetStyles(['custom'], true); |
| 78 |
}} |
| 79 |
isShownByDefault={false} |
| 80 |
> |
| 81 |
<BaseControl id={baseControlLabel} label={baseControlLabel}> |
| 82 |
<Dropdown |
| 83 |
className="ghostkit-extension-customCSS-custom__dropdown" |
| 84 |
contentClassName="ghostkit-extension-customCSS-custom__dropdown-content" |
| 85 |
popoverProps={{ |
| 86 |
placement: 'left-start', |
| 87 |
offset: 36, |
| 88 |
shift: true, |
| 89 |
}} |
| 90 |
renderToggle={({ isOpen, onToggle }) => ( |
| 91 |
<Button |
| 92 |
className={classnames( |
| 93 |
'ghostkit-extension-customCSS-custom__dropdown-content-toggle', |
| 94 |
isOpen |
| 95 |
? 'ghostkit-extension-customCSS-custom__dropdown-content-toggle-active' |
| 96 |
: '' |
| 97 |
)} |
| 98 |
onClick={() => { |
| 99 |
onToggle(); |
| 100 |
}} |
| 101 |
> |
| 102 |
<span>{__('Edit CSS', 'ghostkit')}</span> |
| 103 |
<CodeEditor |
| 104 |
mode="css" |
| 105 |
value={ |
| 106 |
getStyle('custom', device) || |
| 107 |
defaultPlaceholder |
| 108 |
} |
| 109 |
maxLines={7} |
| 110 |
minLines={3} |
| 111 |
height="200px" |
| 112 |
showPrintMargin={false} |
| 113 |
showGutter={false} |
| 114 |
highlightActiveLine={false} |
| 115 |
setOptions={{ |
| 116 |
enableBasicAutocompletion: false, |
| 117 |
enableLiveAutocompletion: false, |
| 118 |
enableSnippets: false, |
| 119 |
showLineNumbers: false, |
| 120 |
}} |
| 121 |
/> |
| 122 |
</Button> |
| 123 |
)} |
| 124 |
renderContent={() => ( |
| 125 |
<> |
| 126 |
<BaseControl |
| 127 |
id={baseControlLabel} |
| 128 |
label={baseControlLabel} |
| 129 |
/> |
| 130 |
<CodeEditor |
| 131 |
mode="css" |
| 132 |
onChange={(value) => { |
| 133 |
if (value !== placeholder) { |
| 134 |
setStyles({ custom: value }, device); |
| 135 |
} |
| 136 |
|
| 137 |
// Reset placeholder. |
| 138 |
if (defaultPlaceholder) { |
| 139 |
setDefaultPlaceholder(''); |
| 140 |
} |
| 141 |
}} |
| 142 |
value={ |
| 143 |
getStyle('custom', device) || |
| 144 |
defaultPlaceholder |
| 145 |
} |
| 146 |
maxLines={20} |
| 147 |
minLines={5} |
| 148 |
height="300px" |
| 149 |
editorProps={{ |
| 150 |
id: 'gkt-custom-css-editor', |
| 151 |
}} |
| 152 |
/> |
| 153 |
<p style={{ marginBottom: 20 }} /> |
| 154 |
<details> |
| 155 |
<summary |
| 156 |
label={__( |
| 157 |
'Examples to use selector', |
| 158 |
'ghostkit' |
| 159 |
)} |
| 160 |
dangerouslySetInnerHTML={{ |
| 161 |
__html: __( |
| 162 |
'Use %s rule to change block styles.', |
| 163 |
'ghostkit' |
| 164 |
).replace( |
| 165 |
'%s', |
| 166 |
'<code>selector</code>' |
| 167 |
), |
| 168 |
}} |
| 169 |
/> |
| 170 |
<p>{__('Example:', 'ghostkit')}</p> |
| 171 |
<pre className="ghostkit-control-pre-custom-css"> |
| 172 |
{`selector { |
| 173 |
background-color: #2F1747; |
| 174 |
} |
| 175 |
|
| 176 |
selector p { |
| 177 |
color: #2F1747; |
| 178 |
}`} |
| 179 |
</pre> |
| 180 |
</details> |
| 181 |
</> |
| 182 |
)} |
| 183 |
/> |
| 184 |
</BaseControl> |
| 185 |
</ToolsPanelItem> |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
addFilter( |
| 190 |
'ghostkit.extension.customCSS.tools', |
| 191 |
'ghostkit/extension/customCSS/tools/custom', |
| 192 |
(children, { props }) => { |
| 193 |
const hasCustomSupport = |
| 194 |
hasBlockSupport(props.name, ['ghostkit', 'customCSS', 'custom']) || |
| 195 |
getBlockSupport(props.name, ['ghostkit', 'customCSS']) === true; |
| 196 |
|
| 197 |
if (!hasCustomSupport) { |
| 198 |
return children; |
| 199 |
} |
| 200 |
|
| 201 |
return ( |
| 202 |
<> |
| 203 |
{children} |
| 204 |
<CustomCSSCustomTools {...props} /> |
| 205 |
</> |
| 206 |
); |
| 207 |
} |
| 208 |
); |
| 209 |
|