Skip to content

Commit

Permalink
Simplify server-side transformation flow
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelklehr committed Apr 1, 2013
1 parent 9fe1b0d commit 9055d8b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 8 additions & 4 deletions lib/Document.js
Expand Up @@ -65,7 +65,7 @@ Document.prototype.dispatchEdit = function(oriConnection, edit) {

// Transform against all edits that have happened in the meantime

if(oriConnection.sent && (!~this.history.getAllAfter(edit.parent).indexOf(oriConnection.sent) && edit.parent != oriConnection.sent.id)) {
/*if(oriConnection.sent && (!~this.history.getAllAfter(edit.parent).indexOf(oriConnection.sent) && edit.parent != oriConnection.sent.id)) {
// Only transfrom, if it's not one of this edit's ancestors
console.log('transforming incoming against sent edit:', Changeset.unpack(oriConnection.sent.diff).inspect(), change.inspect(), '->', change.transformAgainst(Changeset.unpack(oriConnection.sent.diff)).inspect())
change = change.transformAgainst(Changeset.unpack(oriConnection.sent.diff))
Expand All @@ -79,12 +79,15 @@ Document.prototype.dispatchEdit = function(oriConnection, edit) {
console.log('transforming incoming against unsent edits:', Changeset.unpack(newEdit.diff).inspect(), change.inspect(), '->', change.transformAgainst(Changeset.unpack(newEdit.diff)).inspect())
change = change.transformAgainst(Changeset.unpack(newEdit.diff))
edit.parent = newEdit.id // update ancestor
}.bind(this))
}.bind(this))*/

// ISN'T THE ABOVE ACTUALLY THE SAME AS BELOW?

// Transform against possibly missed edits from history
this.history.getAllAfter(edit.parent).forEach(function(oldEdit) {
console.log('transforming against history:', Changeset.unpack(oldEdit.diff).inspect(), change.inspect(), change.transformAgainst(Changeset.unpack(oldEdit.diff)))
change = change.transformAgainst(Changeset.unpack(oldEdit.diff))
var historicalCs = Changeset.unpack(oldEdit.diff)
console.log('transforming against history:', historicalCs.inspect(), change.inspect(), change.transformAgainst(historicalCs))
change = change.transformAgainst(historicalCs)
edit.parent = oldEdit.id // update ancestor
})

Expand All @@ -93,6 +96,7 @@ Document.prototype.dispatchEdit = function(oriConnection, edit) {
}catch(e) {
console.warn('Applying edit "'+edit.id+'" failed:', e)
// Mh. we better tell them...
// oriConnection.destroy()
return
}
console.log(this.content)
Expand Down
7 changes: 4 additions & 3 deletions lib/TextinputAdapter.js
Expand Up @@ -64,18 +64,19 @@ TextinputAdapter.prototype.retainScrollView = function(selector, main) {
TextinputAdapter.prototype.retainSelection = function(main) {
var content = this.getContent()

// Insert some selection markers (dummy nodes) that will be used to remember the current selection
// Construct a changeset with the current selection start and end positions
var caret = new cs.text.Changeset(
new cs.text.Insert(content.length, this.textarea.selectionStart, '*', 0),
new cs.text.Insert(content.length, this.textarea.selectionEnd, '|', 0)
)

// function should return a changeset
var change = main()

if(!change) throw new Error('Wrapped function should return applied changeset')


caret = caret.transformAgainst(change)

// restore selection after updating contents
this.textarea.selectionStart = caret[0].pos
this.textarea.selectionEnd = caret[1].pos
Expand Down

0 comments on commit 9055d8b

Please sign in to comment.