Skip to content

Commit 216a875

Browse files
committed
fix
1 parent 41b4ef8 commit 216a875

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

web_src/js/features/comp/QuickSubmit.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {querySingleVisibleElem} from '../../utils/dom.ts';
2+
13
export function handleGlobalEnterQuickSubmit(target) {
24
let form = target.closest('form');
35
if (form) {
@@ -12,7 +14,11 @@ export function handleGlobalEnterQuickSubmit(target) {
1214
}
1315
form = target.closest('.ui.form');
1416
if (form) {
15-
form.querySelector('.ui.primary.button')?.click();
17+
// A form should only have at most one "primary" button to do quick-submit.
18+
// Here we don't use a special class to mark the primary button,
19+
// because there could be a lot of forms with a primary button, the quick submit should work out-of-box,
20+
// but not keeps asking developers to add that special class again and again (it could be forgotten easily)
21+
querySingleVisibleElem<HTMLButtonElement>(form, '.ui.primary.button')?.click();
1622
return true;
1723
}
1824
return false;

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {handleReply} from './repo-issue.ts';
33
import {getComboMarkdownEditor, initComboMarkdownEditor, ComboMarkdownEditor} from './comp/ComboMarkdownEditor.ts';
44
import {POST} from '../modules/fetch.ts';
55
import {showErrorToast} from '../modules/toast.ts';
6-
import {hideElem, showElem} from '../utils/dom.ts';
6+
import {hideElem, querySingleVisibleElem, showElem} from '../utils/dom.ts';
77
import {attachRefIssueContextPopup} from './contextpopup.ts';
88
import {initCommentContent, initMarkupContent} from '../markup/content.ts';
99
import {triggerUploadStateChanged} from './comp/EditorUpload.ts';
@@ -77,20 +77,22 @@ async function onEditContent(event) {
7777
}
7878
};
7979

80+
// Show write/preview tab and copy raw content as needed
81+
showElem(editContentZone);
82+
hideElem(renderContent);
83+
8084
comboMarkdownEditor = getComboMarkdownEditor(editContentZone.querySelector('.combo-markdown-editor'));
8185
if (!comboMarkdownEditor) {
8286
editContentZone.innerHTML = document.querySelector('#issue-comment-editor-template').innerHTML;
83-
const saveButton = editContentZone.querySelector('.ui.primary.button');
87+
const saveButton = querySingleVisibleElem<HTMLButtonElement>(editContentZone, '.ui.primary.button');
88+
const cancelButton = querySingleVisibleElem<HTMLButtonElement>(editContentZone, '.ui.cancel.button');
8489
comboMarkdownEditor = await initComboMarkdownEditor(editContentZone.querySelector('.combo-markdown-editor'));
8590
const syncUiState = () => saveButton.disabled = comboMarkdownEditor.isUploading();
8691
comboMarkdownEditor.container.addEventListener(ComboMarkdownEditor.EventUploadStateChanged, syncUiState);
87-
editContentZone.querySelector('.ui.cancel.button').addEventListener('click', cancelAndReset);
92+
cancelButton.addEventListener('click', cancelAndReset);
8893
saveButton.addEventListener('click', saveAndRefresh);
8994
}
9095

91-
// Show write/preview tab and copy raw content as needed
92-
showElem(editContentZone);
93-
hideElem(renderContent);
9496
// FIXME: ideally here should reload content and attachment list from backend for existing editor, to avoid losing data
9597
if (!comboMarkdownEditor.value()) {
9698
comboMarkdownEditor.value(rawContent.textContent);

web_src/js/utils/dom.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,13 @@ export function animateOnce(el: Element, animationClassName: string): Promise<vo
330330
el.classList.add(animationClassName);
331331
});
332332
}
333+
334+
export function querySingleVisibleElem<T extends HTMLElement>(parent: Element, selector: string) : T|null {
335+
const elems = parent.querySelectorAll<HTMLElement>(selector);
336+
const candidates = [];
337+
for (const button of elems) {
338+
if (isElemVisible(button)) candidates.push(button);
339+
}
340+
if (candidates.length > 1) throw new Error('multiple primary buttons found, only one could be defined');
341+
return candidates.length ? candidates[0] as T : null;
342+
}

0 commit comments

Comments
 (0)