Drawio: Added unsaved restore prompt and logic

This commit is contained in:
Dan Brown 2023-08-23 14:16:20 +01:00
parent a4fbde9185
commit dd71658d70
No known key found for this signature in database
GPG key ID: 46D9F943C24A2EF9
4 changed files with 37 additions and 2 deletions

View file

@ -239,6 +239,8 @@ return [
'pages_md_insert_drawing' => 'Insert Drawing', 'pages_md_insert_drawing' => 'Insert Drawing',
'pages_md_show_preview' => 'Show preview', 'pages_md_show_preview' => 'Show preview',
'pages_md_sync_scroll' => 'Sync preview scroll', 'pages_md_sync_scroll' => 'Sync preview scroll',
'pages_drawing_unsaved' => 'Unsaved Drawing',
'pages_drawing_unsaved_confirm' => 'Unsaved drawing data was found from a previous failed drawing save attempt. Would you like to restore and continue editing this unsaved drawing?',
'pages_not_in_chapter' => 'Page is not in a chapter', 'pages_not_in_chapter' => 'Page is not in a chapter',
'pages_move' => 'Move Page', 'pages_move' => 'Move Page',
'pages_copy' => 'Copy Page', 'pages_copy' => 'Copy Page',

View file

@ -67,18 +67,43 @@ function drawReceive(event) {
} }
} }
/**
* Attempt to prompt and restore unsaved drawing content if existing.
* @returns {Promise<void>}
*/
async function attemptRestoreIfExists() {
const backupVal = await store.get(saveBackupKey);
const dialogEl = document.getElementById('unsaved-drawing-dialog');
if (!dialogEl) {
console.error('Missing expected unsaved-drawing dialog');
}
if (backupVal) {
/** @var {ConfirmDialog} */
const dialog = window.$components.firstOnElement(dialogEl, 'confirm-dialog');
const restore = await dialog.show();
if (restore) {
onInit = async () => backupVal;
}
}
}
/** /**
* Show the draw.io editor. * Show the draw.io editor.
* onSaveCallback must return a promise that resolves on successful save and errors on failure. * onSaveCallback must return a promise that resolves on successful save and errors on failure.
* onInitCallback must return a promise with the xml to load for the editor. * onInitCallback must return a promise with the xml to load for the editor.
* Will attempt to provide an option to restore unsaved changes if found to exist.
* @param {String} drawioUrl * @param {String} drawioUrl
* @param {Function<Promise<String>>} onInitCallback * @param {Function<Promise<String>>} onInitCallback
* @param {Function<Promise>} onSaveCallback - Is called with the drawing data on save. * @param {Function<Promise>} onSaveCallback - Is called with the drawing data on save.
*/ */
export function show(drawioUrl, onInitCallback, onSaveCallback) { export async function show(drawioUrl, onInitCallback, onSaveCallback) {
onInit = onInitCallback; onInit = onInitCallback;
onSave = onSaveCallback; onSave = onSaveCallback;
await attemptRestoreIfExists();
iFrame = document.createElement('iframe'); iFrame = document.createElement('iframe');
iFrame.setAttribute('frameborder', '0'); iFrame.setAttribute('frameborder', '0');
window.addEventListener('message', drawReceive); window.addEventListener('message', drawReceive);

View file

@ -1,5 +1,6 @@
<div components="popup confirm-dialog" <div components="popup confirm-dialog"
refs="confirm-dialog@popup {{ $ref }}" @if($id ?? false) id="{{ $id }}" @endif
refs="confirm-dialog@popup {{ $ref ?? false }}"
class="popup-background"> class="popup-background">
<div class="popup-body very-small" tabindex="-1"> <div class="popup-body very-small" tabindex="-1">

View file

@ -69,4 +69,11 @@
{{ trans('entities.pages_edit_delete_draft_confirm') }} {{ trans('entities.pages_edit_delete_draft_confirm') }}
</p> </p>
@endcomponent @endcomponent
{{--Saved Drawing--}}
@component('common.confirm-dialog', ['title' => trans('entities.pages_drawing_unsaved'), 'id' => 'unsaved-drawing-dialog'])
<p>
{{ trans('entities.pages_drawing_unsaved_confirm') }}
</p>
@endcomponent
</div> </div>