Skip to content

Commit

Permalink
refactor(collaboration): move positions to system field values array
Browse files Browse the repository at this point in the history
merge remove_record_position and set_record_position operation #WIK-16688

refactor(collaboration): move positions to system field values array
merge remove_record_position and set_record_position operation #WIK-16688
  • Loading branch information
pubuzhixing8 committed Oct 18, 2024
1 parent 0635a42 commit db8a1df
Show file tree
Hide file tree
Showing 19 changed files with 280 additions and 240 deletions.
34 changes: 21 additions & 13 deletions packages/state/src/action/general.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { ActionName, AITableAction, AITableView, AITableViewField, AITableViewFields, AITableViewRecord, AITableViewRecords, AIViewTable } from '../types';
import {
ActionName,
AITableAction,
AITableView,
AITableViewField,
AITableViewFields,
AITableViewRecord,
AITableViewRecords,
AIViewTable
} from '../types';
import { createDraft, finishDraft } from 'immer';
import { AITableField, AITableFields, getDefaultFieldValue } from '@ai-table/grid';
import { createDefaultPositions, isPathEqual } from '../utils';
Expand Down Expand Up @@ -154,23 +163,22 @@ const apply = (aiTable: AIViewTable, records: AITableViewRecords, fields: AITabl
}
break;
}
case ActionName.AddRecordPosition: {
const { position, path } = action;
case ActionName.SetRecordPositions: {
const { positions, path } = action;
const record = records.find((item) => item._id === path[0]);
if (record) {
record.positions = {
...record.positions,
...position
};
const newPositions = { ...record.positions };
for (const key in positions) {
if (positions[key] === null || positions[key] === undefined) {
delete newPositions[key];
} else {
newPositions[key] = positions[key] as number;
}
}
record.positions = newPositions;
}
break;
}
case ActionName.RemoveRecordPosition: {
const { path } = action;
const record = records.find((item) => item._id === path[1]);
delete record?.positions[path[0]];
break;
}
}
return {
records,
Expand Down
4 changes: 2 additions & 2 deletions packages/state/src/action/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FieldActions } from './field';
import { GeneralActions } from './general';
import { PositionActions } from './position';
import { PositionsActions } from './position';
import { RecordActions } from './record';
import { ViewActions } from './view';

Expand All @@ -9,5 +9,5 @@ export const Actions = {
...RecordActions,
...FieldActions,
...ViewActions,
...PositionActions
...PositionsActions
};
25 changes: 8 additions & 17 deletions packages/state/src/action/position.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import { ActionName, AddRecordPositionAction, AIViewTable, Positions, RemoveRecordPositionAction } from "../types";
import { IdPath } from "@ai-table/grid";
import { ActionName, SetRecordPositionAction, AIViewTable, Positions, RemovePositions } from '../types';
import { IdPath } from '@ai-table/grid';

export function setRecordPosition(aiTable: AIViewTable, position: Positions, path: IdPath) {
const operation: AddRecordPositionAction = {
type: ActionName.AddRecordPosition,
position,
export function setRecordPositions(aiTable: AIViewTable, positions: Positions | RemovePositions, path: IdPath) {
const operation: SetRecordPositionAction = {
type: ActionName.SetRecordPositions,
positions,
path
};
aiTable.apply(operation);
}

export function removeRecordPosition(aiTable: AIViewTable, path: [string, string]) {
const operation: RemoveRecordPositionAction = {
type: ActionName.RemoveRecordPosition,
path
};
aiTable.apply(operation);
}

export const PositionActions = {
setRecordPosition,
removeRecordPosition
export const PositionsActions = {
setRecordPositions
};
54 changes: 31 additions & 23 deletions packages/state/src/shared/to-table/array-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
AITableViewFields,
AITableViewRecords,
AIViewTable,
Positions,
SharedType,
SyncArrayElement,
SyncMapElement
} from '../../types';
import { getIdBySystemFieldValues, getShareTypeNumberPath, getTrackableEntityBySystemFieldValues, translatePositionToPath } from '../utils';
import { getPositionsBySystemFieldValues, getSharedMapValueId, getSharedRecordId, translateToRecordValues } from '../utils/translate';
import { getPositionsBySystemFieldValues, getSharedMapValueId, getSharedRecordId, getValuesByCustomFieldValues, POSITIONS_INDEX } from '../utils/translate';
import { AIFieldValueIdPath, AITableField, AITableQueries, IdPath, NumberPath } from '@ai-table/grid';

export default function translateArrayEvent(aiTable: AIViewTable, sharedType: SharedType, event: Y.YEvent<any>): AITableAction[] {
Expand Down Expand Up @@ -76,7 +77,7 @@ export default function translateArrayEvent(aiTable: AIViewTable, sharedType: Sh
_id: getIdBySystemFieldValues(systemFieldValues),
...getTrackableEntityBySystemFieldValues(systemFieldValues),
positions: getPositionsBySystemFieldValues(customFieldValues),
values: translateToRecordValues(customFieldValues, aiTable.fields() as AITableViewFields)
values: getValuesByCustomFieldValues(customFieldValues, aiTable.fields() as AITableViewFields)
}
});
});
Expand All @@ -88,32 +89,25 @@ export default function translateArrayEvent(aiTable: AIViewTable, sharedType: Sh
const recordIndex = targetPath[0] as number;
const fieldIndex = offset;
const record = (aiTable.records() as AITableViewRecords)[recordIndex];
if (isPositionOperation(fieldIndex, sharedFields)) {
for (const key in item) {
if (!record.positions[key] && record.positions[key] !== 0) {
actions.push({
type: ActionName.AddRecordPosition,
path: [record._id],
position: {
[key]: item[key]
}
});
}
}
for (const key in record.positions) {
if (!item[key] && item[key] !== 0) {
actions.push({
type: ActionName.RemoveRecordPosition,
path: [key, record._id]
});
if (isSystemFieldOperation(targetPath)) {
if (isPositionsOperation(fieldIndex)) {
const newPositions: Positions = {};
for (const key in item) {
newPositions[key] = item[key];
}
actions.push({
type: ActionName.SetRecordPositions,
path: [record._id],
positions: newPositions
});
} else {
console.log('更新其它系统字段,比如修改人、修改时间等');
}
} else {
const recordId = getSharedRecordId(sharedRecords, recordIndex);
const fieldId = getSharedMapValueId(sharedFields, fieldIndex);
const path = [recordId, fieldId] as AIFieldValueIdPath;
const fieldValue = AITableQueries.getFieldValue(aiTable, path);

// To exclude insert triggered by field inserts.
if (fieldValue !== item) {
actions.push({
Expand Down Expand Up @@ -163,8 +157,22 @@ export function isAddOrRemove(targetPath: number[]): boolean {
return targetPath.length === 0;
}

export function isPositionOperation(fieldIndex: number, sharedFields: Y.Array<SyncMapElement>): boolean {
return fieldIndex === sharedFields.length;
export function isSystemFieldOperation(targetPath: number[]): boolean {
if (targetPath.length === 2 && targetPath[1] === 0) {
return true;
}
return false;
}

export function isCustomFieldOperation(targetPath: number[]): boolean {
if (targetPath.length === 2 && targetPath[1] === 1) {
return true;
}
return false;
}

export function isPositionsOperation(fieldIndex: number): boolean {
return fieldIndex === POSITIONS_INDEX;
}

export function getRemoveIds(event: Y.YEvent<any>, type: ActionName.RemoveField | ActionName.RemoveRecord) {
Expand Down
24 changes: 14 additions & 10 deletions packages/state/src/shared/to-yjs/add-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import {
ActionName,
AddFieldAction,
AddRecordAction,
AddRecordPositionAction,
SetRecordPositionAction,
AddViewAction,
AITableViewRecord,
SharedType,
SyncArrayElement,
SyncMapElement
} from '../../types';
import { getSharedRecordIndex, toRecordSyncElement, toSyncElement } from '../utils';
import { getPositionsByRecordSyncElement, getSharedRecordIndex, toRecordSyncElement, toSyncElement, setRecordPositions } from '../utils';

export default function addNode(
sharedType: SharedType,
action: AddFieldAction | AddRecordAction | AddViewAction | AddRecordPositionAction
action: AddFieldAction | AddRecordAction | AddViewAction | SetRecordPositionAction
): SharedType {
const records = sharedType.get('records')! as Y.Array<SyncArrayElement>;
const views = sharedType.get('views')!;
Expand All @@ -27,16 +27,20 @@ export default function addNode(
case ActionName.AddView:
views && views.push([toSyncElement(action.view)]);
break;
case ActionName.AddRecordPosition:
case ActionName.SetRecordPositions:
if (records) {
const recordIndex = getSharedRecordIndex(records, action.path[0]);
const record = records.get(recordIndex);
const customField = record.get(1) as Y.Array<any>;
const positionsIndex = customField.length - 1;
const positions = customField.get(positionsIndex);
const newPositions = { ...positions, ...action.position };
customField.delete(positionsIndex);
customField.insert(positionsIndex, [newPositions]);
const positions = getPositionsByRecordSyncElement(record);
const newPositions = { ...positions };
for (const key in action.positions) {
if (action.positions[key] === null || action.positions[key] === undefined) {
delete newPositions[key];
} else {
newPositions[key] = action.positions[key];
}
}
setRecordPositions(record, newPositions);
}
break;
case ActionName.AddField:
Expand Down
23 changes: 11 additions & 12 deletions packages/state/src/shared/to-yjs/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AITable } from '@ai-table/grid';
import updateFieldValue from './update-field-value';
import { AITableAction, SharedType } from '../../types';
import { ActionName, AITableAction, SharedType } from '../../types';
import setNode from './set-node';
import addNode from './add-node';
import removeNode from './remove-node';
Expand All @@ -12,17 +12,16 @@ export type ActionMapper<O extends AITableAction = AITableAction> = {
export type ApplyFunc<O extends AITableAction = AITableAction> = (sharedType: SharedType, op: O) => SharedType;

export const actionMappers: Partial<ActionMapper<any>> = {
add_record: addNode,
update_field_value: updateFieldValue,
remove_record: removeNode,
add_field: addNode,
set_field: setNode,
remove_field: removeNode,
set_view: setNode,
add_view: addNode,
remove_view: removeNode,
add_record_position: addNode,
remove_record_position: removeNode
[ActionName.AddRecord]: addNode,
[ActionName.UpdateFieldValue]: updateFieldValue,
[ActionName.RemoveRecord]: removeNode,
[ActionName.AddField]: addNode,
[ActionName.SetField]: setNode,
[ActionName.RemoveField]: removeNode,
[ActionName.SetView]: setNode,
[ActionName.AddView]: addNode,
[ActionName.RemoveView]: removeNode,
[ActionName.SetRecordPositions]: addNode
};

export function applyActionOps(sharedType: SharedType, actions: AITableAction[], aiTable: AITable): SharedType {
Expand Down
18 changes: 1 addition & 17 deletions packages/state/src/shared/to-yjs/remove-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ import {
ActionName,
RemoveFieldAction,
RemoveRecordAction,
RemoveRecordPositionAction,
RemoveViewAction,
SharedType,
SyncArrayElement,
SyncMapElement
} from '../../types';
import { getSharedMapValueIndex, getSharedRecordIndex } from '../utils';

export default function removeNode(
sharedType: SharedType,
action: RemoveFieldAction | RemoveRecordAction | RemoveViewAction | RemoveRecordPositionAction
): SharedType {
export default function removeNode(sharedType: SharedType, action: RemoveFieldAction | RemoveRecordAction | RemoveViewAction): SharedType {
const fields = sharedType.get('fields') as Y.Array<SyncMapElement>;
const records = sharedType.get('records') as Y.Array<SyncArrayElement>;
const views = sharedType.get('views')! as Y.Array<SyncMapElement>;
Expand All @@ -36,18 +32,6 @@ export default function removeNode(
}
}
break;
case ActionName.RemoveRecordPosition:
if (records) {
const recordIndex = getSharedRecordIndex(records, action.path[1]);
const record = records.get(recordIndex);
const customField = record.get(1) as Y.Array<any>;
const positionsIndex = customField.length - 1;
const positions = customField.get(positionsIndex);
delete positions[action.path[0]];
customField.delete(positionsIndex);
customField.insert(positionsIndex, [positions]);
}
break;
case ActionName.RemoveField:
if (fields && records) {
const fieldIndex = getSharedMapValueIndex(fields, action.path[0]);
Expand Down
3 changes: 1 addition & 2 deletions packages/state/src/shared/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './init-shared';
export * from './init-table';
export * from './initialize';
export * from './translate';
72 changes: 0 additions & 72 deletions packages/state/src/shared/utils/init-shared.ts

This file was deleted.

Loading

0 comments on commit db8a1df

Please sign in to comment.