import {
createElement,
Fragment,
useRef,
useEffect,
useState,
} from '@wordpress/element'
import { registerPlugin, withPluginContext } from '@wordpress/plugins'
import { PluginSidebar, PluginSidebarMoreMenuItem } from '@wordpress/edit-post'
import { withSelect, withDispatch } from '@wordpress/data'
import { compose } from '@wordpress/compose'
import { IconButton, Button } from '@wordpress/components'
import { handleMetaboxValueChange } from './editor/sync'
import ctEvents from 'ct-events'
import { __, sprintf } from 'ct-i18n'
import {
OptionsPanel,
getValueFromInput,
PanelLevel,
DeviceManagerProvider,
} from 'blocksy-options'
import { SVG, Path } from '@wordpress/primitives'
const closeSmall = (
)
const starEmpty = (
)
const starFilled = (
)
const BlocksyOptions = ({ name, value, options, onChange, isActive }) => {
const containerRef = useRef()
const parentContainerRef = useRef()
const [values, setValues] = useState(null)
useEffect(() => {
document.body.classList[isActive ? 'add' : 'remove'](
'blocksy-sidebar-active'
)
}, [isActive])
const handleChange = ({ id: key, value: v }) => {
const futureValue = {
...(values || getValueFromInput(options, value || {})),
[key]: v,
}
handleMetaboxValueChange(key, v)
onChange({
...value,
[key]: v,
})
setValues(futureValue)
}
useEffect(() => {
ctEvents.on('ct:metabox:options:trigger-change', handleChange)
return () => {
ctEvents.off('ct:metabox:options:trigger-change', handleChange)
}
}, [])
return (
{sprintf(
__('%s Page Settings', 'blocksy'),
ct_localizations.product_name
)}
}
className="ct-components-panel"
title={sprintf(
__('%s Page Settings', 'blocksy'),
ct_localizations.product_name
)}>
{
const futureValue = {
...(values ||
getValueFromInput(
options,
value || {}
)),
[key]: v,
}
handleMetaboxValueChange(key, v)
onChange({
...value,
[key]: v,
})
setValues(futureValue)
}}
value={
values ||
getValueFromInput(options, value || {})
}
options={options}
/>
)
}
const BlocksyOptionsComposed = compose(
withPluginContext((context, { name }) => ({
sidebarName: `${context.name}/${name}`,
})),
withSelect((select, { sidebarName }) => {
const value =
select('core/editor').getEditedPostAttribute('blocksy_meta')
const { getActiveGeneralSidebarName, isPluginItemPinned } =
select('core/edit-post')
return {
isActive: getActiveGeneralSidebarName() === sidebarName,
value: Array.isArray(value) ? {} : value || {},
options: ct_editor_localizations.post_options,
}
}),
withDispatch((dispatch, { sidebarName }) => {
return {
onChange: (blocksy_meta) => {
dispatch('core/editor').editPost({
blocksy_meta,
})
},
}
})
)(BlocksyOptions)
if (ct_editor_localizations.post_options) {
registerPlugin('blocksy', {
render: () => ,
})
}