Commit 269b40de authored by Alexander.Trofimov's avatar Alexander.Trofimov

fix jszip load history

parent 3ad44948
...@@ -254,98 +254,104 @@ function loadFileContent(url, callback) { ...@@ -254,98 +254,104 @@ function loadFileContent(url, callback) {
}); });
} }
function getJSZipUtils() { function getJSZipUtils() {
return window['JSZipUtils'] ? window['JSZipUtils'] : require('jsziputils'); return window['JSZipUtils'] ? window['JSZipUtils'] : require('jsziputils');
}
function getImageFromChanges (name) {
var file;
var ext = GetFileExtension(name);
if (null !== ext && oZipChanges && (file = oZipChanges.files[name])) {
var oFileArray = file.asUint8Array();
return 'data:image/' + ext + ';base64,' + AscCommon.Base64Encode(oFileArray, oFileArray.length, 0);
} }
return null;
}
function openFileCommand(binUrl, changesUrl, Signature, callback) {
var bError = false, oResult = new OpenFileResult(), bEndLoadFile = false, bEndLoadChanges = false;
var onEndOpen = function() {
if (bEndLoadFile && bEndLoadChanges) {
if (callback) {
callback(bError, oResult);
}
}
};
var sFileUrl = binUrl;
sFileUrl = sFileUrl.replace(/\\/g, "/");
if (!window['IS_NATIVE_EDITOR']) {
asc_ajax({
url: sFileUrl,
dataType: "text",
success: function(result) {
//получаем url к папке с файлом
var url;
var nIndex = sFileUrl.lastIndexOf("/");
url = (-1 !== nIndex) ? sFileUrl.substring(0, nIndex + 1) : sFileUrl;
if (0 < result.length) {
oResult.bSerFormat = Signature === result.substring(0, Signature.length);
oResult.data = result;
oResult.url = url;
} else {
bError = true;
}
bEndLoadFile = true;
onEndOpen();
},
error: function() {
bEndLoadFile = true;
bError = true;
onEndOpen();
}
});
}
if (null != changesUrl) { function getImageFromChanges(name) {
getJSZipUtils().getBinaryContent(changesUrl, function(err, data) { var file;
bEndLoadChanges = true; var ext = GetFileExtension(name);
if (err) { if (null !== ext && oZipChanges && (file = oZipChanges.files[name])) {
bError = true; var oFileArray = file.asUint8Array();
onEndOpen(); return 'data:image/' + ext + ';base64,' + AscCommon.Base64Encode(oFileArray, oFileArray.length, 0);
return; }
} return null;
}
oZipChanges = new (require('jszip'))(data); function openFileCommand(binUrl, changesUrl, Signature, callback) {
oResult.changes = []; var bError = false, oResult = new OpenFileResult(), bEndLoadFile = false, bEndLoadChanges = false;
for (var i in oZipChanges.files) { var onEndOpen = function () {
if (i.endsWith('.json')) { if (bEndLoadFile && bEndLoadChanges) {
// Заглушка на имя файла (стоило его начинать с цифры) if (callback) {
oResult.changes[parseInt(i.slice('changes'.length))] = JSON.parse(oZipChanges.files[i].asText()); callback(bError, oResult);
} }
} }
onEndOpen(); };
}); var sFileUrl = binUrl;
} else { sFileUrl = sFileUrl.replace(/\\/g, "/");
bEndLoadChanges = true;
} if (!window['IS_NATIVE_EDITOR']) {
asc_ajax({
url: sFileUrl, dataType: "text", success: function (result) {
//получаем url к папке с файлом
var url;
var nIndex = sFileUrl.lastIndexOf("/");
url = (-1 !== nIndex) ? sFileUrl.substring(0, nIndex + 1) : sFileUrl;
if (0 < result.length) {
oResult.bSerFormat = Signature === result.substring(0, Signature.length);
oResult.data = result;
oResult.url = url;
} else {
bError = true;
}
bEndLoadFile = true;
onEndOpen();
}, error: function () {
bEndLoadFile = true;
bError = true;
onEndOpen();
}
});
}
if (window['IS_NATIVE_EDITOR']) { if (changesUrl) {
var result = window["native"]["openFileCommand"](sFileUrl, changesUrl, Signature); getJSZipUtils().getBinaryContent(changesUrl, function (err, data) {
if (err) {
bEndLoadChanges = true;
bError = true;
onEndOpen();
return;
}
var url; oResult.changes = [];
var nIndex = sFileUrl.lastIndexOf("/"); require('jszip').loadAsync(data).then(function (zipChanges) {
url = (-1 !== nIndex) ? sFileUrl.substring(0, nIndex + 1) : sFileUrl; var promises = [];
if (0 < result.length) { zipChanges.forEach(function (relativePath, file) {
oResult.bSerFormat = Signature === result.substring(0, Signature.length); if (relativePath.endsWith('.json')) {
oResult.data = result; promises[parseInt(relativePath.slice('changes'.length))] = file.async('string');
oResult.url = url; }
});
Promise.all(promises).then(function (values) {
for (var i = 0; i < values.length; ++i) {
oResult.changes[i] = JSON.parse(values[i]);
}
bEndLoadChanges = true;
onEndOpen();
});
});
});
} else { } else {
bError = true; bEndLoadChanges = true;
} }
bEndLoadFile = true; if (window['IS_NATIVE_EDITOR']) {
onEndOpen(); var result = window["native"]["openFileCommand"](sFileUrl, changesUrl, Signature);
var url;
var nIndex = sFileUrl.lastIndexOf("/");
url = (-1 !== nIndex) ? sFileUrl.substring(0, nIndex + 1) : sFileUrl;
if (0 < result.length) {
oResult.bSerFormat = Signature === result.substring(0, Signature.length);
oResult.data = result;
oResult.url = url;
} else {
bError = true;
}
bEndLoadFile = true;
onEndOpen();
}
} }
}
function sendCommand(editor, fCallback, rdata, dataContainer) { function sendCommand(editor, fCallback, rdata, dataContainer) {
//json не должен превышать размера 2097152, иначе при его чтении будет exception //json не должен превышать размера 2097152, иначе при его чтении будет exception
var docConnectionId = editor.CoAuthoringApi.getDocId(); var docConnectionId = editor.CoAuthoringApi.getDocId();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment