Skip to content

Commit 228fec8

Browse files
author
Tim Wundenberg
committed
Warn on navigation if currently editing comment or title (#32223)
1 parent f47fb4f commit 228fec8

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

web_src/js/features/repo-issue-edit.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ async function tryOnEditContent(e) {
2424
e.preventDefault();
2525
showElem(renderContent);
2626
hideElem(editContentZone);
27+
window.removeEventListener('beforeunload', showUserHintOnNavigate);
2728
comboMarkdownEditor.dropzoneReloadFiles();
2829
};
2930

@@ -32,6 +33,7 @@ async function tryOnEditContent(e) {
3233
renderContent.classList.add('is-loading');
3334
showElem(renderContent);
3435
hideElem(editContentZone);
36+
window.removeEventListener('beforeunload', showUserHintOnNavigate);
3537
try {
3638
const params = new URLSearchParams({
3739
content: comboMarkdownEditor.value(),
@@ -82,6 +84,7 @@ async function tryOnEditContent(e) {
8284
// Show write/preview tab and copy raw content as needed
8385
showElem(editContentZone);
8486
hideElem(renderContent);
87+
window.addEventListener('beforeunload', showUserHintOnNavigate);
8588

8689
comboMarkdownEditor = getComboMarkdownEditor(editContentZone.querySelector('.combo-markdown-editor'));
8790
if (!comboMarkdownEditor) {
@@ -148,6 +151,11 @@ async function tryOnQuoteReply(e) {
148151
editor.moveCursorToEnd();
149152
}
150153

154+
// Show a warning message when the user tries to navigate, even though he is currently editing
155+
function showUserHintOnNavigate(e: BeforeUnloadEvent) {
156+
e.preventDefault();
157+
}
158+
151159
export function initRepoIssueCommentEdit() {
152160
document.addEventListener('click', (e) => {
153161
tryOnEditContent(e); // Edit issue or comment content

web_src/js/features/repo-issue.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,15 @@ export function initRepoIssueTitleEdit() {
546546
issueTitleInput.value = oldTitle;
547547
}
548548
issueTitleInput.focus();
549+
550+
window.addEventListener('beforeunload', showUserHintOnNavigate);
549551
});
550552
issueTitleEditor.querySelector('.ui.cancel.button').addEventListener('click', () => {
551553
hideElem(issueTitleEditor);
552554
hideElem('#pull-desc-editor');
553555
showElem(issueTitleDisplay);
554556
showElem('#pull-desc-display');
557+
window.removeEventListener('beforeunload', showUserHintOnNavigate);
555558
});
556559

557560
const pullDescEditor = document.querySelector('#pull-desc-editor'); // it may not exist for a merged PR
@@ -577,6 +580,7 @@ export function initRepoIssueTitleEdit() {
577580
}
578581
}
579582
}
583+
window.removeEventListener('beforeunload', showUserHintOnNavigate);
580584
window.location.reload();
581585
} catch (error) {
582586
console.error(error);
@@ -585,6 +589,11 @@ export function initRepoIssueTitleEdit() {
585589
});
586590
}
587591

592+
// Show a warning message when the user tries to navigate, even though he is currently editing
593+
function showUserHintOnNavigate(e: BeforeUnloadEvent) {
594+
e.preventDefault();
595+
}
596+
588597
export function initRepoIssueBranchSelect() {
589598
document.querySelector('#branch-select')?.addEventListener('click', (e: MouseEvent & {target: HTMLElement}) => {
590599
const el = e.target.closest('.item[data-branch]');

0 commit comments

Comments
 (0)