-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make collabora save and exit actions more intuitive #12 #16
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -37,28 +37,50 @@ | |||||
<syntaxId>xwiki/2.1</syntaxId> | ||||||
<hidden>true</hidden> | ||||||
<content>{{velocity output="false"}} | ||||||
#macro (renderCollaboraContent $mode) | ||||||
## Display logo and some information about the currently edited file. Show Cancel action on the right. | ||||||
## Save action is included in Collabora editor. | ||||||
<div class="actionMenu"> | ||||||
<div> | ||||||
<a href="${xwiki.getURL('Main.WebHome')}"><img src="$xwiki.getSkinFile('logo.svg')"></a> | ||||||
</div> | ||||||
<div> | ||||||
<strong>$currentAction</strong> | ||||||
<em>$escapetool.xml($request.filename)</em> | ||||||
$escapetool.xml($services.localization.render('collabora.editor.onPage')) | ||||||
<a href="${fileDoc.URL}#Attachments" title="$escapetool.xml($services.localization.render('cancel'))"> | ||||||
$escapetool.xml($fileDoc.displayTitle) | ||||||
</a> | ||||||
#macro (unsavedChangesModal) | ||||||
<div class="modal fade" id="editUnsavedChanges" tabindex="-1" role="dialog" aria-hidden="true" | ||||||
aria-label="$escapetool.xml($services.localization.render('collabora.editor.unsaved.label'))"> | ||||||
<div class="modal-dialog modal-sm"> | ||||||
<div class="modal-content"> | ||||||
<div class="modal-body"> | ||||||
$escapetool.xml($services.localization.render('collabora.editor.unsaved.info')) | ||||||
</div> | ||||||
<div> | ||||||
<a href="${fileDoc.URL}#Attachments" class="btn btn-default" | ||||||
title="$escapetool.xml($services.localization.render('cancel'))"> | ||||||
$escapetool.xml($services.localization.render('cancel')) | ||||||
</a> | ||||||
<div class="modal-footer"> | ||||||
<input type="submit" class="btn btn-danger" | ||||||
value="$escapetool.xml($services.localization.render('collabora.editor.unsaved.submit'))"> | ||||||
<input type="button" class="btn btn-default" data-dismiss="modal" | ||||||
value="$escapetool.xml($services.localization.render('collabora.editor.unsaved.close'))"> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to also have the option to save before closing the editor? |
||||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
#end | ||||||
#macro (renderCollaboraContent $mode) | ||||||
## Display logo and some information about the currently edited file. Save, Save and exit and Close actions are | ||||||
## included in Collabora editor. | ||||||
<div class="actionMenu"> | ||||||
<div> | ||||||
<a href="${xwiki.getURL('Main.WebHome')}"><img src="$xwiki.getSkinFile('logo.svg')"></a> | ||||||
</div> | ||||||
<div> | ||||||
<strong>$currentAction</strong> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know you do the escaping below, when setting the value, but I find it more clear to perform the escaping in the place where the value is used (because the escaping depends on the context where the value is used). specially since you use the value only once, here. |
||||||
<em>$escapetool.xml($request.filename)</em> | ||||||
$escapetool.xml($services.localization.render('collabora.editor.onPage')) | ||||||
<a id='fileHomePage' href="${fileDoc.URL}#Attachments" | ||||||
title="$escapetool.xml($services.localization.render('cancel'))"> | ||||||
$escapetool.xml($fileDoc.displayTitle) | ||||||
</a> | ||||||
</div> | ||||||
</div> | ||||||
#set ($saveAndExitIconURL = $xwiki.getDocument('Collabora.Code.UI').getAttachmentURL('saveAndExit.png')) | ||||||
#set ($closeIconURL = $xwiki.getDocument('Collabora.Code.UI').getAttachmentURL('close.png')) | ||||||
#set ($saveAndExitMessage = $escapetool.xml($services.localization.render('collabora.editor.saveAndExit'))) | ||||||
#set ($closeMessage = $escapetool.xml($services.localization.render('collabora.editor.close'))) | ||||||
## TODO: The translations are added for now as a data attribute. This should be changed once the application starts | ||||||
## depending on a XWiki version >= 13.8 to include XWIKI-18973: Simplify the way JavaScript code loads translation | ||||||
## messages. | ||||||
<span id="collaboraConfig" data-save-exit-icon="$saveAndExitIconURL" data-close-icon="$closeIconURL" | ||||||
data-save-exit-message="$saveAndExitMessage" data-close-message="$closeMessage"></span> | ||||||
## Information needed by collabora to be able to edit the current file. | ||||||
#set ($fileId = $escapetool.xml($services.model.serialize($attachment.getReference(), 'default'))) | ||||||
#set ($errorMessage = $escapetool.xml($services.localization.render('collabora.editor.error'))) | ||||||
|
@@ -70,6 +92,7 @@ | |||||
</form> | ||||||
## Where the document will be displayed. | ||||||
<iframe id="collaboraViewer" name="collaboraViewer"></iframe> | ||||||
#unsavedChangesModal() | ||||||
#end | ||||||
{{/velocity}} | ||||||
|
||||||
|
@@ -197,6 +220,99 @@ | |||||
<property> | ||||||
<code>require(['jquery'], function($) { | ||||||
const collaboraPath ='/rest/collabora/files/'; | ||||||
const iframeWindow = document.getElementById('collaboraViewer').contentWindow; | ||||||
var isDocModified = false; | ||||||
|
||||||
var getSaveAndExitButtonMessage = function() { | ||||||
return JSON.stringify({ | ||||||
'MessageId': 'Insert_Button', | ||||||
'SendTime': Date.now(), | ||||||
'Values': { | ||||||
'id': 'saveAndExitEditor', | ||||||
'imgurl': window.location.origin + $("#collaboraConfig").data('saveExitIcon'), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
'hint': $("#collaboraConfig").data('saveExitMessage'), | ||||||
'mobile': true, | ||||||
'label': $("#collaboraConfig").data('saveExitMessage') | ||||||
} | ||||||
}); | ||||||
}; | ||||||
|
||||||
var getCloseButtonMessage = function() { | ||||||
return JSON.stringify({ | ||||||
'MessageId': 'Insert_Button', | ||||||
'SendTime': Date.now(), | ||||||
'Values': { | ||||||
'id': 'closeEditor', | ||||||
'imgurl': window.location.origin + $("#collaboraConfig").data('closeIcon'), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. |
||||||
'hint': $("#collaboraConfig").data('closeMessage'), | ||||||
'mobile': true, | ||||||
'label': $("#collaboraConfig").data('closeMessage') | ||||||
} | ||||||
}); | ||||||
}; | ||||||
|
||||||
var getSaveDocumentMessage = function() { | ||||||
return JSON.stringify({ | ||||||
"MessageId" : "Action_Save", | ||||||
"SendTime": Date.now(), | ||||||
"Values" : { | ||||||
"DontTerminateEdit" : true, | ||||||
"DontSaveIfUnmodified": true, | ||||||
"Notify" : true | ||||||
} | ||||||
}); | ||||||
} | ||||||
|
||||||
/** | ||||||
* If there are unsaved changes, ask the user if these should be discarded. Otherwise, go to the file home page. | ||||||
*/ | ||||||
var onCloseEditor = function() { | ||||||
if (isDocModified) { | ||||||
$('#editUnsavedChanges').modal('show'); | ||||||
} else { | ||||||
window.location.href = $('#fileHomePage').attr('href'); | ||||||
} | ||||||
}; | ||||||
|
||||||
var onDocumentLoaded = function() { | ||||||
// Notify the server that postMessage is going to be used. | ||||||
iframeWindow.postMessage(JSON.stringify({ 'MessageId': 'Host_PostmessageReady' }), '*'); | ||||||
// Add the saveAndExit and close custom buttons. | ||||||
iframeWindow.postMessage(getSaveAndExitButtonMessage(), '*'); | ||||||
iframeWindow.postMessage(getCloseButtonMessage(), '*'); | ||||||
}; | ||||||
|
||||||
var receiveMessage = function(event) { | ||||||
// Skip messages that are not from this iframe. | ||||||
if (event.source !== iframeWindow) { | ||||||
return; | ||||||
} | ||||||
var msg = JSON.parse(event.data); | ||||||
if (!msg) { | ||||||
return; | ||||||
} | ||||||
|
||||||
if (msg.MessageId == 'App_LoadingStatus' && msg.Values && msg.Values.Status == 'Document_Loaded') { | ||||||
onDocumentLoaded(); | ||||||
} else if (msg.MessageId == 'Doc_ModifiedStatus') { | ||||||
isDocModified = msg.Values && msg.Values.Modified; | ||||||
} else if (msg.MessageId == 'Clicked_Button') { | ||||||
if (msg.Values && msg.Values.Id == 'saveAndExitEditor') { | ||||||
// Save the file and let the editor close action be handleded after this is finished, on Action_Save_Resp. | ||||||
iframeWindow.postMessage(getSaveDocumentMessage(), '*'); | ||||||
} else if (msg.Values && msg.Values.Id == 'closeEditor') { | ||||||
onCloseEditor(); | ||||||
} | ||||||
} else if (msg.MessageId == 'Action_Save_Resp') { | ||||||
// This event is send when Action_Save or Action_Save_As are triggered from our code, and UI_Save when using the | ||||||
// editor's default save button. Right now, we only fire Action_Save on the custom saveAndExit button. So after | ||||||
// this save is succesfull, a redirect to the file home page is required. | ||||||
if (msg.Values && msg.Values.success) { | ||||||
window.location.href = $('#fileHomePage').attr('href'); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
$(function() { | ||||||
const fileId = $("#collaboraServer").data('fileId'); | ||||||
const userCanWrite = $("#collaboraServer").data('mode') === 'edit'; | ||||||
|
@@ -219,6 +335,13 @@ | |||||
}).fail(function(jqxhr, textStatus, error) { | ||||||
new XWiki.widgets.Notification(errorMessage + error, 'error'); | ||||||
}); | ||||||
|
||||||
// Listen to messages send by Collabora server. | ||||||
window.addEventListener("message", receiveMessage, false); | ||||||
}); | ||||||
|
||||||
$(document).on('click', '#editUnsavedChanges input.btn-danger', function(e) { | ||||||
window.location.href = $('#fileHomePage').attr('href'); | ||||||
}); | ||||||
|
||||||
$(window).unload(function() { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.