diff --git a/src/Util/relativePosition.js b/src/Util/relativePosition.js index fa292c53..77118b7f 100644 --- a/src/Util/relativePosition.js +++ b/src/Util/relativePosition.js @@ -2,37 +2,31 @@ import ID from './ID.js' import RootID from './RootID.js' export function getRelativePosition (type, offset) { - if (offset === 0) { - return ['startof', type._id.user, type._id.clock || null, type._id.name || null, type._id.type || null] - } else { - let t = type._start - while (t !== null) { - if (t._deleted === false) { - if (t._length >= offset) { - return [t._id.user, t._id.clock + offset - 1] - } - if (t._right === null) { - return [t._id.user, t._id.clock + t._length - 1] - } - offset -= t._length + let t = type._start + while (t !== null) { + if (t._deleted === false) { + if (t._length > offset) { + return [t._id.user, t._id.clock + offset] } - t = t._right + offset -= t._length } - return null + t = t._right } + return ['endof', type._id.user, type._id.clock || null, type._id.name || null, type._id.type || null] } export function fromRelativePosition (y, rpos) { - if (rpos[0] === 'startof') { + if (rpos[0] === 'endof') { let id if (rpos[3] === null) { id = new ID(rpos[1], rpos[2]) } else { id = new RootID(rpos[3], rpos[4]) } + const type = y.os.get(id) return { - type: y.os.get(id), - offset: 0 + type, + offset: type.length } } else { let offset = 0 @@ -42,7 +36,7 @@ export function fromRelativePosition (y, rpos) { return null } if (!struct._deleted) { - offset = rpos[1] - struct._id.clock + 1 + offset = rpos[1] - struct._id.clock } struct = struct._left while (struct !== null) {