| 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 |
if (document.getElementById(id)) return; |
| 17 |
const agent = Object.assign(document.createElement('li'), { |
| 18 |
className: 'extendify-agent', |
| 19 |
id, |
| 20 |
}); |
| 21 |
agent.style.height = '1.75rem'; |
| 22 |
agent.style.marginInlineEnd = '4px'; |
| 23 |
// TODO: If we want to allow swapping live we need to rethink this |
| 24 |
const loc = |
| 25 |
window.extAgentData.agentPosition === 'floating' |
| 26 |
? '#wp-admin-bar-my-account' |
| 27 |
: '#wp-admin-bar-wp-logo'; |
| 28 |
document.querySelector(loc)?.before(agent); |
| 29 |
render(<AdminBar />, agent); |
| 30 |
}); |
| 31 |
|
| 32 |
// Mobile |
| 33 |
domReady(() => { |
| 34 |
if (isOnLaunch()) return; |
| 35 |
const id = 'extendify-agent-mobile-btn'; |
| 36 |
if (document.getElementById(id)) return; |
| 37 |
const agent = Object.assign(document.createElement('div'), { |
| 38 |
className: 'extendify-agent', |
| 39 |
id, |
| 40 |
}); |
| 41 |
agent.style.position = 'sticky'; |
| 42 |
agent.style.top = 'calc(100% - var(--extendify-agent-mobile-btn-height))'; |
| 43 |
agent.style.bottom = '0'; |
| 44 |
agent.style.zIndex = '99999'; |
| 45 |
document.body.appendChild(agent); |
| 46 |
render(<Mobile />, agent); |
| 47 |
}); |
| 48 |
|
| 49 |
// In editor |
| 50 |
registerPlugin('extendify-agent-buttons', { |
| 51 |
render: () => <AgentButton />, |
| 52 |
}); |
| 53 |
const AgentButton = () => { |
| 54 |
useEffect(() => { |
| 55 |
if (isOnLaunch()) return; |
| 56 |
const id = 'extendify-agent-editor-btn'; |
| 57 |
if (document.getElementById(id)) return; |
| 58 |
|
| 59 |
const agent = Object.assign(document.createElement('span'), { |
| 60 |
className: 'extendify-agent', |
| 61 |
id, |
| 62 |
}); |
| 63 |
setTimeout(() => { |
| 64 |
if (document.getElementById(id)) return; |
| 65 |
const page = '[aria-controls="edit-post:document"]'; |
| 66 |
const fse = '[aria-controls="edit-site:template"]'; |
| 67 |
document.querySelector(page)?.after(agent); |
| 68 |
document.querySelector(fse)?.after(agent); |
| 69 |
render(<PostEditor />, agent); |
| 70 |
}, 300); |
| 71 |
}, []); |
| 72 |
return null; |
| 73 |
}; |
| 74 |
|