retab + jshint

pull/1/head
yflory 5 years ago
parent 4e537e6ddc
commit a1a2e6659e

@ -18,7 +18,6 @@ define([
'/common/outer/worker-channel.js', '/common/outer/worker-channel.js',
'/bower_components/file-saver/FileSaver.min.js', '/bower_components/file-saver/FileSaver.min.js',
'/common/sframe-common-file.js',
'css!/bower_components/bootstrap/dist/css/bootstrap.min.css', 'css!/bower_components/bootstrap/dist/css/bootstrap.min.css',
'less!/bower_components/components-font-awesome/css/font-awesome.min.css', 'less!/bower_components/components-font-awesome/css/font-awesome.min.css',
@ -40,8 +39,7 @@ define([
EmptyCell, EmptyCell,
EmptyDoc, EmptyDoc,
EmptySlide, EmptySlide,
Channel, Channel)
SFCFile)
{ {
var saveAs = window.saveAs; var saveAs = window.saveAs;
var Nacl = window.nacl; var Nacl = window.nacl;
@ -56,7 +54,7 @@ define([
var debug = function (x) { var debug = function (x) {
if (!window.CP_DEV_MODE) { return; } if (!window.CP_DEV_MODE) { return; }
console.log(x); console.debug(x);
}; };
var stringify = function (obj) { var stringify = function (obj) {
@ -89,11 +87,8 @@ define([
var mediasData = {}; var mediasData = {};
var getMediasSources = APP.getMediasSources = function() { var getMediasSources = APP.getMediasSources = function() {
if (!content.mediasSources) { return content.mediasSources = content.mediaSources || {};
content.mediasSources = {} };
}
return content.mediasSources;
}
var getId = function () { var getId = function () {
return metadataMgr.getNetfluxId() + '-' + privateData.clientId; return metadataMgr.getNetfluxId() + '-' + privateData.clientId;
@ -242,8 +237,7 @@ define([
var editor = window.frames[0].editor; var editor = window.frames[0].editor;
// if we are not in the sheet app // if we are not in the sheet app
// we should not call this code // we should not call this code
if (typeof editor.GetSheets === 'undefined') if (typeof editor.GetSheets === 'undefined') { return; }
return;
var s = editor.GetSheets(); var s = editor.GetSheets();
if (s.length === 0) { return; } if (s.length === 0) { return; }
var wb = s[0].worksheet.workbook; var wb = s[0].worksheet.workbook;
@ -717,6 +711,7 @@ define([
'#panel-settings-general tr.autosave { display: none !important; }' + '#panel-settings-general tr.autosave { display: none !important; }' +
'#panel-settings-general tr.coauth { display: none !important; }' + '#panel-settings-general tr.coauth { display: none !important; }' +
'#header { display: none !important; }'; '#header { display: none !important; }';
// #asc-gen566 // XXX
// + '#id-toolbar-full-placeholder-btn-insertimage { display: none; }' + // + '#id-toolbar-full-placeholder-btn-insertimage { display: none; }' +
// '#id-toolbar-full-placeholder-btn-insertequation { display: none; }'; // '#id-toolbar-full-placeholder-btn-insertequation { display: none; }';
$('<style>').text(css).appendTo($tb); $('<style>').text(css).appendTo($tb);
@ -734,81 +729,83 @@ define([
}; };
common.initFilePicker({ common.initFilePicker({
onSelect: function (data) { onSelect: function (data) {
if (data.type !== 'file') { if (data.type !== 'file') {
console.log("Unexpected data type picked " + data.type); debug("Unexpected data type picked " + data.type);
return; return;
} }
if (data.type !== 'file') { console.log('unhandled embed type ' + data.type); return; } var privateDat = cpNfInner.metadataMgr.getPrivateData();
var privateDat = cpNfInner.metadataMgr.getPrivateData(); var origin = privateDat.fileHost || privateDat.origin;
var origin = privateDat.fileHost || privateDat.origin; var name = data.name;
var name = data.name;
// Add image to the list
// Add image to the list var mediasSources = getMediasSources();
var mediasSources = getMediasSources(); mediasSources[name] = data;
mediasSources[name] = data;
APP.getImageURL(name, function(url) {
APP.getImageURL(name, function(url) { debug("CRYPTPAD success add " + name);
console.log("CRYPTPAD success add " + name); APP.AddImageSuccessCallback({
APP.AddImageSuccessCallback({ name: name,
name: name, url: url
url: url });
}); });
}); }
}
}); });
APP.AddImage = function(cb1, cb2) { APP.AddImage = function(cb1, cb2) {
APP.AddImageSuccessCallback = cb1; APP.AddImageSuccessCallback = cb1;
APP.AddImageErrorCallback = cb2; APP.AddImageErrorCallback = cb2;
common.openFilePicker({ common.openFilePicker({
types: ['file'], types: ['file'],
where: ['root'] where: ['root']
}); });
} };
APP.getImageURL = function(name, callback) { APP.getImageURL = function(name, callback) {
var mediasSources = getMediasSources(); var mediasSources = getMediasSources();
var data = mediasSources[name]; var data = mediasSources[name];
if (typeof data === 'undefined') { if (typeof data === 'undefined') {
console.log("CryptPad - could not find matching media for " + name); debug("CryptPad - could not find matching media for " + name);
callback(""); return void callback("");
return;
}
var blobUrl = (typeof mediasData[data.src] === 'undefined') ? "" : mediasData[data.src].src;
if (blobUrl != "") {
console.log("CryptPad Image already loaded " + blobUrl);
callback(blobUrl);
return;
}
Util.fetch(data.src, function (err, u8) {
try {
console.log("Decrypt with key " + data.key);
FileCrypto.decrypt(u8, Nacl.util.decodeBase64(data.key), function (err, res) {
if (err || !res.content) { console.log("Decrypting failed"); callback(""); }
var blobUrl = URL.createObjectURL(res.content);
// store media blobUrl and content for cache and export
var mediaData = { blobUrl : blobUrl, content : "" };
mediasData[data.src] = mediaData;
var reader = new FileReader();
reader.onloadend = (event) => {
console.log("MediaData set");
mediaData.content = reader.result;
}
reader.readAsArrayBuffer(res.content);
console.log("Adding CryptPad Image " + data.name + ": " + blobUrl);
window.frames[0].AscCommon.g_oDocumentUrls.addImageUrl(data.name, blobUrl);
callback(blobUrl);
});
} catch(e) {
console.log("Exception decrypting image " + data.name);
callback("");
} }
});
} var blobUrl = (typeof mediasData[data.src] === 'undefined') ? "" : mediasData[data.src].src;
if (blobUrl) {
debug("CryptPad Image already loaded " + blobUrl);
return void callback(blobUrl);
}
Util.fetch(data.src, function (err, u8) {
try {
debug("Decrypt with key " + data.key);
FileCrypto.decrypt(u8, Nacl.util.decodeBase64(data.key), function (err, res) {
if (err || !res.content) {
debug("Decrypting failed");
return void callback("");
}
var blobUrl = URL.createObjectURL(res.content);
// store media blobUrl and content for cache and export
var mediaData = { blobUrl : blobUrl, content : "" };
mediasData[data.src] = mediaData;
var reader = new FileReader();
reader.onloadend = function (event) {
debug("MediaData set");
mediaData.content = reader.result;
}
reader.readAsArrayBuffer(res.content);
debug("Adding CryptPad Image " + data.name + ": " + blobUrl);
window.frames[0].AscCommon.g_oDocumentUrls.addImageUrl(data.name, blobUrl);
callback(blobUrl);
});
} catch (e) {
debug("Exception decrypting image " + data.name);
console.error(e);
callback("");
}
});
};
APP.docEditor = new window.DocsAPI.DocEditor("cp-app-oo-placeholder", APP.ooconfig); APP.docEditor = new window.DocsAPI.DocEditor("cp-app-oo-placeholder", APP.ooconfig);
ooLoaded = true; ooLoaded = true;
@ -829,212 +826,195 @@ define([
var x2tInitialized = false; var x2tInitialized = false;
var x2tInit = function(x2t) { var x2tInit = function(x2t) {
console.log("x2t mount"); debug("x2t mount");
// x2t.FS.mount(x2t.MEMFS, {} , '/'); // x2t.FS.mount(x2t.MEMFS, {} , '/');
x2t.FS.mkdir('/working'); x2t.FS.mkdir('/working');
x2t.FS.mkdir('/working/media'); x2t.FS.mkdir('/working/media');
console.log("x2t mount done"); x2tInitialized = true;
} debug("x2t mount done");
};
/*
Converting Data /*
Converting Data
This function converts a data in a specific format to the outputformat
The filename extension needs to represent the input format This function converts a data in a specific format to the outputformat
Example: fileName=cryptpad.bin outputFormat=xlsx The filename extension needs to represent the input format
*/ Example: fileName=cryptpad.bin outputFormat=xlsx
var x2tConvertDataInternal = function(x2t, data, fileName, outputFormat) { */
console.log("Converting Data for " + fileName + " to " + outputFormat); var x2tConvertDataInternal = function(x2t, data, fileName, outputFormat) {
// writing file to mounted working disk (in memory) debug("Converting Data for " + fileName + " to " + outputFormat);
x2t.FS.writeFile('/working/' + fileName, data); // writing file to mounted working disk (in memory)
x2t.FS.writeFile('/working/' + fileName, data);
// Adding images
for (var mediaFileName in window.frames[0].AscCommon.g_oDocumentUrls.urls) { // Adding images
var mediaFileName = mediaFileName.substring(6); for (var mediaFileName in window.frames[0].AscCommon.g_oDocumentUrls.urls) {
var mediasSources = getMediasSources(); var mediaFileName = mediaFileName.substring(6);
var mediaSource = mediasSources[mediaFileName]; var mediasSources = getMediasSources();
var mediaData = (typeof mediaSource === 'undefined') ? mediaSource : mediasData[mediaSource.src]; var mediaSource = mediasSources[mediaFileName];
if (typeof mediaData !== 'undefined') { var mediaData = mediaSource ? mediasData[mediaSource.src] : undefined;
console.log("Writing media data " + mediaFileName); if (mediaData) {
console.log("Data"); debug("Writing media data " + mediaFileName);
debug("Data");
var fileData = mediaData.content; var fileData = mediaData.content;
x2t.FS.writeFile('/working/media/' + mediaFileName, new Uint8Array(fileData)); x2t.FS.writeFile('/working/media/' + mediaFileName, new Uint8Array(fileData));
} else { } else {
console.log("Could not find media content for " + mediaFileName); debug("Could not find media content for " + mediaFileName);
}
} }
}
var params = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" var params = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<TaskQueueDataConvert xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" + "<TaskQueueDataConvert xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
+ "<m_sFileFrom>/working/" + fileName + "</m_sFileFrom>" + "<m_sFileFrom>/working/" + fileName + "</m_sFileFrom>"
+ "<m_sFileTo>/working/" + fileName + "." + outputFormat + "</m_sFileTo>" + "<m_sFileTo>/working/" + fileName + "." + outputFormat + "</m_sFileTo>"
+ "<m_bIsNoBase64>false</m_bIsNoBase64>" + "<m_bIsNoBase64>false</m_bIsNoBase64>"
+ "</TaskQueueDataConvert>" + "</TaskQueueDataConvert>";
// writing params file to mounted working disk (in memory) // writing params file to mounted working disk (in memory)
x2t.FS.writeFile('/working/params.xml', params); x2t.FS.writeFile('/working/params.xml', params);
// running conversion // running conversion
x2t.ccall("runX2T", ["number"], ["string"], ["/working/params.xml"]); x2t.ccall("runX2T", ["number"], ["string"], ["/working/params.xml"]);
// reading output file from working disk (in memory) // reading output file from working disk (in memory)
try { try {
var result = x2t.FS.readFile('/working/' + fileName + "." + outputFormat); var result = x2t.FS.readFile('/working/' + fileName + "." + outputFormat);
} catch (e) { } catch (e) {
console.log("Failed reading converted file"); debug("Failed reading converted file");
return ""; return "";
} }
return result; return result;
} };
var x2tSaveAndConvertDataInternal = function(x2t, data, filename, extension, finalFilename) { var x2tSaveAndConvertDataInternal = function(x2t, data, filename, extension, finalFilename) {
var xlsData = x2tConvertDataInternal(x2t, data, filename, extension); var xlsData = x2tConvertDataInternal(x2t, data, filename, extension);
if (xlsData!="") { if (xlsData) {
var blob = new Blob([xlsData], {type: "application/bin;charset=utf-8"}); var blob = new Blob([xlsData], {type: "application/bin;charset=utf-8"});
saveAs(blob, finalFilename); saveAs(blob, finalFilename);
} }
} };
var x2tSaveAndConvertData = function(data, filename, extension, finalFilename) { var x2tSaveAndConvertData = function(data, filename, extension, finalFilename) {
// Perform the x2t conversion // Perform the x2t conversion
require(['/common/onlyoffice/x2t/x2t.js'], function() { require(['/common/onlyoffice/x2t/x2t.js'], function() {
var x2t = Module; var x2t = Module;
x2t.run(); x2t.run();
if (x2tInitialized) { if (x2tInitialized) {
console.log("x2t runtime already initialized"); debug("x2t runtime already initialized");
x2tSaveAndConvertDataInternal(x2t, data, filename, extension, finalFilename); x2tSaveAndConvertDataInternal(x2t, data, filename, extension, finalFilename);
} }
x2t.onRuntimeInitialized = function() { x2t.onRuntimeInitialized = function() {
console.log("x2t in runtime initialized"); debug("x2t in runtime initialized");
// Init x2t js module // Init x2t js module
x2tInit(x2t); x2tInit(x2t);
x2tInitialized = true; x2tSaveAndConvertDataInternal(x2t, data, filename, extension, finalFilename);
x2tSaveAndConvertDataInternal(x2t, data, filename, extension, finalFilename); };
} });
}); };
}
var exportXLSXFile = function() { var exportXLSXFile = function() {
var text = getContent(); var text = getContent();
var suggestion = Title.suggestTitle(Title.defaultTitle); var suggestion = Title.suggestTitle(Title.defaultTitle);
var ext="xlsx"; var ext = "xlsx";
var type = common.getMetadataMgr().getPrivateData().ooType; var type = common.getMetadataMgr().getPrivateData().ooType;
if (type==="ooslide") if (type==="ooslide") {
ext = "pptx"; ext = "pptx";
else if (type==="oodoc") } else if (type==="oodoc") {
ext = "docx"; ext = "docx";
}
UI.prompt(Messages.exportPrompt, UI.prompt(Messages.exportPrompt, Util.fixFileName(suggestion) + '.' + ext, function (filename) {
Util.fixFileName(suggestion) + '.' + ext, function (filename) { if (!(typeof(filename) === 'string' && filename)) { return; }
if (!(typeof(filename) === 'string' && filename)) { return; } x2tSaveAndConvertData(text, "filename.bin", ext, filename);
x2tSaveAndConvertData(text, "filename.bin", ext, filename); });
});
}; };
var x2tImportImagesInternal = function(x2t, images, i, callback) { var x2tImportImagesInternal = function(x2t, images, i, callback) {
if (i>=images.length) { if (i >= images.length) {
callback(); callback();
} else { } else {
console.log("Import image " + i); debug("Import image " + i);
var handleFileData = { var handleFileData = {
name: images[i], name: images[i],
mediasSources: getMediasSources(), mediasSources: getMediasSources(),
callback: function() { callback: function() {
console.log("next image"); debug("next image");
x2tImportImagesInternal(x2t, images, i+1, callback); x2tImportImagesInternal(x2t, images, i+1, callback);
}, },
}; };
var filePath = "/working/media/" + images[i]; var filePath = "/working/media/" + images[i];
console.log("Import filename " + filePath); debug("Import filename " + filePath);
var fileData = x2t.FS.readFile("/working/media/" + images[i], { encoding : "binary" }); var fileData = x2t.FS.readFile("/working/media/" + images[i], { encoding : "binary" });
console.log("Importing data"); debug("Importing data");
console.log("Buffer"); debug("Buffer");
console.log(fileData.buffer); debug(fileData.buffer);
var blob = new Blob([fileData.buffer], {type: 'image/png'}); var blob = new Blob([fileData.buffer], {type: 'image/png'});
blob.name = images[i]; blob.name = images[i];
APP.FMImages.handleFile(blob, handleFileData); APP.FMImages.handleFile(blob, handleFileData);
} }
} };
var x2tImportImages = function (x2t, callback) { var x2tImportImages = function (x2t, callback) {
if (!APP.FMImages) { if (!APP.FMImages) {
var fmConfigImages = { var fmConfigImages = {
noHandlers: true, noHandlers: true,
noStore: true, noStore: true,
body: $('body'), body: $('body'),
onUploaded: function (ev, data) { onUploaded: function (ev, data) {
if (!ev.callback) { return; } if (!ev.callback) { return; }
console.log("Image uploaded at " + data.url); debug("Image uploaded at " + data.url);
var parsed = Hash.parsePadUrl(data.url); var parsed = Hash.parsePadUrl(data.url);
if (parsed.type === 'file') { if (parsed.type === 'file') {
var secret = Hash.getSecrets('file', parsed.hash, data.password); var secret = Hash.getSecrets('file', parsed.hash, data.password);
var fileHost = privateData.fileHost || privateData.origin; var fileHost = privateData.fileHost || privateData.origin;
var src = fileHost + Hash.getBlobPathFromHex(secret.channel); var src = fileHost + Hash.getBlobPathFromHex(secret.channel);
var key = Hash.encodeBase64(secret.keys.cryptKey); var key = Hash.encodeBase64(secret.keys.cryptKey);
console.log("Final src: " + src); debug("Final src: " + src);
ev.mediasSources[ev.name] = { name : ev.name, src : src, key : key }; ev.mediasSources[ev.name] = { name : ev.name, src : src, key : key };
} }
ev.callback(); ev.callback();
}
};
APP.FMImages = common.createFileManager(fmConfigImages);
} }
};
APP.FMImages = common.createFileManager(fmConfigImages);
}
// Import Images
console.log("Import Images");
var files = x2t.FS.readdir("/working/media/");
var images = [];
files.forEach(file => {
if (file!="." && file!="..")
images.push(file);
});
x2tImportImagesInternal(x2t, images, 0, function() {
console.log("Sync media sources elements");
console.log(getMediasSources());
APP.onLocal();
console.log("Import Images finalized");
callback();
});
}
var x2tConvertData = function (x2t, data, filename, extension, callback) {
var convertedContent;
if (filename.endsWith(".ods")) {
convertedContent = x2tConvertDataInternal(x2t, new Uint8Array(data), filename, "xlsx");
convertedContent = x2tConvertDataInternal(x2t, convertedContent, filename + ".xlsx", extension);
} else {
convertedContent = x2tConvertDataInternal(x2t, new Uint8Array(data), filename, extension);
}
x2tImportImages(x2t, function() {
callback(convertedContent);
});
}
var importXLSXFile = function(content, filename) { // Import Images
// Perform the x2t conversion debug("Import Images");
console.log("Filename"); var files = x2t.FS.readdir("/working/media/");
console.log(filename); var images = [];
require(['/common/onlyoffice/x2t/x2t.js'], function() { files.forEach(function (file) {
var x2t = Module; if (file !== "." && file !== "..") {
x2t.run(); images.push(file);
if (x2tInitialized) {
console.log("x2t runtime already initialized");
x2tConvertData(x2t, new Uint8Array(content), filename.name, "bin", function(convertedContent) {
importFile(convertedContent);
});
} }
});
x2tImportImagesInternal(x2t, images, 0, function() {
debug("Sync media sources elements");
debug(getMediasSources());
APP.onLocal();
debug("Import Images finalized");
callback();
});
};
x2t.onRuntimeInitialized = function() {
console.log("x2t in runtime initialized"); var x2tConvertData = function (x2t, data, filename, extension, callback) {
// Init x2t js module var convertedContent;
x2tInit(x2t); // Convert from ODF format:
x2tInitialized = true; // first convert to Office format then to the selected extension
var convertedContent = x2tConvertData(x2t, new Uint8Array(content), filename.name, "bin", function(convertedContent) { if (filename.endsWith(".ods")) {
importFile(convertedContent); convertedContent = x2tConvertDataInternal(x2t, new Uint8Array(data), filename, "xlsx");
convertedContent = x2tConvertDataInternal(x2t, convertedContent, filename + ".xlsx", extension);
} else if (filename.endsWith(".odt")) {
convertedContent = x2tConvertDataInternal(x2t, new Uint8Array(data), filename, "docx");
convertedContent = x2tConvertDataInternal(x2t, convertedContent, filename + ".docx", extension);
} else if (filename.endsWith(".odp")) {
convertedContent = x2tConvertDataInternal(x2t, new Uint8Array(data), filename, "pptx");
convertedContent = x2tConvertDataInternal(x2t, convertedContent, filename + ".pptx", extension);
} else {
convertedContent = x2tConvertDataInternal(x2t, new Uint8Array(data), filename, extension);
}
x2tImportImages(x2t, function() {
callback(convertedContent);
}); });
} };
});
}
var importFile = function(content) { var importFile = function(content) {
// Abort if there is another real user in the channel (history keeper excluded) // Abort if there is another real user in the channel (history keeper excluded)
@ -1044,7 +1024,7 @@ define([
if (m.length > 1) { if (m.length > 1) {
return void UI.alert(Messages.oo_cantUpload); return void UI.alert(Messages.oo_cantUpload);
} }
if (content=="") { if (!content) {
return void UI.alert(Messages.oo_cantUpload); return void UI.alert(Messages.oo_cantUpload);
   }    }
var blob = new Blob([content], {type: 'plain/text'}); var blob = new Blob([content], {type: 'plain/text'});
@ -1053,7 +1033,7 @@ define([
var uploadedCallback = function() { var uploadedCallback = function() {
UI.confirm(Messages.oo_uploaded, function (yes) { UI.confirm(Messages.oo_uploaded, function (yes) {
try { try {
window.frames[0].editor.setViewModeDisconnect(); window.frames[0].editor.setViewModeDisconnect();
} catch (e) {} } catch (e) {}
if (!yes) { return; } if (!yes) { return; }
common.gotoURL(); common.gotoURL();
@ -1067,9 +1047,30 @@ define([
APP.FM.handleFile(blob, data); APP.FM.handleFile(blob, data);
}; };
var importXLSXFile = function(content, filename) {
// Perform the x2t conversion
debug("Filename");
debug(filename);
require(['/common/onlyoffice/x2t/x2t.js'], function() {
var x2t = Module;
x2t.run();
if (x2tInitialized) {
debug("x2t runtime already initialized");
x2tConvertData(x2t, new Uint8Array(content), filename.name, "bin", function(convertedContent) {
importFile(convertedContent);
});
}
x2t.onRuntimeInitialized = function() {
debug("x2t in runtime initialized");
// Init x2t js module
x2tInit(x2t);
var convertedContent = x2tConvertData(x2t, new Uint8Array(content), filename.name, "bin", function(convertedContent) {
importFile(convertedContent);
});
};
});
};
var loadLastDocument = function () { var loadLastDocument = function () {
var lastCp = getLastCp(); var lastCp = getLastCp();
@ -1139,7 +1140,7 @@ define([
JSON.parse(content); JSON.parse(content);
return true; return true;
} catch (e) { } catch (e) {
console.log("Failed to parse, rejecting patch"); debug("Failed to parse, rejecting patch");
return false; return false;
} }
} }
@ -1151,7 +1152,7 @@ define([
window.frames[0].editor.setViewModeDisconnect(true); window.frames[0].editor.setViewModeDisconnect(true);
} catch (e) {} } catch (e) {}
} }
console.log(state); debug(state);
}; };
var stringifyInner = function () { var stringifyInner = function () {

Loading…
Cancel
Save