Skip to content

Commit

Permalink
refactor(core): rename and modify type (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
pubuzhixing8 authored Jan 6, 2025
1 parent a34bbde commit 143233c
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<a
thyDropdownMenuItem
href="javascript:;"
[ngClass]="{ 'ai-table-prohibit-clear-selection remove-record': !disabled }"
[ngClass]="{ 'ai-table-prevent-clear-selection remove-record': !disabled }"
(click)="execute(menu)"
[thyDisabled]="disabled"
>
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/src/constants/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const AI_TABLE_FIELD_ADD_BUTTON_WIDTH = 100; // 添加列宽度
export const AI_TABLE_FIELD_HEAD_ICON_GAP_SIZE = 8; // 字段表列头图标的间距
export const AI_TABLE_FIELD_HEAD_MORE = 'AI_TABLE_FIELD_HEAD_MORE'; // 更多图标名称

export const AI_TABLE_PROHIBIT_CLEAR_SELECTION_CLASS = '.ai-table-prohibit-clear-selection';
export const AI_TABLE_PREVENT_CLEAR_SELECTION_CLASS = '.ai-table-prevent-clear-selection';

export const AI_TABLE_ICON_COMMON_SIZE = 16; // 表格图标的通用尺寸
export const AI_TABLE_CELL = 'AI_TABLE_CELL'; // 单元格标识
Expand Down
10 changes: 5 additions & 5 deletions packages/grid/src/core/types/ai-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Signal, WritableSignal } from '@angular/core';
import { Colors } from '../../constants/colors';
import { AITableReferences, AITableSelection } from '../../types';
import { RendererContext } from '../context';
import { AIRecordFieldPosition, AITableField, AITableFields, AITableRecord, AITableRecords } from './core';
import { AIRecordFieldIdPath, AITableField, AITableFields, AITableRecord, AITableRecords } from './core';

export interface AITable {
records: WritableSignal<AITableRecords>;
Expand All @@ -29,10 +29,10 @@ export const AITable = {
getVisibleRows(aiTable: AITable): AITableRecords {
return aiTable.records();
},
getActiveCell(aiTable: AITable): AIRecordFieldPosition | null {
getActiveCell(aiTable: AITable): AIRecordFieldIdPath | null {
return aiTable.selection().activeCell;
},
getSelectedRecordIds(aiTable: AITable): string[] {
getActiveRecordIds(aiTable: AITable): string[] {
const selectedRecords = aiTable.selection().selectedRecords;
const selectedCells = aiTable.selection().selectedCells;
let selectedRecordIds: string[] = [];
Expand All @@ -45,7 +45,7 @@ export const AITable = {
}
return selectedRecordIds;
},
isCellVisible(aiTable: AITable, cell: AIRecordFieldPosition): boolean {
isCellVisible(aiTable: AITable, cell: AIRecordFieldIdPath): boolean {
const visibleRowIndexMap = aiTable.context!.visibleRowsIndexMap();
const visibleColumnIndexMap = aiTable.context!.visibleColumnsMap();
let isVisible = false;
Expand All @@ -55,7 +55,7 @@ export const AITable = {
}
return isVisible;
},
getCellIndex(aiTable: AITable, cell: AIRecordFieldPosition): { rowIndex: number; columnIndex: number } | null {
getCellIndex(aiTable: AITable, cell: AIRecordFieldIdPath): { rowIndex: number; columnIndex: number } | null {
const visibleRowIndexMap = aiTable.context!.visibleRowsIndexMap();
const visibleColumnIndexMap = aiTable.context!.visibleColumnsMap();
if (AITable.isCellVisible(aiTable, cell)) {
Expand Down
8 changes: 3 additions & 5 deletions packages/grid/src/core/types/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export interface AddFieldOptions {

export interface UpdateFieldValueOptions<T = unknown> {
value: T;
path: AIFieldValueIdPath;
path: AIRecordFieldIdPath;
}

export interface SetFieldOptions<T = unknown> {
Expand All @@ -193,8 +193,6 @@ export type NumberPath = [number];

export type IdPath = [string];

export type AIFieldValueIdPath = [string, string];
export type AIRecordFieldIdPath = [string, string];

export type AIRecordFieldPosition = [string, string];

export type Path = NumberPath | IdPath | AIFieldValueIdPath;
export type Path = NumberPath | IdPath | AIRecordFieldIdPath;
4 changes: 2 additions & 2 deletions packages/grid/src/core/utils/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
AITable,
AITableField,
AITableRecord,
AIFieldValueIdPath,
AIRecordFieldIdPath,
IdPath,
NumberPath,
AITableFieldType,
Expand All @@ -28,7 +28,7 @@ export const AITableQueries = {
}
throw new Error(`can not find the field path: ${JSON.stringify({ ...(field || {}) })}`);
},
getFieldValue(aiTable: AITable, path: AIFieldValueIdPath): any {
getFieldValue(aiTable: AITable, path: AIRecordFieldIdPath): any {
if (!aiTable) {
throw new Error(`aiTable does not exist`);
}
Expand Down
34 changes: 17 additions & 17 deletions packages/grid/src/grid.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
AI_TABLE_FIELD_HEAD_HEIGHT,
AI_TABLE_FIELD_HEAD_MORE,
AI_TABLE_FIELD_HEAD_SELECT_CHECKBOX,
AI_TABLE_PROHIBIT_CLEAR_SELECTION_CLASS,
AI_TABLE_PREVENT_CLEAR_SELECTION_CLASS,
AI_TABLE_ROW_ADD_BUTTON,
AI_TABLE_ROW_HEAD_WIDTH,
AI_TABLE_ROW_SELECT_CHECKBOX,
Expand All @@ -34,7 +34,7 @@ import {
DEFAULT_SCROLL_STATE,
MOUSEOVER_EDIT_TYPE
} from './constants';
import { AIRecordFieldPosition, AITable, Coordinate, RendererContext, UpdateFieldValueOptions } from './core';
import { AIRecordFieldIdPath, AITable, Coordinate, RendererContext, UpdateFieldValueOptions } from './core';
import { AITableGridBase } from './grid-base.component';
import { AITableRenderer } from './renderer/renderer.component';
import { AITableGridEventService } from './services/event.service';
Expand All @@ -59,9 +59,9 @@ import { AITableGridMatchCellService } from './services/match-cell.service';
export class AITableGrid extends AITableGridBase implements OnInit, OnDestroy {
private viewContainerRef = inject(ViewContainerRef);

private isSelecting = false;
private isDragSelecting = false;

private selectionStart: AIRecordFieldPosition | null = null;
private dragSelectionStart: AIRecordFieldIdPath | null = null;

timer!: number | null;

Expand Down Expand Up @@ -203,11 +203,11 @@ export class AITableGrid extends AITableGridBase implements OnInit, OnDestroy {
context!.setPointPosition(curMousePosition);
this.timer = null;

if (this.isSelecting) {
if (this.isDragSelecting) {
const { fieldId, recordId } = getDetailByTargetName(curMousePosition.realTargetName);
if (fieldId && recordId) {
const startCell = this.selectionStart;
const endCell: AIRecordFieldPosition = [recordId, fieldId];
const startCell = this.dragSelectionStart;
const endCell: AIRecordFieldIdPath = [recordId, fieldId];
if (startCell && !!startCell.length) {
this.aiTableGridSelectionService.selectCells(startCell, endCell);
}
Expand Down Expand Up @@ -239,9 +239,9 @@ export class AITableGrid extends AITableGridBase implements OnInit, OnDestroy {
case AI_TABLE_CELL:
if (!recordId || !fieldId) return;
this.aiTableGridEventService.closeCellEditor();
const selectionStart: AIRecordFieldPosition = [recordId, fieldId];
this.dragSelectionState(true, selectionStart);
this.aiTableGridSelectionService.selectCells(selectionStart);
const dragSelectionStart: AIRecordFieldIdPath = [recordId, fieldId];
this.updateDragSelectionState(true, dragSelectionStart);
this.aiTableGridSelectionService.selectCells(dragSelectionStart);
return;
case AI_TABLE_ROW_ADD_BUTTON:
case AI_TABLE_FIELD_ADD_BUTTON:
Expand All @@ -254,7 +254,7 @@ export class AITableGrid extends AITableGridBase implements OnInit, OnDestroy {
}

stageMouseup(e: KoEventObject<MouseEvent>) {
this.dragSelectionState(false, null);
this.updateDragSelectionState(false, null);
}

stageContextmenu(e: KoEventObject<MouseEvent>) {
Expand Down Expand Up @@ -434,21 +434,21 @@ export class AITableGrid extends AITableGridBase implements OnInit, OnDestroy {
e.target instanceof Element &&
!this.containerElement().contains(e.target) &&
!(
e.target.closest(AI_TABLE_PROHIBIT_CLEAR_SELECTION_CLASS) &&
AITable.getSelectedRecordIds(this.aiTable).length > 0
e.target.closest(AI_TABLE_PREVENT_CLEAR_SELECTION_CLASS) &&
AITable.getActiveRecordIds(this.aiTable).length > 0
)
),
takeUntilDestroyed(this.destroyRef)
)
.subscribe(() => {
this.dragSelectionState(false, null);
this.updateDragSelectionState(false, null);
this.aiTableGridSelectionService.clearSelection();
});
}

private dragSelectionState(isSelecting: boolean, selectionStart: AIRecordFieldPosition | null) {
this.isSelecting = isSelecting;
this.selectionStart = selectionStart;
private updateDragSelectionState(isDragSelecting: boolean, dragSelectionStart: AIRecordFieldIdPath | null) {
this.isDragSelecting = isDragSelecting;
this.dragSelectionStart = dragSelectionStart;
}

private resetScrolling = () => {
Expand Down
14 changes: 7 additions & 7 deletions packages/grid/src/renderer/creations/create-cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
AI_TABLE_ROW_ADD_BUTTON,
DEFAULT_FONT_STYLE
} from '../../constants';
import { AIRecordFieldPosition, AITable, AITableQueries, RendererContext } from '../../core';
import { AIRecordFieldIdPath, AITable, AITableQueries, RendererContext } from '../../core';
import { AITableCellsDrawerConfig, AITableRender, AITableRowType } from '../../types';
import { getCellHorizontalPosition, transformCellValue } from '../../utils';
import { addRowLayout } from '../drawers/add-row-layout-drawer';
Expand Down Expand Up @@ -77,7 +77,7 @@ export const createCells = (config: AITableCellsDrawerConfig) => {
}
case AITableRowType.record: {
const fieldId = field._id;
const cell: AIRecordFieldPosition = [recordId, fieldId];
const cell: AIRecordFieldIdPath = [recordId, fieldId];
let background = getCellBackground(cell, isHover, targetName, aiTable);

recordRowLayout.init({
Expand Down Expand Up @@ -140,7 +140,7 @@ export const createCells = (config: AITableCellsDrawerConfig) => {
}
};

const getCellBackground = (cell: AIRecordFieldPosition, isHover: boolean, targetName: string, aiTable: AITable): string => {
const getCellBackground = (cell: AIRecordFieldIdPath, isHover: boolean, targetName: string, aiTable: AITable): string => {
const colors = AITable.getColors();
const [recordId, fieldId] = cell;
let background = colors.white;
Expand All @@ -164,7 +164,7 @@ const getCellBackground = (cell: AIRecordFieldPosition, isHover: boolean, target
return background;
};

const isActiveCell = (cell: AIRecordFieldPosition, aiTable: AITable): boolean => {
const isActiveCell = (cell: AIRecordFieldIdPath, aiTable: AITable): boolean => {
let isActive = false;
const activeCell = AITable.getActiveCell(aiTable);
if (Array.isArray(activeCell) && !!activeCell.length) {
Expand All @@ -177,7 +177,7 @@ const isActiveCell = (cell: AIRecordFieldPosition, aiTable: AITable): boolean =>
return isActive;
};

const isSiblingActiveCell = (cell: AIRecordFieldPosition, aiTable: AITable): boolean => {
const isSiblingActiveCell = (cell: AIRecordFieldIdPath, aiTable: AITable): boolean => {
let isSiblingCell = false;
const activeCell = AITable.getActiveCell(aiTable);
if (Array.isArray(activeCell) && !!activeCell.length) {
Expand All @@ -193,7 +193,7 @@ const isSiblingActiveCell = (cell: AIRecordFieldPosition, aiTable: AITable): boo
return isSiblingCell;
};

const isMatchedCell = (cell: AIRecordFieldPosition, aiTable: AITable): boolean => {
const isMatchedCell = (cell: AIRecordFieldIdPath, aiTable: AITable): boolean => {
const [recordId, fieldId] = cell;
let matchedCellsMap: { [key: string]: boolean } = {};
aiTable.matchedCells().forEach((key) => {
Expand All @@ -203,7 +203,7 @@ const isMatchedCell = (cell: AIRecordFieldPosition, aiTable: AITable): boolean =
return isMatchedCell;
};

const isSelectedCell = (cell: AIRecordFieldPosition, aiTable: AITable): boolean => {
const isSelectedCell = (cell: AIRecordFieldIdPath, aiTable: AITable): boolean => {
const [recordId, fieldId] = cell;
const selectedCells = aiTable.selection().selectedCells;
const isSelectedCell = selectedCells.has(`${recordId}:${fieldId}`);
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/src/services/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ThyPopover, ThyPopoverRef } from 'ngx-tethys/popover';
import { debounceTime, fromEvent, Subject } from 'rxjs';
import { AbstractEditCellEditor } from '../components';
import { GRID_CELL_EDITOR_MAP } from '../constants';
import { AIRecordFieldPosition, AITable, AITableFieldType } from '../core';
import { AIRecordFieldIdPath, AITable, AITableFieldType } from '../core';
import { AITableContextMenuOptions, AITableGridCellRenderSchema, AITableOpenEditOptions } from '../types';
import { getCellHorizontalPosition, getEditorBoxOffset, getEditorSpace, getHoverEditorBoxOffset, getHoverEditorSpace } from '../utils';
import { AITableContextMenu } from '../components/context-menu/context-menu.component';
Expand Down Expand Up @@ -115,7 +115,7 @@ export class AITableGridEventService {
const { container, coordinate, recordId, fieldId, isHoverEdit } = options;
const { scrollState } = aiTable.context!;
const { rowHeight, columnCount } = coordinate;
const cell: AIRecordFieldPosition = [recordId, fieldId];
const cell: AIRecordFieldIdPath = [recordId, fieldId];
const { rowIndex, columnIndex } = AITable.getCellIndex(aiTable, cell)!;
const originX = coordinate.getColumnOffset(columnIndex);
const originY = coordinate.getRowOffset(rowIndex);
Expand Down
6 changes: 3 additions & 3 deletions packages/grid/src/services/selection.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { AIRecordFieldPosition, AITable } from '../core';
import { AIRecordFieldIdPath, AITable } from '../core';

@Injectable()
export class AITableGridSelectionService {
Expand All @@ -20,7 +20,7 @@ export class AITableGridSelectionService {
});
}

setActiveCell(activeCell: AIRecordFieldPosition) {
setActiveCell(activeCell: AIRecordFieldIdPath) {
this.aiTable.selection().activeCell = activeCell;
}

Expand Down Expand Up @@ -78,7 +78,7 @@ export class AITableGridSelectionService {
}
}

selectCells(startCell: AIRecordFieldPosition, endCell?: AIRecordFieldPosition) {
selectCells(startCell: AIRecordFieldIdPath, endCell?: AIRecordFieldIdPath) {
const [startRecordId, startFieldId] = startCell;

if (
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/src/types/grid.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Signal, WritableSignal } from '@angular/core';
import { Dictionary } from 'ngx-tethys/types';
import {
AIRecordFieldPosition,
AIRecordFieldIdPath,
AITable,
AITableField,
AITableFieldType,
Expand All @@ -28,7 +28,7 @@ export interface AITableSelection {
selectedRecords: Set<string>; // `${recordId}`
selectedFields: Set<string>; // `${fieldId}`
selectedCells: Set<string>; // `${recordId}:${fieldId}`
activeCell: AIRecordFieldPosition | null;
activeCell: AIRecordFieldIdPath | null;
}

export interface AIFieldConfig {
Expand Down
4 changes: 2 additions & 2 deletions packages/state/src/action/record.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AIFieldValueIdPath, AITableQueries, AITableRecord, AITableRecordUpdatedInfo, IdPath, NumberPath } from '@ai-table/grid';
import { AIRecordFieldIdPath, AITableQueries, AITableRecord, AITableRecordUpdatedInfo, IdPath, NumberPath } from '@ai-table/grid';
import {
UpdateFieldValueAction,
ActionName,
Expand All @@ -9,7 +9,7 @@ import {
UpdateSystemFieldValue
} from '../types';

export function updateFieldValue(aiTable: AIViewTable, value: any, path: AIFieldValueIdPath) {
export function updateFieldValue(aiTable: AIViewTable, value: any, path: AIRecordFieldIdPath) {
const oldValue = AITableQueries.getFieldValue(aiTable, path);
const operation: UpdateFieldValueAction = {
type: ActionName.UpdateFieldValue,
Expand Down
2 changes: 1 addition & 1 deletion packages/state/src/constants/context-menu-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const RemoveRecordsItem: AITableContextMenuItem = {
position: { x: number; y: number },
aiTableGridSelectionService: AITableGridSelectionService
) => {
let selectedRecordIds = AITable.getSelectedRecordIds(aiTable);
let selectedRecordIds = AITable.getActiveRecordIds(aiTable);

selectedRecordIds.forEach((id: string) => {
Actions.removeRecord(aiTable as AIViewTable, [id]);
Expand Down
4 changes: 2 additions & 2 deletions packages/state/src/shared/to-table/array-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
getValuesByCustomFieldValues,
SystemFieldIndex
} from '../utils/translate';
import { AIFieldValueIdPath, AITableField, AITableQueries, IdPath, NumberPath } from '@ai-table/grid';
import { AIRecordFieldIdPath, AITableField, AITableQueries, IdPath, NumberPath } from '@ai-table/grid';

export default function translateArrayEvent(aiTable: AIViewTable, sharedType: SharedType, event: Y.YEvent<any>): AITableAction[] {
let offset = 0;
Expand Down Expand Up @@ -124,7 +124,7 @@ export default function translateArrayEvent(aiTable: AIViewTable, sharedType: Sh
} else {
const recordId = getSharedRecordId(sharedRecords, recordIndex);
const fieldId = getSharedMapValueId(sharedFields, fieldIndex);
const path = [recordId, fieldId] as AIFieldValueIdPath;
const path = [recordId, fieldId] as AIRecordFieldIdPath;
const fieldValue = AITableQueries.getFieldValue(aiTable, path);
// To exclude insert triggered by field inserts.
if (fieldValue !== item) {
Expand Down
4 changes: 2 additions & 2 deletions packages/state/src/types/action.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AIFieldValueIdPath, AITableField, AITableRecord, AITableRecordUpdatedInfo, IdPath, NumberPath } from '@ai-table/grid';
import { AIRecordFieldIdPath, AITableField, AITableRecord, AITableRecordUpdatedInfo, IdPath, NumberPath } from '@ai-table/grid';
import { AITableView, AITableViewRecord, Positions, RemovePositions } from './view';

export enum ActionName {
Expand All @@ -25,7 +25,7 @@ export enum ExecuteType {

export type UpdateFieldValueAction = {
type: ActionName.UpdateFieldValue;
path: AIFieldValueIdPath;
path: AIRecordFieldIdPath;
fieldValue: any;
newFieldValue: any;
};
Expand Down

0 comments on commit 143233c

Please sign in to comment.