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(core): support move_record and move_field #WIK-16196 #24

Merged
merged 5 commits into from
Jul 31, 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
21 changes: 20 additions & 1 deletion packages/grid/src/core/action/field.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { ActionName, AddFieldAction, AIFieldPath, AITable, AITableField, RemoveFieldAction, SetFieldAction } from '../types';
import {
ActionName,
AddFieldAction,
AIFieldPath,
AITable,
AITableField,
MoveFieldAction,
RemoveFieldAction,
SetFieldAction
} from '../types';
import { AITableQueries } from '../utils';

export function addField(aiTable: AITable, field: AITableField, path: AIFieldPath) {
Expand All @@ -10,6 +19,15 @@ export function addField(aiTable: AITable, field: AITableField, path: AIFieldPat
aiTable.apply(operation);
}

export function moveField(aiTable: AITable, path: AIFieldPath, newPath: AIFieldPath) {
const operation: MoveFieldAction = {
type: ActionName.MoveField,
path,
newPath
};
aiTable.apply(operation);
}

export function removeField(aiTable: AITable, path: AIFieldPath) {
const operation: RemoveFieldAction = {
type: ActionName.RemoveField,
Expand Down Expand Up @@ -45,6 +63,7 @@ export function setField(aiTable: AITable, value: Partial<AITableField>, path: A

export const FieldActions = {
addField,
moveField,
removeField,
setField
};
20 changes: 20 additions & 0 deletions packages/grid/src/core/action/general.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ActionName, AITable, AITableAction, AITableFields, AITableRecords } from '../types';
import { createDraft, finishDraft } from 'immer';
import { isPathEqual } from '../utils';

const apply = (aiTable: AITable, records: AITableRecords, fields: AITableFields, options: AITableAction) => {
switch (options.type) {
Expand Down Expand Up @@ -36,6 +37,25 @@ const apply = (aiTable: AITable, records: AITableRecords, fields: AITableFields,

break;
}
case ActionName.MoveRecord: {
if (isPathEqual(options.path, options.newPath)) {
return;
}
const record = records[options.path[0]];
records.splice(options.path[0], 1);
records.splice(options.newPath[0], 0, record);
break;
}
case ActionName.MoveField: {
if (isPathEqual(options.path, options.newPath)) {
return;
}
const field = fields[options.path[0]];
fields.splice(options.path[0], 1);
fields.splice(options.newPath[0], 0, field);

break;
}
case ActionName.RemoveField: {
const [fieldIndex] = options.path;
if (fieldIndex > -1) {
Expand Down
11 changes: 11 additions & 0 deletions packages/grid/src/core/action/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AITable,
AITableRecord,
AIFieldValuePath,
MoveRecordAction,
RemoveRecordAction
} from '../types';
import { AITableQueries } from '../utils';
Expand All @@ -32,6 +33,15 @@ export function addRecord(aiTable: AITable, record: AITableRecord, path: AIRecor
aiTable.apply(operation);
}

export function moveRecord(aiTable: AITable, path: AIRecordPath, newPath: AIRecordPath) {
const operation: MoveRecordAction = {
type: ActionName.MoveRecord,
path,
newPath
};
aiTable.apply(operation);
}

export function removeRecord(aiTable: AITable, path: AIRecordPath) {
const operation: RemoveRecordAction = {
type: ActionName.RemoveRecord,
Expand All @@ -43,5 +53,6 @@ export function removeRecord(aiTable: AITable, path: AIRecordPath) {
export const RecordActions = {
addRecord,
updateFieldValue,
moveRecord,
removeRecord
};
19 changes: 17 additions & 2 deletions packages/grid/src/core/types/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export enum ActionName {
UpdateFieldValue = 'update_field_value',
AddRecord = 'add_record',
AddField = 'add_field',
MoveField = 'move_field',
MoveRecord = 'move_record',
RemoveField = 'remove_field',
RemoveRecord = 'remove_record',
SetField = 'set_field'
Expand Down Expand Up @@ -42,6 +44,18 @@ export type AddFieldAction = {
field: AITableField;
};

export type MoveFieldAction = {
type: ActionName.MoveField;
path: AIFieldPath;
newPath: AIFieldPath;
};

export type MoveRecordAction = {
type: ActionName.MoveRecord;
path: AIRecordPath;
newPath: AIRecordPath;
};

export type RemoveFieldAction = {
type: ActionName.RemoveField;
path: AIFieldPath;
Expand All @@ -52,7 +66,6 @@ export type RemoveRecordAction = {
path: AIRecordPath;
};


export type SetFieldAction = {
type: ActionName.SetField;
path: AIFieldPath;
Expand All @@ -66,4 +79,6 @@ export type AITableAction =
| AddFieldAction
| RemoveRecordAction
| RemoveFieldAction
| SetFieldAction;
| SetFieldAction
| MoveFieldAction
| MoveRecordAction;
2 changes: 1 addition & 1 deletion packages/grid/src/core/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './core';
export * from './action';
export * from './action';
6 changes: 5 additions & 1 deletion packages/grid/src/core/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Actions } from '../action';
import { AITable, AITableAction, AITableFields, AITableRecords } from '../types';
import { AITable, AITableAction, AITableFields, AITableRecords, Path } from '../types';
import { FLUSHING } from './weak-map';
import { WritableSignal, signal } from '@angular/core';

Expand Down Expand Up @@ -30,3 +30,7 @@ export function createAITable(records: WritableSignal<AITableRecords>, fields: W
};
return aiTable;
}

export function isPathEqual(path: Path, another: Path): boolean {
return path.length === another.length && path.every((n, i) => n === another[i]);
}
10 changes: 10 additions & 0 deletions packages/grid/src/core/utils/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,15 @@ export const AITableQueries = {
throw new Error(`path does not exist as path [${path}]`);
}
return aiTable.fields()[path[0]];
},

getRecord(aiTable: AITable, path: AIRecordPath): AITableRecord {
if (!aiTable) {
throw new Error(`aiTable does not exist`);
}
if (!path) {
throw new Error(`path does not exist as path [${path}]`);
}
return aiTable.records()[path[0]];
}
};
3 changes: 3 additions & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<thy-option *ngFor="let option of listOfOption" [thyValue]="option.value" [thyLabelText]="option.text"> </thy-option>
</thy-select> -->
<a thyAction class="mb-2" thyIcon="trash" href="javascript:;" (click)="removeRecord()">删除行</a>
<a thyAction class="mb-2" thyIcon="move" href="javascript:;" (click)="moveRecord()">移动选中行到第三行</a>
<a thyAction class="mb-2" thyIcon="move" href="javascript:;" (click)="moveField()">移动选中列到第三列</a>
<ai-table-grid
[(aiRecords)]="records"
[(aiFields)]="fields"
Expand All @@ -11,3 +13,4 @@
[aiPlugins]="plugins"
(aiTableInitialized)="aiTableInitialized($event)"
></ai-table-grid>

39 changes: 38 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {
EditFieldPropertyItem,
DividerMenuItem,
RemoveFieldItem,
Actions
Actions,
AIRecordPath,
AITableRecord,
AITableQueries,
AIFieldPath
} from '@ai-table/grid';
import { ThyIconRegistry } from 'ngx-tethys/icon';
import { ThyPopover, ThyPopoverModule } from 'ngx-tethys/popover';
Expand Down Expand Up @@ -294,6 +298,39 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
}
}

moveField() {
const newIndex = 2;
const selectedFieldIds = [...this.aiTable.selection().selectedFields.keys()];
const selectedRecords = this.aiTable.fields().filter((item) => selectedFieldIds.includes(item.id));
selectedRecords.forEach((item) => {
const path = AITableQueries.findPath(this.aiTable, item) as AIFieldPath;
Actions.moveField(this.aiTable, path, [newIndex]);
});
}

moveRecord() {
const selectedRecordIds = [...this.aiTable.selection().selectedRecords.keys()];
const selectedRecords = this.aiTable.records().filter((item) => selectedRecordIds.includes(item.id));
const selectedRecordsAfterNewPath: AITableRecord[] = [];
let offset = 0;
const newIndex = 2;
selectedRecords.forEach((item) => {
const path = AITableQueries.findPath(this.aiTable, undefined, item) as AIRecordPath;
if (path[0] < newIndex) {
Actions.moveRecord(this.aiTable, path, [newIndex]);
offset = 1;
} else {
selectedRecordsAfterNewPath.push(item);
}
});

selectedRecordsAfterNewPath.reverse().forEach((item) => {
const newPath = [newIndex + offset] as AIRecordPath;
const path = AITableQueries.findPath(this.aiTable, undefined, item) as AIRecordPath;
Actions.moveRecord(this.aiTable, path, newPath);
});
}

ngOnDestroy(): void {
this.disconnect();
}
Expand Down