PluginProbe
Extendify / 3.1.5
Extendify v3.1.5
3.2.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 All 127 releases
extendify / src / Agent / buttons.js

buttons.js in Extendify 3.1.5, at src/Agent/buttons.js

83 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { AdminBar } from '@agent/components/buttons/AdminBar';
2 import { Mobile } from '@agent/components/buttons/Mobile';
3 import { PostEditor } from '@agent/components/buttons/PostEditor';
4 import { render } from '@shared/lib/dom';
5 import { isOnLaunch } from '@shared/lib/utils';
6 import domReady from '@wordpress/dom-ready';
7 import { useEffect } from '@wordpress/element';
8 import { registerPlugin } from '@wordpress/plugins';
9
10 // TODO: Sometimes the admin bar is crowded, so a smarter way would be to do some analysis first and position these accordingly.
11
12 // Global toolbar
13 domReady(() => {
14 if (isOnLaunch()) return;
15 const id = 'wp-admin-bar-extendify-agent-btn';
16 // Skeleton.php may already own this node; reuse it so the item never moves.
17 const agent =
18 document.getElementById(id) ??
19 Object.assign(document.createElement('li'), {
20 className: 'extendify-agent',
21 id,
22 });
23 // Presence no longer proves a mount, so the marker guards it.
24 if (agent.dataset.extendifyMounted) return;
25 agent.dataset.extendifyMounted = '1';
26 agent.style.height = '1.75rem';
27 agent.style.marginInlineEnd = '4px';
28 if (!agent.isConnected) {
29 // TODO: If we want to allow swapping live we need to rethink this
30 const loc =
31 window.extAgentData.agentPosition === 'floating'
32 ? '#wp-admin-bar-my-account'
33 : '#wp-admin-bar-wp-logo';
34 document.querySelector(loc)?.before(agent);
35 }
36 // Clearing the slot first paints it empty when React commits a task later.
37 const mount = agent.appendChild(document.createElement('span'));
38 render(<AdminBar />, mount);
39 });
40
41 // Mobile
42 domReady(() => {
43 if (isOnLaunch()) return;
44 const id = 'extendify-agent-mobile-btn';
45 if (document.getElementById(id)) return;
46 const agent = Object.assign(document.createElement('div'), {
47 className: 'extendify-agent',
48 id,
49 });
50 agent.style.position = 'sticky';
51 agent.style.top = 'calc(100% - var(--extendify-agent-mobile-btn-height))';
52 agent.style.bottom = '0';
53 agent.style.zIndex = '99999';
54 document.body.appendChild(agent);
55 render(<Mobile />, agent);
56 });
57
58 // In editor
59 registerPlugin('extendify-agent-buttons', {
60 render: () => <AgentButton />,
61 });
62 const AgentButton = () => {
63 useEffect(() => {
64 if (isOnLaunch()) return;
65 const id = 'extendify-agent-editor-btn';
66 if (document.getElementById(id)) return;
67
68 const agent = Object.assign(document.createElement('span'), {
69 className: 'extendify-agent',
70 id,
71 });
72 setTimeout(() => {
73 if (document.getElementById(id)) return;
74 const page = '[aria-controls="edit-post:document"]';
75 const fse = '[aria-controls="edit-site:template"]';
76 document.querySelector(page)?.after(agent);
77 document.querySelector(fse)?.after(agent);
78 render(<PostEditor />, agent);
79 }, 300);
80 }, []);
81 return null;
82 };
83