mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 21:27:20 +00:00
refactor(editor): move database selection into the corresponding view (#9752)
This commit is contained in:
@@ -5,5 +5,4 @@ export * from '../group-by/matcher.js';
|
||||
export type { GroupByConfig } from '../group-by/types.js';
|
||||
export type { GroupRenderProps } from '../group-by/types.js';
|
||||
export * from './css-variable.js';
|
||||
export * from './selection-schema.js';
|
||||
export * from './types.js';
|
||||
|
||||
@@ -1,133 +0,0 @@
|
||||
import { BaseSelection, SelectionExtension } from '@blocksuite/store';
|
||||
import { z } from 'zod';
|
||||
|
||||
import type { DataViewSelection, GetDataViewSelection } from '../types.js';
|
||||
|
||||
const TableViewSelectionSchema = z.union([
|
||||
z.object({
|
||||
viewId: z.string(),
|
||||
type: z.literal('table'),
|
||||
selectionType: z.literal('area'),
|
||||
rowsSelection: z.object({
|
||||
start: z.number(),
|
||||
end: z.number(),
|
||||
}),
|
||||
columnsSelection: z.object({
|
||||
start: z.number(),
|
||||
end: z.number(),
|
||||
}),
|
||||
focus: z.object({
|
||||
rowIndex: z.number(),
|
||||
columnIndex: z.number(),
|
||||
}),
|
||||
isEditing: z.boolean(),
|
||||
}),
|
||||
z.object({
|
||||
viewId: z.string(),
|
||||
type: z.literal('table'),
|
||||
selectionType: z.literal('row'),
|
||||
rows: z.array(
|
||||
z.object({ id: z.string(), groupKey: z.string().optional() })
|
||||
),
|
||||
}),
|
||||
]);
|
||||
|
||||
const KanbanCellSelectionSchema = z.object({
|
||||
selectionType: z.literal('cell'),
|
||||
groupKey: z.string(),
|
||||
cardId: z.string(),
|
||||
columnId: z.string(),
|
||||
isEditing: z.boolean(),
|
||||
});
|
||||
|
||||
const KanbanCardSelectionSchema = z.object({
|
||||
selectionType: z.literal('card'),
|
||||
cards: z.array(
|
||||
z.object({
|
||||
groupKey: z.string(),
|
||||
cardId: z.string(),
|
||||
})
|
||||
),
|
||||
});
|
||||
|
||||
const KanbanGroupSelectionSchema = z.object({
|
||||
selectionType: z.literal('group'),
|
||||
groupKeys: z.array(z.string()),
|
||||
});
|
||||
|
||||
const DatabaseSelectionSchema = z.object({
|
||||
blockId: z.string(),
|
||||
viewSelection: z.union([
|
||||
TableViewSelectionSchema,
|
||||
KanbanCellSelectionSchema,
|
||||
KanbanCardSelectionSchema,
|
||||
KanbanGroupSelectionSchema,
|
||||
]),
|
||||
});
|
||||
|
||||
export class DatabaseSelection extends BaseSelection {
|
||||
static override group = 'note';
|
||||
|
||||
static override type = 'database';
|
||||
|
||||
readonly viewSelection: DataViewSelection;
|
||||
|
||||
get viewId() {
|
||||
return this.viewSelection.viewId;
|
||||
}
|
||||
|
||||
constructor({
|
||||
blockId,
|
||||
viewSelection,
|
||||
}: {
|
||||
blockId: string;
|
||||
viewSelection: DataViewSelection;
|
||||
}) {
|
||||
super({
|
||||
blockId,
|
||||
});
|
||||
|
||||
this.viewSelection = viewSelection;
|
||||
}
|
||||
|
||||
static override fromJSON(json: Record<string, unknown>): DatabaseSelection {
|
||||
DatabaseSelectionSchema.parse(json);
|
||||
return new DatabaseSelection({
|
||||
blockId: json.blockId as string,
|
||||
viewSelection: json.viewSelection as DataViewSelection,
|
||||
});
|
||||
}
|
||||
|
||||
override equals(other: BaseSelection): boolean {
|
||||
if (!(other instanceof DatabaseSelection)) {
|
||||
return false;
|
||||
}
|
||||
return this.blockId === other.blockId;
|
||||
}
|
||||
|
||||
getSelection<T extends DataViewSelection['type']>(
|
||||
type: T
|
||||
): GetDataViewSelection<T> | undefined {
|
||||
return this.viewSelection.type === type
|
||||
? (this.viewSelection as GetDataViewSelection<T>)
|
||||
: undefined;
|
||||
}
|
||||
|
||||
override toJSON(): Record<string, unknown> {
|
||||
return {
|
||||
type: 'database',
|
||||
blockId: this.blockId,
|
||||
viewSelection: this.viewSelection,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface Selection {
|
||||
database: typeof DatabaseSelection;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const DatabaseSelectionExtension = SelectionExtension(DatabaseSelection);
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { KanbanCardSelection } from '../../view-presets';
|
||||
import type { KanbanCard } from '../../view-presets/kanban/pc/card.js';
|
||||
import { KanbanCell } from '../../view-presets/kanban/pc/cell.js';
|
||||
import type { KanbanCardSelection } from '../../view-presets/kanban/types.js';
|
||||
import type { RecordDetail } from './detail.js';
|
||||
import { RecordField } from './field.js';
|
||||
|
||||
|
||||
@@ -1,17 +1,11 @@
|
||||
import type { KanbanViewSelectionWithType } from '../view-presets/kanban/types.js';
|
||||
import type { TableViewSelectionWithType } from '../view-presets/table/types.js';
|
||||
import type {
|
||||
KanbanViewSelectionWithType,
|
||||
TableViewSelectionWithType,
|
||||
} from '../view-presets';
|
||||
|
||||
export type DataViewSelection =
|
||||
| TableViewSelectionWithType
|
||||
| KanbanViewSelectionWithType;
|
||||
export type GetDataViewSelection<
|
||||
K extends DataViewSelection['type'],
|
||||
T = DataViewSelection,
|
||||
> = T extends {
|
||||
type: K;
|
||||
}
|
||||
? T
|
||||
: never;
|
||||
export type DataViewSelectionState = DataViewSelection | undefined;
|
||||
export type PropertyDataUpdater<
|
||||
Data extends Record<string, unknown> = Record<string, unknown>,
|
||||
@@ -20,7 +14,3 @@ export type PropertyDataUpdater<
|
||||
export interface DatabaseFlags {
|
||||
enable_number_formatting: boolean;
|
||||
}
|
||||
|
||||
export const defaultDatabaseFlags: Readonly<DatabaseFlags> = {
|
||||
enable_number_formatting: false,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user