Skip to content
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
30 changes: 27 additions & 3 deletions lib/json1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
// import log from './log'
import deepEqual from './deepEqual.js'
import deepClone from './deepClone.js'
import {ReadCursor, WriteCursor, readCursor, writeCursor, advancer, eachChildOf, isValidPathItem} from './cursor.js'
import { Doc, JSONOpComponent, Path, Key, JSONOp, JSONOpList, Conflict, ConflictType } from './types.js'
import { ReadCursor, WriteCursor, readCursor, writeCursor, advancer, eachChildOf, isValidPathItem } from './cursor.js'
import { Doc, JSONOpComponent, Path, Presence, Key, JSONOp, JSONOpList, Conflict, ConflictType } from './types.js'

const RELEASE_MODE = process.env.JSON1_RELEASE_MODE
const log: (...args: any) => void = RELEASE_MODE ? (() => {}) : require('./log').default
Expand Down Expand Up @@ -57,6 +57,7 @@ export const type = {

apply,
transformPosition,
transformPresence,
compose,
tryTransform,
transform,
Expand Down Expand Up @@ -692,7 +693,11 @@ function transformPosition(path: Path, op: JSONOp): Path | null {
if (et) {
const e = getEdit(c!)
log('Embedded edit', e, et)
if (et.transformPosition) path[i] = et.transformPosition(path[i], e)
if (et.transformPosition) {
path[i] = et.transformPosition(path[i], e)
} else if (et.transformCursor) {
path[i] = et.transformCursor(path[i], e)
}
// Its invalid for the operation to have drops inside the embedded edit here.
break
}
Expand Down Expand Up @@ -725,6 +730,25 @@ function transformPosition(path: Path, op: JSONOp): Path | null {
return path
}

function transformPresence(presence: Presence | null, op: JSONOp, isOwnOperation: boolean): Presence | null {
if (!presence || !presence.start || !presence.end) {
return null
}

const start = transformPosition(presence.start, op)
const end = transformPosition(presence.end, op)

if (start && end) {
return { ...presence, start, end }
} else if (start) {
return { ...presence, start, end: start }
} else if (end) {
return { ...presence, start: end, end }
}

return null
}


// ****** Compose

Expand Down
5 changes: 5 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export type JSONOp = null | JSONOpList

export type Key = number | string
export type Path = Key[]
export type Presence = {
start: Path,
end: Path,
[key: string]: any
}

/**
* JSON documents must be able to round-trip through JSON.stringify /
Expand Down
Loading