Skip to content

Commit d38bffc

Browse files
authored
Merge pull request #5823 from Laravel-Backpack/fix-keyboard-shortcut-save
Fix keyboard shortcut on forms save actions
2 parents 6a84281 + fc472b3 commit d38bffc

File tree

1 file changed

+51
-13
lines changed

1 file changed

+51
-13
lines changed

src/resources/views/crud/form_content.blade.php

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,26 +127,64 @@ function preventUnload(event) {
127127
@endif
128128
129129
// Save button has multiple actions: save and exit, save and edit, save and new
130-
var saveActions = $('#saveActions')
131-
crudForm = saveActions.parents('form')
132-
130+
document.querySelectorAll('form').forEach(function(form) {
131+
if (form.querySelector('.saveActions')) {
132+
// prevent duplicate entries on double-clicking the submit form
133+
form.addEventListener('submit', function(event) {
134+
window.removeEventListener('beforeunload', preventUnload);
135+
const submitButtons = form.querySelectorAll('button[type=submit]');
136+
submitButtons.forEach(button => button.disabled = true);
137+
});
138+
}
139+
});
140+
133141
// Ctrl+S and Cmd+S trigger Save button click
134-
$(document).keydown(function(e) {
135-
if ((e.which == '115' || e.which == '83' ) && (e.ctrlKey || e.metaKey))
136-
{
142+
document.addEventListener('keydown', function(e) {
143+
if ((e.which === 115 || e.which === 83) && (e.ctrlKey || e.metaKey)) {
137144
e.preventDefault();
138-
$("button[type=submit]").trigger('click');
145+
146+
// Find the form that contains the currently focused element
147+
let activeForm = null;
148+
const focusedElement = document.activeElement;
149+
150+
if (focusedElement) {
151+
activeForm = focusedElement.closest('form');
152+
// Check if this form has saveActions
153+
if (!activeForm || !activeForm.querySelector('.saveActions')) {
154+
activeForm = null;
155+
}
156+
}
157+
158+
// If no focused form with save actions, use the first form with save actions
159+
if (!activeForm) {
160+
const formsWithSaveActions = document.querySelectorAll('form');
161+
for (let form of formsWithSaveActions) {
162+
if (form.querySelector('.saveActions')) {
163+
activeForm = form;
164+
break;
165+
}
166+
}
167+
}
168+
169+
if (activeForm) {
170+
const submitButton = activeForm.querySelector('.saveActions button[type=submit]');
171+
172+
if (submitButton) {
173+
submitButton.click();
174+
} else {
175+
// Create and dispatch a submit event
176+
const submitEvent = new Event('submit', {
177+
bubbles: true,
178+
cancelable: true
179+
});
180+
activeForm.dispatchEvent(submitEvent);
181+
}
182+
}
139183
return false;
140184
}
141185
return true;
142186
});
143187
144-
// prevent duplicate entries on double-clicking the submit form
145-
crudForm.submit(function (event) {
146-
window.removeEventListener('beforeunload', preventUnload);
147-
$("button[type=submit]").prop('disabled', true);
148-
});
149-
150188
// Place the focus on the first element in the form
151189
@if( $crud->getAutoFocusOnFirstField() )
152190
@php

0 commit comments

Comments
 (0)