Skip to content

Commit d0726b6

Browse files
author
Tim Wundenberg
committed
fix(issue): #32223 warn on navigation if currently editing comment or title
1 parent f47fb4f commit d0726b6

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ async function tryOnEditContent(e) {
1818
const renderContent = segment.querySelector('.render-content');
1919
const rawContent = segment.querySelector('.raw-content');
2020

21-
let comboMarkdownEditor : ComboMarkdownEditor;
21+
let comboMarkdownEditor: ComboMarkdownEditor;
2222

2323
const cancelAndReset = (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: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function initRepoIssueLabelFilter(elDropdown: Element) {
6161
selectedLabelIds.add(`${Math.abs(parseInt(id))}`); // "labels" contains negative ids, which are excluded
6262
}
6363

64-
const excludeLabel = (e: MouseEvent|KeyboardEvent, item: Element) => {
64+
const excludeLabel = (e: MouseEvent | KeyboardEvent, item: Element) => {
6565
e.preventDefault();
6666
e.stopPropagation();
6767
const labelId = item.getAttribute('data-label-id');
@@ -124,7 +124,7 @@ export function initRepoIssueFilterItemLabel() {
124124

125125
export function initRepoIssueCommentDelete() {
126126
// Delete comment
127-
document.addEventListener('click', async (e: MouseEvent & {target: HTMLElement}) => {
127+
document.addEventListener('click', async (e: MouseEvent & { target: HTMLElement }) => {
128128
if (!e.target.matches('.delete-comment')) return;
129129
e.preventDefault();
130130

@@ -199,7 +199,7 @@ export function initRepoIssueDependencyDelete() {
199199

200200
export function initRepoIssueCodeCommentCancel() {
201201
// Cancel inline code comment
202-
document.addEventListener('click', (e: MouseEvent & {target: HTMLElement}) => {
202+
document.addEventListener('click', (e: MouseEvent & { target: HTMLElement }) => {
203203
if (!e.target.matches('.cancel-code-comment')) return;
204204

205205
const form = e.target.closest('form');
@@ -217,7 +217,7 @@ export function initRepoPullRequestUpdate() {
217217
const pullUpdateButton = document.querySelector('.update-button > button');
218218
if (!pullUpdateButton) return;
219219

220-
pullUpdateButton.addEventListener('click', async function (e) {
220+
pullUpdateButton.addEventListener('click', async function(e) {
221221
e.preventDefault();
222222
const redirect = this.getAttribute('data-redirect');
223223
this.classList.add('is-loading');
@@ -273,9 +273,11 @@ export function initRepoPullRequestAllowMaintainerEdit() {
273273
const url = `${wrapper.getAttribute('data-url')}/set_allow_maintainer_edit`;
274274
wrapper.classList.add('is-loading');
275275
try {
276-
const resp = await POST(url, {data: new URLSearchParams({
277-
allow_maintainer_edit: String(checkbox.checked),
278-
})});
276+
const resp = await POST(url, {
277+
data: new URLSearchParams({
278+
allow_maintainer_edit: String(checkbox.checked),
279+
}),
280+
});
279281
if (!resp.ok) {
280282
throw new Error('Failed to update maintainer edit permission');
281283
}
@@ -340,7 +342,7 @@ export function initRepoIssueWipTitle() {
340342
export function initRepoIssueComments() {
341343
if (!$('.repository.view.issue .timeline').length) return;
342344

343-
document.addEventListener('click', (e: MouseEvent & {target: HTMLElement}) => {
345+
document.addEventListener('click', (e: MouseEvent & { target: HTMLElement }) => {
344346
const urlTarget = document.querySelector(':target');
345347
if (!urlTarget) return;
346348

@@ -406,7 +408,7 @@ export function initRepoPullRequestReview() {
406408
}
407409
}
408410

409-
$(document).on('click', '.show-outdated', function (e) {
411+
$(document).on('click', '.show-outdated', function(e) {
410412
e.preventDefault();
411413
const id = this.getAttribute('data-comment');
412414
hideElem(this);
@@ -415,7 +417,7 @@ export function initRepoPullRequestReview() {
415417
showElem(`#hide-outdated-${id}`);
416418
});
417419

418-
$(document).on('click', '.hide-outdated', function (e) {
420+
$(document).on('click', '.hide-outdated', function(e) {
419421
e.preventDefault();
420422
const id = this.getAttribute('data-comment');
421423
hideElem(this);
@@ -424,7 +426,7 @@ export function initRepoPullRequestReview() {
424426
showElem(`#show-outdated-${id}`);
425427
});
426428

427-
$(document).on('click', 'button.comment-form-reply', async function (e) {
429+
$(document).on('click', 'button.comment-form-reply', async function(e) {
428430
e.preventDefault();
429431
await handleReply(this);
430432
});
@@ -492,7 +494,7 @@ export function initRepoPullRequestReview() {
492494

493495
export function initRepoIssueReferenceIssue() {
494496
// Reference issue
495-
$(document).on('click', '.reference-issue', function (e) {
497+
$(document).on('click', '.reference-issue', function(e) {
496498
const target = this.getAttribute('data-target');
497499
const content = document.querySelector(`#${target}`)?.textContent ?? '';
498500
const poster = this.getAttribute('data-poster-username');
@@ -546,12 +548,15 @@ export function initRepoIssueTitleEdit() {
546548
issueTitleInput.value = oldTitle;
547549
}
548550
issueTitleInput.focus();
551+
552+
window.addEventListener('beforeunload', showUserHintOnNavigate);
549553
});
550554
issueTitleEditor.querySelector('.ui.cancel.button').addEventListener('click', () => {
551555
hideElem(issueTitleEditor);
552556
hideElem('#pull-desc-editor');
553557
showElem(issueTitleDisplay);
554558
showElem('#pull-desc-display');
559+
window.removeEventListener('beforeunload', showUserHintOnNavigate);
555560
});
556561

557562
const pullDescEditor = document.querySelector('#pull-desc-editor'); // it may not exist for a merged PR
@@ -577,6 +582,7 @@ export function initRepoIssueTitleEdit() {
577582
}
578583
}
579584
}
585+
window.removeEventListener('beforeunload', showUserHintOnNavigate);
580586
window.location.reload();
581587
} catch (error) {
582588
console.error(error);
@@ -585,8 +591,13 @@ export function initRepoIssueTitleEdit() {
585591
});
586592
}
587593

594+
// Show a warning message when the user tries to navigate, even though he is currently editing
595+
function showUserHintOnNavigate(e: BeforeUnloadEvent) {
596+
e.preventDefault();
597+
}
598+
588599
export function initRepoIssueBranchSelect() {
589-
document.querySelector('#branch-select')?.addEventListener('click', (e: MouseEvent & {target: HTMLElement}) => {
600+
document.querySelector('#branch-select')?.addEventListener('click', (e: MouseEvent & { target: HTMLElement }) => {
590601
const el = e.target.closest('.item[data-branch]');
591602
if (!el) return;
592603
const pullTargetBranch = document.querySelector('#pull-target-branch');

0 commit comments

Comments
 (0)