mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-29 08:09:52 +08:00
refactor(editor): enable the noUncheckedIndexedAccess rule for the block-database package (#9691)
close: BS-2269
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user