PluginProbe
Extendify / 1.17.1
Extendify v1.17.1
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 / Draft / app.js

app.js in Extendify 1.17.1, at src/Draft/app.js

111 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { store as blockEditorStore } from '@wordpress/block-editor';
2 import { createBlock } from '@wordpress/blocks';
3 import { Flex, FlexBlock } from '@wordpress/components';
4 import { useSelect, useDispatch } from '@wordpress/data';
5 import { store as editPostStore } from '@wordpress/edit-post';
6 import { PluginSidebar, PluginSidebarMoreMenuItem } from '@wordpress/edit-post';
7 import { useEffect, useRef } from '@wordpress/element';
8 import { addFilter } from '@wordpress/hooks';
9 import { __ } from '@wordpress/i18n';
10 import { registerPlugin } from '@wordpress/plugins';
11 import { useEditorReady } from '@shared/hooks/gutenberg';
12 import { Draft } from '@draft/Draft';
13 import '@draft/app.css';
14 import { GenerateImageButtons } from '@draft/components/GenerateImageButtons';
15 import { ToolbarMenu } from '@draft/components/ToolbarMenu';
16 import { useRouter } from '@draft/hooks/useRouter';
17 import { magic } from '@draft/svg';
18
19 registerPlugin('extendify-draft', {
20 render: () => (
21 <ExtendifyDraft>
22 <PluginSidebarMoreMenuItem target="draft">
23 {__('AI Tools', 'extendify-local')}
24 </PluginSidebarMoreMenuItem>
25 <PluginSidebar
26 name="draft"
27 icon={magic}
28 title={__('AI Tools', 'extendify-local')}
29 className="extendify-draft h-full">
30 <Flex direction="column" expanded justify="space-between">
31 <FlexBlock>
32 <Draft />
33 </FlexBlock>
34 </Flex>
35 </PluginSidebar>
36 </ExtendifyDraft>
37 ),
38 });
39
40 const ExtendifyDraft = ({ children }) => {
41 const { insertBlocks, selectBlock } = useDispatch(blockEditorStore);
42 const { navigateTo } = useRouter();
43 const { openGeneralSidebar } = useDispatch(editPostStore);
44 const sidebarName = useSelect((select) =>
45 select(editPostStore).getActiveGeneralSidebarName(),
46 );
47 const ready = useEditorReady();
48 const once = useRef(false);
49
50 const { getBlocks } = useSelect((select) => select(blockEditorStore), []);
51
52 useEffect(() => {
53 const search = new URLSearchParams(window.location.search);
54 // Lets Assist add an image block to highlight the feature
55 if (!search.has('ext-add-image-block')) return;
56 search.delete('ext-add-image-block');
57 window.history.replaceState(
58 {},
59 '',
60 window.location.pathname + '?' + search.toString(),
61 );
62
63 navigateTo('ai-image');
64
65 const imageBlock = getBlocks()?.find(
66 (block) => block.name === 'core/image',
67 );
68 requestAnimationFrame(() =>
69 imageBlock
70 ? selectBlock(imageBlock.clientId)
71 : insertBlocks([createBlock('core/image')]),
72 );
73 setTimeout(() => {
74 // Focus the textarea but give time for wp to finish it's autofocus
75 document.getElementById('draft-ai-image-textarea')?.focus();
76 }, 300);
77 }, [selectBlock, insertBlocks, navigateTo, getBlocks]);
78
79 useEffect(() => {
80 if (!ready || once.current) return;
81
82 const id = requestAnimationFrame(() => {
83 if (sidebarName === 'extendify-draft/draft') {
84 once.current = true;
85 return;
86 }
87
88 openGeneralSidebar('extendify-draft/draft');
89 });
90
91 return () => cancelAnimationFrame(id);
92 }, [openGeneralSidebar, sidebarName, ready]);
93
94 return children;
95 };
96
97 // Add the toolbar
98 addFilter(
99 'editor.BlockEdit',
100 'extendify-draft/draft-toolbar',
101 (CurrentMenuItems) => (props) => ToolbarMenu(CurrentMenuItems, props),
102 );
103
104 // Add the Generate with AI button
105 addFilter(
106 'editor.BlockEdit',
107 'extendify-draft/draft-image',
108 (CurrentComponents) => (props) =>
109 GenerateImageButtons(CurrentComponents, props),
110 );
111