Skip to content

Commit dd71658

Browse files
committed
Drawio: Added unsaved restore prompt and logic
1 parent a4fbde9 commit dd71658

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

lang/en/entities.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@
239239
'pages_md_insert_drawing' => 'Insert Drawing',
240240
'pages_md_show_preview' => 'Show preview',
241241
'pages_md_sync_scroll' => 'Sync preview scroll',
242+
'pages_drawing_unsaved' => 'Unsaved Drawing',
243+
'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?',
242244
'pages_not_in_chapter' => 'Page is not in a chapter',
243245
'pages_move' => 'Move Page',
244246
'pages_copy' => 'Copy Page',

resources/js/services/drawio.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,43 @@ function drawReceive(event) {
6767
}
6868
}
6969

70+
/**
71+
* Attempt to prompt and restore unsaved drawing content if existing.
72+
* @returns {Promise<void>}
73+
*/
74+
async function attemptRestoreIfExists() {
75+
const backupVal = await store.get(saveBackupKey);
76+
const dialogEl = document.getElementById('unsaved-drawing-dialog');
77+
78+
if (!dialogEl) {
79+
console.error('Missing expected unsaved-drawing dialog');
80+
}
81+
82+
if (backupVal) {
83+
/** @var {ConfirmDialog} */
84+
const dialog = window.$components.firstOnElement(dialogEl, 'confirm-dialog');
85+
const restore = await dialog.show();
86+
if (restore) {
87+
onInit = async () => backupVal;
88+
}
89+
}
90+
}
91+
7092
/**
7193
* Show the draw.io editor.
7294
* onSaveCallback must return a promise that resolves on successful save and errors on failure.
7395
* onInitCallback must return a promise with the xml to load for the editor.
96+
* Will attempt to provide an option to restore unsaved changes if found to exist.
7497
* @param {String} drawioUrl
7598
* @param {Function<Promise<String>>} onInitCallback
7699
* @param {Function<Promise>} onSaveCallback - Is called with the drawing data on save.
77100
*/
78-
export function show(drawioUrl, onInitCallback, onSaveCallback) {
101+
export async function show(drawioUrl, onInitCallback, onSaveCallback) {
79102
onInit = onInitCallback;
80103
onSave = onSaveCallback;
81104

105+
await attemptRestoreIfExists();
106+
82107
iFrame = document.createElement('iframe');
83108
iFrame.setAttribute('frameborder', '0');
84109
window.addEventListener('message', drawReceive);

resources/views/common/confirm-dialog.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<div components="popup confirm-dialog"
2-
refs="confirm-dialog@popup {{ $ref }}"
2+
@if($id ?? false) id="{{ $id }}" @endif
3+
refs="confirm-dialog@popup {{ $ref ?? false }}"
34
class="popup-background">
45
<div class="popup-body very-small" tabindex="-1">
56

resources/views/pages/parts/form.blade.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,11 @@ class="text-link text-button hide-over-m page-save-mobile-button">@icon('save')<
6969
{{ trans('entities.pages_edit_delete_draft_confirm') }}
7070
</p>
7171
@endcomponent
72+
73+
{{--Saved Drawing--}}
74+
@component('common.confirm-dialog', ['title' => trans('entities.pages_drawing_unsaved'), 'id' => 'unsaved-drawing-dialog'])
75+
<p>
76+
{{ trans('entities.pages_drawing_unsaved_confirm') }}
77+
</p>
78+
@endcomponent
7279
</div>

0 commit comments

Comments
 (0)