Files
AFFiNE-Mirror/blocksuite/affine/blocks/table/src/selection-schema.ts
Saul-Mirone 1f45cc5dec refactor(editor): unify directories naming (#11516)
**Directory Structure Changes**

- Renamed multiple block-related directories by removing the "block-" prefix:
  - `block-attachment` → `attachment`
  - `block-bookmark` → `bookmark`
  - `block-callout` → `callout`
  - `block-code` → `code`
  - `block-data-view` → `data-view`
  - `block-database` → `database`
  - `block-divider` → `divider`
  - `block-edgeless-text` → `edgeless-text`
  - `block-embed` → `embed`
2025-04-07 12:34:40 +00:00

111 lines
2.5 KiB
TypeScript

import { BaseSelection, SelectionExtension } from '@blocksuite/store';
import { z } from 'zod';
const TableAreaSelectionSchema = z.object({
type: z.literal('area'),
rowStartIndex: z.number(),
rowEndIndex: z.number(),
columnStartIndex: z.number(),
columnEndIndex: z.number(),
});
export type TableAreaSelection = z.TypeOf<typeof TableAreaSelectionSchema>;
const TableRowSelectionSchema = z.object({
type: z.literal('row'),
rowId: z.string(),
});
const TableColumnSelectionSchema = z.object({
type: z.literal('column'),
columnId: z.string(),
});
const TableSelectionDataSchema = z.union([
TableAreaSelectionSchema,
TableRowSelectionSchema,
TableColumnSelectionSchema,
]);
export type TableSelectionData = z.TypeOf<typeof TableSelectionDataSchema>;
export const TableSelectionData = {
equals(a?: TableSelectionData, b?: TableSelectionData) {
if (a === b) {
return true;
}
if (a == null || b == null) {
return a === b;
}
if (a.type !== b.type) {
return false;
}
if (a.type === 'area' && b.type === 'area') {
return (
a.rowStartIndex === b.rowStartIndex &&
a.rowEndIndex === b.rowEndIndex &&
a.columnStartIndex === b.columnStartIndex &&
a.columnEndIndex === b.columnEndIndex
);
}
if (a.type === 'row' && b.type === 'row') {
return a.rowId === b.rowId;
}
if (a.type === 'column' && b.type === 'column') {
return a.columnId === b.columnId;
}
return false;
},
};
const TableSelectionSchema = z.object({
blockId: z.string(),
data: TableSelectionDataSchema,
});
export class TableSelection extends BaseSelection {
static override group = 'note';
static override type = 'table';
readonly data: TableSelectionData;
constructor({
blockId,
data,
}: {
blockId: string;
data: TableSelectionData;
}) {
super({
blockId,
});
this.data = data;
}
static override fromJSON(json: Record<string, unknown>): TableSelection {
TableSelectionSchema.parse(json);
return new TableSelection({
blockId: json.blockId as string,
data: json.data as TableSelectionData,
});
}
override equals(other: BaseSelection): boolean {
if (!(other instanceof TableSelection)) {
return false;
}
return this.blockId === other.blockId;
}
override toJSON(): Record<string, unknown> {
return {
type: 'table',
blockId: this.blockId,
data: this.data,
};
}
}
export const TableSelectionExtension = SelectionExtension(TableSelection);