PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.3.2
Block Animations, Motion & Scroll Effects – Ghost Kit v3.3.2
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 / extend / block-actions-copy-paste / index.js

index.js in Block Animations, Motion & Scroll Effects – Ghost Kit 3.3.2, at gutenberg/extend/block-actions-copy-paste/index.js

181 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { BlockSettingsMenuControls } from '@wordpress/block-editor';
2 import { hasBlockSupport, serialize } from '@wordpress/blocks';
3 import { Dropdown, MenuGroup, MenuItem } from '@wordpress/components';
4 import { useCopyToClipboard } from '@wordpress/compose';
5 import { useDispatch, useSelect } from '@wordpress/data';
6 import { addFilter } from '@wordpress/hooks';
7 import { __, _n, sprintf } from '@wordpress/i18n';
8 import { store as noticesStore } from '@wordpress/notices';
9
10 import { EXTENSIONS } from '../constants';
11 import usePasteExtensions from './use-paste-extensions';
12
13 function CopyMenuItem({ blocks, onCopy, children }) {
14 const ref = useCopyToClipboard(() => serialize(blocks), onCopy);
15
16 return <MenuItem ref={ref}>{children}</MenuItem>;
17 }
18
19 function blocksHasSupport(blocks, support) {
20 return blocks.every((block) => {
21 return !!block && hasBlockSupport(block.name, support);
22 });
23 }
24
25 /**
26 * Add block action controls.
27 *
28 * @param original
29 * @param root0
30 * @param root0.props
31 */
32 function GhostKitExtensionCopyPaste(original, { props }) {
33 const { name, clientId } = props;
34
35 const hasExtensionsSupport = hasBlockSupport(name, ['ghostkit']);
36
37 const { createSuccessNotice } = useDispatch(noticesStore);
38 const { getBlocksByClientId } = useSelect((select) => {
39 return select('core/block-editor');
40 });
41
42 const pasteExtensions = usePasteExtensions();
43
44 if (!hasExtensionsSupport) {
45 return original;
46 }
47
48 return (
49 <>
50 {original}
51 <BlockSettingsMenuControls>
52 {(menuProps) => {
53 if (menuProps.firstBlockClientId !== clientId) {
54 return null;
55 }
56
57 const blocks = getBlocksByClientId(
58 menuProps.selectedClientIds
59 );
60
61 const canCopyExtensions = blocksHasSupport(
62 blocks,
63 'ghostkit'
64 );
65
66 if (!canCopyExtensions) {
67 return null;
68 }
69
70 return (
71 <MenuGroup
72 className="ghostkit-block-actions-copy-paste"
73 label={__('Ghost Kit', 'ghostkit')}
74 >
75 <CopyMenuItem
76 blocks={blocks}
77 onCopy={() => {
78 createSuccessNotice(
79 sprintf(
80 // Translators: %d: Number of blocks being copied.
81 _n(
82 'Copied %d block to clipboard.',
83 'Copied %d blocks to clipboard.',
84 blocks.length
85 ),
86 blocks.length
87 ),
88 {
89 type: 'snackbar',
90 }
91 );
92 }}
93 >
94 {__('Copy extensions', 'ghostkit')}
95 </CopyMenuItem>
96
97 <Dropdown
98 className="ghostkit-block-actions-dropdown-paste"
99 popoverProps={{
100 placement: 'right-start',
101 offset: 36,
102 shift: true,
103 }}
104 renderToggle={({ isOpen, onToggle }) => (
105 <MenuItem
106 onClick={onToggle}
107 aria-expanded={isOpen}
108 icon={
109 <svg
110 width="24"
111 height="24"
112 viewBox="0 0 24 24"
113 fill="none"
114 xmlns="http://www.w3.org/2000/svg"
115 >
116 <path
117 d="M11.6 6.5L16 12L11.6 17.5L10.4 16.6L14 12L10.4 7.5L11.6 6.5Z"
118 fill="black"
119 />
120 </svg>
121 }
122 >
123 {__('Paste extensions', 'ghostkit')}
124 </MenuItem>
125 )}
126 renderContent={() => (
127 <MenuGroup className="ghostkit-block-actions-dropdown-paste-menu">
128 <MenuItem
129 onClick={() => {
130 pasteExtensions(blocks);
131 }}
132 >
133 {__('Paste All', 'ghostkit')}
134 </MenuItem>
135 {Object.keys(EXTENSIONS).map(
136 (extName) => {
137 if (
138 !blocksHasSupport(blocks, [
139 'ghostkit',
140 extName,
141 ])
142 ) {
143 return null;
144 }
145
146 return (
147 <MenuItem
148 key={extName}
149 onClick={() => {
150 pasteExtensions(
151 blocks,
152 extName
153 );
154 }}
155 >
156 {
157 EXTENSIONS[extName]
158 .label
159 }
160 </MenuItem>
161 );
162 }
163 )}
164 </MenuGroup>
165 )}
166 />
167 </MenuGroup>
168 );
169 }}
170 </BlockSettingsMenuControls>
171 </>
172 );
173 }
174
175 // Init filters.
176 addFilter(
177 'ghostkit.editor.extensions',
178 'ghostkit/extension/copy-paste',
179 GhostKitExtensionCopyPaste
180 );
181