Skip to content

Commit

Permalink
Merge pull request #2887 from ONLYOFFICE/feature/open-from-bin
Browse files Browse the repository at this point in the history
Feature/open from bin
  • Loading branch information
JuliaRadzhabova authored Mar 18, 2024
2 parents 2299157 + e32050c commit 56fc6f0
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 31 deletions.
29 changes: 22 additions & 7 deletions apps/api/documents/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@
'onRequestOpen': <try to open external link>,
'onRequestSelectDocument': <try to open document>, // used for compare and combine documents. must call setRequestedDocument method. use instead of onRequestCompareFile/setRevisedFile
'onRequestSelectSpreadsheet': <try to open spreadsheet>, // used for mailmerge id de. must call setRequestedSpreadsheet method. use instead of onRequestMailMergeRecipients/setMailMergeRecipients
'onRequestReferenceSource': <try to change source for external link>, // used for external links in sse. must call setReferenceSource method
'onRequestReferenceSource': <try to change source for external link>, // used for external links in sse. must call setReferenceSource method,
'onSaveDocument': 'save document from binary'
}
}
Expand Down Expand Up @@ -355,6 +356,7 @@
_config.editorConfig.canRequestSelectDocument = _config.events && !!_config.events.onRequestSelectDocument;
_config.editorConfig.canRequestSelectSpreadsheet = _config.events && !!_config.events.onRequestSelectSpreadsheet;
_config.editorConfig.canRequestReferenceSource = _config.events && !!_config.events.onRequestReferenceSource;
_config.editorConfig.canSaveDocumentToBinary = _config.events && !!_config.events.onSaveDocument;
_config.frameEditorId = placeholderId;
_config.parentOrigin = window.location.origin;

Expand Down Expand Up @@ -564,9 +566,9 @@
}
};

var _sendCommand = function(cmd) {
var _sendCommand = function(cmd, buffer) {
if (iframe && iframe.contentWindow)
postMessage(iframe.contentWindow, cmd);
postMessage(iframe.contentWindow, cmd, buffer);
};

var _init = function(editorConfig) {
Expand All @@ -587,6 +589,13 @@
});
};

var _openDocumentFromBinary = function(doc) {
doc && _sendCommand({
command: 'openDocumentFromBinary',
data: doc.buffer
}, doc.buffer);
};

var _showMessage = function(title, msg) {
msg = msg || title;
_sendCommand({
Expand Down Expand Up @@ -846,7 +855,8 @@
setReferenceData : _setReferenceData,
setRequestedDocument: _setRequestedDocument,
setRequestedSpreadsheet: _setRequestedSpreadsheet,
setReferenceSource: _setReferenceSource
setReferenceSource: _setReferenceSource,
openDocument: _openDocumentFromBinary
}
};

Expand Down Expand Up @@ -897,6 +907,12 @@
var _onMessage = function(msg) {
// TODO: check message origin
if (msg && window.JSON && _scope.frameOrigin==msg.origin ) {
if (msg.data && msg.data.event === 'onSaveDocument') {
if (_fn) {
_fn.call(_scope, msg.data);
}
return;
}

try {
var msg = window.JSON.parse(msg.data);
Expand Down Expand Up @@ -1107,12 +1123,11 @@
return iframe;
}

function postMessage(wnd, msg) {
function postMessage(wnd, msg, buffer) {
if (wnd && wnd.postMessage && window.JSON) {
// TODO: specify explicit origin
wnd.postMessage(window.JSON.stringify(msg), "*");
buffer ? wnd.postMessage(msg, "*", [buffer]) : wnd.postMessage(window.JSON.stringify(msg), "*");
}

}

function extend(dest, src) {
Expand Down
23 changes: 21 additions & 2 deletions apps/common/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ if (window.Common === undefined) {
$me.trigger('opendocument', data);
},

'openDocumentFromBinary': function(data) {
$me.trigger('opendocumentfrombinary', data);
},

'showMessage': function(data) {
$me.trigger('showmessage', data);
},
Expand Down Expand Up @@ -156,11 +160,11 @@ if (window.Common === undefined) {
}
};

var _postMessage = function(msg) {
var _postMessage = function(msg, buffer) {
// TODO: specify explicit origin
if (window.parent && window.JSON) {
msg.frameEditorId = window.frameEditorId;
window.parent.postMessage(window.JSON.stringify(msg), "*");
buffer ? window.parent.postMessage(msg, "*", [buffer]) : window.parent.postMessage(window.JSON.stringify(msg), "*");
}
};

Expand All @@ -169,6 +173,14 @@ if (window.Common === undefined) {
if (msg.origin !== window.parentOrigin && msg.origin !== window.location.origin && !(msg.origin==="null" && (window.parentOrigin==="file://" || window.location.origin==="file://"))) return;

var data = msg.data;
if (data && data.command === 'openDocumentFromBinary') {
handler = commandMap[data.command];
if (handler) {
handler.call(this, data.data);
}
return;
}

if (Object.prototype.toString.apply(data) !== '[object String]' || !window.JSON) {
return;
}
Expand Down Expand Up @@ -386,6 +398,13 @@ if (window.Common === undefined) {
_postMessage({ event: 'onPluginsReady' });
},

saveDocument: function(data) {
data && _postMessage({
event: 'onSaveDocument',
data: data.buffer
}, data.buffer);
},

on: function(event, handler){
var localHandler = function(event, data){
handler.call(me, data)
Expand Down
18 changes: 15 additions & 3 deletions apps/documenteditor/main/app/controller/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ define([
Common.Gateway.on('init', _.bind(this.loadConfig, this));
Common.Gateway.on('showmessage', _.bind(this.onExternalMessage, this));
Common.Gateway.on('opendocument', _.bind(this.loadDocument, this));
Common.Gateway.on('opendocumentfrombinary', _.bind(this.loadBinary, this));
Common.Gateway.on('grabfocus', _.bind(this.onGrabFocus, this));
Common.Gateway.appReady();

Expand Down Expand Up @@ -463,6 +464,7 @@ define([
this.appOptions.uiRtl = !(Common.Controllers.Desktop.isActive() && Common.Controllers.Desktop.uiRtlSupported()) && !Common.Utils.isIE;
this.appOptions.disableNetworkFunctionality = !!(window["AscDesktopEditor"] && window["AscDesktopEditor"]["isSupportNetworkFunctionality"] && false === window["AscDesktopEditor"]["isSupportNetworkFunctionality"]());
this.appOptions.mentionShare = !((typeof (this.appOptions.customization) == 'object') && (this.appOptions.customization.mentionShare==false));
this.appOptions.canSaveDocumentToBinary = this.editorConfig.canSaveDocumentToBinary;
this.appOptions.user.guest && this.appOptions.canRenameAnonymous && Common.NotificationCenter.on('user:rename', _.bind(this.showRenameUserDialog, this));

this.appOptions.canRequestClose = this.editorConfig.canRequestClose;
Expand Down Expand Up @@ -541,6 +543,7 @@ define([
docInfo.put_EncryptedInfo(this.editorConfig.encryptionKeys);
docInfo.put_Lang(this.editorConfig.lang);
docInfo.put_Mode(this.editorConfig.mode);
docInfo.put_SupportsOnSaveDocument(this.editorConfig.canSaveDocumentToBinary);

var enable = !this.editorConfig.customization || (this.editorConfig.customization.macros!==false);
docInfo.asc_putIsEnabledMacroses(!!enable);
Expand Down Expand Up @@ -1025,7 +1028,7 @@ define([

if (this.api && this.appOptions.isEdit && !toolbarView._state.previewmode) {
var cansave = this.api.asc_isDocumentCanSave(),
forcesave = this.appOptions.forcesave,
forcesave = this.appOptions.forcesave || this.appOptions.canSaveDocumentToBinary,
isSyncButton = (toolbarView.btnCollabChanges.rendered) ? toolbarView.btnCollabChanges.cmpEl.hasClass('notify') : false,
isDisabled = !cansave && !isSyncButton && !forcesave || this._state.isDisconnected || this._state.fastCoauth && this._state.usersCount>1 && !forcesave;
toolbarView.btnSave.setDisabled(isDisabled);
Expand Down Expand Up @@ -1904,6 +1907,7 @@ define([
me.api.asc_registerCallback('asc_onCollaborativeChanges', _.bind(me.onCollaborativeChanges, me));
me.api.asc_registerCallback('asc_OnTryUndoInFastCollaborative',_.bind(me.onTryUndoInFastCollaborative, me));
me.api.asc_registerCallback('asc_onConvertEquationToMath',_.bind(me.onConvertEquationToMath, me));
me.appOptions.canSaveDocumentToBinary && me.api.asc_registerCallback('asc_onSaveDocument',_.bind(me.onSaveDocumentBinary, me));
/** coauthoring end **/

if (me.stackLongActions.exist({id: ApplyEditRights, type: Asc.c_oAscAsyncActionType['BlockInteraction']})) {
Expand Down Expand Up @@ -2321,7 +2325,7 @@ define([
var toolbarView = this.getApplication().getController('Toolbar').getView();
if (toolbarView && toolbarView.btnCollabChanges && !toolbarView._state.previewmode) {
var isSyncButton = toolbarView.btnCollabChanges.cmpEl.hasClass('notify'),
forcesave = this.appOptions.forcesave,
forcesave = this.appOptions.forcesave || this.appOptions.canSaveDocumentToBinary,
isDisabled = !isModified && !isSyncButton && !forcesave || this._state.isDisconnected || this._state.fastCoauth && this._state.usersCount>1 && !forcesave;
toolbarView.btnSave.setDisabled(isDisabled);
}
Expand All @@ -2338,7 +2342,7 @@ define([

if (toolbarView && this.api && !toolbarView._state.previewmode) {
var isSyncButton = toolbarView.btnCollabChanges.cmpEl.hasClass('notify'),
forcesave = this.appOptions.forcesave,
forcesave = this.appOptions.forcesave || this.appOptions.canSaveDocumentToBinary,
isDisabled = !isCanSave && !isSyncButton && !forcesave || this._state.isDisconnected || this._state.fastCoauth && this._state.usersCount>1 && !forcesave;
toolbarView.btnSave.setDisabled(isDisabled);
}
Expand Down Expand Up @@ -3158,6 +3162,14 @@ define([
}
},

onSaveDocumentBinary: function(data) {
Common.Gateway.saveDocument(data);
},

loadBinary: function(data) {
data && this.api.asc_openDocumentFromBytes(new Uint8Array(data));
},

leavePageText: 'You have unsaved changes in this document. Click \'Stay on this Page\' then \'Save\' to save them. Click \'Leave this Page\' to discard all the unsaved changes.',
criticalErrorTitle: 'Error',
notcriticalErrorTitle: 'Warning',
Expand Down
4 changes: 2 additions & 2 deletions apps/documenteditor/main/app/controller/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1078,13 +1078,13 @@ define([
if (this.api) {
var isModified = this.api.asc_isDocumentCanSave();
var isSyncButton = toolbar.btnCollabChanges && toolbar.btnCollabChanges.cmpEl.hasClass('notify');
if (!isModified && !isSyncButton && !toolbar.mode.forcesave)
if (!isModified && !isSyncButton && !toolbar.mode.forcesave && !toolbar.mode.canSaveDocumentToBinary)
return;

this.api.asc_Save();
}

toolbar.btnSave.setDisabled(!toolbar.mode.forcesave);
toolbar.btnSave.setDisabled(!toolbar.mode.forcesave && !toolbar.mode.canSaveDocumentToBinary);

Common.NotificationCenter.trigger('edit:complete', toolbar);

Expand Down
2 changes: 1 addition & 1 deletion apps/documenteditor/main/app/view/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2949,7 +2949,7 @@ define([
this.synchTooltip.hide();
this.btnCollabChanges.updateHint(this.btnSaveTip);

this.btnSave.setDisabled(!me.mode.forcesave);
this.btnSave.setDisabled(!me.mode.forcesave && !me.mode.canSaveDocumentToBinary);
this._state.hasCollaborativeChanges = false;
}
}
Expand Down
18 changes: 15 additions & 3 deletions apps/pdfeditor/main/app/controller/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ define([
Common.Gateway.on('init', _.bind(this.loadConfig, this));
Common.Gateway.on('showmessage', _.bind(this.onExternalMessage, this));
Common.Gateway.on('opendocument', _.bind(this.loadDocument, this));
Common.Gateway.on('opendocumentfrombinary', _.bind(this.loadBinary, this));
Common.Gateway.on('grabfocus', _.bind(this.onGrabFocus, this));
Common.Gateway.appReady();

Expand Down Expand Up @@ -400,6 +401,7 @@ define([
this.appOptions.compatibleFeatures = true;
this.appOptions.uiRtl = !(Common.Controllers.Desktop.isActive() && Common.Controllers.Desktop.uiRtlSupported()) && !Common.Utils.isIE;
this.appOptions.mentionShare = !((typeof (this.appOptions.customization) == 'object') && (this.appOptions.customization.mentionShare==false));
this.appOptions.canSaveDocumentToBinary = this.editorConfig.canSaveDocumentToBinary;
this.appOptions.user.guest && this.appOptions.canRenameAnonymous && Common.NotificationCenter.on('user:rename', _.bind(this.showRenameUserDialog, this));

this.appOptions.canRequestClose = this.editorConfig.canRequestClose;
Expand Down Expand Up @@ -467,6 +469,7 @@ define([
docInfo.put_EncryptedInfo(this.editorConfig.encryptionKeys);
docInfo.put_Lang(this.editorConfig.lang);
docInfo.put_Mode(this.editorConfig.mode);
docInfo.put_SupportsOnSaveDocument(this.editorConfig.canSaveDocumentToBinary);

var enable = !this.editorConfig.customization || (this.editorConfig.customization.macros!==false);
docInfo.asc_putIsEnabledMacroses(!!enable);
Expand Down Expand Up @@ -758,7 +761,7 @@ define([

if (this.api && this.appOptions.isEdit && !toolbarView._state.previewmode) {
var cansave = this.api.asc_isDocumentCanSave(),
forcesave = this.appOptions.forcesave,
forcesave = this.appOptions.forcesave || this.appOptions.canSaveDocumentToBinary,
isSyncButton = (toolbarView.btnCollabChanges.rendered) ? toolbarView.btnCollabChanges.cmpEl.hasClass('notify') : false,
isDisabled = !cansave && !isSyncButton && !forcesave || this._state.isDisconnected || this._state.fastCoauth && this._state.usersCount>1 && !forcesave || !this.appOptions.isPDFEdit && !this.appOptions.isPDFAnnotate;
toolbarView.btnSave.setDisabled(isDisabled && !this.appOptions.saveAlwaysEnabled);
Expand Down Expand Up @@ -1506,6 +1509,7 @@ define([
/** coauthoring begin **/
me.api.asc_registerCallback('asc_onCollaborativeChanges', _.bind(me.onCollaborativeChanges, me));
me.api.asc_registerCallback('asc_OnTryUndoInFastCollaborative',_.bind(me.onTryUndoInFastCollaborative, me));
me.appOptions.canSaveDocumentToBinary && me.api.asc_registerCallback('asc_onSaveDocument',_.bind(me.onSaveDocumentBinary, me));
/** coauthoring end **/

if (me.stackLongActions.exist({id: ApplyEditRights, type: Asc.c_oAscAsyncActionType['BlockInteraction']})) {
Expand Down Expand Up @@ -1937,7 +1941,7 @@ define([
var toolbarView = this.getApplication().getController('Toolbar').getView();
if (toolbarView && toolbarView.btnCollabChanges && !toolbarView._state.previewmode) {
var isSyncButton = toolbarView.btnCollabChanges.cmpEl.hasClass('notify'),
forcesave = this.appOptions.forcesave,
forcesave = this.appOptions.forcesave || this.appOptions.canSaveDocumentToBinary,
isDisabled = !isModified && !isSyncButton && !forcesave || this._state.isDisconnected || this._state.fastCoauth && this._state.usersCount>1 && !forcesave || !this.appOptions.isPDFEdit && !this.appOptions.isPDFAnnotate;
toolbarView.btnSave.setDisabled(isDisabled && !this.appOptions.saveAlwaysEnabled);
}
Expand All @@ -1954,7 +1958,7 @@ define([

if (toolbarView && this.api && !toolbarView._state.previewmode) {
var isSyncButton = toolbarView.btnCollabChanges.cmpEl.hasClass('notify'),
forcesave = this.appOptions.forcesave,
forcesave = this.appOptions.forcesave || this.appOptions.canSaveDocumentToBinary,
isDisabled = !isCanSave && !isSyncButton && !forcesave || this._state.isDisconnected || this._state.fastCoauth && this._state.usersCount>1 && !forcesave || !this.appOptions.isPDFEdit && !this.appOptions.isPDFAnnotate;
toolbarView.btnSave.setDisabled(isDisabled && !this.appOptions.saveAlwaysEnabled);
}
Expand Down Expand Up @@ -2448,6 +2452,14 @@ define([
}
},

onSaveDocumentBinary: function(data) {
Common.Gateway.saveDocument(data);
},

loadBinary: function(data) {
data && this.api.asc_openDocumentFromBytes(new Uint8Array(data));
},

leavePageText: 'You have unsaved changes in this document. Click \'Stay on this Page\' then \'Save\' to save them. Click \'Leave this Page\' to discard all the unsaved changes.',
criticalErrorTitle: 'Error',
notcriticalErrorTitle: 'Warning',
Expand Down
4 changes: 2 additions & 2 deletions apps/pdfeditor/main/app/controller/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,11 @@ define([
} else if (this.api) {
// var isModified = this.api.asc_isDocumentCanSave();
// var isSyncButton = toolbar.btnCollabChanges && toolbar.btnCollabChanges.cmpEl.hasClass('notify');
// if (!isModified && !isSyncButton && !toolbar.mode.forcesave)
// if (!isModified && !isSyncButton && !toolbar.mode.forcesave && !toolbar.mode.canSaveDocumentToBinary)
// return;

this.api.asc_Save();
toolbar.btnSave && toolbar.btnSave.setDisabled(!toolbar.mode.forcesave && !toolbar.mode.saveAlwaysEnabled);
toolbar.btnSave && toolbar.btnSave.setDisabled(!toolbar.mode.forcesave && !toolbar.mode.saveAlwaysEnabled && !toolbar.mode.canSaveDocumentToBinary);
Common.component.Analytics.trackEvent('Save');
Common.component.Analytics.trackEvent('ToolBar', 'Save');
}
Expand Down
2 changes: 1 addition & 1 deletion apps/pdfeditor/main/app/view/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ define([
this.synchTooltip.hide();
this.btnCollabChanges.updateHint(this.btnSaveTip);

this.btnSave.setDisabled(!me.mode.forcesave || !me.mode.isPDFEdit && !me.mode.isPDFAnnotate && !me.mode.saveAlwaysEnabled);
this.btnSave.setDisabled(!me.mode.forcesave && !me.mode.canSaveDocumentToBinary || !me.mode.isPDFEdit && !me.mode.isPDFAnnotate && !me.mode.saveAlwaysEnabled);
this._state.hasCollaborativeChanges = false;
}
}
Expand Down
Loading

0 comments on commit 56fc6f0

Please sign in to comment.