| 1 |
import { |
| 2 |
InspectorControls, |
| 3 |
store as blockEditorStore, |
| 4 |
} from "@wordpress/block-editor"; |
| 5 |
import { useDispatch, useSelect } from "@wordpress/data"; |
| 6 |
import { |
| 7 |
PanelBody, |
| 8 |
__experimentalToggleGroupControl as ToggleGroupControl, |
| 9 |
__experimentalToggleGroupControlOption as ToggleGroupControlOption, |
| 10 |
} from "@wordpress/components"; |
| 11 |
import { __ } from "@wordpress/i18n"; |
| 12 |
import { SizeControl } from "@tableberg/components"; |
| 13 |
|
| 14 |
import { ColumnConfig, TableConfig } from "../../attributes"; |
| 15 |
import { cellColumn, GridRow } from "../table/grid-model"; |
| 16 |
|
| 17 |
export const DEFAULT_FIXED_COLUMN_WIDTH = "150px"; |
| 18 |
export const DEFAULT_FIXED_ROW_HEIGHT = "50px"; |
| 19 |
|
| 20 |
/** |
| 21 |
* Column width and row height for the selected cell. |
| 22 |
* |
| 23 |
* These live on the cell because that is what the user selects, but neither |
| 24 |
* value belongs to the cell block: a column width is one entry of the table |
| 25 |
* block's `columns` attribute, and a row height is the parent row block's |
| 26 |
* `height`. Both are written straight to the owning block, which is also |
| 27 |
* where the renderer reads them from. |
| 28 |
*/ |
| 29 |
export function CellDimensionsControls({ clientId }: { clientId: string }) { |
| 30 |
const { updateBlockAttributes } = useDispatch(blockEditorStore) as any; |
| 31 |
|
| 32 |
const info = useSelect( |
| 33 |
select => { |
| 34 |
const be = select(blockEditorStore) as any; |
| 35 |
const rowClientId = be.getBlockRootClientId(clientId); |
| 36 |
const tableClientId = rowClientId |
| 37 |
? be.getBlockRootClientId(rowClientId) |
| 38 |
: null; |
| 39 |
|
| 40 |
if (!rowClientId || !tableClientId) { |
| 41 |
return null; |
| 42 |
} |
| 43 |
|
| 44 |
const tableAttrs = be.getBlockAttributes(tableClientId) ?? {}; |
| 45 |
const columns: ColumnConfig[] = Array.isArray(tableAttrs.columns) |
| 46 |
? tableAttrs.columns |
| 47 |
: []; |
| 48 |
|
| 49 |
// The column index has to account for the spans of earlier cells |
| 50 |
// and rows, so it comes from the same grid walk the table |
| 51 |
// operations use. Reading the grid through `select` keeps it |
| 52 |
// fresh when a column is inserted or removed. |
| 53 |
const rows: GridRow[] = ( |
| 54 |
be.getBlock(tableClientId)?.innerBlocks ?? [] |
| 55 |
) |
| 56 |
.filter((b: any) => b.name === "tableberg/row") |
| 57 |
.map((rowBlock: any) => |
| 58 |
rowBlock.innerBlocks |
| 59 |
.filter((b: any) => b.name === "tableberg/cell") |
| 60 |
.map((cellBlock: any) => ({ |
| 61 |
id: cellBlock.clientId, |
| 62 |
rowSpan: cellBlock.attributes?.span?.rowSpan ?? 1, |
| 63 |
colSpan: cellBlock.attributes?.span?.colSpan ?? 1, |
| 64 |
})) |
| 65 |
); |
| 66 |
|
| 67 |
return { |
| 68 |
rowClientId, |
| 69 |
tableClientId, |
| 70 |
columns, |
| 71 |
column: cellColumn(rows, clientId), |
| 72 |
rowHeight: be.getBlockAttributes(rowClientId)?.height as |
| 73 |
| string |
| 74 |
| undefined, |
| 75 |
fixedColumnWidths: |
| 76 |
(tableAttrs.table as TableConfig | undefined) |
| 77 |
?.fixedColumnWidths ?? true, |
| 78 |
}; |
| 79 |
}, |
| 80 |
[clientId] |
| 81 |
); |
| 82 |
|
| 83 |
if (!info) { |
| 84 |
return null; |
| 85 |
} |
| 86 |
|
| 87 |
const { |
| 88 |
rowClientId, |
| 89 |
tableClientId, |
| 90 |
columns, |
| 91 |
column, |
| 92 |
rowHeight, |
| 93 |
fixedColumnWidths, |
| 94 |
} = info; |
| 95 |
|
| 96 |
const columnWidth = |
| 97 |
column === null ? undefined : columns[column]?.width; |
| 98 |
const columnWidthMode = columnWidth ? "fixed" : "auto"; |
| 99 |
const rowHeightMode = rowHeight ? "fixed" : "auto"; |
| 100 |
|
| 101 |
const setColumnWidth = (width: string | undefined) => { |
| 102 |
if (column === null) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
const next = columns.slice(); |
| 107 |
// A column with no settings left is dropped rather than kept as an |
| 108 |
// empty object, so the attribute stays as small as it was before. |
| 109 |
const { width: _dropped, ...rest } = next[column] ?? {}; |
| 110 |
next[column] = width ? { ...rest, width } : rest; |
| 111 |
|
| 112 |
updateBlockAttributes(tableClientId, { columns: next }); |
| 113 |
}; |
| 114 |
|
| 115 |
const setRowHeight = (height: string | undefined) => { |
| 116 |
// `null`, not `undefined`: updateBlockAttributes ignores undefined. |
| 117 |
updateBlockAttributes(rowClientId, { height: height ?? null }); |
| 118 |
}; |
| 119 |
|
| 120 |
return ( |
| 121 |
<InspectorControls> |
| 122 |
<PanelBody title={__("Column & Row Dimensions", "tableberg")}> |
| 123 |
{column !== null && ( |
| 124 |
<> |
| 125 |
<ToggleGroupControl |
| 126 |
label={__("Width Mode", "tableberg")} |
| 127 |
value={columnWidthMode} |
| 128 |
isBlock |
| 129 |
onChange={value => { |
| 130 |
if (fixedColumnWidths) { |
| 131 |
return; |
| 132 |
} |
| 133 |
setColumnWidth( |
| 134 |
(value || columnWidthMode) === "fixed" |
| 135 |
? columnWidth || |
| 136 |
DEFAULT_FIXED_COLUMN_WIDTH |
| 137 |
: undefined |
| 138 |
); |
| 139 |
}} |
| 140 |
> |
| 141 |
<ToggleGroupControlOption |
| 142 |
value="auto" |
| 143 |
label={__("Auto", "tableberg")} |
| 144 |
disabled={fixedColumnWidths} |
| 145 |
/> |
| 146 |
<ToggleGroupControlOption |
| 147 |
value="fixed" |
| 148 |
label={__("Fixed", "tableberg")} |
| 149 |
disabled={fixedColumnWidths} |
| 150 |
/> |
| 151 |
</ToggleGroupControl> |
| 152 |
|
| 153 |
{columnWidthMode === "fixed" && ( |
| 154 |
<SizeControl |
| 155 |
label={__("Column Width", "tableberg")} |
| 156 |
value={ |
| 157 |
columnWidth || DEFAULT_FIXED_COLUMN_WIDTH |
| 158 |
} |
| 159 |
disabled={fixedColumnWidths} |
| 160 |
onChange={(value: string) => |
| 161 |
setColumnWidth( |
| 162 |
value || DEFAULT_FIXED_COLUMN_WIDTH |
| 163 |
) |
| 164 |
} |
| 165 |
/> |
| 166 |
)} |
| 167 |
|
| 168 |
{fixedColumnWidths && ( |
| 169 |
<p style={{ marginBottom: 0, color: "#757575" }}> |
| 170 |
{__( |
| 171 |
"Turn off 'Equal width columns' in the table settings to set a width for this column.", |
| 172 |
"tableberg" |
| 173 |
)} |
| 174 |
</p> |
| 175 |
)} |
| 176 |
|
| 177 |
<div style={{ marginTop: "16px" }} /> |
| 178 |
</> |
| 179 |
)} |
| 180 |
|
| 181 |
<ToggleGroupControl |
| 182 |
label={__("Height Mode", "tableberg")} |
| 183 |
value={rowHeightMode} |
| 184 |
isBlock |
| 185 |
onChange={value => |
| 186 |
setRowHeight( |
| 187 |
(value || rowHeightMode) === "fixed" |
| 188 |
? rowHeight || DEFAULT_FIXED_ROW_HEIGHT |
| 189 |
: undefined |
| 190 |
) |
| 191 |
} |
| 192 |
> |
| 193 |
<ToggleGroupControlOption |
| 194 |
value="auto" |
| 195 |
label={__("Auto", "tableberg")} |
| 196 |
/> |
| 197 |
<ToggleGroupControlOption |
| 198 |
value="fixed" |
| 199 |
label={__("Fixed", "tableberg")} |
| 200 |
/> |
| 201 |
</ToggleGroupControl> |
| 202 |
|
| 203 |
{rowHeightMode === "fixed" && ( |
| 204 |
<SizeControl |
| 205 |
label={__("Row Height", "tableberg")} |
| 206 |
value={rowHeight || DEFAULT_FIXED_ROW_HEIGHT} |
| 207 |
onChange={(value: string) => |
| 208 |
setRowHeight(value || DEFAULT_FIXED_ROW_HEIGHT) |
| 209 |
} |
| 210 |
/> |
| 211 |
)} |
| 212 |
</PanelBody> |
| 213 |
</InspectorControls> |
| 214 |
); |
| 215 |
} |
| 216 |
|