mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-01-31 19:02:59 +01:00
update pad clients
This commit is contained in:
parent
46bc328896
commit
9d39c9591a
1 changed files with 88 additions and 82 deletions
|
@ -583,102 +583,108 @@ exports.deletePad = function(padID, callback)
|
||||||
{code:0, message:"ok", data:null}
|
{code:0, message:"ok", data:null}
|
||||||
{code: 1, message:"padID does not exist", data: null}
|
{code: 1, message:"padID does not exist", data: null}
|
||||||
*/
|
*/
|
||||||
exports.restoreRevision = function(padID, rev, callback)
|
exports.restoreRevision = function (padID, rev, callback)
|
||||||
{
|
{
|
||||||
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
||||||
|
var padMessage = require("ep_etherpad-lite/node/handler/PadMessageHandler.js");
|
||||||
//check if rev is a number
|
|
||||||
if(rev !== undefined && typeof rev != "number")
|
|
||||||
{
|
|
||||||
//try to parse the number
|
|
||||||
if(!isNaN(parseInt(rev)))
|
|
||||||
{
|
|
||||||
rev = parseInt(rev);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
callback(new customError("rev is not a number", "apierror"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//ensure this is not a negativ number
|
//check if rev is a number
|
||||||
if(rev !== undefined && rev < 0)
|
if (rev !== undefined && typeof rev != "number")
|
||||||
{
|
{
|
||||||
callback(new customError("rev is a negativ number","apierror"));
|
//try to parse the number
|
||||||
return;
|
if (!isNaN(parseInt(rev)))
|
||||||
}
|
{
|
||||||
|
rev = parseInt(rev);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
callback(new customError("rev is not a number", "apierror"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//ensure this is not a float value
|
//ensure this is not a negativ number
|
||||||
if(rev !== undefined && !is_int(rev))
|
if (rev !== undefined && rev < 0)
|
||||||
{
|
{
|
||||||
callback(new customError("rev is a float value","apierror"));
|
callback(new customError("rev is a negativ number", "apierror"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//get the pad
|
//ensure this is not a float value
|
||||||
getPadSafe(padID, true, function(err, pad)
|
if (rev !== undefined && !is_int(rev))
|
||||||
{
|
{
|
||||||
if(ERR(err, callback)) return;
|
callback(new customError("rev is a float value", "apierror"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//get the pad
|
||||||
|
getPadSafe(padID, true, function (err, pad)
|
||||||
|
{
|
||||||
|
if (ERR(err, callback)) return;
|
||||||
|
|
||||||
|
|
||||||
//check if this is a valid revision
|
//check if this is a valid revision
|
||||||
if(rev > pad.getHeadRevisionNumber())
|
if (rev > pad.getHeadRevisionNumber())
|
||||||
{
|
{
|
||||||
callback(new customError("rev is higher than the head revision of the pad","apierror"));
|
callback(new customError("rev is higher than the head revision of the pad", "apierror"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pad.getInternalRevisionAText(rev, function(err, atext)
|
pad.getInternalRevisionAText(rev, function (err, atext)
|
||||||
{
|
{
|
||||||
if(ERR(err, callback)) return;
|
if (ERR(err, callback)) return;
|
||||||
|
|
||||||
var oldText = pad.text();
|
var oldText = pad.text();
|
||||||
atext.text += "\n";
|
atext.text += "\n";
|
||||||
function eachAttribRun(attribs, func)
|
function eachAttribRun(attribs, func)
|
||||||
{
|
{
|
||||||
var attribsIter = Changeset.opIterator(attribs);
|
var attribsIter = Changeset.opIterator(attribs);
|
||||||
var textIndex = 0;
|
var textIndex = 0;
|
||||||
var newTextStart = 0;
|
var newTextStart = 0;
|
||||||
var newTextEnd = atext.text.length;
|
var newTextEnd = atext.text.length;
|
||||||
while (attribsIter.hasNext())
|
while (attribsIter.hasNext())
|
||||||
{
|
{
|
||||||
var op = attribsIter.next();
|
var op = attribsIter.next();
|
||||||
var nextIndex = textIndex + op.chars;
|
var nextIndex = textIndex + op.chars;
|
||||||
if (!(nextIndex <= newTextStart || textIndex >= newTextEnd))
|
if (!(nextIndex <= newTextStart || textIndex >= newTextEnd))
|
||||||
{
|
{
|
||||||
func(Math.max(newTextStart, textIndex), Math.min(newTextEnd, nextIndex), op.attribs);
|
func(Math.max(newTextStart, textIndex), Math.min(newTextEnd, nextIndex), op.attribs);
|
||||||
}
|
}
|
||||||
textIndex = nextIndex;
|
textIndex = nextIndex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a new changeset with a helper builder object
|
// create a new changeset with a helper builder object
|
||||||
var builder = Changeset.builder(oldText.length);
|
var builder = Changeset.builder(oldText.length);
|
||||||
|
|
||||||
// assemble each line into the builder
|
// assemble each line into the builder
|
||||||
eachAttribRun(atext.attribs, function(start, end, attribs)
|
eachAttribRun(atext.attribs, function (start, end, attribs)
|
||||||
{
|
{
|
||||||
builder.insert(atext.text.substring(start, end), attribs);
|
builder.insert(atext.text.substring(start, end), attribs);
|
||||||
});
|
});
|
||||||
|
|
||||||
var lastNewlinePos = oldText.lastIndexOf('\n');
|
var lastNewlinePos = oldText.lastIndexOf('\n');
|
||||||
if (lastNewlinePos < 0) {
|
if (lastNewlinePos < 0)
|
||||||
builder.remove(oldText.length-1,0);
|
{
|
||||||
} else {
|
builder.remove(oldText.length - 1, 0);
|
||||||
builder.remove(lastNewlinePos, oldText.match(/\n/g).length-1);
|
} else
|
||||||
builder.remove(oldText.length - lastNewlinePos-1,0);
|
{
|
||||||
}
|
builder.remove(lastNewlinePos, oldText.match(/\n/g).length - 1);
|
||||||
|
builder.remove(oldText.length - lastNewlinePos - 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
var changeset = builder.toString();
|
var changeset = builder.toString();
|
||||||
|
|
||||||
//append the changeset
|
//append the changeset
|
||||||
pad.appendRevision(changeset);
|
pad.appendRevision(changeset);
|
||||||
//
|
//
|
||||||
callback(null, null);
|
padMessage.updatePadClients(pad, function ()
|
||||||
});
|
{
|
||||||
|
});
|
||||||
|
callback(null, null);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue