better relative cursor positions for text editing - decrease number of generated messages for cursor

This commit is contained in:
Kevin Jahns 2017-12-31 16:14:02 +01:00
parent fc5be5c7cc
commit 0ba97d78f8

View File

@ -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) {