import * as React from 'react'
import {__, _x, sprintf} from '@wordpress/i18n'
import {
hasBlockSupport,
getBlockSupport,
// @ts-expect-error
} from '@wordpress/blocks'
import {
addFilter,
applyFilters,
} from '@wordpress/hooks'
import {
InspectorControls,
InspectorAdvancedControls,
BlockControls,
// @ts-expect-error
} from '@wordpress/block-editor'
import {
TextControl,
ToggleControl,
ToolbarGroup,
} from '@wordpress/components'
import {
createHigherOrderComponent,
} from '@wordpress/compose'
import {
ButtonGroup,
WithTooltip,
} from '@ska/components'
import {
SkaPanelBody,
} from '@ska/plugin'
import {
URLPicker,
} from '../components'
import {
useDynamicLinks,
type LinkValue,
} from './link'
import {
useHasPermissions,
} from '../plugins/permissions'
import type {
tBlockEditProps,
tBlockNameOrType,
tBlockSaveProps,
tBlockType,
} from '@ska/shared'
export interface AsValue extends LinkValue {
/** Tag name to render as. */
element?: string
/** Custom tag name to render as, used when `element` is set to `custom`. */
customElement?: string
/** Render as a void element (without children). */
isVoid?: boolean
}
export interface AsAttributes {
skaBlocksAs?: AsValue
}
export const AS_SUPPORT_KEY = 'skaBlocksAs'
export const AS_SUPPORT_DEFAULT_ENABLED = false
export const hasAsSupport = (block: tBlockNameOrType) => hasBlockSupport(block, AS_SUPPORT_KEY, AS_SUPPORT_DEFAULT_ENABLED)
/**
* Default value can be provided in block `supports` attribute.
*
* @example
* ```json
* "supports": {
* "skaBlocksAs": {
* "element": "span"
* }
* }
* ```
*/
const getDefaultValue = (block: tBlockType) => {
const support = getBlockSupport(block, AS_SUPPORT_KEY, AS_SUPPORT_DEFAULT_ENABLED)
return {
...(typeof support === 'object' && {
...support,
}),
}
}
const withCustomAttributes = (settings: tBlockType) => {
if(!hasAsSupport(settings)) {
return settings
}
if(!settings.attributes.skaBlocksAs) {
const defaultValue = getDefaultValue(settings)
Object.assign(settings.attributes, {
skaBlocksAs: {
type: 'object',
...(Object.keys(defaultValue).length > 0 && {
default: defaultValue,
}),
},
})
}
return settings
}
export const AS_ELEMENTS = applyFilters('ska.blocks.asElements', [
{
label: '
',
value: 'div',
},
{
label: '
',
value: 'span',
},
{
label: '',
value: 'a',
},
{
label: '