From 0567032e75d7ddf616eaa494361bb7b43299ec77 Mon Sep 17 00:00:00 2001 From: Chaoyue Zhao Date: Fri, 15 Nov 2024 12:02:29 +0000 Subject: [PATCH 01/11] Update edit blob to use commit change modal Changelog: changed --- .../javascripts/blob_edit/blob_bundle.js | 9 +- .../javascripts/blob_edit/blob_edit_header.js | 44 +++++++++ app/assets/javascripts/blob_edit/edit_blob.js | 4 + .../components/commit_changes_modal.vue | 57 +++++++---- .../repository/pages/blob_edit_header.vue | 98 +++++++++++++++++++ app/helpers/blob_helper.rb | 16 +++ app/views/projects/blob/edit.html.haml | 19 ++-- ee/spec/helpers/ee/blob_helper_spec.rb | 2 +- locale/gitlab.pot | 3 - qa/qa/page/file/shared/editor.rb | 26 +++++ qa/qa/page/file/show.rb | 5 +- .../file/delete_file_via_web_spec.rb | 3 +- .../repository/file/edit_file_via_web_spec.rb | 3 +- .../source_editor_toolbar_spec.rb | 3 +- .../user/user_inherited_access_spec.rb | 5 +- .../maintainer_edits_fork_spec.rb | 7 ++ spec/features/projects/blobs/edit_spec.rb | 26 +++-- .../projects/files/editing_a_file_spec.rb | 10 +- .../projects/files/user_edits_files_spec.rb | 32 ++++-- spec/frontend/blob_edit/blob_bundle_spec.js | 5 +- spec/frontend/blob_edit/edit_blob_spec.js | 9 +- .../components/commit_changes_modal_spec.js | 61 +++++++++--- .../repository/pages/blob_edit_header_spec.js | 80 +++++++++++++++ spec/helpers/blob_helper_spec.rb | 98 ++++++++++++++++++- 24 files changed, 548 insertions(+), 77 deletions(-) create mode 100644 app/assets/javascripts/blob_edit/blob_edit_header.js create mode 100644 app/assets/javascripts/repository/pages/blob_edit_header.vue create mode 100644 spec/frontend/repository/pages/blob_edit_header_spec.js diff --git a/app/assets/javascripts/blob_edit/blob_bundle.js b/app/assets/javascripts/blob_edit/blob_bundle.js index 730ea9fe80125d..7e097041beb874 100644 --- a/app/assets/javascripts/blob_edit/blob_bundle.js +++ b/app/assets/javascripts/blob_edit/blob_bundle.js @@ -1,6 +1,6 @@ import $ from 'jquery'; import { createAlert } from '~/alert'; -import NewCommitForm from '../new_commit_form'; +import initBlobEditHeader from '~/blob_edit/blob_edit_header'; export default () => { const editBlobForm = $('.js-edit-blob-form'); @@ -20,8 +20,7 @@ export default () => { import('./edit_blob') .then(({ default: EditBlob } = {}) => { - // eslint-disable-next-line no-new - new EditBlob({ + const editor = new EditBlob({ assetsPath: `${urlRoot}${assetsPath}`, filePath, currentAction, @@ -30,6 +29,8 @@ export default () => { isMarkdown, previewMarkdownPath, }); + + initBlobEditHeader(editor); }) .catch((e) => createAlert({ @@ -47,8 +48,6 @@ export default () => { window.onbeforeunload = null; }); - new NewCommitForm(editBlobForm); // eslint-disable-line no-new - // returning here blocks page navigation window.onbeforeunload = () => ''; } diff --git a/app/assets/javascripts/blob_edit/blob_edit_header.js b/app/assets/javascripts/blob_edit/blob_edit_header.js new file mode 100644 index 00000000000000..ea2891a191187f --- /dev/null +++ b/app/assets/javascripts/blob_edit/blob_edit_header.js @@ -0,0 +1,44 @@ +import Vue from 'vue'; +import { parseBoolean } from '~/lib/utils/common_utils'; +import BlobEditHeader from '~/repository/pages/blob_edit_header.vue'; + +export default function initBlobEditHeader(editor) { + const el = document.querySelector('.js-blob-edit-header'); + + if (!el) { + return null; + } + + const { + updatePath, + cancelPath, + originalBranch, + targetBranch, + canPushCode, + canPushToBranch, + emptyRepo, + isUsingLfs, + blobName, + branchAllowsCollaboration, + lastCommitSha, + } = el.dataset; + + return new Vue({ + el, + provide: { + editor, + updatePath, + cancelPath, + originalBranch, + targetBranch, + blobName, + lastCommitSha, + emptyRepo: parseBoolean(emptyRepo), + canPushCode: parseBoolean(canPushCode), + canPushToBranch: parseBoolean(canPushToBranch), + isUsingLfs: parseBoolean(isUsingLfs), + branchAllowsCollaboration: parseBoolean(branchAllowsCollaboration), + }, + render: (createElement) => createElement(BlobEditHeader), + }); +} diff --git a/app/assets/javascripts/blob_edit/edit_blob.js b/app/assets/javascripts/blob_edit/edit_blob.js index 123e59bdff0431..0a35ba64c075d0 100644 --- a/app/assets/javascripts/blob_edit/edit_blob.js +++ b/app/assets/javascripts/blob_edit/edit_blob.js @@ -216,4 +216,8 @@ export default class EditBlob { this.$toggleButton.toggleClass('soft-wrap-active', this.isSoftWrapped); this.editor.updateOptions({ wordWrap: this.isSoftWrapped ? 'on' : 'off' }); } + + getFileContent() { + return this.editor?.getValue(); + } } diff --git a/app/assets/javascripts/repository/components/commit_changes_modal.vue b/app/assets/javascripts/repository/components/commit_changes_modal.vue index 074cfe1c766898..4a70f043958649 100644 --- a/app/assets/javascripts/repository/components/commit_changes_modal.vue +++ b/app/assets/javascripts/repository/components/commit_changes_modal.vue @@ -16,6 +16,7 @@ import csrf from '~/lib/utils/csrf'; import { __, s__ } from '~/locale'; import validation, { initFormField } from '~/vue_shared/directives/validation'; import { helpPagePath } from '~/helpers/help_page_helper'; +import { getParameterByName } from '~/lib/utils/url_utility'; import { SECONDARY_OPTIONS_TEXT, COMMIT_LABEL, @@ -106,6 +107,26 @@ export default { required: false, default: false, }, + method: { + type: String, + required: false, + default: 'delete', + }, + fileContent: { + type: String, + required: false, + default: null, + }, + filePath: { + type: String, + required: false, + default: null, + }, + isEdit: { + type: Boolean, + required: false, + default: false, + }, branchAllowsCollaboration: { type: Boolean, required: false, @@ -153,6 +174,7 @@ export default { variant: 'confirm', loading: this.loading, disabled: this.loading || !this.form.state || !this.valid, + 'data-testid': 'commit-change-modal-commit-button', }, }; @@ -189,7 +211,7 @@ export default { ); }, showLfsWarning() { - return this.isUsingLfs && !this.lfsWarningDismissed; + return this.isUsingLfs && !this.lfsWarningDismissed && !this.isEdit; }, title() { return this.showLfsWarning @@ -197,22 +219,10 @@ export default { : this.$options.i18n.COMMIT_CHANGES; }, showForm() { - return !this.isUsingLfs || (this.isUsingLfs && this.lfsWarningDismissed); + return !this.isUsingLfs || (this.isUsingLfs && this.lfsWarningDismissed) || this.isEdit; }, - }, - watch: { - createNewBranch: { - handler(newValue) { - if (newValue) { - this.form.fields.branch_name.value = ''; - } else { - this.form.fields.branch_name = { - ...this.form.fields.branch_name, - value: this.originalBranch, - state: true, - }; - } - }, + fromMergeRequestIid() { + return getParameterByName('from_merge_request_iid') || ''; }, }, methods: { @@ -229,6 +239,7 @@ export default { this.$refs.message?.$el.focus(); }, async handlePrimaryAction(e) { + window.onbeforeunload = null; e.preventDefault(); // Prevent modal from closing if (this.showLfsWarning) { @@ -264,6 +275,7 @@ export default { v-bind="$attrs" :modal-id="modalId" :title="title" + data-testid="commit-change-modal" :action-primary="primaryOptions" :action-cancel="cancelOptions" @primary="handlePrimaryAction" @@ -286,7 +298,14 @@ export default {

- + + + @@ -364,7 +382,7 @@ export default {