|
| 1 | +import $ from 'jquery'; |
| 2 | + |
| 3 | +export function initRepoEditorCommit() { |
| 4 | + const commitButton = document.querySelector('#commit-changes-button'); |
| 5 | + const commitModal = document.querySelector('#commit-changes-modal'); |
| 6 | + const modalCommitButton = document.querySelector<HTMLButtonElement>('#commit-button'); |
| 7 | + |
| 8 | + if (!commitButton || !commitModal) return; |
| 9 | + |
| 10 | + const elForm = document.querySelector<HTMLFormElement>('.repository.editor .edit.form'); |
| 11 | + const dirtyFileClass = 'dirty-file'; |
| 12 | + |
| 13 | + // Sync the top commit button state with the modal commit button state |
| 14 | + const syncTopCommitButtonState = () => { |
| 15 | + // Check if form has changes (using the same dirty class from repo-editor.ts) |
| 16 | + const hasChanges = elForm?.classList.contains(dirtyFileClass); |
| 17 | + |
| 18 | + // Also check the modal commit button state as fallback |
| 19 | + const modalButtonDisabled = modalCommitButton?.disabled; |
| 20 | + |
| 21 | + if (hasChanges || !modalButtonDisabled) { |
| 22 | + commitButton.classList.remove('disabled'); |
| 23 | + } else { |
| 24 | + commitButton.classList.add('disabled'); |
| 25 | + } |
| 26 | + }; |
| 27 | + |
| 28 | + // For upload page - enable button when files are added |
| 29 | + const dropzone = document.querySelector('.dropzone'); |
| 30 | + if (dropzone) { |
| 31 | + const observer = new MutationObserver(() => { |
| 32 | + const filesContainer = dropzone.querySelector('.files'); |
| 33 | + const hasFiles = filesContainer && filesContainer.children.length > 0; |
| 34 | + |
| 35 | + if (hasFiles) { |
| 36 | + commitButton.classList.remove('disabled'); |
| 37 | + } else { |
| 38 | + commitButton.classList.add('disabled'); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + const filesContainer = dropzone.querySelector('.files'); |
| 43 | + if (filesContainer) { |
| 44 | + observer.observe(filesContainer, {childList: true}); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Watch for changes in the form's dirty state |
| 49 | + if (elForm) { |
| 50 | + const observer = new MutationObserver(syncTopCommitButtonState); |
| 51 | + observer.observe(elForm, {attributes: true, attributeFilter: ['class']}); |
| 52 | + |
| 53 | + // Initial sync |
| 54 | + syncTopCommitButtonState(); |
| 55 | + } |
| 56 | + |
| 57 | + // Also sync when modal commit button state changes |
| 58 | + if (modalCommitButton) { |
| 59 | + const observer = new MutationObserver(syncTopCommitButtonState); |
| 60 | + observer.observe(modalCommitButton, {attributes: true, attributeFilter: ['disabled']}); |
| 61 | + } |
| 62 | + |
| 63 | + commitButton.addEventListener('click', (e) => { |
| 64 | + e.preventDefault(); |
| 65 | + if (!commitButton.classList.contains('disabled')) { |
| 66 | + $(commitModal).modal('show'); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + // Handle form submission - close modal after submit |
| 71 | + if (elForm) { |
| 72 | + elForm.addEventListener('submit', () => { |
| 73 | + $(commitModal).modal('hide'); |
| 74 | + }); |
| 75 | + } |
| 76 | +} |
0 commit comments