Improve import and export UI. Fix race condition with office import.

pull/1/head
yflory 5 years ago
parent bc8fee8a29
commit 5a26be5531

@ -66,8 +66,10 @@ define([
$files.on('change', function (e) { $files.on('change', function (e) {
var file = e.target.files[0]; var file = e.target.files[0];
var reader = new FileReader(); var reader = new FileReader();
reader.onload = function (e) { f(e.target.result, file); }; var parsed = file && file.name && /.+\.([^.]+)$/.exec(file.name);
if (cfg && cfg.binary) { var ext = parsed && parsed[1];
reader.onload = function (e) { f(e.target.result, file, ext); };
if (cfg && cfg.binary && cfg.binary.indexOf(ext) !== -1) {
reader.readAsArrayBuffer(file, type); reader.readAsArrayBuffer(file, type);
} else { } else {
reader.readAsText(file, type); reader.readAsText(file, type);
@ -1982,9 +1984,10 @@ define([
// Old import button, used in settings // Old import button, used in settings
button button
.click(common.prepareFeedback(type)) .click(common.prepareFeedback(type))
.click(importContent((data && data.binary) ? 'application/octet-stream' : 'text/plain', function (content, file) { .click(importContent((data && data.binary) ? 'application/octet-stream' : 'text/plain', callback, {
callback(content, file); accept: data ? data.accept : undefined,
}, {accept: data ? data.accept : undefined, binary: data ? data.binary : undefined })); binary: data ? data.binary : undefined
}));
//} //}
break; break;
case 'upload': case 'upload':

@ -7,6 +7,7 @@ define([
'/common/common-interface.js', '/common/common-interface.js',
'/common/common-hash.js', '/common/common-hash.js',
'/common/common-util.js', '/common/common-util.js',
'/common/common-ui-elements.js',
'/api/config', '/api/config',
'/customize/messages.js', '/customize/messages.js',
'/customize/application_config.js', '/customize/application_config.js',
@ -31,6 +32,7 @@ define([
UI, UI,
Hash, Hash,
Util, Util,
UIElements,
ApiConfig, ApiConfig,
Messages, Messages,
AppConfig, AppConfig,
@ -269,9 +271,11 @@ define([
APP.realtime.onSettle(function () { APP.realtime.onSettle(function () {
fixSheets(); fixSheets();
UI.log(Messages.saved); UI.log(Messages.saved);
if (ev.callback) { APP.realtime.onSettle(function () {
return void ev.callback(); if (ev.callback) {
} return void ev.callback();
}
});
}); });
sframeChan.query('Q_OO_COMMAND', { sframeChan.query('Q_OO_COMMAND', {
cmd: 'UPDATE_HASH', cmd: 'UPDATE_HASH',
@ -486,6 +490,7 @@ define([
ooChannel.queue.forEach(function (data) { ooChannel.queue.forEach(function (data) {
Array.prototype.push.apply(changes, data.msg.changes); Array.prototype.push.apply(changes, data.msg.changes);
}); });
ooChannel.lastHash = getLastCp().hash;
send({ send({
type: "authChanges", type: "authChanges",
changes: changes changes: changes
@ -839,18 +844,6 @@ define([
makeChannel(); makeChannel();
}; };
var exportFile = function() {
var text = getContent();
var suggestion = Title.suggestTitle(Title.defaultTitle);
UI.prompt(Messages.exportPrompt,
Util.fixFileName(suggestion) + '.bin', function (filename) {
if (!(typeof(filename) === 'string' && filename)) { return; }
var blob = new Blob([text], {type: "application/bin;charset=utf-8"});
saveAs(blob, filename);
});
};
var x2tInitialized = false; var x2tInitialized = false;
var x2tInit = function(x2t) { var x2tInit = function(x2t) {
debug("x2t mount"); debug("x2t mount");
@ -911,7 +904,19 @@ define([
}; };
var x2tSaveAndConvertDataInternal = function(x2t, data, filename, extension, finalFilename) { var x2tSaveAndConvertDataInternal = function(x2t, data, filename, extension, finalFilename) {
var xlsData = x2tConvertDataInternal(x2t, data, filename, extension); var type = common.getMetadataMgr().getPrivateData().ooType;
var xlsData;
if (type === "sheet" && extension !== 'xlsx') {
xlsData = x2tConvertDataInternal(x2t, data, filename, 'xlsx');
filename += '.xlsx';
} else if (type === "ooslide" && extension !== "pptx") {
xlsData = x2tConvertDataInternal(x2t, data, filename, 'pptx');
filename += '.pptx';
} else if (type === "oodoc" && extension !== "docx") {
xlsData = x2tConvertDataInternal(x2t, data, filename, 'docx');
filename += '.docx';
}
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);
@ -941,18 +946,55 @@ define([
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', /*'.ods',*/ '.bin'];
var type = common.getMetadataMgr().getPrivateData().ooType; var type = common.getMetadataMgr().getPrivateData().ooType;
var warning = '';
if (type==="ooslide") { if (type==="ooslide") {
ext = "pptx"; ext = ['.pptx', /*'.odp',*/ '.bin'];
} else if (type==="oodoc") { } else if (type==="oodoc") {
ext = "docx"; ext = ['.docx', /*'.odt',*/ '.bin'];
} }
UI.prompt(Messages.exportPrompt, Util.fixFileName(suggestion) + '.' + ext, function (filename) { if (typeof(Atomics) === "undefined") {
if (!(typeof(filename) === 'string' && filename)) { return; } ext = ['.bin'];
x2tSaveAndConvertData(text, "filename.bin", ext, filename); warning = 'Use chrome'; // XXX tell the user they can export as Office format with Chrome?
}
var types = ext.map(function (val) {
return {
tag: 'a',
attributes: {
'data-value': val,
href: '#'
},
content: val
};
}); });
var dropdownConfig = {
text: ext[0], // Button initial text
caretDown: true,
options: types, // Entries displayed in the menu
isSelect: true,
initialValue: ext[0],
common: common
};
var $select = UIElements.createDropdown(dropdownConfig);
var warningText = warning ? ('<br>' + warning) : '';
UI.prompt(Messages.exportPrompt+warningText, Util.fixFileName(suggestion), function (filename) {
// $select.getValue()
if (!(typeof(filename) === 'string' && filename)) { return; }
var ext = ($select.getValue() || '').slice(1);
if (ext === 'bin') {
var blob = new Blob([text], {type: "application/bin;charset=utf-8"});
saveAs(blob, filename+'.bin');
return;
}
x2tSaveAndConvertData(text, "filename.bin", ext, filename+'.'+ext);
}, {
typeInput: $select[0]
}, true);
}; };
var x2tImportImagesInternal = function(x2t, images, i, callback) { var x2tImportImagesInternal = function(x2t, images, i, callback) {
@ -1053,7 +1095,7 @@ define([
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.error); // XXX?
   }    }
var blob = new Blob([content], {type: 'plain/text'}); var blob = new Blob([content], {type: 'plain/text'});
var file = getFileType(); var file = getFileType();
@ -1075,10 +1117,13 @@ define([
APP.FM.handleFile(blob, data); APP.FM.handleFile(blob, data);
}; };
var importXLSXFile = function(content, filename) { var importXLSXFile = function(content, filename, ext) {
// Perform the x2t conversion // Perform the x2t conversion
debug("Filename"); debug("Filename");
debug(filename); debug(filename);
if (ext === "bin") {
return void importFile(content);
}
require(['/common/onlyoffice/x2t/x2t.js'], function() { require(['/common/onlyoffice/x2t/x2t.js'], function() {
var x2t = window.Module; var x2t = window.Module;
x2t.run(); x2t.run();
@ -1251,21 +1296,11 @@ define([
}).attr('title', 'Restore last checkpoint').appendTo($rightside); }).attr('title', 'Restore last checkpoint').appendTo($rightside);
} }
var $export = common.createButton('export', true, {}, exportFile); var $exportXLSX = common.createButton('export', true, {}, exportXLSXFile);
$export.appendTo($rightside); $exportXLSX.appendTo($rightside);
if (typeof(Atomics) !== "undefined") { var $importXLSX = common.createButton('import', true, { accept: [".bin", ".ods", ".xlsx"], binary : ["ods", "xlsx"] }, importXLSXFile);
var $exportXLSX = common.createButton('export', true, {}, exportXLSXFile); $importXLSX.appendTo($rightside);
$exportXLSX.appendTo($rightside);
}
var $import = common.createButton('import', true, {}, importFile);
$import.appendTo($rightside);
if (typeof(Atomics) !== "undefined") {
var $importXLSX = common.createButton('import', true, { accept: ["xlsx"], types: ["xlsx"], binary : true }, importXLSXFile);
$importXLSX.appendTo($rightside);
}
if (common.isLoggedIn()) { if (common.isLoggedIn()) {
common.createButton('hashtag', true).appendTo($rightside); common.createButton('hashtag', true).appendTo($rightside);

Loading…
Cancel
Save