PluginProbe
Extendify / 3.1.4
Extendify v3.1.4
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 3.1.4, at src/Draft/app.js

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