From 6ce1eebd3ad5a8b36233e027c0f1f32b05c70c32 Mon Sep 17 00:00:00 2001 From: Thomas Randolph Date: Tue, 19 Oct 2021 23:48:17 -0600 Subject: [PATCH 1/2] Add utility specifically for diff lines --- .../javascripts/diffs/utils/diff_line.js | 10 ++++++++ spec/frontend/diffs/utils/diff_line_spec.js | 25 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 app/assets/javascripts/diffs/utils/diff_line.js create mode 100644 spec/frontend/diffs/utils/diff_line_spec.js diff --git a/app/assets/javascripts/diffs/utils/diff_line.js b/app/assets/javascripts/diffs/utils/diff_line.js new file mode 100644 index 00000000000000..b40bf1b983d168 --- /dev/null +++ b/app/assets/javascripts/diffs/utils/diff_line.js @@ -0,0 +1,10 @@ +export function pickDirection({ line, code } = {}) { + const { left, right } = line; + let direction = left; + + if (right.line_code === code) { + direction = right; + } + + return direction; +} diff --git a/spec/frontend/diffs/utils/diff_line_spec.js b/spec/frontend/diffs/utils/diff_line_spec.js new file mode 100644 index 00000000000000..800872f90c2a8f --- /dev/null +++ b/spec/frontend/diffs/utils/diff_line_spec.js @@ -0,0 +1,25 @@ +import { pickDirection } from '~/diffs/utils/diff_line'; + +describe('diff_line utilities', () => { + describe('pickDirection', () => { + const left = { + line_code: 'left', + }; + const right = { + line_code: 'right', + }; + const line = { + left, + right, + }; + + it.each` + code | pick | pickDescription + ${'left'} | ${left} | ${'the left line'} + ${'right'} | ${right} | ${'the right line'} + ${'junk'} | ${left} | ${'the default: the left line'} + `('when provided a line and a line code `$code`, picks $pickDescription', ({ code, pick }) => { + expect(pickDirection({ line, code })).toBe(pick); + }); + }); +}); -- GitLab From 56bf3793b4de9cb2fa8c7711f1217003053d6f81 Mon Sep 17 00:00:00 2001 From: Thomas Randolph Date: Tue, 19 Oct 2021 23:49:10 -0600 Subject: [PATCH 2/2] Fix multiple comment forms suggesting the wrong lines This fix basically uses a local state for "what lines is the comment form attached to" instead of placing that info unnecessarily in a global variable. When the comment form is constructed, it's provided the correct lines, and the highlighting/suggestions are created from that. If the line range is updated manually by the user, the note form gets the updated range and properly updates for new suggestions. Changelog: fixed --- .../diffs/components/diff_comment_cell.vue | 6 ++++++ .../diffs/components/diff_line_note_form.vue | 18 +++++++++++++--- .../diffs/components/diff_view.vue | 15 ++++++++++++- .../javascripts/diffs/utils/diff_line.js | 4 ++-- .../components/multiline_comment_form.vue | 6 ++---- .../notes/components/note_form.vue | 2 +- .../components/diff_line_note_form_spec.js | 13 ++++++------ spec/frontend/diffs/utils/diff_line_spec.js | 21 ++++++++++++------- .../components/multiline_comment_form_spec.js | 12 ----------- 9 files changed, 60 insertions(+), 37 deletions(-) diff --git a/app/assets/javascripts/diffs/components/diff_comment_cell.vue b/app/assets/javascripts/diffs/components/diff_comment_cell.vue index 4af4b46f94c7a3..a4fae652d020ee 100644 --- a/app/assets/javascripts/diffs/components/diff_comment_cell.vue +++ b/app/assets/javascripts/diffs/components/diff_comment_cell.vue @@ -29,6 +29,11 @@ export default { required: false, default: false, }, + lineRange: { + type: Object, + required: false, + default: null, + }, linePosition: { type: String, required: false, @@ -59,6 +64,7 @@ export default { @@ -199,7 +209,9 @@ export default { import { GlFormSelect, GlSprintf } from '@gitlab/ui'; -import { mapActions, mapState } from 'vuex'; +import { mapActions } from 'vuex'; import { getSymbol, getLineClasses } from './multiline_comment_utils'; export default { @@ -27,13 +27,12 @@ export default { }; }, computed: { - ...mapState({ selectedCommentPosition: ({ notes }) => notes.selectedCommentPosition }), lineNumber() { return this.commentLineOptions[this.commentLineOptions.length - 1].text; }, }, created() { - const line = this.selectedCommentPosition?.start || this.lineRange?.start || this.line; + const line = this.lineRange?.start || this.line; this.commentLineStart = { line_code: line.line_code, @@ -42,7 +41,6 @@ export default { new_line: line.new_line, }; - if (this.selectedCommentPosition) return; this.highlightSelection(); }, destroyed() { diff --git a/app/assets/javascripts/notes/components/note_form.vue b/app/assets/javascripts/notes/components/note_form.vue index b05643e5e13c33..5129fd1317df0e 100644 --- a/app/assets/javascripts/notes/components/note_form.vue +++ b/app/assets/javascripts/notes/components/note_form.vue @@ -334,13 +334,13 @@ export default { :markdown-docs-path="markdownDocsPath" :quick-actions-docs-path="quickActionsDocsPath" :line="line" + :lines="lines" :note="discussionNote" :can-suggest="canSuggest" :add-spacing-classes="false" :help-page-path="helpPagePath" :show-suggest-popover="showSuggestPopover" :textarea-value="updatedNoteBody" - :lines="lines" @handleSuggestDismissed="() => $emit('handleSuggestDismissed')" >