Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: initial implementation of cursors #306

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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: 18 additions & 12 deletions backend/op_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ function patchList(opSet, objectId, index, elemId, action, ops) {
return [opSet, [edit]]
}

// find the index of the closest preceding list element
function findClosestListIndex(opSet, objectId, elemId) {
const elemIds = opSet.getIn(['byObject', objectId, '_elemIds'])

let prevId = elemId
while (true) {
index = -1
prevId = getPrevious(opSet, objectId, prevId)
if (!prevId) break
index = elemIds.indexOf(prevId)
if (index >= 0) break
}

return index
}

function updateListElement(opSet, objectId, elemId) {
const ops = getFieldOps(opSet, objectId, elemId)
const elemIds = opSet.getIn(['byObject', objectId, '_elemIds'])
Expand All @@ -155,17 +171,7 @@ function updateListElement(opSet, objectId, elemId) {

} else {
if (ops.isEmpty()) return [opSet, []] // deleting a non-existent element = no-op

// find the index of the closest preceding list element
let prevId = elemId
while (true) {
index = -1
prevId = getPrevious(opSet, objectId, prevId)
if (!prevId) break
index = elemIds.indexOf(prevId)
if (index >= 0) break
}

index = findClosestListIndex(opSet, objectId, elemId)
return patchList(opSet, objectId, index + 1, elemId, 'insert', ops)
}
}
Expand Down Expand Up @@ -569,5 +575,5 @@ function listIterator(opSet, listId, context) {
module.exports = {
init, addChange, getMissingChanges, getChangesForActor, getMissingDeps,
getObjectFields, getObjectField, getObjectConflicts, getFieldOps,
listElemByIndex, listLength, listIterator, ROOT_ID
listElemByIndex, listLength, listIterator, findClosestListIndex, ROOT_ID
}
29 changes: 27 additions & 2 deletions frontend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { ROOT_ID, isObject, copyObject } = require('../src/common')
const uuid = require('../src/uuid')
const { applyDiffs, updateParentObjects, cloneRootObject } = require('./apply_patch')
const { rootObjectProxy } = require('./proxies')
const { findClosestListIndex } = require('../backend/op_set')
const { Context } = require('./context')
const { Text } = require('./text')
const { Table } = require('./table')
Expand Down Expand Up @@ -492,10 +493,34 @@ function getElementIds(list) {
return list[ELEM_IDS]
}

/**
* Finds the latest integer index of a cursor object.
* If the character was deleted, returns -1.
*
* todos:
* - consider returning the closest character if deleted
* - consider optimizing the linear search
*/
function findCursorIndex (cursor, doc, findClosest) {
Copy link
Author

@geoffreylitt geoffreylitt Jan 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's odd that this function requires doc as a separate argument, but I couldn't find any way to navigate from a cursor object to the root doc.

if(cursor.textId === undefined || cursor.elemId === undefined) {
throw new TypeError('Invalid cursor object')
}

const backend = getBackendState(doc)
const elemIds = backend.getIn(['opSet', 'byObject', cursor.textId, '_elemIds'])
let index = elemIds.indexOf(cursor.elemId)

if (index === -1 && findClosest) {
index = findClosestListIndex(backend.getIn(['opSet']), cursor.textId, cursor.elemId)
}

return index
}

module.exports = {
init, from, change, emptyChange, applyPatch,
canUndo, undo, canRedo, redo,
getObjectId, getObjectById, getActorId, setActorId, getConflicts,
getBackendState, getElementIds,
Text, Table, Counter
getBackendState, getElementIds, findCursorIndex,
Text, Table, Counter,
}
12 changes: 12 additions & 0 deletions frontend/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ class Text {
return this.elems[index].elemId
}

/**
* Returns a cursor that points to a specific point in the text.
* For now, represented by a plain object.
*/
getCursorAt (index) {
return {
// todo: are there any points in the lifecycle where the Text object doesn't have an ID?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ept I see some other places that handle the case where the object ID is not set.

Would it be sensible to add a similar check here and forbid getting a cursor if there's not an object ID? I don't yet understand when/how that case occurs.

textId: this[OBJECT_ID],
elemId: this.getElemId(index)
}
}

/**
* Iterates over the text elements character by character, including any
* inline objects.
Expand Down
46 changes: 46 additions & 0 deletions test/text_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,52 @@ describe('Automerge.Text', () => {
})
})

describe('cursors', () => {
let s1
beforeEach(() => {
s1 = Automerge.change(Automerge.init(), doc => {
doc.text = new Automerge.Text('hello world')
doc.cursor = doc.text.getCursorAt(2)
})
})

it('can retrieve the initial index on the cursor', () => {
assert.deepStrictEqual(Automerge.Frontend.findCursorIndex(s1.cursor, s1), 2)
})

it('updates the cursor index when text is updated', () => {
s1 = Automerge.change(s1, doc => {
doc.text.insertAt(0, 'a', 'b', 'c')
})

assert.deepStrictEqual(Automerge.Frontend.findCursorIndex(s1.cursor, s1), 5)
})

it('throws an error if a non-cursor is passed in', () => {
s1 = Automerge.change(s1, doc => {
doc.value = "random string"
})

assert.throws(() => Automerge.Frontend.findCursorIndex(s1.value, s1), /Invalid cursor object/)
})

it('returns -1 by default if character was deleted', () => {
s1 = Automerge.change(s1, doc => {
doc.text.deleteAt(2)
})

assert.deepStrictEqual(Automerge.Frontend.findCursorIndex(s1.cursor, s1), -1)
})

it('returns closest index if character was deleted and findClosest is set to true', () => {
s1 = Automerge.change(s1, doc => {
doc.text.deleteAt(2)
})

assert.deepStrictEqual(Automerge.Frontend.findCursorIndex(s1.cursor, s1, true), 1)
})
})

describe('non-textual control characters', () => {
let s1
beforeEach(() => {
Expand Down