-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
deafb13
commit 488a8e1
Showing
15 changed files
with
787 additions
and
12 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,3 @@ | |
[aiFieldConfig]="aiFieldConfig" | ||
(aiTableInitialized)="aiTableInitialized($event)" | ||
></ai-table-grid> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { ActionName, AIFieldValuePath, AITable, AITableAction, AITableField, AITableQueries } from '@ai-table/grid'; | ||
import * as Y from 'yjs'; | ||
import { toTablePath, translateRecord } from '../utils/translate-to-table'; | ||
import { isArray } from 'ngx-tethys/util'; | ||
|
||
export default function translateArrayEvent(aiTable: AITable, event: Y.YEvent<any>): AITableAction[] { | ||
const actions: AITableAction[] = []; | ||
let offset = 0; | ||
let targetPath = toTablePath(event.path); | ||
const isRecordsTranslate = event.path.includes('records'); | ||
const isFieldsTranslate = event.path.includes('fields'); | ||
|
||
event.changes.delta.forEach((delta) => { | ||
if ('retain' in delta) { | ||
offset += delta.retain ?? 0; | ||
} | ||
if ('insert' in delta) { | ||
if (isArray(delta.insert)) { | ||
if (isRecordsTranslate) { | ||
if (targetPath.length) { | ||
try { | ||
delta.insert?.map((item: any) => { | ||
const path = [targetPath[0], offset] as AIFieldValuePath; | ||
const fieldValue = AITableQueries.getFieldValue(aiTable, path); | ||
// To exclude insert triggered by field inserts. | ||
if (fieldValue !== item) { | ||
actions.push({ | ||
type: ActionName.UpdateFieldValue, | ||
path, | ||
fieldValue, | ||
newFieldValue: item | ||
}); | ||
} | ||
}); | ||
} catch (error) {} | ||
} 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: fixedField[0], | ||
value: translateRecord(customField, aiTable.fields()) | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
if (isFieldsTranslate) { | ||
delta.insert?.map((item: Y.Map<any>, index) => { | ||
const data = item.toJSON(); | ||
if (event.path.includes('fields')) { | ||
actions.push({ | ||
type: ActionName.AddField, | ||
path: [offset + index], | ||
field: data as AITableField | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
}); | ||
return actions; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as Y from 'yjs'; | ||
import { AITable, AITableAction } from '@ai-table/grid'; | ||
import translateArrayEvent from './array-event'; | ||
import { YjsAITable } from '../yjs-table'; | ||
|
||
export function translateYjsEvent(aiTable: AITable, event: Y.YEvent<any>): AITableAction[] { | ||
if (event instanceof Y.YArrayEvent) { | ||
return translateArrayEvent(aiTable, event); | ||
} | ||
return []; | ||
} | ||
|
||
export function applyYjsEvents(aiTable: AITable, events: Y.YEvent<any>[]): void { | ||
if (YjsAITable.isUndo(aiTable)) { | ||
events.forEach((event) => | ||
translateYjsEvent(aiTable, event).forEach((item) => { | ||
aiTable.apply(item); | ||
}) | ||
); | ||
} else { | ||
YjsAITable.asRemote(aiTable, () => { | ||
events.forEach((event) => | ||
translateYjsEvent(aiTable, event).forEach((item) => { | ||
aiTable.apply(item); | ||
}) | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { SharedType, SyncArrayElement, toSyncElement } from '../shared'; | ||
import { AddFieldAction, getDefaultFieldValue } from '@ai-table/grid'; | ||
|
||
export default function addField(sharedType: SharedType, action: AddFieldAction): SharedType { | ||
const fields = sharedType.get('fields'); | ||
const path = action.path[0]; | ||
if (fields) { | ||
fields.insert(path, [toSyncElement(action.field)]); | ||
} | ||
const records = sharedType.get('records') as SyncArrayElement; | ||
if (records) { | ||
for (let value of records) { | ||
const newRecord = getDefaultFieldValue(action.field.type); | ||
const customField = value.get(1); | ||
customField.insert(path, [newRecord]); | ||
} | ||
} | ||
|
||
return sharedType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { SharedType, toRecordSyncElement } from '../shared'; | ||
import { AddRecordAction } from '@ai-table/grid'; | ||
|
||
export default function addRecord(sharedType: SharedType, action: AddRecordAction): SharedType { | ||
const records = sharedType.get('records'); | ||
if (records) { | ||
const path = action.path[0]; | ||
records.insert(path, [toRecordSyncElement(action.record)]); | ||
} | ||
|
||
return sharedType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { AITable, AITableAction } from '@ai-table/grid'; | ||
import { SharedType } from '../shared'; | ||
import updateFieldValue from './update-field-value'; | ||
import addRecord from './add-record'; | ||
import addField from './add-field'; | ||
|
||
export type ActionMapper<O extends AITableAction = AITableAction> = { | ||
[K in O['type']]: O extends { type: K } ? ApplyFunc<O> : never; | ||
}; | ||
|
||
export type ApplyFunc<O extends AITableAction = AITableAction> = (sharedType: SharedType, op: O) => SharedType; | ||
|
||
export const actionMappers: Partial<ActionMapper<AITableAction>> = { | ||
update_field_value: updateFieldValue, | ||
add_record: addRecord, | ||
add_field: addField | ||
}; | ||
|
||
export default function applyActionOps(sharedType: SharedType, actions: AITableAction[], aiTable: AITable): SharedType { | ||
if (actions.length > 0) { | ||
sharedType.doc!.transact(() => { | ||
actions.forEach((action) => { | ||
const apply = actionMappers[action.type] as ApplyFunc<typeof action>; | ||
if (apply) { | ||
return apply(sharedType, action); | ||
} | ||
return null; | ||
}); | ||
}, aiTable); | ||
} | ||
|
||
return sharedType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { SharedType, SyncArrayElement } from '../shared'; | ||
import { UpdateFieldValueAction } from '@ai-table/grid'; | ||
|
||
export default function updateFieldValue(sharedType: SharedType, action: UpdateFieldValueAction): SharedType { | ||
const records = sharedType.get('records'); | ||
if (records) { | ||
const record = records?.get(action.path[0]) as SyncArrayElement; | ||
const customField = record.get(1); | ||
const index = action.path[1]; | ||
customField.delete(index); | ||
customField.insert(index, [action.newFieldValue]); | ||
} | ||
|
||
return sharedType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
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/25', doc); | ||
provider.connect(); | ||
return provider; | ||
}; |
Oops, something went wrong.