RelativePosition(type, tname, item)

A relative position is based on the Yjs model and is not affected by document changes. E.g. If you place a relative position before a certain character, it will always point to this character. If you place a relative position at the end of a type, it will always point to the end of the type.

A numeric position is often unsuited for user selections, because it does not change when content is inserted before or after.

Insert(0, 'x')('a|bc') = 'xa|bc' Where | is the relative position.

One of the properties must be defined.

new RelativePosition(type, tname, item)

Parameters:
Name Type Description
type ID | null
tname string | null
item ID | null
Example
// Current cursor position is at position 10
  const relativePosition = createRelativePositionFromIndex(yText, 10)
  // modify yText
  yText.insert(0, 'abc')
  yText.delete(3, 10)
  // Compute the cursor position
  const absolutePosition = createAbsolutePositionFromRelativePosition(y, relativePosition)
  absolutePosition.type === yText // => true
  console.log('cursor location is ' + absolutePosition.index) // => cursor location is 3

Classes

RelativePosition

Members

item :ID|null

tname :string|null

type :ID|null