PluginProbe
Extendify / 2.2.2
Extendify v2.2.2
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / src / Library / components / BlockPreviewButton.jsx

BlockPreviewButton.jsx in Extendify 2.2.2, at src/Library/components/BlockPreviewButton.jsx

57 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { BlockPreview } from '@wordpress/block-editor';
2 import { rawHandler } from '@wordpress/blocks';
3 import { useMemo, useRef, useState, useEffect } from '@wordpress/element';
4 import { __ } from '@wordpress/i18n';
5 import classNames from 'classnames';
6 import { usePreviewIframe } from '@library/hooks/usePreviewIframe';
7
8 export const BlockPreviewButton = ({ insertPattern, code }) => {
9 const [ready, setReady] = useState(false);
10 const blockRef = useRef();
11 const blocks = useMemo(
12 () => rawHandler({ HTML: lowerImageQuality(code) }),
13 [code],
14 );
15 const { ready: show } = usePreviewIframe({
16 container: blockRef.current,
17 ready,
18 onIFrameLoaded: () => undefined,
19 loadDelay: 50,
20 });
21
22 useEffect(() => setReady(true), []);
23
24 return (
25 <button
26 ref={blockRef}
27 type="button"
28 aria-label={__('Insert Pattern', 'extendify-local')}
29 className={classNames(
30 'library-pattern relative z-10 m-0 mb-8 inline-block w-full border bg-transparent p-0 focus:shadow-sm focus:outline-none focus:ring-wp focus:ring-design-main focus:ring-offset-2 focus:ring-offset-[#FAFAFA]',
31 {
32 'border-transparent opacity-0': !show,
33 'border-gray-400 opacity-100': show,
34 },
35 )}
36 onClick={() => insertPattern(blocks)}>
37 <BlockPreview
38 blocks={blocks}
39 live={false}
40 viewportWidth={1400}
41 additionalStyles={[
42 {
43 css: '.rich-text [data-rich-text-placeholder]:after { content: "" }',
44 },
45 ]}
46 />
47 </button>
48 );
49 };
50
51 const lowerImageQuality = (html) => {
52 return html.replace(
53 /(https?:\/\/\S+\?w=\d+)/gi,
54 '$1&q=10&auto=format,compress&fm=avif',
55 );
56 };
57