| 1 |
import { ReactNode } from "react"; |
| 2 |
import { applyFilters } from "@wordpress/hooks"; |
| 3 |
|
| 4 |
import { CellElement, CellKey } from "./attributes"; |
| 5 |
import { BindableAttribute } from "./dynamic-data"; |
| 6 |
|
| 7 |
export interface ExtendedElementRendererProps { |
| 8 |
element: CellElement; |
| 9 |
cellCoords: CellKey; |
| 10 |
elementIndex: number; |
| 11 |
} |
| 12 |
|
| 13 |
export interface ExtendedElementDefinition { |
| 14 |
name: string; |
| 15 |
title: string; |
| 16 |
icon: ReactNode; |
| 17 |
blockName: string; |
| 18 |
create: () => CellElement; |
| 19 |
render: (props: ExtendedElementRendererProps) => ReactNode; |
| 20 |
bindableAttributes?: BindableAttribute[]; |
| 21 |
autocompleteLabel?: string; |
| 22 |
} |
| 23 |
|
| 24 |
export function getExtendedElementDefinitions(): ExtendedElementDefinition[] { |
| 25 |
return applyFilters( |
| 26 |
"tableberg.extendedElementDefinitions", |
| 27 |
[] as ExtendedElementDefinition[] |
| 28 |
) as ExtendedElementDefinition[]; |
| 29 |
} |
| 30 |
|
| 31 |
export function getExtendedElementDefinition( |
| 32 |
name: string |
| 33 |
): ExtendedElementDefinition | undefined { |
| 34 |
return getExtendedElementDefinitions().find( |
| 35 |
definition => definition.name === name |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
export function createExtendedElement(name: string): CellElement | null { |
| 40 |
return getExtendedElementDefinition(name)?.create() ?? null; |
| 41 |
} |
| 42 |
|
| 43 |
export function renderExtendedElement( |
| 44 |
element: CellElement, |
| 45 |
cellCoords: CellKey, |
| 46 |
elementIndex: number |
| 47 |
): ReactNode | null { |
| 48 |
return ( |
| 49 |
getExtendedElementDefinition(element.name)?.render({ |
| 50 |
element, |
| 51 |
cellCoords, |
| 52 |
elementIndex, |
| 53 |
}) ?? null |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
export function getExtendedElementBlockName( |
| 58 |
element: CellElement |
| 59 |
): string | undefined { |
| 60 |
return getExtendedElementDefinition(element.name)?.blockName; |
| 61 |
} |
| 62 |
|