Skip to content

Commit

Permalink
fix: adjust record array
Browse files Browse the repository at this point in the history
  • Loading branch information
huanhuanwa committed Jul 25, 2024
1 parent dc110b7 commit 4ae0bbc
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 49 deletions.
17 changes: 12 additions & 5 deletions packages/grid/src/core/utils/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@ export const AITableQueries = {
if (!isUndefinedOrNull(fieldIndex) && fieldIndex > -1) {
return [fieldIndex] as AIFieldPath;
}
throw new Error(`Unable to find the path: ${JSON.stringify({ ...(field || {}), ...(record || {}) })}`);
throw new Error(`can not find the path: ${JSON.stringify({ ...(field || {}), ...(record || {}) })}`);
},
getFieldValue(aiTable: AITable, path: [number, number]): any {
if (!aiTable || !aiTable.records() || !aiTable.fields()) {
throw new Error(`Cannot find a descendant at path [${path}]`);
if (!aiTable) {
throw new Error(`aiTable does not exist [${path}]`);
}
if (!aiTable.records()) {
throw new Error(`aiTable has no records [${path}]`);
}
if (!aiTable.fields()) {
throw new Error(`aiTable has no fields [${path}]`);
}

const field = aiTable.fields()[path[1]];
if(!field){
throw new Error(`Cannot find a descendant at path [${path}]`);
if (!field) {
throw new Error(`can not find field at path [${path}]`);
}
return aiTable.records()[path[0]].value[field.id];
}
Expand Down
1 change: 1 addition & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
if (!YjsAITable.isLocal(this.aiTable)) {
if (!isInitialized) {
const data = translateSharedTypeToTable(this.sharedType!);
console.log(123, data);
this.records.set(data.records);
this.fields.set(data.fields);
isInitialized = true;
Expand Down
9 changes: 5 additions & 4 deletions src/app/share/apply-to-table/array-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export default function translateArrayEvent(aiTable: AITable, event: Y.YEvent<an
if (targetPath.length) {
try {
delta.insert?.map((item: any) => {
const path = [targetPath[0], offset - 1] as AIFieldValuePath;
const path = [targetPath[0], offset] as AIFieldValuePath;
const fieldValue = AITableQueries.getFieldValue(aiTable, path);
// To exclude insert triggered by field inserts.
// To exclude insert triggered by field inserts.
if (fieldValue !== item) {
actions.push({
type: ActionName.UpdateFieldValue,
Expand All @@ -36,12 +36,13 @@ export default function translateArrayEvent(aiTable: AITable, event: Y.YEvent<an
} else {
delta.insert?.map((item: Y.Array<any>, index) => {
const data = item.toJSON();
const [fixedField, customField] = data;
actions.push({
type: ActionName.AddRecord,
path: [offset + index],
record: {
id: data[0],
value: translateRecord(data, aiTable.fields())
id: fixedField[0],
value: translateRecord(customField, aiTable.fields())
}
});
});
Expand Down
25 changes: 0 additions & 25 deletions src/app/share/apply-to-table/map-event.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/app/share/apply-to-yjs/add-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export default function addField(sharedType: SharedType, action: AddFieldAction)
if (records) {
for (let value of records) {
const newRecord = getDefaultFieldValue(action.field.type);
value.insert(path + 1, [newRecord]);
const customField = value.get(1);
customField.insert(path, [newRecord]);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/app/share/apply-to-yjs/update-field-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ export default function updateFieldValue(sharedType: SharedType, action: UpdateF
const records = sharedType.get('records');
if (records) {
const record = records?.get(action.path[0]) as SyncArrayElement;
const index = action.path[1] + 1;
record.delete(index);
record.insert(index, [action.newFieldValue]);
const customField = record.get(1);
const index = action.path[1];
customField.delete(index);
customField.insert(index, [action.newFieldValue]);
}

return sharedType;
Expand Down
2 changes: 1 addition & 1 deletion src/app/share/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { WebsocketProvider } from 'y-websocket';
import * as Y from 'yjs';

export const connectProvider = (doc: Y.Doc) => {
const provider = new WebsocketProvider('wss://demos.yjs.dev/ws', 'ai-table-demo-2024/7/24', doc);
const provider = new WebsocketProvider('wss://demos.yjs.dev/ws', 'ai-table-demo-2024/7/25', doc);
provider.connect();
return provider;
};
20 changes: 13 additions & 7 deletions src/app/share/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function toSharedType(
sharedType.set('fields', fieldSharedType);
fieldSharedType.insert(0, data.fields.map(toSyncElement));

const recordSharedType = new Y.Array();
const recordSharedType = new Y.Array<Y.Array<any>>();
sharedType.set('records', recordSharedType);
recordSharedType.insert(0, data.records.map(toRecordSyncElement));
}
Expand All @@ -57,13 +57,19 @@ export function toSyncElement(node: any): SyncMapElement {
return element;
}

export function toRecordSyncElement(record: AITableRecord): Y.Array<any> {
// To save memory, convert map to array.
const element = new Y.Array();
const recordArray = [record['id']];
export function toRecordSyncElement(record: AITableRecord): Y.Array<Y.Array<any>> {
const fixedFieldArray = new Y.Array();
fixedFieldArray.insert(0, [record['id']]);

const customFieldArray = new Y.Array();
const customFields = [];
for (const fieldId in record['value']) {
recordArray.push(record['value'][fieldId]);
customFields.push(record['value'][fieldId]);
}
element.insert(0, recordArray);
customFieldArray.insert(0, customFields);

// To save memory, convert map to array.
const element = new Y.Array<Y.Array<any>>();
element.insert(0, [fixedFieldArray, customFieldArray]);
return element;
}
7 changes: 4 additions & 3 deletions src/app/share/utils/translate-to-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const translateRecord = (arrayRecord: any[], fields: AITableFields) => {
const fieldIds = fields.map((item) => item.id);
const recordValue: Record<string, any> = {};
fieldIds.forEach((item, index) => {
recordValue[item] = arrayRecord[index + 1] || '';
recordValue[item] = arrayRecord[index] || '';
});
return recordValue;
};
Expand All @@ -14,9 +14,10 @@ export const translateSharedTypeToTable = (sharedType: SharedType) => {
const data = sharedType.toJSON();
const fields: AITableFields = data['fields'];
const records: AITableRecords = data['records'].map((record: any) => {
const [fixedField, customField] = record;
return {
id: record[0],
value: translateRecord(record, fields)
id: fixedField[0],
value: translateRecord(customField, fields)
};
});
return {
Expand Down

0 comments on commit 4ae0bbc

Please sign in to comment.