import { InspectorControls, store as blockEditorStore, } from "@wordpress/block-editor"; import { useDispatch, useRegistry, useSelect } from "@wordpress/data"; import { PanelBody, __experimentalToggleGroupControl as ToggleGroupControl, __experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon, } from "@wordpress/components"; import { __ } from "@wordpress/i18n"; import { justifyCenter, justifyLeft, justifyRight } from "@wordpress/icons"; import { ElementAlignment, elementBlockAlignmentAttrs, getElementBlockAlignment, isElementAlignment, } from "../../alignment"; import { buildOccupancy, cellColumn, GridRow } from "../table/grid-model"; const ALIGNMENT_OPTIONS = [ { value: "left", icon: justifyLeft, label: __("Left", "tableberg") }, { value: "center", icon: justifyCenter, label: __("Center", "tableberg") }, { value: "right", icon: justifyRight, label: __("Right", "tableberg") }, ] as const; /** * Elements alignment for the whole column of the selected cell. * * Like the table's "Common Elements Alignment", but for one column: a column * has no block of its own, so this writes `align` on every element block in * every cell the column covers (a merged cell counts once). Each element * keeps its own value afterwards, so one can still be aligned differently. */ export function CellColumnAlignmentControls({ clientId, }: { clientId: string; }) { const { updateBlockAttributes } = useDispatch(blockEditorStore) as any; const registry = useRegistry() as any; const info = useSelect( select => { const be = select(blockEditorStore) as any; const rowClientId = be.getBlockRootClientId(clientId); const tableClientId = rowClientId ? be.getBlockRootClientId(rowClientId) : null; if (!rowClientId || !tableClientId) { return null; } const rows: GridRow[] = ( be.getBlock(tableClientId)?.innerBlocks ?? [] ) .filter((b: any) => b.name === "tableberg/row") .map((rowBlock: any) => rowBlock.innerBlocks .filter((b: any) => b.name === "tableberg/cell") .map((cellBlock: any) => ({ id: cellBlock.clientId, rowSpan: cellBlock.attributes?.span?.rowSpan ?? 1, colSpan: cellBlock.attributes?.span?.colSpan ?? 1, })) ); const column = cellColumn(rows, clientId); if (column === null) { return null; } const occupancy = buildOccupancy(rows); const elementIds: string[] = []; const seen = new Set(); for (let r = 0; r < rows.length; r++) { const cellId = occupancy.matrix[r]?.[column]; if (!cellId || seen.has(cellId)) { continue; } seen.add(cellId); elementIds.push(...be.getBlockOrder(cellId)); } // Shown as selected only when every element agrees. let uniform: ElementAlignment | undefined; for (const id of elementIds) { const align = getElementBlockAlignment(be.getBlock(id)); if (uniform === undefined) { uniform = align; } else if (uniform !== align) { uniform = undefined; break; } } // A string keeps the selector's result stable between renders. return JSON.stringify({ column, elementIds, uniform }); }, [clientId] ); if (!info) { return null; } const { column, elementIds, uniform } = JSON.parse(info) as { column: number; elementIds: string[]; uniform?: ElementAlignment; }; const applyAlignment = (align: ElementAlignment) => { const be = registry.select(blockEditorStore); registry.batch(() => { elementIds.forEach(id => updateBlockAttributes( id, elementBlockAlignmentAttrs(be.getBlock(id), align) ) ); }); }; return ( 0 ? __( "Aligns the elements in every cell of this column.", "tableberg" ) : __( "This column has no elements to align yet.", "tableberg" ) } value={uniform} isBlock onChange={newAlign => { if (isElementAlignment(newAlign)) { applyAlignment(newAlign); } }} // A fresh control per column, so a value picked in one // column never lingers when another cell is selected. key={column} > {ALIGNMENT_OPTIONS.map(({ value, icon, label }) => ( ))} ); }