Commit 4edfa1cd authored by Boris Kocherov's avatar Boris Kocherov

[erp5_only_office] use Editor.bin as file content document and simplify save/open

parent ff0d9fdf
/*global window, rJS, RSVP, DocsAPI, console, document, /*global window, rJS, RSVP, DocsAPI, console, document,
Common, require, jIO, URL, FileReader, atob, ArrayBuffer, Common, require, jIO, URL, FileReader, atob, ArrayBuffer,
Uint8Array, XMLHttpRequest, Blob, Rusha, define*/ Uint8Array, XMLHttpRequest, Blob, Rusha, define,
TextDecoder, DesktopOfflineAppDocumentEndSave*/
/*jslint nomen: true, maxlen:80, indent:2*/ /*jslint nomen: true, maxlen:80, indent:2*/
"use strict"; "use strict";
if (Common === undefined) { if (Common === undefined) {
...@@ -71,8 +72,8 @@ DocsAPI.DocEditor.version = function () { ...@@ -71,8 +72,8 @@ DocsAPI.DocEditor.version = function () {
.declareMethod("jio_getAttachment", function (docId, attId, opt) { .declareMethod("jio_getAttachment", function (docId, attId, opt) {
var g = this, var g = this,
queue; queue;
if (attId === 'body.txt') { if (attId === 'body.txt' || attId === 'Editor.bin') {
opt = 'asText'; opt = 'asBinArray';
if (!docId) { if (!docId) {
docId = '/'; docId = '/';
} }
...@@ -89,6 +90,11 @@ DocsAPI.DocEditor.version = function () { ...@@ -89,6 +90,11 @@ DocsAPI.DocEditor.version = function () {
.then(function (evt) { .then(function (evt) {
return evt.target.result; return evt.target.result;
}); });
} else if (opt === "asBinArray") {
data = jIO.util.readBlobAsArrayBuffer(blob)
.then(function (evt) {
return new Uint8ClampedArray(evt.target.result);
});
} else if (opt === "asBlobURL") { } else if (opt === "asBlobURL") {
data = URL.createObjectURL(blob); data = URL.createObjectURL(blob);
} else if (opt === "asDataURL") { } else if (opt === "asDataURL") {
...@@ -246,7 +252,7 @@ DocsAPI.DocEditor.version = function () { ...@@ -246,7 +252,7 @@ DocsAPI.DocEditor.version = function () {
return ""; return "";
}) })
.push(function (portal_type) { .push(function (portal_type) {
var value, documentType, magic; var value;
portal_type = portal_type || options.portal_type; portal_type = portal_type || options.portal_type;
g.props.binary_loader = false; g.props.binary_loader = false;
g.props.jio_key = options.jio_key; g.props.jio_key = options.jio_key;
...@@ -276,27 +282,36 @@ DocsAPI.DocEditor.version = function () { ...@@ -276,27 +282,36 @@ DocsAPI.DocEditor.version = function () {
type: "zipfile", type: "zipfile",
file: value file: value
}); });
return g.props.value_zip_storage.getAttachment('/', 'body.txt') return g.jio_getAttachment('/', 'body.txt')
.push(undefined, function (error) {
if (error.status_code === 404) {
return g.jio_getAttachment('/', 'Editor.bin')
.push(undefined, function (error) { .push(undefined, function (error) {
if (error.status_code === 404) { if (error.status_code === 404) {
throw 'not supported format of document: body.txt absent "' + throw 'not supported format of document:' +
' body.txt/Editor.bin absent "' +
value.slice(0, 100) + '"'; value.slice(0, 100) + '"';
} }
throw error; throw error;
})
.push(jIO.util.readBlobAsText)
.push(function (evt) {
return evt.target.result;
}); });
} }
throw error;
});
} }
}
})
.push(function (value) {
var documentType, magic;
g.props.value = value; g.props.value = value;
if (!g.props.documentType && value === "") { if (!g.props.documentType && value === "") {
throw "can not create empty document " + throw "can not create empty document " +
"because portal_type is unknown"; "because portal_type is unknown";
} }
if (value) { if (value) {
magic = g.props.value.slice(0, 4); magic = value.slice(0, 4);
if (typeof magic !== 'string') {
magic = String.fromCharCode.apply(null, magic);
}
switch (magic) { switch (magic) {
case 'XLSY': case 'XLSY':
documentType = "spreadsheet"; documentType = "spreadsheet";
...@@ -427,23 +442,55 @@ DocsAPI.DocEditor.version = function () { ...@@ -427,23 +442,55 @@ DocsAPI.DocEditor.version = function () {
}); });
}) })
.declareMethod("jio_save", function (data) {
var g = this,
zip = g.props.value_zip_storage;
return new RSVP.Queue()
.push(function () {
if (data) {
return g.jio_putAttachment('/', 'body.txt', data)
.push(function () {
// cleanup if Editor.bin exist
return zip.removeAttachment('/', 'Editor.bin')
.push(undefined, function (error) {
if (error.status_code !== 404) {
throw error;
}
});
})
.push(undefined, function (error) {
display_error(g, error);
});
}
})
.push(function () {
if (g.props.save_defer) {
// if we are run from getContent
g.props.save_defer.resolve();
g.props.save_defer = null;
} else {
g.triggerSubmit();
}
});
})
.declareMethod('getContent', function () { .declareMethod('getContent', function () {
var g = this, var g = this,
zip = g.props.value_zip_storage, zip = g.props.value_zip_storage,
queue = new RSVP.Queue(), queue = new RSVP.Queue();
save_defer = RSVP.defer(); if (g.props.handlers.save()) {
g.props.save_defer = save_defer; g.props.save_defer = RSVP.defer();
g.props.handlers.save(); }
return queue.push(function () { return queue.push(function () {
return save_defer.promise; if (g.props.save_defer) {
}) return g.props.save_defer.promise;
.push(function (data) {
if (data) {
var body = data[g.props.key];
return zip.putAttachment('/', 'body.txt', body);
} }
}) })
.push(function () { .push(function () {
// prevent save empty zip archive
// check document exist in archive
return zip.getAttachment('/', 'Editor.bin')
.push(undefined, function (error) {
if (error.status_code === 404) {
return zip.getAttachment('/', 'body.txt') return zip.getAttachment('/', 'body.txt')
.push(undefined, function (error) { .push(undefined, function (error) {
if (error.status_code === 404) { if (error.status_code === 404) {
...@@ -451,6 +498,9 @@ DocsAPI.DocEditor.version = function () { ...@@ -451,6 +498,9 @@ DocsAPI.DocEditor.version = function () {
} }
throw error; throw error;
}); });
}
throw error;
});
}) })
.push(function (Editor_bin) { .push(function (Editor_bin) {
if (Editor_bin) { if (Editor_bin) {
...@@ -465,6 +515,10 @@ DocsAPI.DocEditor.version = function () { ...@@ -465,6 +515,10 @@ DocsAPI.DocEditor.version = function () {
.push(function (evt) { .push(function (evt) {
var data = {}; var data = {};
data[g.props.key] = evt.target.result; data[g.props.key] = evt.target.result;
// TODO it should be run on state change
// if fail send int:1
// it clear modification state onlyoffice
DesktopOfflineAppDocumentEndSave(0);
return data; return data;
}); });
}); });
......
...@@ -3302,9 +3302,6 @@ var editor; ...@@ -3302,9 +3302,6 @@ var editor;
var oBinaryFileWriter = new AscCommonExcel.BinaryFileWriter(this.wbModel); var oBinaryFileWriter = new AscCommonExcel.BinaryFileWriter(this.wbModel);
oBinaryFileWriter.Write(undefined, true); oBinaryFileWriter.Write(undefined, true);
var _header = oBinaryFileWriter.WriteFileHeader(oBinaryFileWriter.Memory.GetCurPosition(), Asc.c_nVersionNoBase64);
window["native"]["Save_End"](_header, oBinaryFileWriter.Memory.GetCurPosition());
return oBinaryFileWriter.Memory.ImData.data; return oBinaryFileWriter.Memory.ImData.data;
}; };
......
...@@ -276,15 +276,8 @@ AscCommon.downloadUrlAsBlob = function (url) { ...@@ -276,15 +276,8 @@ AscCommon.downloadUrlAsBlob = function (url) {
AscCommon.baseEditorsApi.prototype.jio_open = function () { AscCommon.baseEditorsApi.prototype.jio_open = function () {
var t = this, var t = this,
g = Common.Gateway; g = Common.Gateway,
return g.jio_getAttachment('/', 'body.txt') doc = g.props.value;
.push(undefined, function (error) {
if (error["status_code"] === 404) {
return g.props.value;
}
throw error;
})
.push(function (doc) {
if (!doc) { if (!doc) {
switch (g.props.documentType) { switch (g.props.documentType) {
case "presentation": case "presentation":
...@@ -299,27 +292,12 @@ AscCommon.baseEditorsApi.prototype.jio_open = function () { ...@@ -299,27 +292,12 @@ AscCommon.baseEditorsApi.prototype.jio_open = function () {
} }
} }
t._OfflineAppDocumentEndLoad('', doc); t._OfflineAppDocumentEndLoad('', doc);
})
.push(undefined, function (error) {
console.log(error);
});
}; };
AscCommon.baseEditorsApi.prototype.jio_save = function () { AscCommon.baseEditorsApi.prototype.jio_save = function () {
var t = this, var t = this,
g = Common.Gateway, g = Common.Gateway,
result = {}, // data = t.asc_nativeGetFile(); // base64 file
data = t.asc_nativeGetFile(); data = new Blob([t.asc_nativeGetFileData()]); // binary file
if (g.props.save_defer) { return g.jio_save(data);
// if we are run from getContent
result[g.props.key] = data;
g.props.save_defer.resolve(result);
g.props.save_defer = null;
} else {
// TODO: rewrite to put_attachment
return g.jio_putAttachment('/', 'body.txt', data)
.push(undefined, function (error) {
console.log(error);
});
}
}; };
\ No newline at end of file
...@@ -49,16 +49,16 @@ Asc['asc_docs_api'].prototype._OfflineAppDocumentStartLoad = function() ...@@ -49,16 +49,16 @@ Asc['asc_docs_api'].prototype._OfflineAppDocumentStartLoad = function()
AscCommon.History.UserSaveMode = true; AscCommon.History.UserSaveMode = true;
return this.jio_open(); return this.jio_open();
}; };
Asc['asc_docs_api'].prototype._OfflineAppDocumentEndLoad = function(_url, _data) Asc['asc_docs_api'].prototype._OfflineAppDocumentEndLoad = function(_url, _binary)
{ {
//AscCommon.g_oIdCounter.m_sUserId = window["AscDesktopEditor"]["CheckUserId"](); //AscCommon.g_oIdCounter.m_sUserId = window["AscDesktopEditor"]["CheckUserId"]();
if (_data == "") if (_binary == "")
{ {
this.sendEvent("asc_onError", c_oAscError.ID.ConvertationOpenError, c_oAscError.Level.Critical); this.sendEvent("asc_onError", c_oAscError.ID.ConvertationOpenError, c_oAscError.Level.Critical);
return; return;
} }
this.OpenDocument2(_url, _data); this.OpenDocument2(_url, _binary);
this.WordControl.m_oLogicDocument.Set_FastCollaborativeEditing(false); this.WordControl.m_oLogicDocument.Set_FastCollaborativeEditing(false);
this.DocumentOrientation = (null == this.WordControl.m_oLogicDocument) ? true : !this.WordControl.m_oLogicDocument.Orientation; this.DocumentOrientation = (null == this.WordControl.m_oLogicDocument) ? true : !this.WordControl.m_oLogicDocument.Orientation;
DesktopOfflineUpdateLocalName(this); DesktopOfflineUpdateLocalName(this);
......
...@@ -6704,9 +6704,6 @@ background-repeat: no-repeat;\ ...@@ -6704,9 +6704,6 @@ background-repeat: no-repeat;\
this.WordControl.m_oLogicDocument.CalculateComments(); this.WordControl.m_oLogicDocument.CalculateComments();
writer.WriteDocument3(this.WordControl.m_oLogicDocument); writer.WriteDocument3(this.WordControl.m_oLogicDocument);
var _header = "PPTY;v10;" + writer.pos + ";";
window["native"]["Save_End"](_header, writer.pos);
return writer.ImData.data; return writer.ImData.data;
}; };
......
...@@ -49,21 +49,26 @@ Asc['asc_docs_api'].prototype._OfflineAppDocumentStartLoad = function() ...@@ -49,21 +49,26 @@ Asc['asc_docs_api'].prototype._OfflineAppDocumentStartLoad = function()
AscCommon.History.UserSaveMode = true; AscCommon.History.UserSaveMode = true;
return this.jio_open(); return this.jio_open();
}; };
Asc['asc_docs_api'].prototype._OfflineAppDocumentEndLoad = function(_url, _data) Asc['asc_docs_api'].prototype._OfflineAppDocumentEndLoad = function(_url, _binary)
{ {
//AscCommon.g_oIdCounter.m_sUserId = window["AscDesktopEditor"]["CheckUserId"](); //AscCommon.g_oIdCounter.m_sUserId = window["AscDesktopEditor"]["CheckUserId"]();
if (_data == "") if (_binary == "")
{ {
this.sendEvent("asc_onError", c_oAscError.ID.ConvertationOpenError, c_oAscError.Level.Critical); this.sendEvent("asc_onError", c_oAscError.ID.ConvertationOpenError, c_oAscError.Level.Critical);
return; return;
} }
if (AscCommon.c_oSerFormat.Signature !== _data.substring(0, AscCommon.c_oSerFormat.Signature.length)) var _sign_len = AscCommon.c_oSerFormat.Signature.length;
var _signature = _binary.slice(0, _sign_len);
if (typeof _signature !== 'string') {
_signature = String.fromCharCode.apply(null, _signature);
}
if (AscCommon.c_oSerFormat.Signature !== _signature)
{ {
this.OpenDocument(_url, _data); this.OpenDocument(_url, _binary);
} }
else else
{ {
this.OpenDocument2(_url, _data); this.OpenDocument2(_url, _binary);
this.WordControl.m_oLogicDocument.Set_FastCollaborativeEditing(false); this.WordControl.m_oLogicDocument.Set_FastCollaborativeEditing(false);
} }
DesktopOfflineUpdateLocalName(this); DesktopOfflineUpdateLocalName(this);
......
...@@ -7985,9 +7985,6 @@ background-repeat: no-repeat;\ ...@@ -7985,9 +7985,6 @@ background-repeat: no-repeat;\
oBinaryFileWriter.Write(true); oBinaryFileWriter.Write(true);
var _header = AscCommon.c_oSerFormat.Signature + ";v" + Asc.c_nVersionNoBase64 + ";" + _memory.GetCurPosition() + ";";
window["native"]["Save_End"](_header, _memory.GetCurPosition());
return _memory.ImData.data; return _memory.ImData.data;
}; };
......
...@@ -933,12 +933,9 @@ define([ ...@@ -933,12 +933,9 @@ define([
Common.Gateway.on('processmouse', _.bind(me.onProcessMouse, me)); Common.Gateway.on('processmouse', _.bind(me.onProcessMouse, me));
Common.Gateway.on('refreshhistory', _.bind(me.onRefreshHistory, me)); Common.Gateway.on('refreshhistory', _.bind(me.onRefreshHistory, me));
Common.Gateway.on('save', function () { Common.Gateway.on('save', function () {
var g = Common.Gateway;
if (me.api.isDocumentModified()) { if (me.api.isDocumentModified()) {
me.api.asc_Save(); me.api.asc_Save();
} return true;
if (g.props.save_defer) {
g.props.save_defer.resolve(undefined);
} }
}); });
Common.Gateway.on('downloadas', _.bind(me.onDownloadAs, me)); Common.Gateway.on('downloadas', _.bind(me.onDownloadAs, me));
......
...@@ -713,12 +713,9 @@ define([ ...@@ -713,12 +713,9 @@ define([
Common.Gateway.on('processrightschange', _.bind(me.onProcessRightsChange, me)); Common.Gateway.on('processrightschange', _.bind(me.onProcessRightsChange, me));
Common.Gateway.on('processmouse', _.bind(me.onProcessMouse, me)); Common.Gateway.on('processmouse', _.bind(me.onProcessMouse, me));
Common.Gateway.on('save', function () { Common.Gateway.on('save', function () {
var g = Common.Gateway;
if (me.api.isDocumentModified()) { if (me.api.isDocumentModified()) {
me.api.asc_Save(); me.api.asc_Save();
} return true;
if (g.props.save_defer) {
g.props.save_defer.resolve(undefined);
} }
}); });
Common.Gateway.on('downloadas', _.bind(me.onDownloadAs, me)); Common.Gateway.on('downloadas', _.bind(me.onDownloadAs, me));
......
...@@ -716,12 +716,9 @@ define([ ...@@ -716,12 +716,9 @@ define([
Common.Gateway.on('processrightschange', _.bind(me.onProcessRightsChange, me)); Common.Gateway.on('processrightschange', _.bind(me.onProcessRightsChange, me));
Common.Gateway.on('processmouse', _.bind(me.onProcessMouse, me)); Common.Gateway.on('processmouse', _.bind(me.onProcessMouse, me));
Common.Gateway.on('save', function () { Common.Gateway.on('save', function () {
var g = Common.Gateway;
if (me.api.asc_isDocumentModified()) { if (me.api.asc_isDocumentModified()) {
me.api.asc_Save(); me.api.asc_Save();
} return true;
if (g.props.save_defer) {
g.props.save_defer.resolve(undefined);
} }
}); });
Common.Gateway.on('downloadas', _.bind(me.onDownloadAs, me)); Common.Gateway.on('downloadas', _.bind(me.onDownloadAs, me));
......
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