Comparing version 1.1.6 to 1.2.0
@@ -12,3 +12,2 @@ var domSerialize = require('serialize-dom') | ||
}) | ||
return snapshot | ||
@@ -48,3 +47,3 @@ } | ||
function unpackOps(unpackOps) { | ||
var unpackOps = exports.unpackOps = function(unpackOps) { | ||
return unpackOps.map(function(op) { | ||
@@ -51,0 +50,0 @@ switch(op.type) { |
@@ -66,15 +66,4 @@ var Manipulate = require('./ops/manipulate') | ||
summary.removed.forEach(function(node) { | ||
var oldSibling = summary.getOldPreviousSibling(node) | ||
, oldParent = summary.getOldParentNode(node) | ||
, oldPath | ||
if(oldSibling) { | ||
// My older brother | ||
oldPath = pathTo(oldSibling, rootNode) | ||
oldPath[oldPath.length-1]++ | ||
}else if(oldParent && rootNode.contains(oldParent)){ | ||
// I was the oldest | ||
oldPath = pathTo(oldParent, rootNode) | ||
oldPath.push(0) | ||
}else{ | ||
var oldParent = summary.getOldParentNode(node) | ||
if(oldParent && !rootNode.contains(oldParent)) { | ||
// my parent doesn't exist anymore, so it has prolly been removed | ||
@@ -84,3 +73,3 @@ // ergo this delete operation is pointless | ||
} | ||
oplist.push(new Move(oldPath, null, serialize(node))) | ||
oplist.push(new Move(node.domOT_path.split('').map(Number), null, serialize(node))) | ||
}) | ||
@@ -91,15 +80,6 @@ } | ||
summary.reparented.forEach(function(node) { | ||
var oldSibling = summary.getOldPreviousSibling(node) | ||
, oldParent = summary.getOldParentNode(node) | ||
, oldPath | ||
if(oldSibling) { | ||
// My older brother | ||
oldPath = pathTo(oldSibling, rootNode) | ||
oldPath[oldPath.length-1]++ | ||
}else if(oldParent && rootNode.contains(oldParent)){ | ||
// I was the oldest | ||
oldPath = pathTo(oldParent, rootNode) | ||
oldPath.push(0) | ||
}else{ | ||
var oldParent = summary.getOldParentNode(node) | ||
, oldPath = node.domOT_path.split('').map(Number) | ||
if(oldParent && !rootNode.contains(oldParent)) { | ||
// my parent doesn't exist anymore, so it has prolly been removed | ||
@@ -115,26 +95,11 @@ // ergo, this can be treated as an insert (this is the worst case and prolly needs fixing!) | ||
summary.reordered.forEach(function(node) { | ||
var oldSibling = summary.getOldPreviousSibling(node) | ||
, newPath = pathTo(node, rootNode) | ||
, oldPath | ||
if(oldSibling) { | ||
// My older brother | ||
oldPath = pathTo(oldSibling, rootNode) | ||
oldPath[oldPath.length-1]++ | ||
}else { | ||
// I was the oldest | ||
oldPath = pathTo(summary.getOldParentNode(node), rootNode) | ||
oldPath.push(0) | ||
var oldParent = summary.getOldParentNode(node) | ||
if(oldParent && !rootNode.contains(oldParent)) { | ||
// parent was removed, ergo this is op pointless | ||
return | ||
} | ||
oplist.push(new Move(oldPath, newPath)) | ||
oplist.push(new Move(node.domOT_path.split('').map(Number), pathTo(node, rootNode))) | ||
}) | ||
} | ||
// transform old paths against each other in the order of operations | ||
oplist.forEach(function(op, i) { | ||
oplist.slice(i+1).forEach(function(op2) { | ||
op2.transformAgainst(op) | ||
}) | ||
}) | ||
if (Array.isArray(summary.added)) { | ||
@@ -166,2 +131,36 @@ summary.added | ||
}) | ||
oplist.sort(function(op1, op2) { | ||
var path1 = (op1.path || op1.to || op1.from).join('') | ||
var path2 = (op2.path || op2.to || op2.from).join('') | ||
return path1 == path2? 0 : (path1 > path2? 1 : -1) | ||
}) | ||
// transform non-inserts against Inserts/Moves in the order of operations | ||
oplist.forEach(function(op) { | ||
if(op.from || op.path) { | ||
for(var i=0; i<oplist.length; i++) { | ||
var op2 = oplist[i] | ||
, path = (op.from || op.path) && (op.from || op.path).join('') | ||
, path2 = op2.to && op2.to.join('') | ||
if(path && path2 && path > path2) { | ||
op.transformAgainst(op2) | ||
} | ||
} | ||
} | ||
}) | ||
// transfrom moves and manipulates against (re)moves | ||
oplist.forEach(function(op) { | ||
if(op.from || op.path) { | ||
for(var i=0; i<oplist.length; i++) { | ||
var op2 = oplist[i] | ||
, path = (op.from || op.path) && (op.from || op.path).join('') | ||
, path2 = op2.from && op2.from.join('') | ||
if(path && path2 && path > path2) { | ||
op.transformAgainst(op2) | ||
} | ||
} | ||
} | ||
}) | ||
@@ -214,1 +213,15 @@ if(summary.attributeChanged) { | ||
} | ||
exports.createIndex = function(rootNode) { | ||
setIndex(rootNode, '') | ||
} | ||
function setIndex(node, path) { | ||
node.domOT_path = path | ||
if(node.childNodes) { | ||
for(var i=0; i<node.childNodes.length; i++) { | ||
node.childNodes[i].domOT_path = path+i | ||
setIndex(node.childNodes[i], path+i) | ||
} | ||
} | ||
} |
var isPrefixOf = require('./is-prefix') | ||
module.exports = function isYoungerSiblingOf(path1, path2) { | ||
// check if they are on the same tree level | ||
if(path2.length != path1.length) return false | ||
// check if path2 is on a higher tree level | ||
if(path2.length > path1.length) return false | ||
// check if they have the same parent | ||
var parent = path1.concat() | ||
parent.pop() | ||
if(!isPrefixOf(parent, path2)) return false | ||
do { | ||
parent.pop() | ||
if(isPrefixOf(parent, path2)) { | ||
if(path1[parent.length] > path2[parent.length] && parent.length+1 === path2.length) | ||
return parent.length | ||
} | ||
} while(parent.length); | ||
// check if path1 is younger than path2 | ||
if(path1[path1.length-1] > path2[path2.length-1]) return true | ||
return false | ||
} |
@@ -24,3 +24,3 @@ var Manipulate = require('./manipulate') | ||
ManipulateText.prototype.apply = function (rootNode) { | ||
ManipulateText.prototype.apply = function (rootNode, index) { | ||
var targetNode = nodeAt(this.path, rootNode) | ||
@@ -27,0 +27,0 @@ |
@@ -19,2 +19,3 @@ var Move = require('./move') | ||
var path = this.path.slice(0) | ||
, level | ||
@@ -29,3 +30,3 @@ if (op.from) { | ||
if (isYoungerSiblingOf(this.path, op.from)) | ||
if ((level = isYoungerSiblingOf(this.path, op.from)) !== false) | ||
path[this.path.length-1]-- // (shift to left) | ||
@@ -35,7 +36,4 @@ } | ||
if (op.to) { | ||
if (isYoungerSiblingOf(this.path, op.to)) | ||
path[this.path.length-1]++ // (shift to right) | ||
if(isPrefixOf(op.to, this.path)) | ||
if(!left) path[op.to.length-1]++ // right side's gotta budge | ||
if ((level = isYoungerSiblingOf(this.path, op.to)) !== false) | ||
if(left) path[this.path.length-1]++ // (shift to right) | ||
} | ||
@@ -48,3 +46,3 @@ | ||
Manipulate.prototype.apply = function (rootNode) { | ||
Manipulate.prototype.apply = function (rootNode, index) { | ||
var myNode = nodeAt(this.path, rootNode) | ||
@@ -51,0 +49,0 @@ |
@@ -23,2 +23,3 @@ var isYoungerSiblingOf = require('./is-youngerSibling') | ||
, to = this.to && this.to.slice(0) | ||
, level | ||
@@ -35,12 +36,9 @@ if(this.from) { | ||
if (isYoungerSiblingOf(this.from, op.from)) | ||
from[this.from.length-1]-- // (shift to left) | ||
if ((level = isYoungerSiblingOf(this.from, op.from)) !== false) | ||
from[level]-- // (shift to left) | ||
} | ||
if(op.to) { | ||
if (isYoungerSiblingOf(this.from, op.to)) | ||
from[this.from.length-1]++ // (shift to right) | ||
if(isPrefixOf(op.to, this.from)) | ||
if(!left) from[op.to.length-1]++ | ||
if ((level = isYoungerSiblingOf(this.from, op.to)) !== false) | ||
if(!left) from[level]++ // (shift to right) | ||
} | ||
@@ -53,4 +51,4 @@ | ||
if(op.from) { | ||
if (isYoungerSiblingOf(this.to, op.from)) | ||
to[this.to.length-1]-- // (shift to left) | ||
if ((level = isYoungerSiblingOf(this.to, op.from)) !== false) | ||
to[level]-- // (shift to left) | ||
@@ -66,7 +64,4 @@ if (isPrefixOf(op.from, this.to) && op.from.length < this.to.length) { | ||
if(op.to) { | ||
if (isYoungerSiblingOf(this.to, op.to)) | ||
if ((level = isYoungerSiblingOf(this.to, op.to)) !== false) | ||
to[this.to.length-1]++ // (shift to right) | ||
if(isPrefixOf(op.to, this.to)) | ||
if(!left) to[op.to.length-1]++ // the right side's gotta budge | ||
} | ||
@@ -81,4 +76,5 @@ | ||
Move.prototype.apply = function(rootNode) { | ||
Move.prototype.apply = function(rootNode, index, dry) { | ||
var myNode | ||
if(dry) return this.applyDry(rootNode, index) | ||
@@ -110,2 +106,33 @@ if (this.from) { | ||
} | ||
if(index) this.applyDry(rootNode) | ||
} | ||
Move.prototype.applyDry = function(rootNode) { | ||
if(this.to) { | ||
var parentPath = this.to.slice(0, this.to.length-1) | ||
, parent = nodeAt(parentPath, rootNode) | ||
, node = nodeAt(this.to, rootNode) | ||
var parentPathString = parentPath.join('') | ||
// set new location + set new location for child nodes | ||
setIndex(node, this.to.join('')) | ||
// shift all after this.to | ||
for(var i=0; i<parent.childNodes.length; i++) { | ||
setIndex(parent.childNodes[i], parentPathString+i) | ||
} | ||
} | ||
} | ||
function setIndex(node, path) { | ||
node.domOT_path = path | ||
if(node.childNodes) { | ||
for(var i=0; i<node.childNodes.length; i++) { | ||
node.childNodes[i].domOT_path = path+i | ||
setIndex(node.childNodes[i], path+i) | ||
} | ||
} | ||
} |
{ | ||
"name": "dom-ot", | ||
"version": "1.1.6", | ||
"version": "1.2.0", | ||
"description": "Operational transform library for DOM operations (conforms to shareJS' spec)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22951
613