refactor(editor): enable the noUncheckedIndexedAccess rule for the block-database package (#9691)

close: BS-2269
This commit is contained in:
zzj3720
2025-01-14 14:35:46 +00:00
parent aa2a8fbf9b
commit ff295f383f
24 changed files with 324 additions and 344 deletions
@@ -1,6 +1,14 @@
import type { Column, SerializedCells } from '@blocksuite/affine-model';
import type { DeltaInsert } from '@blocksuite/inline';
import type { BlockSnapshot } from '@blocksuite/store';
import { databaseBlockModels } from '../properties/model';
function calculateColumnWidths(rows: string[][]): number[] {
return rows[0].map((_, colIndex) =>
Math.max(...rows.map(row => (row[colIndex] || '').length))
return (
rows[0]?.map((_, colIndex) =>
Math.max(...rows.map(row => (row[colIndex] || '').length))
) ?? []
);
}
@@ -10,7 +18,7 @@ function formatRow(
isHeader: boolean
): string {
const cells = row.map((cell, colIndex) =>
cell.padEnd(columnWidths[colIndex], ' ')
cell?.padEnd(columnWidths[colIndex] ?? 0, ' ')
);
const rowString = `| ${cells.join(' | ')} |`;
return isHeader
@@ -30,3 +38,73 @@ export function formatTable(rows: string[][]): string {
);
return formattedRows.join('\n');
}
export const isDelta = (value: unknown): value is { delta: DeltaInsert[] } => {
if (typeof value === 'object' && value !== null) {
return '$blocksuite:internal:text$' in value;
}
return false;
};
type Table = {
headers: Column[];
rows: Row[];
};
type Row = {
cells: Cell[];
};
type Cell = {
value: string | { delta: DeltaInsert[] };
};
export const processTable = (
columns: Column[],
children: BlockSnapshot[],
cells: SerializedCells
): Table => {
const table: Table = {
headers: columns,
rows: [],
};
children.forEach(v => {
const row: Row = {
cells: [],
};
const title = v.props.text;
if (isDelta(title)) {
row.cells.push({
value: title,
});
} else {
row.cells.push({
value: '',
});
}
columns.forEach(col => {
const property = databaseBlockModels[col.type];
const cell = cells[v.id]?.[col.id];
if (col.type === 'title') {
return;
}
if (!cell || !property) {
row.cells.push({
value: '',
});
return;
}
let value: string | { delta: DeltaInsert[] };
if (isDelta(cell.value)) {
value = cell.value;
} else {
value = property.config.cellToString({
value: cell.value,
data: col.data,
});
}
row.cells.push({
value,
});
});
table.rows.push(row);
});
return table;
};