Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(database): add more cell conversions #7135

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/blocks/src/database-block/columns/rich-text/define.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { nanoid, Text } from '@blocksuite/store';

import { clamp } from '../../../_common/utils/math.js';
import { columnType } from '../../data-view/column/column-config.js';
import { tRichText } from '../../data-view/logical/data-type.js';
import { getTagColor } from '../../data-view/utils/tags/colors.js';
Expand Down Expand Up @@ -37,6 +38,7 @@ export const richTextColumnModelConfig =
};
},
});

richTextColumnModelConfig.addConvert('select', (_column, cells) => {
const options: Record<string, SelectTag> = {};
const getTag = (name: string) => {
Expand Down Expand Up @@ -93,3 +95,33 @@ richTextColumnModelConfig.addConvert('multi-select', (_column, cells) => {
},
};
});

richTextColumnModelConfig.addConvert('number', (_column, cells) => {
return {
column: { decimal: 0 },
cells: cells.map(v => {
const num = v ? parseFloat(v.toString()) : NaN;
return isNaN(num) ? undefined : num;
}),
};
});

richTextColumnModelConfig.addConvert('progress', (_column, cells) => {
return {
column: {},
cells: cells.map(v => {
const progress = v ? parseInt(v.toString()) : NaN;
return !isNaN(progress) ? clamp(progress, 0, 100) : undefined;
}),
};
});

richTextColumnModelConfig.addConvert('checkbox', (_column, cells) => {
const truthyValues = ['yes', 'true'];
return {
column: {},
cells: cells.map(v =>
v && truthyValues.includes(v.toString().toLowerCase()) ? true : undefined
),
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ declare global {
export interface ColumnConfigMap {}
}

export type GetColumnDataFromConfig<T> =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type GetColumnDataFromConfig<T extends ColumnConfig<any, any, any>> =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
T extends ColumnConfig<infer _, infer R, any> ? R : never;
export type GetCellDataFromConfig<T> =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Text } from '@blocksuite/store';

import { tBoolean } from '../../../logical/data-type.js';
import { columnType } from '../../column-config.js';

export const chceckboxColumnType = columnType('checkbox');
export const checkboxColumnType = columnType('checkbox');
declare global {
interface ColumnConfigMap {
[chceckboxColumnType.type]: typeof checkboxColumnModelConfig;
[checkboxColumnType.type]: typeof checkboxColumnModelConfig.model;
}
}

export const checkboxColumnModelConfig =
chceckboxColumnType.modelConfig<boolean>({
checkboxColumnType.modelConfig<boolean>({
name: 'Checkbox',
type: () => tBoolean.create(),
defaultData: () => ({}),
Expand All @@ -21,3 +23,10 @@ export const checkboxColumnModelConfig =
},
cellToJson: data => data ?? null,
});

checkboxColumnModelConfig.addConvert('rich-text', (_columns, cells) => {
return {
column: {},
cells: cells.map(v => new Text(v ? 'Yes' : 'No').yText),
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { columnType } from '../../column-config.js';
export const dateColumnType = columnType('date');
declare global {
interface ColumnConfigMap {
[dateColumnType.type]: typeof dateColumnModelConfig;
[dateColumnType.type]: typeof dateColumnModelConfig.model;
}
}
export const dateColumnModelConfig = dateColumnType.modelConfig<number>({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Text } from '@blocksuite/store';

import { clamp } from '../../../../../_common/utils/math.js';
import { tNumber } from '../../../logical/data-type.js';
import { columnType } from '../../column-config.js';

Expand Down Expand Up @@ -28,7 +29,13 @@ export const numberColumnModelConfig = numberColumnType.modelConfig<
},
cellToJson: data => data ?? null,
});

numberColumnModelConfig.addConvert('rich-text', (_column, cells) => ({
column: {},
cells: cells.map(v => new Text(v?.toString()).yText),
}));

numberColumnModelConfig.addConvert('progress', (_column, cells) => ({
column: {},
cells: cells.map(v => clamp(v ?? 0, 0, 100)),
}));
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ export const progressColumnModelConfig = progressColumnType.modelConfig<number>(
cellToJson: data => data ?? null,
}
);

progressColumnModelConfig.addConvert('rich-text', (_column, cells) => ({
column: {},
cells: cells.map(v => new Text(v?.toString()).yText),
}));

progressColumnModelConfig.addConvert('number', (_column, cells) => ({
column: { decimal: 0 },
golok727 marked this conversation as resolved.
Show resolved Hide resolved
cells: cells.map(v => v),
}));
39 changes: 38 additions & 1 deletion tests/database/column.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ test.describe('switch column type', () => {

await switchColumnType(page, 'Number');
await assertDatabaseCellNumber(page, {
text: '',
text: '123',
});
});

Expand Down Expand Up @@ -322,6 +322,43 @@ test.describe('switch column type', () => {
await expect(checkbox).not.toHaveClass('checked');
});

test('checkbox to text', async ({ page }) => {
await enterPlaygroundRoom(page);
await initEmptyDatabaseState(page);

await initDatabaseColumn(page);
await initDatabaseDynamicRowWithData(page, '', true);
await pressEscape(page);
await switchColumnType(page, 'Checkbox');

let checkbox = getFirstColumnCell(page, 'checkbox');
await expect(checkbox).not.toHaveClass('checked');

// checked
await checkbox.click();
await switchColumnType(page, 'Text');
await clickDatabaseOutside(page);
await waitNextFrame(page, 100);
await assertDatabaseCellRichTexts(page, { text: 'Yes' });
await clickDatabaseOutside(page);
await waitNextFrame(page, 100);
await switchColumnType(page, 'Checkbox');
checkbox = getFirstColumnCell(page, 'checkbox');
await expect(checkbox).toHaveClass(/checked/);

// not checked
await checkbox.click();
await switchColumnType(page, 'Text');
await clickDatabaseOutside(page);
await waitNextFrame(page, 100);
await assertDatabaseCellRichTexts(page, { text: 'No' });
await clickDatabaseOutside(page);
await waitNextFrame(page, 100);
await switchColumnType(page, 'Checkbox');
checkbox = getFirstColumnCell(page, 'checkbox');
await expect(checkbox).not.toHaveClass('checked');
});

test('switch to progress', async ({ page }) => {
await enterPlaygroundRoom(page);
await initEmptyDatabaseState(page);
Expand Down
Loading