mirror of
https://github.com/cryptpad/cryptpad.git
synced 2026-09-14 11:05:41 +05:00
refactor
This commit is contained in:
parent
a1fe68731e
commit
00c1a919c2
@ -25,12 +25,9 @@ define([
|
||||
|
||||
var cpIndex = -1;
|
||||
var msgIndex = -1;
|
||||
var APP = window.APP
|
||||
|
||||
|
||||
var APP = window.APP;
|
||||
var ooMessages = {};
|
||||
var ooCheckpoints = {};
|
||||
var currentMessages = {}
|
||||
var loading = false;
|
||||
var update = function () {};
|
||||
var currentTime;
|
||||
@ -38,49 +35,38 @@ define([
|
||||
// Get an array of the checkpoint IDs sorted their patch index
|
||||
var hashes = config.onlyoffice.hashes;
|
||||
var sortedCp = Object.keys(hashes).map(Number);
|
||||
var id
|
||||
var id;
|
||||
|
||||
var getId = function () {
|
||||
id = Object.keys(ooMessages).length
|
||||
var cps = sortedCp.length;
|
||||
console.log("ooMSGID", ooMessages)
|
||||
return id
|
||||
id = Object.keys(ooMessages).length;
|
||||
return id;
|
||||
};
|
||||
|
||||
|
||||
var endWithCp = sortedCp.length &&
|
||||
config.onlyoffice.lastHash === hashes[sortedCp[sortedCp.length - 1]].hash;
|
||||
|
||||
var fillOO = function (id, messages, ooCheckpoints) {
|
||||
// if (!id) { return; }
|
||||
var checkpoints = []
|
||||
var checkpoints = [];
|
||||
Object.keys(ooCheckpoints).forEach(function(key) {
|
||||
checkpoints.push(ooCheckpoints[key].index)
|
||||
checkpoints.push(ooCheckpoints[key].index);
|
||||
})
|
||||
checkpoints.forEach((current, index) => {
|
||||
var preceding = index > 0 ? checkpoints[index - 1] : 1;
|
||||
ooMessages[index+1] = messages.slice(preceding-1, current-1)
|
||||
ooMessages[index+1] = messages.slice(preceding-1, current-1);
|
||||
});
|
||||
var cpMessages = Object.values(ooMessages).flat().length;
|
||||
var messageDiff = messages.length - cpMessages
|
||||
var messageDiff = messages.length - cpMessages;
|
||||
if (messageDiff !== 0 && cpMessages) {
|
||||
var keys = Object.keys(ooMessages)
|
||||
var keys = Object.keys(ooMessages);
|
||||
var currentM = parseInt(keys[keys.length - 1]);
|
||||
ooMessages[currentM+1] = messages.slice(-messageDiff)
|
||||
ooMessages[currentM+1] = messages.slice(-messageDiff);
|
||||
} else if (messageDiff !== 0 && !cpMessages) {
|
||||
ooMessages[1] = messages
|
||||
ooMessages[1] = messages;
|
||||
}
|
||||
|
||||
id = id ? id : getId()
|
||||
id = id ? id : getId();
|
||||
update();
|
||||
console.log("next fill", id, ooMessages, messages, hashes)
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
// var id = getId();
|
||||
|
||||
if (endWithCp) { cpIndex = 0; }
|
||||
|
||||
var $version, $time, $share;
|
||||
@ -159,7 +145,7 @@ define([
|
||||
var cp = {};
|
||||
|
||||
if (cb) {
|
||||
cb()
|
||||
cb();
|
||||
}
|
||||
|
||||
showVersion();
|
||||
@ -226,6 +212,13 @@ define([
|
||||
}
|
||||
};
|
||||
|
||||
var loadingFalse = function () {
|
||||
setTimeout(function () {
|
||||
$('iframe').blur();
|
||||
loading = false;
|
||||
}, 200);
|
||||
}
|
||||
|
||||
|
||||
var next = function () {
|
||||
msgIndex++;
|
||||
@ -240,6 +233,7 @@ define([
|
||||
var patch = msgs[msgs.length + msgIndex] ? msgs[msgs.length + msgIndex] : undefined
|
||||
var cp = hashes[id-1]
|
||||
config.onPatchBack(cp, [patch])
|
||||
loadingFalse()
|
||||
return
|
||||
} else {
|
||||
id++
|
||||
@ -248,23 +242,20 @@ define([
|
||||
msgIndex = -msgs.length
|
||||
var patch = msgs[msgs.length + msgIndex] ? msgs[msgs.length + msgIndex] : undefined
|
||||
var cp = hashes[id-1]
|
||||
|
||||
config.onPatchBack(cp, [patch])
|
||||
}
|
||||
|
||||
}
|
||||
msgs = ooMessages[id]
|
||||
// var patch = msgs[msgs.length + msgIndex];
|
||||
}
|
||||
// else {
|
||||
var patch = msgs[msgs.length + msgIndex];
|
||||
} else {
|
||||
var patch = msgs[msgs.length + msgIndex];
|
||||
}
|
||||
// }
|
||||
|
||||
config.onPatch(patch)
|
||||
showVersion();
|
||||
setTimeout(function () {
|
||||
$('iframe').blur();
|
||||
loading = false;
|
||||
}, 200);
|
||||
loadingFalse()
|
||||
|
||||
};
|
||||
|
||||
@ -272,30 +263,28 @@ define([
|
||||
|
||||
var prev = function () {
|
||||
loadMoreOOHistory(function() {
|
||||
msgs = ooMessages[id];
|
||||
if (!Object.keys(hashes).length) {
|
||||
msgs = ooMessages[id]
|
||||
var queue = msgs.slice(0, msgIndex)
|
||||
config.onPatchBack({}, queue)
|
||||
|
||||
var cp = {};
|
||||
} else {
|
||||
msgs = ooMessages[id]
|
||||
if (msgs.length+1 === Math.abs(msgIndex) && id !== 0) {
|
||||
id--
|
||||
msgIndex = -1
|
||||
msgs = ooMessages[id]
|
||||
if ((msgs.length+1 === Math.abs(msgIndex) && id !== 0) || (!msgs.length && msgIndex < -1)) {
|
||||
id--;
|
||||
msgIndex = -1;
|
||||
msgs = ooMessages[id];
|
||||
}
|
||||
if (!msgs.length && msgIndex < -1) {
|
||||
id--
|
||||
msgIndex = -1
|
||||
msgs = ooMessages[id]
|
||||
}
|
||||
var queue = msgs.slice(0, msgIndex)
|
||||
var cp = hashes[id-1]
|
||||
config.onPatchBack(cp, queue)
|
||||
}
|
||||
var cp = hashes[id-1];
|
||||
console.log("prev", ooMessages, id, msgs, cp)
|
||||
|
||||
}
|
||||
var queue = msgs.slice(0, msgIndex);
|
||||
console.log("prev0", ooMessages, id, msgs, cp, queue)
|
||||
|
||||
config.onPatchBack(cp, queue);
|
||||
showVersion();
|
||||
msgIndex--;
|
||||
|
||||
});
|
||||
loadingFalse();
|
||||
};
|
||||
|
||||
|
||||
@ -400,7 +389,7 @@ define([
|
||||
update();
|
||||
});
|
||||
$prev.click(function () {
|
||||
// if (loading) { return; }
|
||||
if (loading) { return; }
|
||||
loading = true;
|
||||
prev();
|
||||
update();
|
||||
|
||||
@ -575,7 +575,7 @@ define([
|
||||
APP.oldCursor = d.GetSelectionState();
|
||||
}
|
||||
}
|
||||
if (APP.docEditor) { APP.docEditor.destroyEditor(); } // Kill the old editor
|
||||
if (APP.docEditor && APP.docEditor.destroyEditor() ) { APP.docEditor.destroyEditor(); } // Kill the old editor
|
||||
$('iframe[name="frameEditor"]').after(h('div#cp-app-oo-placeholder-a')).remove();
|
||||
ooLoaded = false;
|
||||
oldLocks = {};
|
||||
@ -587,7 +587,7 @@ define([
|
||||
startOO(blob, type, true);
|
||||
};
|
||||
|
||||
var saveToServer = function (blob, title, msgs) {
|
||||
var saveToServer = function (blob, title) {
|
||||
if (APP.cantCheckpoint) { return; } // TOO_LARGE
|
||||
var text = !blob && getContent();
|
||||
if (!text && !blob) {
|
||||
@ -629,8 +629,7 @@ define([
|
||||
|
||||
var noLogin = false;
|
||||
|
||||
var makeCheckpoint = function (force, msgs) {
|
||||
console.log("revert", APP.revert)
|
||||
var makeCheckpoint = function (force) {
|
||||
if (APP.cantCheckpoint) { return; } // TOO_LARGE
|
||||
|
||||
var locked = content.saveLock;
|
||||
@ -666,7 +665,7 @@ define([
|
||||
content.saveLock = myOOId;
|
||||
APP.onLocal();
|
||||
APP.realtime.onSettle(function () {
|
||||
saveToServer(null, null, msgs);
|
||||
saveToServer();
|
||||
});
|
||||
}
|
||||
};
|
||||
@ -3154,12 +3153,12 @@ Uncaught TypeError: Cannot read property 'calculatedType' of null
|
||||
if (!privateData.ooVersionHash) {
|
||||
(function () {
|
||||
/* add a history button */
|
||||
var commit = function (msgs) {
|
||||
var commit = function () {
|
||||
// Wait for the checkpoint to be uploaded before leaving history mode
|
||||
// (race condition). We use "stopHistory" to remove the history
|
||||
// flag only when the checkpoint is ready.
|
||||
APP.stopHistory = true;
|
||||
makeCheckpoint(true, msgs);
|
||||
makeCheckpoint(true);
|
||||
};
|
||||
var onPatch = function (patch) {
|
||||
// Patch on the current cp
|
||||
@ -3171,7 +3170,7 @@ Uncaught TypeError: Cannot read property 'calculatedType' of null
|
||||
};
|
||||
var onPatchBack = function (cp, msgs) {
|
||||
if (msgs) {
|
||||
msgsFormatted = []
|
||||
msgsFormatted = [];
|
||||
msgs.forEach(function(msg) {
|
||||
var parsedMsg = JSON.parse(msg.msg);
|
||||
|
||||
@ -3181,20 +3180,13 @@ Uncaught TypeError: Cannot read property 'calculatedType' of null
|
||||
author: msg.author,
|
||||
time: msg.time
|
||||
};
|
||||
|
||||
msgsFormatted.push(formattedMsg)
|
||||
|
||||
msgsFormatted.push(formattedMsg);
|
||||
})
|
||||
ooChannel.queue = msgsFormatted;
|
||||
console.log("patch", ooChannel.queue)
|
||||
setTimeout(() => {
|
||||
loadCp(cp, true);
|
||||
}, 100);
|
||||
loadCp(cp, true);
|
||||
} else {
|
||||
loadCp(cp)
|
||||
loadCp(cp);
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
var setHistoryMode = function (bool) {
|
||||
if (bool) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user