Skip to content

Commit 3ad1e7e

Browse files
committed
Enable commit button only if something has changed
1 parent 9811c28 commit 3ad1e7e

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

templates/repo/editor/commit_form.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
</div>
7878
{{end}}
7979
</div>
80-
<button id="commit-button" type="submit" class="ui primary button">
80+
<button id="commit-button" type="submit" class="ui primary button" {{if .PageIsEdit}}disabled{{end}}>
8181
{{if eq .commit_choice "commit-to-new-branch"}}{{ctx.Locale.Tr "repo.editor.propose_file_change"}}{{else}}{{ctx.Locale.Tr "repo.editor.commit_changes"}}{{end}}
8282
</button>
8383
<a class="ui button red" href="{{if .ReturnURI}}{{.ReturnURI}}{{else}}{{$.BranchLink}}/{{PathEscapeSegments .TreePath}}{{end}}">{{ctx.Locale.Tr "repo.editor.cancel"}}</a>

web_src/js/features/repo-editor.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,38 +141,39 @@ export function initRepoEditor() {
141141
}
142142
});
143143

144+
const elForm = document.querySelector<HTMLFormElement>('.repository.editor .edit.form');
145+
146+
// Using events from https://github.com/codedance/jquery.AreYouSure#advanced-usage
147+
// to enable or disable the commit button
148+
const commitButton = document.querySelector<HTMLButtonElement>('#commit-button');
149+
const dirtyFileClass = 'dirty-file';
150+
151+
// Enabling the button at the start if the page has posted
152+
if (document.querySelector<HTMLInputElement>('input[name="page_has_posted"]')?.value === 'true') {
153+
commitButton.disabled = false;
154+
}
155+
156+
// Registering a custom listener for the file path and the file content
157+
// FIXME: it is not quite right here (old bug), it causes double-init, the global areYouSure "dirty" class will also be added
158+
applyAreYouSure(elForm, {
159+
silent: true,
160+
dirtyClass: dirtyFileClass,
161+
fieldSelector: ':input:not(.commit-form-wrapper :input)',
162+
change($form: any) {
163+
const dirty = $form[0]?.classList.contains(dirtyFileClass);
164+
commitButton.disabled = !dirty;
165+
},
166+
});
167+
144168
// on the upload page, there is no editor(textarea)
145169
const editArea = document.querySelector<HTMLTextAreaElement>('.page-content.repository.editor textarea#edit_area');
146170
if (!editArea) return;
147171

148-
const elForm = document.querySelector<HTMLFormElement>('.repository.editor .edit.form');
149172
initEditPreviewTab(elForm);
150173

151174
(async () => {
152175
const editor = await createCodeEditor(editArea, filenameInput);
153176

154-
// Using events from https://github.com/codedance/jquery.AreYouSure#advanced-usage
155-
// to enable or disable the commit button
156-
const commitButton = document.querySelector<HTMLButtonElement>('#commit-button');
157-
const dirtyFileClass = 'dirty-file';
158-
159-
// Disabling the button at the start
160-
if (document.querySelector<HTMLInputElement>('input[name="page_has_posted"]').value !== 'true') {
161-
commitButton.disabled = true;
162-
}
163-
164-
// Registering a custom listener for the file path and the file content
165-
// FIXME: it is not quite right here (old bug), it causes double-init, the global areYouSure "dirty" class will also be added
166-
applyAreYouSure(elForm, {
167-
silent: true,
168-
dirtyClass: dirtyFileClass,
169-
fieldSelector: ':input:not(.commit-form-wrapper :input)',
170-
change($form: any) {
171-
const dirty = $form[0]?.classList.contains(dirtyFileClass);
172-
commitButton.disabled = !dirty;
173-
},
174-
});
175-
176177
// Update the editor from query params, if available,
177178
// only after the dirtyFileClass initialization
178179
const params = new URLSearchParams(window.location.search);

0 commit comments

Comments
 (0)