better relative cursor positions for text editing - decrease number of generated messages for cursor
This commit is contained in:
		
							parent
							
								
									fc5be5c7cc
								
							
						
					
					
						commit
						0ba97d78f8
					
				@ -2,37 +2,31 @@ import ID from './ID.js'
 | 
				
			|||||||
import RootID from './RootID.js'
 | 
					import RootID from './RootID.js'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function getRelativePosition (type, offset) {
 | 
					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
 | 
					  let t = type._start
 | 
				
			||||||
  while (t !== null) {
 | 
					  while (t !== null) {
 | 
				
			||||||
    if (t._deleted === false) {
 | 
					    if (t._deleted === false) {
 | 
				
			||||||
        if (t._length >= offset) {
 | 
					      if (t._length > offset) {
 | 
				
			||||||
          return [t._id.user, t._id.clock + offset - 1]
 | 
					        return [t._id.user, t._id.clock + offset]
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        if (t._right === null) {
 | 
					 | 
				
			||||||
          return [t._id.user, t._id.clock + t._length - 1]
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      offset -= t._length
 | 
					      offset -= t._length
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    t = t._right
 | 
					    t = t._right
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
    return null
 | 
					  return ['endof', type._id.user, type._id.clock || null, type._id.name || null, type._id.type || null]
 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function fromRelativePosition (y, rpos) {
 | 
					export function fromRelativePosition (y, rpos) {
 | 
				
			||||||
  if (rpos[0] === 'startof') {
 | 
					  if (rpos[0] === 'endof') {
 | 
				
			||||||
    let id
 | 
					    let id
 | 
				
			||||||
    if (rpos[3] === null) {
 | 
					    if (rpos[3] === null) {
 | 
				
			||||||
      id = new ID(rpos[1], rpos[2])
 | 
					      id = new ID(rpos[1], rpos[2])
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      id = new RootID(rpos[3], rpos[4])
 | 
					      id = new RootID(rpos[3], rpos[4])
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    const type = y.os.get(id)
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
      type: y.os.get(id),
 | 
					      type,
 | 
				
			||||||
      offset: 0
 | 
					      offset: type.length
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    let offset = 0
 | 
					    let offset = 0
 | 
				
			||||||
@ -42,7 +36,7 @@ export function fromRelativePosition (y, rpos) {
 | 
				
			|||||||
      return null
 | 
					      return null
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (!struct._deleted) {
 | 
					    if (!struct._deleted) {
 | 
				
			||||||
      offset = rpos[1] - struct._id.clock + 1
 | 
					      offset = rpos[1] - struct._id.clock
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    struct = struct._left
 | 
					    struct = struct._left
 | 
				
			||||||
    while (struct !== null) {
 | 
					    while (struct !== null) {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user