PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.3.3
Block Animations, Motion & Scroll Effects – Ghost Kit v3.3.3
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / gutenberg / blocks / markdown / edit.js

edit.js in Block Animations, Motion & Scroll Effects – Ghost Kit 3.3.3, at gutenberg/blocks/markdown/edit.js

112 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import classnames from 'classnames/dedupe';
2
3 import {
4 BlockControls,
5 PlainText,
6 useBlockProps,
7 } from '@wordpress/block-editor';
8 import { ToolbarButton, ToolbarGroup } from '@wordpress/components';
9 import { useDispatch, useSelect } from '@wordpress/data';
10 import { useEffect, useRef, useState } from '@wordpress/element';
11 import { __ } from '@wordpress/i18n';
12
13 import MDRender from './render';
14
15 /**
16 * Block Edit Class.
17 *
18 * @param props
19 */
20 export default function BlockEdit(props) {
21 const { attributes, setAttributes, isSelected } = props;
22 const { content } = attributes;
23
24 let { className } = props;
25
26 const [activeTab, setActiveTab] = useState('editor');
27 const ref = useRef();
28
29 // Change active tab when block is not selected.
30 useEffect(() => {
31 if (!isSelected && activeTab === 'preview') {
32 setActiveTab('editor');
33 }
34 }, [isSelected, activeTab]);
35
36 // Focus input.
37 useEffect(() => {
38 if (isSelected && activeTab === 'editor' && ref?.current) {
39 ref?.current?.focus?.();
40 }
41 }, [ref, isSelected, activeTab]);
42
43 const { currentBlockId } = useSelect((select) => ({
44 currentBlockId: select('core/block-editor').getSelectedBlockClientId(),
45 }));
46
47 const { removeBlocks } = useDispatch('core/block-editor');
48
49 function removeBlock() {
50 removeBlocks(currentBlockId);
51 }
52
53 className = classnames('ghostkit-markdown', className);
54
55 const blockProps = useBlockProps({ className });
56
57 if (!isSelected && (!content || content.trim() === '')) {
58 return (
59 <div {...blockProps}>
60 <p>{__('Write your _Markdown_ **here**…', 'ghostkit')}</p>
61 </div>
62 );
63 }
64
65 return (
66 <div {...blockProps}>
67 <BlockControls>
68 <ToolbarGroup>
69 <ToolbarButton
70 className={classnames(
71 'components-button components-tab-button',
72 activeTab === 'editor' && 'is-pressed'
73 )}
74 onClick={() => setActiveTab('editor')}
75 >
76 <span>{__('Markdown', 'ghostkit')}</span>
77 </ToolbarButton>
78 <ToolbarButton
79 className={classnames(
80 'components-button components-tab-button',
81 activeTab === 'preview' && 'is-pressed'
82 )}
83 onClick={() => setActiveTab('preview')}
84 >
85 <span>{__('Preview', 'ghostkit')}</span>
86 </ToolbarButton>
87 </ToolbarGroup>
88 </BlockControls>
89
90 {activeTab === 'preview' || !isSelected ? (
91 <MDRender content={content} />
92 ) : (
93 <PlainText
94 onChange={(val) => {
95 setAttributes({ content: val });
96 }}
97 onKeyDown={(e) => {
98 // Remove the block if content is empty and we're pressing the Backspace key
99 if (e.keyCode === 8 && content === '') {
100 removeBlock();
101 e.preventDefault();
102 }
103 }}
104 aria-label={__('Markdown', 'ghostkit')}
105 ref={ref}
106 value={content}
107 />
108 )}
109 </div>
110 );
111 }
112