Skip to content

Commit

Permalink
refactor(grid): merge field model from state to grid
Browse files Browse the repository at this point in the history
  • Loading branch information
minlovehua committed Dec 31, 2024
1 parent bc5d894 commit 4127696
Show file tree
Hide file tree
Showing 30 changed files with 374 additions and 461 deletions.
1 change: 1 addition & 0 deletions packages/grid/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './field';
export * from './grid';
export * from './layout';
export * from './row';
export * from './view';
24 changes: 24 additions & 0 deletions packages/grid/src/types/view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Id } from 'ngx-tethys/types';

export enum AITableFilterOperation {
eq = 'eq',
gte = 'gte',
lte = 'lte',
gt = 'gt',
lt = 'lt',
in = 'in',
contain = 'contain',
ne = 'ne',
nin = 'nin',
between = 'between',
besides = 'besides',
empty = 'empty',
exists = 'exists',
notContain = 'not_contain'
}

export interface AITableFilterCondition<TValue = unknown> {
field_id: Id;
operation: AITableFilterOperation;
value: TValue;
}
5 changes: 0 additions & 5 deletions packages/grid/src/utils/field/field.ts

This file was deleted.

23 changes: 0 additions & 23 deletions packages/grid/src/utils/field/index.ts

This file was deleted.

13 changes: 0 additions & 13 deletions packages/grid/src/utils/field/link.ts

This file was deleted.

20 changes: 0 additions & 20 deletions packages/grid/src/utils/field/member.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Field } from './field';
import { DateFieldValue } from '@ai-table/grid';
import { fromUnixTime, subDays } from 'date-fns';
import { isArray, TinyDate } from 'ngx-tethys/util';
import { AITableFilterCondition, AITableFilterOperation } from '../../../types'
import { isEmpty } from '../../common';
import { isArray, isEmpty, TinyDate } from 'ngx-tethys/util';
import { Field } from './field';
import { AITableFilterCondition, AITableFilterOperation } from '../../../types';
import { DateFieldValue } from '../../../core';
import { compareNumber } from '../operate';

export class DateField extends Field {
override isMeetFilter(condition: AITableFilterCondition<string>, cellValue: DateFieldValue) {
Expand All @@ -26,6 +26,12 @@ export class DateField extends Field {
}
}

override compare(cellValue1: DateFieldValue, cellValue2: DateFieldValue): number {
const value1 = cellValueToSortValue(cellValue1);
const value2 = cellValueToSortValue(cellValue2);
return compareNumber(value1, value2);
}

getTimeRange(value: string | number | number[]) {
switch (value) {
case 'today':
Expand Down Expand Up @@ -55,26 +61,8 @@ export class DateField extends Field {
];
}
}
}

cellValueToString(_cellValue: DateFieldValue): string | null {
return null;
}

static _compare(cellValue1: DateFieldValue, cellValue2: DateFieldValue): number {
if (isEmpty(cellValue1?.timestamp) && isEmpty(cellValue2?.timestamp)) {
return 0;
}
if (isEmpty(cellValue1?.timestamp)) {
return -1;
}
if (isEmpty(cellValue2?.timestamp)) {
return 1;
}

return cellValue1.timestamp === cellValue2.timestamp ? 0 : cellValue1.timestamp > cellValue2.timestamp ? 1 : -1;
}

override compare(cellValue1: DateFieldValue, cellValue2: DateFieldValue): number {
return DateField._compare(cellValue1, cellValue2);
}
function cellValueToSortValue(cellValue: DateFieldValue): number {
return cellValue?.timestamp;
}
44 changes: 44 additions & 0 deletions packages/grid/src/utils/field/model/field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { AITableFilterCondition, AITableFilterOperation, AITableReferences } from '../../../types';
import { AITableField, FieldValue } from '../../../core';
import { isEmpty } from '../../common';

export abstract class Field {
// 排序
abstract compare(cellValue1: FieldValue, cellValue2: FieldValue, field: AITableField, references?: AITableReferences): number;

// 筛选
isMeetFilter(condition: AITableFilterCondition, cellValue: FieldValue) {
switch (condition.operation) {
case AITableFilterOperation.empty:
case AITableFilterOperation.exists: {
return this.isEmptyOrNot(condition.operation, cellValue);
}
default: {
return true;
}
}
}

// 查找
cellFullText(transformValue: any, field: AITableField, references?: AITableReferences): string[] {
let fullText: string[] = [];
if (!isEmpty(transformValue)) {
fullText.push(String(transformValue));
}
return fullText;
}

isEmptyOrNot(operation: AITableFilterOperation.empty | AITableFilterOperation.exists, cellValue: FieldValue) {
switch (operation) {
case AITableFilterOperation.empty: {
return isEmpty(cellValue);
}
case AITableFilterOperation.exists: {
return !isEmpty(cellValue);
}
default: {
throw new Error('compare operator type error');
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { AITableFieldType } from '@ai-table/grid';
import { Field } from './field';
import { TextField } from './text';
import { SelectField } from './select';
import { AITableFieldType } from '../../../core';
import { DateField } from './date';
import { NumberField } from './number';
import { RateField } from './rate';
import { Field } from './field';

import { LinkField } from './link';
import { MemberField } from './member';
import { NumberField } from './number';
import { ProgressField } from './progress';
import { RateField } from './rate';
import { SelectField } from './select';
import { TextField } from './text';

export const ViewOperationMap: Record<AITableFieldType, Field> = {
[AITableFieldType.text]: new TextField(),
Expand All @@ -19,7 +21,7 @@ export const ViewOperationMap: Record<AITableFieldType, Field> = {
[AITableFieldType.rate]: new RateField(),
[AITableFieldType.link]: new LinkField(),
[AITableFieldType.member]: new MemberField(),
[AITableFieldType.progress]: new NumberField(),
[AITableFieldType.progress]: new ProgressField(),
[AITableFieldType.createdBy]: new MemberField(),
[AITableFieldType.updatedBy]: new MemberField()
};
37 changes: 37 additions & 0 deletions packages/grid/src/utils/field/model/link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { FieldValue, LinkFieldValue } from '../../../core';
import { AITableFilterCondition, AITableFilterOperation } from '../../../types';
import { isEmpty } from '../../common';
import { compareString, stringInclude } from '../operate';
import { Field } from './field';

export class LinkField extends Field {
override isMeetFilter(condition: AITableFilterCondition<string>, cellValue: FieldValue) {
const cellTextValue = cellValue?.text;
switch (condition.operation) {
case AITableFilterOperation.empty:
return isEmpty(cellTextValue);
case AITableFilterOperation.exists:
return !isEmpty(cellTextValue);
case AITableFilterOperation.contain:
return !isEmpty(cellTextValue) && stringInclude(cellTextValue, condition.value);
default:
return super.isMeetFilter(condition, cellTextValue);
}
}

override compare(cellValue1: FieldValue, cellValue2: FieldValue): number {
return compareString(cellValueToSortValue(cellValue1), cellValueToSortValue(cellValue2));
}

override cellFullText(transformValue: LinkFieldValue): string[] {
let texts: string[] = [];
if (!isEmpty(transformValue?.text)) {
texts.push(transformValue.text);
}
return texts;
}
}

function cellValueToSortValue(cellValue: LinkFieldValue): string | null {
return (cellValue && cellValue.text && cellValue.text.trim()) || null;
}
60 changes: 60 additions & 0 deletions packages/grid/src/utils/field/model/member.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { AITableField, FieldValue, MemberFieldValue } from '../../../core';
import { AITableFilterCondition, AITableFilterOperation, AITableReferences } from '../../../types';
import { isEmpty } from '../../common';
import { compareString, hasIntersect } from '../operate';
import { Field } from './field';

export class MemberField extends Field {
override isMeetFilter(condition: AITableFilterCondition<string>, cellValue: MemberFieldValue) {
switch (condition.operation) {
case AITableFilterOperation.empty:
return isEmpty(cellValue);
case AITableFilterOperation.exists:
return !isEmpty(cellValue);
case AITableFilterOperation.in:
return Array.isArray(condition.value) && hasIntersect(cellValue, condition.value);
case AITableFilterOperation.nin:
return Array.isArray(condition.value) && !hasIntersect(cellValue, condition.value);
default:
return super.isMeetFilter(condition, cellValue);
}
}

override compare(cellValue1: FieldValue, cellValue2: FieldValue, field: AITableField, references: AITableReferences): number {
const value1 = cellValueToSortValue(cellValue1, field, references);
const value2 = cellValueToSortValue(cellValue2, field, references);
return compareString(value1, value2);
}

override cellFullText(transformValue: string[], field: AITableField, references?: AITableReferences): string[] {
let fullText: string[] = [];
if (transformValue?.length && references) {
for (let index = 0; index < transformValue.length; index++) {
const userInfo = references?.members[transformValue[index]];
if (!userInfo) {
continue;
}
if (userInfo.display_name) {
fullText.push(userInfo.display_name);
}
}
}
return fullText;
}
}

function cellValueToSortValue(cellValue: MemberFieldValue, field: AITableField, references: AITableReferences): string | null {
let names: string[] = [];
if (cellValue?.length && references) {
for (let index = 0; index < cellValue.length; index++) {
const userInfo = references?.members[cellValue[index]];
if (!userInfo) {
continue;
}
if (userInfo.display_name_pinyin) {
names.push(userInfo.display_name_pinyin);
}
}
}
return names && names.length ? names.join(', ') : null;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { FieldValue } from '../../../core';
import { AITableFilterCondition, AITableFilterOperation } from '../../../types';
import { Field } from './field';
import { FieldValue } from '@ai-table/grid';
import { isEmpty } from '../../common';
import { compareNumber } from '../operate';
import { Field } from './field';

export class NumberField extends Field {
override isMeetFilter(condition: AITableFilterCondition<number>, cellValue: FieldValue) {
Expand All @@ -27,24 +28,7 @@ export class NumberField extends Field {
}
}

cellValueToString(_cellValue: FieldValue): string | null {
return null;
}

static _compare(cellValue1: number, cellValue2: number): number {
if (isEmpty(cellValue1) && isEmpty(cellValue2)) {
return 0;
}
if (isEmpty(cellValue1)) {
return -1;
}
if (isEmpty(cellValue2)) {
return 1;
}
return cellValue1 === cellValue2 ? 0 : cellValue1 > cellValue2 ? 1 : -1;
}

override compare(cellValue1: number, cellValue2: number): number {
return NumberField._compare(cellValue1, cellValue2);
return compareNumber(cellValue1, cellValue2);
}
}
Loading

0 comments on commit 4127696

Please sign in to comment.