| 1 |
import classnames from 'classnames/dedupe'; |
| 2 |
|
| 3 |
import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; |
| 4 |
import { |
| 5 |
ExternalLink, |
| 6 |
PanelBody, |
| 7 |
Placeholder, |
| 8 |
TextControl, |
| 9 |
ToggleControl, |
| 10 |
} from '@wordpress/components'; |
| 11 |
import { useEffect, useRef, useState } from '@wordpress/element'; |
| 12 |
import { applyFilters } from '@wordpress/hooks'; |
| 13 |
import { __ } from '@wordpress/i18n'; |
| 14 |
|
| 15 |
import { loadBlockEditorAssets } from '../../utils/block-editor-asset-loader'; |
| 16 |
import getIcon from '../../utils/get-icon'; |
| 17 |
import GistFilesSelect from './file-select'; |
| 18 |
|
| 19 |
const { gistSimple } = window; |
| 20 |
|
| 21 |
/** |
| 22 |
* Block Edit Class. |
| 23 |
* |
| 24 |
* @param props |
| 25 |
*/ |
| 26 |
export default function BlockEdit(props) { |
| 27 |
const { attributes, setAttributes } = props; |
| 28 |
const { url, file, caption, showFooter, showLineNumbers } = attributes; |
| 29 |
|
| 30 |
const [sUrl, setUrl] = useState(attributes.url); |
| 31 |
const gistNode = useRef(); |
| 32 |
const cachedRequest = useRef(); |
| 33 |
const urlTimeout = useRef(); |
| 34 |
|
| 35 |
let { className = '' } = props; |
| 36 |
|
| 37 |
function getValidGistUrl() { |
| 38 |
if (url) { |
| 39 |
const match = /^https:\/\/gist.github.com?.+\/(.+)/g.exec(url); |
| 40 |
|
| 41 |
if (match && typeof match[1] !== 'undefined') { |
| 42 |
return match[1].split('#')[0]; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
function onUpdate() { |
| 50 |
if (!url || !gistNode?.current) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
const validUrl = getValidGistUrl(); |
| 55 |
|
| 56 |
if (!validUrl) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
if (typeof gistSimple === 'undefined') { |
| 61 |
// eslint-disable-next-line no-console |
| 62 |
console.warn(__('Gist Simple plugin is not defined.', 'ghostkit')); |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
// cache request to prevent reloading. |
| 67 |
const newCachedRequest = |
| 68 |
validUrl + |
| 69 |
file + |
| 70 |
caption + |
| 71 |
(showFooter ? 1 : 0) + |
| 72 |
(showLineNumbers ? 1 : 0); |
| 73 |
if (newCachedRequest === cachedRequest?.current) { |
| 74 |
return; |
| 75 |
} |
| 76 |
cachedRequest.current = newCachedRequest; |
| 77 |
|
| 78 |
setTimeout(() => { |
| 79 |
if (gistNode.current.GistSimple) { |
| 80 |
gistNode.current.GistSimple.destroy(); |
| 81 |
} |
| 82 |
|
| 83 |
gistSimple(gistNode.current, { |
| 84 |
id: validUrl, |
| 85 |
file, |
| 86 |
caption, |
| 87 |
showFooter, |
| 88 |
showLineNumbers, |
| 89 |
}); |
| 90 |
}, 0); |
| 91 |
} |
| 92 |
|
| 93 |
// Mount and update. |
| 94 |
useEffect(() => { |
| 95 |
onUpdate(); |
| 96 |
}); |
| 97 |
|
| 98 |
// Load assets. |
| 99 |
useEffect(() => { |
| 100 |
if (gistNode?.current) { |
| 101 |
loadBlockEditorAssets('css', 'gist-simple-css', gistNode.current); |
| 102 |
} |
| 103 |
}, [gistNode]); |
| 104 |
|
| 105 |
function urlOnChange(value, timeout = 1000) { |
| 106 |
setUrl(value); |
| 107 |
|
| 108 |
clearTimeout(urlTimeout.current); |
| 109 |
|
| 110 |
urlTimeout.current = setTimeout(() => { |
| 111 |
setAttributes({ url: value }); |
| 112 |
}, timeout); |
| 113 |
} |
| 114 |
|
| 115 |
className = classnames('ghostkit-gist', className); |
| 116 |
className = applyFilters('ghostkit.editor.className', className, props); |
| 117 |
|
| 118 |
const blockProps = useBlockProps({ className }); |
| 119 |
|
| 120 |
return ( |
| 121 |
<> |
| 122 |
<InspectorControls> |
| 123 |
<PanelBody> |
| 124 |
<TextControl |
| 125 |
label={__('URL', 'ghostkit')} |
| 126 |
type="url" |
| 127 |
value={sUrl} |
| 128 |
onChange={(val) => urlOnChange(val)} |
| 129 |
onKeyDown={(e) => { |
| 130 |
if (e.keyCode === 13) { |
| 131 |
urlOnChange(sUrl, 0); |
| 132 |
} |
| 133 |
}} |
| 134 |
__next40pxDefaultSize |
| 135 |
__nextHasNoMarginBottom |
| 136 |
/> |
| 137 |
<GistFilesSelect |
| 138 |
label={__('File', 'ghostkit')} |
| 139 |
url={url} |
| 140 |
value={file} |
| 141 |
onChange={(value) => setAttributes({ file: value })} |
| 142 |
/> |
| 143 |
</PanelBody> |
| 144 |
<PanelBody> |
| 145 |
<TextControl |
| 146 |
label={__('Caption', 'ghostkit')} |
| 147 |
value={caption} |
| 148 |
onChange={(value) => setAttributes({ caption: value })} |
| 149 |
__next40pxDefaultSize |
| 150 |
__nextHasNoMarginBottom |
| 151 |
/> |
| 152 |
<ToggleControl |
| 153 |
label={__('Show footer', 'ghostkit')} |
| 154 |
checked={!!showFooter} |
| 155 |
onChange={(val) => setAttributes({ showFooter: val })} |
| 156 |
__nextHasNoMarginBottom |
| 157 |
/> |
| 158 |
<ToggleControl |
| 159 |
label={__('Show line numbers', 'ghostkit')} |
| 160 |
checked={!!showLineNumbers} |
| 161 |
onChange={(val) => |
| 162 |
setAttributes({ showLineNumbers: val }) |
| 163 |
} |
| 164 |
__nextHasNoMarginBottom |
| 165 |
/> |
| 166 |
</PanelBody> |
| 167 |
</InspectorControls> |
| 168 |
|
| 169 |
<div {...blockProps}> |
| 170 |
{!url ? ( |
| 171 |
<Placeholder |
| 172 |
icon={getIcon('block-gist')} |
| 173 |
label={__('Gist URL', 'ghostkit')} |
| 174 |
className={className} |
| 175 |
> |
| 176 |
<TextControl |
| 177 |
placeholder="https://gist.github.com/..." |
| 178 |
value={sUrl} |
| 179 |
onChange={(val) => urlOnChange(val)} |
| 180 |
onKeyDown={(e) => { |
| 181 |
if (e.keyCode === 13) { |
| 182 |
urlOnChange(sUrl, 0); |
| 183 |
} |
| 184 |
}} |
| 185 |
__next40pxDefaultSize |
| 186 |
__nextHasNoMarginBottom |
| 187 |
/> |
| 188 |
<ExternalLink href="https://gist.github.com/"> |
| 189 |
{__('Visit GitHub Gist Site', 'ghostkit')} |
| 190 |
</ExternalLink> |
| 191 |
</Placeholder> |
| 192 |
) : null} |
| 193 |
{url ? ( |
| 194 |
<div |
| 195 |
ref={gistNode} |
| 196 |
className={className} |
| 197 |
data-url={url} |
| 198 |
data-file={file} |
| 199 |
data-caption={caption} |
| 200 |
data-show-footer={showFooter ? 'true' : 'false'} |
| 201 |
data-show-line-numbers={ |
| 202 |
showLineNumbers ? 'true' : 'false' |
| 203 |
} |
| 204 |
/> |
| 205 |
) : null} |
| 206 |
</div> |
| 207 |
</> |
| 208 |
); |
| 209 |
} |
| 210 |
|