Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 1 | /** |
| 2 | * @license |
Ben Rohlfs | 94fcbbc | 2022-05-27 10:45:03 +0200 | [diff] [blame] | 3 | * Copyright 2015 Google LLC |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 5 | */ |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 6 | import '../../../styles/shared-styles'; |
| 7 | import '../../plugins/gr-endpoint-decorator/gr-endpoint-decorator'; |
| 8 | import '../../plugins/gr-endpoint-param/gr-endpoint-param'; |
| 9 | import '../gr-button/gr-button'; |
| 10 | import '../gr-dialog/gr-dialog'; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 11 | import '../gr-formatted-text/gr-formatted-text'; |
Chris Poucet | 1c71386 | 2022-07-25 13:12:24 +0200 | [diff] [blame] | 12 | import '../gr-icon/gr-icon'; |
Ben Rohlfs | 6256117 | 2024-04-18 09:20:38 +0200 | [diff] [blame] | 13 | import '../gr-suggestion-textarea/gr-suggestion-textarea'; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 14 | import '../gr-tooltip-content/gr-tooltip-content'; |
| 15 | import '../gr-confirm-delete-comment-dialog/gr-confirm-delete-comment-dialog'; |
| 16 | import '../gr-account-label/gr-account-label'; |
Milutin Kristofic | 490fa95 | 2023-09-12 20:17:36 +0200 | [diff] [blame] | 17 | import '../gr-suggestion-diff-preview/gr-suggestion-diff-preview'; |
Milutin Kristofic | 03ec98d | 2024-02-20 19:56:39 +0100 | [diff] [blame] | 18 | import '../gr-fix-suggestions/gr-fix-suggestions'; |
Chris Poucet | c6e880b | 2021-11-15 19:57:06 +0100 | [diff] [blame] | 19 | import {getAppContext} from '../../../services/app-context'; |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 20 | import {css, html, LitElement, nothing, PropertyValues} from 'lit'; |
Frank Borden | 42c1a45 | 2022-08-11 16:27:20 +0200 | [diff] [blame] | 21 | import {customElement, property, query, state} from 'lit/decorators.js'; |
Milutin Kristofic | e9dbbe9 | 2023-05-17 21:21:28 +0200 | [diff] [blame] | 22 | import {provide, resolve} from '../../../models/dependency'; |
Ben Rohlfs | 6256117 | 2024-04-18 09:20:38 +0200 | [diff] [blame] | 23 | import {GrSuggestionTextarea} from '../gr-suggestion-textarea/gr-suggestion-textarea'; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 24 | import { |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 25 | AccountDetailInfo, |
Dhruv Srivastava | 65edec8 | 2023-02-28 19:37:59 +0100 | [diff] [blame] | 26 | DraftInfo, |
Ben Rohlfs | 4401b23 | 2021-10-21 13:51:59 +0200 | [diff] [blame] | 27 | NumericChangeId, |
Dhruv Srivastava | 0287bf9 | 2020-09-11 16:56:38 +0200 | [diff] [blame] | 28 | RepoName, |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 29 | RobotCommentInfo, |
Dhruv Srivastava | 4e27dc4 | 2023-03-01 10:49:49 +0100 | [diff] [blame] | 30 | Comment, |
Dhruv Srivastava | 4e27dc4 | 2023-03-01 10:49:49 +0100 | [diff] [blame] | 31 | isRobot, |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 32 | isSaving, |
| 33 | isError, |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 34 | isDraft, |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 35 | isNew, |
Chris Poucet | 7722698 | 2023-08-10 18:10:04 +0200 | [diff] [blame] | 36 | CommentInput, |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 37 | } from '../../../types/common'; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 38 | import {GrConfirmDeleteCommentDialog} from '../gr-confirm-delete-comment-dialog/gr-confirm-delete-comment-dialog'; |
Ben Rohlfs | 1d48706 | 2020-09-26 11:26:03 +0200 | [diff] [blame] | 39 | import { |
Chris Poucet | 7722698 | 2023-08-10 18:10:04 +0200 | [diff] [blame] | 40 | convertToCommentInput, |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 41 | createUserFixSuggestion, |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 42 | getContentInCommentRange, |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 43 | getUserSuggestion, |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 44 | hasUserSuggestion, |
Ben Rohlfs | ba44082 | 2023-04-11 18:08:03 +0200 | [diff] [blame] | 45 | id, |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 46 | NEWLINE_PATTERN, |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 47 | USER_SUGGESTION_START_PATTERN, |
Ben Rohlfs | 31825d8 | 2020-10-02 18:08:04 +0200 | [diff] [blame] | 48 | } from '../../../utils/comment-util'; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 49 | import { |
| 50 | OpenFixPreviewEventDetail, |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 51 | ReplyToCommentEventDetail, |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 52 | ValueChangedEvent, |
| 53 | } from '../../../types/events'; |
Ben Rohlfs | 44f0104 | 2023-02-18 13:27:57 +0100 | [diff] [blame] | 54 | import {fire} from '../../../utils/event-util'; |
Milutin Kristofic | 96ac52e | 2023-09-18 10:28:58 +0200 | [diff] [blame] | 55 | import {assertIsDefined, assert, uuid} from '../../../utils/common-util'; |
Dhruv Srivastava | a49dffd | 2022-10-20 14:04:37 +0200 | [diff] [blame] | 56 | import {Key, Modifier, whenVisible} from '../../../utils/dom-util'; |
Chris Poucet | dae98bf | 2022-01-05 15:23:45 +0100 | [diff] [blame] | 57 | import {commentsModelToken} from '../../../models/comments/comments-model'; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 58 | import {sharedStyles} from '../../../styles/shared-styles'; |
| 59 | import {subscribe} from '../../lit/subscription-controller'; |
| 60 | import {ShortcutController} from '../../lit/shortcut-controller'; |
Frank Borden | 42c1a45 | 2022-08-11 16:27:20 +0200 | [diff] [blame] | 61 | import {classMap} from 'lit/directives/class-map.js'; |
Ben Rohlfs | b995610 | 2023-05-12 17:07:06 +0200 | [diff] [blame] | 62 | import {FILE, LineNumber} from '../../../api/diff'; |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 63 | import {CommentSide, SpecialFilePath} from '../../../constants/constants'; |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 64 | import {Subject} from 'rxjs'; |
| 65 | import {debounceTime} from 'rxjs/operators'; |
Chris Poucet | bf65b8f | 2022-01-18 21:18:12 +0000 | [diff] [blame] | 66 | import {changeModelToken} from '../../../models/change/change-model'; |
Milutin Kristofic | 1cfc021 | 2024-02-01 20:16:06 +0100 | [diff] [blame] | 67 | import { |
| 68 | ChangeInfo, |
Milutin Kristofic | b0bc74a | 2024-02-13 09:55:52 +0100 | [diff] [blame] | 69 | FixSuggestionInfo, |
Milutin Kristofic | 1cfc021 | 2024-02-01 20:16:06 +0100 | [diff] [blame] | 70 | isBase64FileContent, |
| 71 | } from '../../../api/rest-api'; |
Ben Rohlfs | b91a6a4 | 2023-01-13 09:29:31 +0100 | [diff] [blame] | 72 | import {createDiffUrl} from '../../../models/views/change'; |
Chris Poucet | bb0cf83 | 2022-10-24 12:32:10 +0200 | [diff] [blame] | 73 | import {userModelToken} from '../../../models/user/user-model'; |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame] | 74 | import {modalStyles} from '../../../styles/gr-modal-styles'; |
Milutin Kristofic | 734df55 | 2023-08-07 10:38:50 +0200 | [diff] [blame] | 75 | import {KnownExperimentId} from '../../../services/flags/flags'; |
| 76 | import {pluginLoaderToken} from '../gr-js-api-interface/gr-plugin-loader'; |
Milutin Kristofic | e9dbbe9 | 2023-05-17 21:21:28 +0200 | [diff] [blame] | 77 | import { |
| 78 | CommentModel, |
| 79 | commentModelToken, |
| 80 | } from '../gr-comment-model/gr-comment-model'; |
Milutin Kristofic | aa1c08b | 2023-09-06 10:34:16 +0200 | [diff] [blame] | 81 | import {formStyles} from '../../../styles/form-styles'; |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 82 | import {Interaction, Timing} from '../../../constants/reporting'; |
| 83 | import { |
| 84 | AutocompleteCommentResponse, |
| 85 | Suggestion, |
| 86 | SuggestionsProvider, |
| 87 | } from '../../../api/suggestions'; |
Milutin Kristofic | 74caea4b | 2023-11-14 20:10:56 +0100 | [diff] [blame] | 88 | import {when} from 'lit/directives/when.js'; |
Milutin Kristofic | 5942c9d | 2023-11-15 20:34:42 +0100 | [diff] [blame] | 89 | import {getDocUrl} from '../../../utils/url-util'; |
| 90 | import {configModelToken} from '../../../models/config/config-model'; |
Milutin Kristofic | a31d894 | 2023-11-17 13:32:16 +0100 | [diff] [blame] | 91 | import {getFileExtension} from '../../../utils/file-util'; |
Milutin Kristofic | 4d96373 | 2024-01-04 12:10:03 +0100 | [diff] [blame] | 92 | import {storageServiceToken} from '../../../services/storage/gr-storage_impl'; |
Milutin Kristofic | d9a9c83 | 2024-02-20 19:03:30 +0100 | [diff] [blame] | 93 | import {deepEqual} from '../../../utils/deep-util'; |
Milutin Kristofic | 847c46d | 2024-04-15 13:43:06 +0200 | [diff] [blame] | 94 | import {GrSuggestionDiffPreview} from '../gr-suggestion-diff-preview/gr-suggestion-diff-preview'; |
| 95 | import {waitUntil} from '../../../utils/async-util'; |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 96 | import { |
| 97 | AutocompleteCache, |
| 98 | AutocompletionContext, |
| 99 | } from '../../../utils/autocomplete-cache'; |
| 100 | import {HintAppliedEventDetail, HintShownEventDetail} from '../../../api/embed'; |
| 101 | import {levenshteinDistance} from '../../../utils/string-util'; |
Wyatt Allen | 846ac2f | 2018-05-14 12:59:23 -0700 | [diff] [blame] | 102 | |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 103 | // visible for testing |
| 104 | export const AUTO_SAVE_DEBOUNCE_DELAY_MS = 2000; |
Milutin Kristofic | 61bec78 | 2024-04-15 11:40:24 +0000 | [diff] [blame] | 105 | export const GENERATE_SUGGESTION_DEBOUNCE_DELAY_MS = 500; |
Ben Rohlfs | 2ad9756 | 2024-04-26 18:27:16 +0200 | [diff] [blame] | 106 | export const AUTOCOMPLETE_DEBOUNCE_DELAY_MS = 200; |
Milutin Kristofic | 4d96373 | 2024-01-04 12:10:03 +0100 | [diff] [blame] | 107 | export const ENABLE_GENERATE_SUGGESTION_STORAGE_KEY = |
Milutin Kristofic | 409e4a0 | 2024-03-12 13:32:36 +0100 | [diff] [blame] | 108 | 'enableGenerateSuggestionStorageKeyForCommentWithId-'; |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 109 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 110 | declare global { |
| 111 | interface HTMLElementEventMap { |
Dhruv Srivastava | ee018e9 | 2022-08-31 11:37:46 +0200 | [diff] [blame] | 112 | 'comment-editing-changed': CustomEvent<CommentEditingChangedDetail>; |
Dhruv Srivastava | 463bb33 | 2022-08-31 13:00:49 +0200 | [diff] [blame] | 113 | 'comment-unresolved-changed': ValueChangedEvent<boolean>; |
| 114 | 'comment-text-changed': ValueChangedEvent<string>; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 115 | 'comment-anchor-tap': CustomEvent<CommentAnchorTapEventDetail>; |
Milutin Kristofic | a0d350b | 2024-01-10 19:42:33 +0100 | [diff] [blame] | 116 | 'apply-user-suggestion': CustomEvent; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 117 | } |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 120 | export interface CommentAnchorTapEventDetail { |
| 121 | number: LineNumber; |
| 122 | side?: CommentSide; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 123 | } |
Dmitrii Filippov | 3f3c205 | 2020-09-22 16:51:18 +0200 | [diff] [blame] | 124 | |
Dhruv Srivastava | ee018e9 | 2022-08-31 11:37:46 +0200 | [diff] [blame] | 125 | export interface CommentEditingChangedDetail { |
| 126 | editing: boolean; |
| 127 | path: string; |
| 128 | } |
| 129 | |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 130 | @customElement('gr-comment') |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 131 | export class GrComment extends LitElement { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 132 | /** |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 133 | * Fired when the parent thread component should create a reply. |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 134 | * |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 135 | * @event reply-to-comment |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 136 | */ |
Kasper Nilsson | d43d2a7 | 2018-10-19 14:26:41 -0700 | [diff] [blame] | 137 | |
Dmitrii Filippov | 3fd2b10 | 2019-11-15 16:16:46 +0100 | [diff] [blame] | 138 | /** |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 139 | * Fired when the open fix preview action is triggered. |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 140 | * |
| 141 | * @event open-fix-preview |
Tao Zhou | 500437d | 2020-02-14 16:57:27 +0100 | [diff] [blame] | 142 | */ |
Tao Zhou | 500437d | 2020-02-14 16:57:27 +0100 | [diff] [blame] | 143 | |
| 144 | /** |
Tao Zhou | 31f3f10 | 2020-04-27 16:15:29 +0200 | [diff] [blame] | 145 | * Fired when editing status changed. |
| 146 | * |
| 147 | * @event comment-editing-changed |
| 148 | */ |
| 149 | |
| 150 | /** |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 151 | * Fired when the comment's timestamp is tapped. |
| 152 | * |
| 153 | * @event comment-anchor-tap |
| 154 | */ |
Andrew Bonventre | 2816526 | 2016-05-19 17:24:45 -0700 | [diff] [blame] | 155 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 156 | @query('#editTextarea') |
Ben Rohlfs | 6256117 | 2024-04-18 09:20:38 +0200 | [diff] [blame] | 157 | textarea?: GrSuggestionTextarea; |
Viktar Donich | 7ad2892 | 2016-05-23 15:24:05 -0700 | [diff] [blame] | 158 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 159 | @query('#container') |
| 160 | container?: HTMLElement; |
Dhruv Srivastava | 0287bf9 | 2020-09-11 16:56:38 +0200 | [diff] [blame] | 161 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 162 | @query('#resolvedCheckbox') |
| 163 | resolvedCheckbox?: HTMLInputElement; |
Kasper Nilsson | d43d2a7 | 2018-10-19 14:26:41 -0700 | [diff] [blame] | 164 | |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame] | 165 | @query('#confirmDeleteModal') |
| 166 | confirmDeleteModal?: HTMLDialogElement; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 167 | |
Kamil Musin | c7d3f28 | 2022-12-29 13:27:55 +0100 | [diff] [blame] | 168 | @query('#confirmDeleteCommentDialog') |
| 169 | confirmDeleteDialog?: GrConfirmDeleteCommentDialog; |
| 170 | |
Milutin Kristofic | 847c46d | 2024-04-15 13:43:06 +0200 | [diff] [blame] | 171 | @query('#suggestionDiffPreview') |
| 172 | suggestionDiffPreview?: GrSuggestionDiffPreview; |
| 173 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 174 | @property({type: Object}) |
| 175 | comment?: Comment; |
| 176 | |
| 177 | // TODO: Move this out of gr-comment. gr-comment should not have a comments |
| 178 | // property. This is only used for hasHumanReply at the moment. |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 179 | @property({type: Array}) |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 180 | comments?: Comment[]; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 181 | |
| 182 | /** |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 183 | * Initial collapsed state of the comment. |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 184 | */ |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 185 | @property({type: Boolean, attribute: 'initially-collapsed'}) |
| 186 | initiallyCollapsed?: boolean; |
| 187 | |
| 188 | /** |
Dhruv Srivastava | e75f6f7 | 2022-08-25 10:14:37 +0200 | [diff] [blame] | 189 | * Hide the header for patchset level comments used in GrReplyDialog. |
| 190 | */ |
| 191 | @property({type: Boolean, attribute: 'hide-header'}) |
| 192 | hideHeader = false; |
| 193 | |
| 194 | /** |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 195 | * This is the *current* (internal) collapsed state of the comment. Do not set |
| 196 | * from the outside. Use `initiallyCollapsed` instead. This is just a |
| 197 | * reflected property such that css rules can be based on it. |
| 198 | */ |
| 199 | @property({type: Boolean, reflect: true}) |
| 200 | collapsed?: boolean; |
| 201 | |
| 202 | @property({type: Boolean, attribute: 'robot-button-disabled'}) |
| 203 | robotButtonDisabled = false; |
| 204 | |
Dhruv Srivastava | f007abc | 2022-09-06 11:18:57 +0200 | [diff] [blame] | 205 | @property({type: String}) |
| 206 | messagePlaceholder?: string; |
| 207 | |
Dhruv Srivastava | 4e5fd11 | 2022-08-25 12:01:22 +0200 | [diff] [blame] | 208 | // GrReplyDialog requires the patchset level comment to always remain |
| 209 | // editable. |
| 210 | @property({type: Boolean, attribute: 'permanent-editing-mode'}) |
| 211 | permanentEditingMode = false; |
| 212 | |
Chris Poucet | 7722698 | 2023-08-10 18:10:04 +0200 | [diff] [blame] | 213 | // Whether to disable autosaving |
| 214 | @property({type: Boolean}) |
| 215 | disableAutoSaving = false; |
| 216 | |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 217 | @state() |
Ben Rohlfs | 607126f | 2021-12-07 08:21:52 +0100 | [diff] [blame] | 218 | autoSaving?: Promise<DraftInfo>; |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 219 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 220 | @state() |
| 221 | changeNum?: NumericChangeId; |
| 222 | |
| 223 | @state() |
| 224 | editing = false; |
| 225 | |
| 226 | @state() |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 227 | repoName?: RepoName; |
| 228 | |
| 229 | /* The 'dirty' state of the comment.message, which will be saved on demand. */ |
| 230 | @state() |
| 231 | messageText = ''; |
| 232 | |
Ben Rohlfs | 2ad9756 | 2024-04-26 18:27:16 +0200 | [diff] [blame] | 233 | /** |
| 234 | * An hint for autocompleting the comment message from plugin suggestion |
| 235 | * providers. |
| 236 | */ |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 237 | @state() autocompleteHint?: AutocompletionContext; |
| 238 | |
| 239 | private autocompleteAcceptedHints: string[] = []; |
Ben Rohlfs | 2ad9756 | 2024-04-26 18:27:16 +0200 | [diff] [blame] | 240 | |
Ben Rohlfs | b387369 | 2024-05-15 13:40:33 +0200 | [diff] [blame] | 241 | /** Based on user preferences. */ |
| 242 | @state() autocompleteEnabled = true; |
| 243 | |
Ben Rohlfs | 4deb8df5 | 2024-05-10 12:53:19 +0200 | [diff] [blame] | 244 | readonly autocompleteCache = new AutocompleteCache(); |
| 245 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 246 | /* The 'dirty' state of !comment.unresolved, which will be saved on demand. */ |
| 247 | @state() |
| 248 | unresolved = true; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 249 | |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 250 | @state() |
| 251 | generateSuggestion = true; |
| 252 | |
Milutin Kristofic | 96ac52e | 2023-09-18 10:28:58 +0200 | [diff] [blame] | 253 | @state() |
Milutin Kristofic | 8f07c38 | 2023-10-02 13:13:21 +0200 | [diff] [blame] | 254 | generatedSuggestion?: Suggestion; |
Milutin Kristofic | 96ac52e | 2023-09-18 10:28:58 +0200 | [diff] [blame] | 255 | |
| 256 | @state() |
Milutin Kristofic | d9a9c83 | 2024-02-20 19:03:30 +0100 | [diff] [blame] | 257 | generatedFixSuggestion: FixSuggestionInfo | undefined = |
| 258 | this.comment?.fix_suggestions?.[0]; |
Milutin Kristofic | 1cfc021 | 2024-02-01 20:16:06 +0100 | [diff] [blame] | 259 | |
| 260 | @state() |
Milutin Kristofic | 06dfda4 | 2023-11-15 09:51:13 +0100 | [diff] [blame] | 261 | generatedSuggestionId?: string; |
Milutin Kristofic | 96ac52e | 2023-09-18 10:28:58 +0200 | [diff] [blame] | 262 | |
Milutin Kristofic | 74caea4b | 2023-11-14 20:10:56 +0100 | [diff] [blame] | 263 | @state() |
Milutin Kristofic | c0582fc | 2024-04-02 20:30:51 +0200 | [diff] [blame] | 264 | addedGeneratedSuggestion?: string; |
| 265 | |
| 266 | @state() |
Milutin Kristofic | a31d894 | 2023-11-17 13:32:16 +0100 | [diff] [blame] | 267 | suggestionsProvider?: SuggestionsProvider; |
| 268 | |
| 269 | @state() |
Milutin Kristofic | 74caea4b | 2023-11-14 20:10:56 +0100 | [diff] [blame] | 270 | suggestionLoading = false; |
| 271 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 272 | @property({type: Boolean, attribute: 'show-patchset'}) |
| 273 | showPatchset = false; |
Tao Zhou | 500437d | 2020-02-14 16:57:27 +0100 | [diff] [blame] | 274 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 275 | @property({type: Boolean, attribute: 'show-ported-comment'}) |
Dhruv Srivastava | 0287bf9 | 2020-09-11 16:56:38 +0200 | [diff] [blame] | 276 | showPortedComment = false; |
| 277 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 278 | @state() |
| 279 | account?: AccountDetailInfo; |
| 280 | |
| 281 | @state() |
| 282 | isAdmin = false; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 283 | |
Milutin Kristofic | 15bfb3e | 2022-08-16 20:01:33 +0200 | [diff] [blame] | 284 | @state() |
| 285 | isOwner = false; |
| 286 | |
Milutin Kristofic | 78cdec9 | 2023-08-29 14:37:31 +0200 | [diff] [blame] | 287 | @state() |
| 288 | commentedText?: string; |
| 289 | |
Milutin Kristofic | 5942c9d | 2023-11-15 20:34:42 +0100 | [diff] [blame] | 290 | @state() private docsBaseUrl = ''; |
| 291 | |
Chris Poucet | c6e880b | 2021-11-15 19:57:06 +0100 | [diff] [blame] | 292 | private readonly restApiService = getAppContext().restApiService; |
Ben Rohlfs | 43935a4 | 2020-12-01 19:14:09 +0100 | [diff] [blame] | 293 | |
Chris Poucet | c6e880b | 2021-11-15 19:57:06 +0100 | [diff] [blame] | 294 | private readonly reporting = getAppContext().reportingService; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 295 | |
Chris Poucet | bf65b8f | 2022-01-18 21:18:12 +0000 | [diff] [blame] | 296 | private readonly getChangeModel = resolve(this, changeModelToken); |
Chris Poucet | 0142248 | 2021-11-30 19:43:28 +0100 | [diff] [blame] | 297 | |
Chris Poucet | bb0cf83 | 2022-10-24 12:32:10 +0200 | [diff] [blame] | 298 | private readonly getCommentsModel = resolve(this, commentsModelToken); |
Dhruv Srivastava | db2ab60 | 2021-06-24 15:20:29 +0200 | [diff] [blame] | 299 | |
Chris Poucet | bb0cf83 | 2022-10-24 12:32:10 +0200 | [diff] [blame] | 300 | private readonly getUserModel = resolve(this, userModelToken); |
Ben Rohlfs | f92d3b5 | 2021-03-10 23:13:03 +0100 | [diff] [blame] | 301 | |
Milutin Kristofic | 734df55 | 2023-08-07 10:38:50 +0200 | [diff] [blame] | 302 | private readonly getPluginLoader = resolve(this, pluginLoaderToken); |
| 303 | |
Milutin Kristofic | 5942c9d | 2023-11-15 20:34:42 +0100 | [diff] [blame] | 304 | private readonly getConfigModel = resolve(this, configModelToken); |
| 305 | |
Milutin Kristofic | 4d96373 | 2024-01-04 12:10:03 +0100 | [diff] [blame] | 306 | private readonly getStorage = resolve(this, storageServiceToken); |
| 307 | |
Milutin Kristofic | 734df55 | 2023-08-07 10:38:50 +0200 | [diff] [blame] | 308 | private readonly flagsService = getAppContext().flagsService; |
| 309 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 310 | private readonly shortcuts = new ShortcutController(this); |
Ben Rohlfs | f92d3b5 | 2021-03-10 23:13:03 +0100 | [diff] [blame] | 311 | |
Milutin Kristofic | 78cdec9 | 2023-08-29 14:37:31 +0200 | [diff] [blame] | 312 | private commentModel = new CommentModel(this.restApiService); |
Milutin Kristofic | e9dbbe9 | 2023-05-17 21:21:28 +0200 | [diff] [blame] | 313 | |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 314 | /** |
| 315 | * This is triggered when the user types into the editing textarea. We then |
| 316 | * debounce it and call autoSave(). |
| 317 | */ |
Frank Borden | 3801d7d | 2023-03-27 09:00:58 +0000 | [diff] [blame] | 318 | private autoSaveTrigger$ = new Subject(); |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 319 | |
| 320 | /** |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 321 | * This is triggered when the user types into the editing textarea. We then |
| 322 | * debounce it and call generateSuggestEdit(). |
| 323 | */ |
| 324 | private generateSuggestionTrigger$ = new Subject(); |
| 325 | |
| 326 | /** |
Ben Rohlfs | 2ad9756 | 2024-04-26 18:27:16 +0200 | [diff] [blame] | 327 | * This is triggered when the user types into the editing textarea. We then |
| 328 | * debounce it and call autocompleteComment(). |
| 329 | */ |
| 330 | private autocompleteTrigger$ = new Subject(); |
| 331 | |
| 332 | /** |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 333 | * Set to the content of DraftInfo when entering editing mode. |
| 334 | * Only used for "Cancel". |
| 335 | */ |
| 336 | private originalMessage = ''; |
| 337 | |
| 338 | /** |
| 339 | * Set to the content of DraftInfo when entering editing mode. |
| 340 | * Only used for "Cancel". |
| 341 | */ |
| 342 | private originalUnresolved = false; |
| 343 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 344 | constructor() { |
| 345 | super(); |
Milutin Kristofic | e9dbbe9 | 2023-05-17 21:21:28 +0200 | [diff] [blame] | 346 | provide(this, commentModelToken, () => this.commentModel); |
Dhruv Srivastava | e110a37 | 2022-09-08 12:18:33 +0200 | [diff] [blame] | 347 | // Allow the shortcuts to bubble up so that GrReplyDialog can respond to |
| 348 | // them as well. |
| 349 | this.shortcuts.addLocal({key: Key.ESC}, () => this.handleEsc(), { |
| 350 | preventDefault: false, |
| 351 | }); |
Dhruv Srivastava | f43eee7 | 2022-09-14 11:03:01 +0200 | [diff] [blame] | 352 | for (const modifier of [Modifier.CTRL_KEY, Modifier.META_KEY]) { |
| 353 | this.shortcuts.addLocal( |
| 354 | {key: Key.ENTER, modifiers: [modifier]}, |
Milutin Kristofic | ed07cf1 | 2024-03-14 18:02:16 +0100 | [diff] [blame] | 355 | e => { |
Dhruv Srivastava | f43eee7 | 2022-09-14 11:03:01 +0200 | [diff] [blame] | 356 | this.save(); |
Milutin Kristofic | ed07cf1 | 2024-03-14 18:02:16 +0100 | [diff] [blame] | 357 | // We don't stop propagation for patchset comment |
| 358 | // (this.permanentEditingMode = true), but we stop it for normal |
| 359 | // comments. This prevents accidentally sending a reply when |
| 360 | // editing/saving them in the reply dialog. |
| 361 | if (!this.permanentEditingMode) { |
| 362 | e.preventDefault(); |
| 363 | e.stopPropagation(); |
| 364 | } |
Dhruv Srivastava | f43eee7 | 2022-09-14 11:03:01 +0200 | [diff] [blame] | 365 | }, |
| 366 | {preventDefault: false} |
| 367 | ); |
| 368 | } |
| 369 | // For Ctrl+s add shorctut with preventDefault so that it does |
| 370 | // not bubble up to the browser |
| 371 | for (const modifier of [Modifier.CTRL_KEY, Modifier.META_KEY]) { |
| 372 | this.shortcuts.addLocal({key: 's', modifiers: [modifier]}, () => { |
| 373 | this.save(); |
| 374 | }); |
Ben Rohlfs | aadbdd1 | 2021-10-19 11:49:01 +0200 | [diff] [blame] | 375 | } |
Milutin Kristofic | a0d350b | 2024-01-10 19:42:33 +0100 | [diff] [blame] | 376 | this.addEventListener('apply-user-suggestion', () => { |
| 377 | this.handleAppliedFix(); |
| 378 | }); |
Milutin Kristofic | 1ebae37 | 2022-11-22 20:35:38 +0100 | [diff] [blame] | 379 | this.addEventListener('open-user-suggest-preview', e => { |
| 380 | this.handleShowFix(e.detail.code); |
| 381 | }); |
Milutin Kristofic | 490fa95 | 2023-09-12 20:17:36 +0200 | [diff] [blame] | 382 | this.addEventListener('add-generated-suggestion', e => { |
| 383 | this.handleAddGeneratedSuggestion(e.detail.code); |
| 384 | }); |
Ben Rohlfs | b7082e1 | 2023-01-23 11:43:48 +0100 | [diff] [blame] | 385 | this.messagePlaceholder = 'Mention others with @'; |
Chris Poucet | 0b96141 | 2022-01-05 16:24:50 +0100 | [diff] [blame] | 386 | subscribe( |
| 387 | this, |
Chris Poucet | bb0cf83 | 2022-10-24 12:32:10 +0200 | [diff] [blame] | 388 | () => this.getUserModel().account$, |
Chris Poucet | 5ec77f0 | 2022-05-12 11:25:21 +0200 | [diff] [blame] | 389 | x => (this.account = x) |
| 390 | ); |
| 391 | subscribe( |
| 392 | this, |
Chris Poucet | bb0cf83 | 2022-10-24 12:32:10 +0200 | [diff] [blame] | 393 | () => this.getUserModel().isAdmin$, |
Chris Poucet | 5ec77f0 | 2022-05-12 11:25:21 +0200 | [diff] [blame] | 394 | x => (this.isAdmin = x) |
| 395 | ); |
| 396 | |
| 397 | subscribe( |
| 398 | this, |
| 399 | () => this.getChangeModel().repo$, |
| 400 | x => (this.repoName = x) |
| 401 | ); |
| 402 | subscribe( |
| 403 | this, |
| 404 | () => this.getChangeModel().changeNum$, |
Chris Poucet | bf65b8f | 2022-01-18 21:18:12 +0000 | [diff] [blame] | 405 | x => (this.changeNum = x) |
| 406 | ); |
| 407 | subscribe( |
| 408 | this, |
Milutin Kristofic | 15bfb3e | 2022-08-16 20:01:33 +0200 | [diff] [blame] | 409 | () => this.getChangeModel().isOwner$, |
| 410 | x => (this.isOwner = x) |
| 411 | ); |
| 412 | subscribe( |
| 413 | this, |
Chris Poucet | 5ec77f0 | 2022-05-12 11:25:21 +0200 | [diff] [blame] | 414 | () => |
| 415 | this.autoSaveTrigger$.pipe(debounceTime(AUTO_SAVE_DEBOUNCE_DELAY_MS)), |
Chris Poucet | bf65b8f | 2022-01-18 21:18:12 +0000 | [diff] [blame] | 416 | () => { |
| 417 | this.autoSave(); |
| 418 | } |
| 419 | ); |
Milutin Kristofic | 5942c9d | 2023-11-15 20:34:42 +0100 | [diff] [blame] | 420 | subscribe( |
| 421 | this, |
| 422 | () => this.getConfigModel().docsBaseUrl$, |
| 423 | docsBaseUrl => (this.docsBaseUrl = docsBaseUrl) |
| 424 | ); |
Ben Rohlfs | 2ad9756 | 2024-04-26 18:27:16 +0200 | [diff] [blame] | 425 | subscribe( |
| 426 | this, |
| 427 | () => this.getPluginLoader().pluginsModel.suggestionsPlugins$, |
| 428 | // We currently support results from only 1 provider. |
| 429 | suggestionsPlugins => |
| 430 | (this.suggestionsProvider = suggestionsPlugins?.[0]?.provider) |
| 431 | ); |
| 432 | subscribe( |
| 433 | this, |
| 434 | () => |
| 435 | this.autocompleteTrigger$.pipe( |
| 436 | debounceTime(AUTOCOMPLETE_DEBOUNCE_DELAY_MS) |
| 437 | ), |
| 438 | () => { |
| 439 | this.autocompleteComment(); |
| 440 | } |
| 441 | ); |
Milutin Kristofic | 1cfc021 | 2024-02-01 20:16:06 +0100 | [diff] [blame] | 442 | if ( |
| 443 | this.flagsService.isEnabled(KnownExperimentId.ML_SUGGESTED_EDIT) || |
| 444 | this.flagsService.isEnabled(KnownExperimentId.ML_SUGGESTED_EDIT_V2) |
| 445 | ) { |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 446 | subscribe( |
| 447 | this, |
| 448 | () => |
| 449 | this.generateSuggestionTrigger$.pipe( |
| 450 | debounceTime(GENERATE_SUGGESTION_DEBOUNCE_DELAY_MS) |
| 451 | ), |
| 452 | () => { |
Milutin Kristofic | 4d96373 | 2024-01-04 12:10:03 +0100 | [diff] [blame] | 453 | this.generateSuggestEdit(); |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 454 | } |
| 455 | ); |
Milutin Kristofic | ba95beb | 2024-01-12 10:20:25 +0100 | [diff] [blame] | 456 | subscribe( |
| 457 | this, |
| 458 | () => this.getUserModel().preferences$, |
| 459 | prefs => { |
Ben Rohlfs | b387369 | 2024-05-15 13:40:33 +0200 | [diff] [blame] | 460 | this.autocompleteEnabled = !!prefs.allow_autocompleting_comments; |
Milutin Kristofic | ba95beb | 2024-01-12 10:20:25 +0100 | [diff] [blame] | 461 | if ( |
| 462 | this.generateSuggestion !== |
| 463 | !!prefs.allow_suggest_code_while_commenting |
| 464 | ) { |
| 465 | this.generateSuggestion = |
| 466 | !!prefs.allow_suggest_code_while_commenting; |
| 467 | } |
| 468 | } |
| 469 | ); |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 470 | } |
Chris Poucet | 0b96141 | 2022-01-05 16:24:50 +0100 | [diff] [blame] | 471 | } |
| 472 | |
Milutin Kristofic | a31d894 | 2023-11-17 13:32:16 +0100 | [diff] [blame] | 473 | override connectedCallback() { |
| 474 | super.connectedCallback(); |
Milutin Kristofic | 409e4a0 | 2024-03-12 13:32:36 +0100 | [diff] [blame] | 475 | if (this.comment?.id) { |
| 476 | const generateSuggestionStoredContent = |
| 477 | this.getStorage().getEditableContentItem( |
| 478 | ENABLE_GENERATE_SUGGESTION_STORAGE_KEY + this.comment.id |
| 479 | ); |
| 480 | if (generateSuggestionStoredContent?.message === 'false') { |
| 481 | this.generateSuggestion = false; |
| 482 | } |
Milutin Kristofic | 4d96373 | 2024-01-04 12:10:03 +0100 | [diff] [blame] | 483 | } |
Milutin Kristofic | a31d894 | 2023-11-17 13:32:16 +0100 | [diff] [blame] | 484 | } |
| 485 | |
Gerrit Code Review | 86b969c | 2021-08-19 14:33:41 +0000 | [diff] [blame] | 486 | override disconnectedCallback() { |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 487 | // Clean up emoji dropdown. |
| 488 | if (this.textarea) this.textarea.closeDropdown(); |
Ben Rohlfs | 5f520da | 2021-03-10 14:58:43 +0100 | [diff] [blame] | 489 | super.disconnectedCallback(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 490 | } |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 491 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 492 | static override get styles() { |
| 493 | return [ |
Milutin Kristofic | aa1c08b | 2023-09-06 10:34:16 +0200 | [diff] [blame] | 494 | formStyles, |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 495 | sharedStyles, |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame] | 496 | modalStyles, |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 497 | css` |
| 498 | :host { |
| 499 | display: block; |
| 500 | font-family: var(--font-family); |
| 501 | padding: var(--spacing-m); |
| 502 | } |
| 503 | :host([collapsed]) { |
| 504 | padding: var(--spacing-s) var(--spacing-m); |
| 505 | } |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 506 | :host([error]) { |
| 507 | background-color: var(--error-background); |
| 508 | border-radius: var(--border-radius); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 509 | } |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 510 | .header { |
| 511 | align-items: center; |
| 512 | cursor: pointer; |
| 513 | display: flex; |
Dhruv Srivastava | d8f61e7 | 2022-09-16 07:34:34 +0000 | [diff] [blame] | 514 | padding-bottom: var(--spacing-m); |
| 515 | } |
| 516 | :host([collapsed]) .header { |
| 517 | padding-bottom: 0px; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 518 | } |
| 519 | .headerLeft > span { |
| 520 | font-weight: var(--font-weight-bold); |
| 521 | } |
| 522 | .headerMiddle { |
| 523 | color: var(--deemphasized-text-color); |
| 524 | flex: 1; |
| 525 | overflow: hidden; |
| 526 | } |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 527 | .draftTooltip { |
Ben Rohlfs | ba361a4 | 2022-09-01 12:12:45 +0200 | [diff] [blame] | 528 | font-weight: var(--font-weight-bold); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 529 | display: inline; |
| 530 | } |
Ben Rohlfs | ba361a4 | 2022-09-01 12:12:45 +0200 | [diff] [blame] | 531 | .draftTooltip gr-icon { |
| 532 | color: var(--info-foreground); |
| 533 | } |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 534 | .date { |
| 535 | justify-content: flex-end; |
| 536 | text-align: right; |
| 537 | white-space: nowrap; |
| 538 | } |
| 539 | span.date { |
| 540 | color: var(--deemphasized-text-color); |
| 541 | } |
| 542 | span.date:hover { |
| 543 | text-decoration: underline; |
| 544 | } |
| 545 | .actions, |
| 546 | .robotActions { |
| 547 | display: flex; |
Milutin Kristofic | b7929e7 | 2023-09-05 13:13:07 +0200 | [diff] [blame] | 548 | justify-content: space-between; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 549 | padding-top: 0; |
| 550 | } |
| 551 | .robotActions { |
| 552 | /* Better than the negative margin would be to remove the gr-button |
| 553 | * padding, but then we would also need to fix the buttons that are |
| 554 | * inserted by plugins. :-/ */ |
| 555 | margin: 4px 0 -4px; |
| 556 | } |
| 557 | .action { |
| 558 | margin-left: var(--spacing-l); |
| 559 | } |
Milutin Kristofic | b7929e7 | 2023-09-05 13:13:07 +0200 | [diff] [blame] | 560 | .leftActions, |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 561 | .rightActions { |
| 562 | display: flex; |
| 563 | justify-content: flex-end; |
| 564 | } |
Milutin Kristofic | b7929e7 | 2023-09-05 13:13:07 +0200 | [diff] [blame] | 565 | .leftActions gr-button, |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 566 | .rightActions gr-button { |
| 567 | --gr-button-padding: 0 var(--spacing-s); |
| 568 | } |
| 569 | .editMessage { |
| 570 | display: block; |
Dhruv Srivastava | 694e937 | 2022-09-13 10:29:08 +0200 | [diff] [blame] | 571 | margin-bottom: var(--spacing-m); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 572 | width: 100%; |
| 573 | } |
| 574 | .show-hide { |
| 575 | margin-left: var(--spacing-s); |
| 576 | } |
| 577 | .robotId { |
| 578 | color: var(--deemphasized-text-color); |
| 579 | margin-bottom: var(--spacing-m); |
| 580 | } |
| 581 | .robotRun { |
| 582 | margin-left: var(--spacing-m); |
| 583 | } |
| 584 | .robotRunLink { |
| 585 | margin-left: var(--spacing-m); |
| 586 | } |
| 587 | /* just for a11y */ |
| 588 | input.show-hide { |
| 589 | display: none; |
| 590 | } |
| 591 | label.show-hide { |
| 592 | cursor: pointer; |
| 593 | display: block; |
| 594 | } |
Chris Poucet | 1c71386 | 2022-07-25 13:12:24 +0200 | [diff] [blame] | 595 | label.show-hide gr-icon { |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 596 | vertical-align: top; |
| 597 | } |
| 598 | :host([collapsed]) #container .body { |
| 599 | padding-top: 0; |
| 600 | } |
| 601 | #container .collapsedContent { |
| 602 | display: block; |
| 603 | overflow: hidden; |
| 604 | padding-left: var(--spacing-m); |
| 605 | text-overflow: ellipsis; |
| 606 | white-space: nowrap; |
| 607 | } |
| 608 | .resolve, |
| 609 | .unresolved { |
| 610 | align-items: center; |
| 611 | display: flex; |
| 612 | flex: 1; |
| 613 | margin: 0; |
| 614 | } |
| 615 | .resolve label { |
| 616 | color: var(--comment-text-color); |
| 617 | } |
| 618 | gr-dialog .main { |
| 619 | display: flex; |
| 620 | flex-direction: column; |
| 621 | width: 100%; |
| 622 | } |
| 623 | #deleteBtn { |
| 624 | --gr-button-text-color: var(--deemphasized-text-color); |
| 625 | --gr-button-padding: 0; |
| 626 | } |
| 627 | |
| 628 | /** Disable select for the caret and actions */ |
| 629 | .actions, |
| 630 | .show-hide { |
| 631 | -webkit-user-select: none; |
| 632 | -moz-user-select: none; |
| 633 | -ms-user-select: none; |
| 634 | user-select: none; |
| 635 | } |
| 636 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 637 | .pointer { |
| 638 | cursor: pointer; |
| 639 | } |
| 640 | .patchset-text { |
| 641 | color: var(--deemphasized-text-color); |
| 642 | margin-left: var(--spacing-s); |
| 643 | } |
| 644 | .headerLeft gr-account-label { |
| 645 | --account-max-length: 130px; |
| 646 | width: 150px; |
| 647 | } |
| 648 | .headerLeft gr-account-label::part(gr-account-label-text) { |
| 649 | font-weight: var(--font-weight-bold); |
| 650 | } |
| 651 | .draft gr-account-label { |
| 652 | width: unset; |
| 653 | } |
Frank Borden | 0c07884 | 2022-09-19 15:47:26 +0200 | [diff] [blame] | 654 | .draft gr-formatted-text.message { |
Frank Borden | 3b3a4c9 | 2022-09-28 14:14:00 +0200 | [diff] [blame] | 655 | display: block; |
Frank Borden | 0c07884 | 2022-09-19 15:47:26 +0200 | [diff] [blame] | 656 | margin-bottom: var(--spacing-m); |
| 657 | } |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 658 | .portedMessage { |
| 659 | margin: 0 var(--spacing-m); |
| 660 | } |
| 661 | .link-icon { |
Chris Poucet | c414204 | 2022-06-28 17:51:50 +0200 | [diff] [blame] | 662 | margin-left: var(--spacing-m); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 663 | cursor: pointer; |
| 664 | } |
Milutin Kristofic | 80c3c7e | 2023-03-16 20:22:28 +0100 | [diff] [blame] | 665 | .suggestEdit { |
| 666 | /** same height as header */ |
| 667 | --margin: calc(0px - var(--spacing-s)); |
| 668 | margin-right: var(--spacing-s); |
| 669 | } |
| 670 | .suggestEdit gr-icon { |
| 671 | color: inherit; |
| 672 | margin-right: var(--spacing-s); |
| 673 | } |
Milutin Kristofic | 8f07c38 | 2023-10-02 13:13:21 +0200 | [diff] [blame] | 674 | .info { |
| 675 | background-color: var(--info-background); |
| 676 | padding: var(--spacing-l) var(--spacing-xl); |
| 677 | } |
| 678 | .info gr-icon { |
| 679 | color: var(--selected-foreground); |
| 680 | margin-right: var(--spacing-xl); |
| 681 | } |
Milutin Kristofic | 74caea4b | 2023-11-14 20:10:56 +0100 | [diff] [blame] | 682 | /* The basics of .loadingSpin are defined in shared styles. */ |
| 683 | .loadingSpin { |
| 684 | width: calc(var(--line-height-normal) - 2px); |
| 685 | height: calc(var(--line-height-normal) - 2px); |
| 686 | display: inline-block; |
| 687 | vertical-align: top; |
| 688 | position: relative; |
| 689 | /* Making up for the 2px reduced height above. */ |
| 690 | top: 1px; |
| 691 | } |
Milutin Kristofic | 647c8a2 | 2024-05-20 14:37:57 +0200 | [diff] [blame] | 692 | gr-suggestion-diff-preview, |
| 693 | gr-fix-suggestions { |
| 694 | margin-top: var(--spacing-s); |
| 695 | } |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 696 | `, |
| 697 | ]; |
Dhruv Srivastava | cf70e79 | 2020-07-24 15:35:39 +0200 | [diff] [blame] | 698 | } |
| 699 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 700 | override render() { |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 701 | if (!this.comment) return; |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 702 | this.toggleAttribute('saving', isSaving(this.comment)); |
| 703 | this.toggleAttribute('error', isError(this.comment)); |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 704 | const classes = { |
| 705 | container: true, |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 706 | draft: isDraft(this.comment), |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 707 | }; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 708 | return html` |
Ben Rohlfs | 7a16784 | 2022-09-29 21:55:50 +0200 | [diff] [blame] | 709 | <gr-endpoint-decorator name="comment"> |
| 710 | <gr-endpoint-param name="comment" .value=${this.comment}> |
| 711 | </gr-endpoint-param> |
| 712 | <gr-endpoint-param name="editing" .value=${this.editing}> |
| 713 | </gr-endpoint-param> |
Ben Rohlfs | 57c2c59 | 2022-10-25 12:49:11 +0200 | [diff] [blame] | 714 | <gr-endpoint-param name="message" .value=${this.messageText}> |
| 715 | </gr-endpoint-param> |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 716 | <gr-endpoint-param name="isDraft" .value=${isDraft(this.comment)}> |
Ben Rohlfs | 57c2c59 | 2022-10-25 12:49:11 +0200 | [diff] [blame] | 717 | </gr-endpoint-param> |
Ben Rohlfs | 7a16784 | 2022-09-29 21:55:50 +0200 | [diff] [blame] | 718 | <div id="container" class=${classMap(classes)}> |
| 719 | ${this.renderHeader()} |
| 720 | <div class="body"> |
| 721 | ${this.renderRobotAuthor()} ${this.renderEditingTextarea()} |
| 722 | ${this.renderCommentMessage()} |
| 723 | <gr-endpoint-slot name="above-actions"></gr-endpoint-slot> |
| 724 | ${this.renderHumanActions()} ${this.renderRobotActions()} |
Ben Rohlfs | 7a16784 | 2022-09-29 21:55:50 +0200 | [diff] [blame] | 725 | </div> |
Milutin Kristofic | 03ec98d | 2024-02-20 19:56:39 +0100 | [diff] [blame] | 726 | ${/* if this.editing */ this.renderGeneratedSuggestionPreview()} |
| 727 | ${/* if !this.editing */ this.renderFixSuggestionPreview()} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 728 | </div> |
Ben Rohlfs | 7a16784 | 2022-09-29 21:55:50 +0200 | [diff] [blame] | 729 | </gr-endpoint-decorator> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 730 | ${this.renderConfirmDialog()} |
| 731 | `; |
| 732 | } |
| 733 | |
Dhruv Srivastava | e75f6f7 | 2022-08-25 10:14:37 +0200 | [diff] [blame] | 734 | private renderHeader() { |
| 735 | if (this.hideHeader) return nothing; |
| 736 | return html` |
| 737 | <div |
| 738 | class="header" |
| 739 | id="header" |
| 740 | @click=${() => (this.collapsed = !this.collapsed)} |
| 741 | > |
| 742 | <div class="headerLeft"> |
| 743 | ${this.renderAuthor()} ${this.renderPortedCommentMessage()} |
| 744 | ${this.renderDraftLabel()} |
| 745 | </div> |
| 746 | <div class="headerMiddle">${this.renderCollapsedContent()}</div> |
Milutin Kristofic | 8238de5 | 2023-01-12 19:33:45 +0100 | [diff] [blame] | 747 | ${this.renderSuggestEditButton()} ${this.renderRunDetails()} |
| 748 | ${this.renderDeleteButton()} ${this.renderPatchset()} |
Ben Rohlfs | 0ed3359 | 2023-04-12 11:33:27 +0200 | [diff] [blame] | 749 | ${this.renderSeparator()} ${this.renderDate()} ${this.renderToggle()} |
Dhruv Srivastava | e75f6f7 | 2022-08-25 10:14:37 +0200 | [diff] [blame] | 750 | </div> |
| 751 | `; |
| 752 | } |
| 753 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 754 | private renderAuthor() { |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 755 | if (isDraft(this.comment)) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 756 | if (isRobot(this.comment)) { |
| 757 | const id = this.comment.robot_id; |
| 758 | return html`<span class="robotName">${id}</span>`; |
| 759 | } |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 760 | return html` |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 761 | <gr-account-label .account=${this.comment?.author ?? this.account}> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 762 | </gr-account-label> |
| 763 | `; |
| 764 | } |
| 765 | |
| 766 | private renderPortedCommentMessage() { |
| 767 | if (!this.showPortedComment) return; |
| 768 | if (!this.comment?.patch_set) return; |
| 769 | return html` |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 770 | <a href=${this.getUrlForComment()}> |
| 771 | <span class="portedMessage" @click=${this.handlePortedMessageClick}> |
Ben Rohlfs | 9579622 | 2021-12-01 16:39:42 +0100 | [diff] [blame] | 772 | From patchset ${this.comment?.patch_set} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 773 | </span> |
| 774 | </a> |
| 775 | `; |
| 776 | } |
| 777 | |
| 778 | private renderDraftLabel() { |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 779 | if (!isDraft(this.comment)) return; |
Ben Rohlfs | ba361a4 | 2022-09-01 12:12:45 +0200 | [diff] [blame] | 780 | let label = 'Draft'; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 781 | let tooltip = |
| 782 | 'This draft is only visible to you. ' + |
| 783 | "To publish drafts, click the 'Reply' or 'Start review' button " + |
| 784 | "at the top of the change or press the 'a' key."; |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 785 | if (isError(this.comment)) { |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 786 | label += ' (Failed to save)'; |
| 787 | tooltip = 'Unable to save draft. Please try to save again.'; |
| 788 | } |
| 789 | return html` |
| 790 | <gr-tooltip-content |
| 791 | class="draftTooltip" |
| 792 | has-tooltip |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 793 | title=${tooltip} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 794 | max-width="20em" |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 795 | > |
Ben Rohlfs | ba361a4 | 2022-09-01 12:12:45 +0200 | [diff] [blame] | 796 | <gr-icon filled icon="rate_review"></gr-icon> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 797 | <span class="draftLabel">${label}</span> |
| 798 | </gr-tooltip-content> |
| 799 | `; |
| 800 | } |
| 801 | |
| 802 | private renderCollapsedContent() { |
| 803 | if (!this.collapsed) return; |
| 804 | return html` |
| 805 | <span class="collapsedContent">${this.comment?.message}</span> |
| 806 | `; |
| 807 | } |
| 808 | |
| 809 | private renderRunDetails() { |
| 810 | if (!isRobot(this.comment)) return; |
| 811 | if (!this.comment?.url || this.collapsed) return; |
| 812 | return html` |
| 813 | <div class="runIdMessage message"> |
| 814 | <div class="runIdInformation"> |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 815 | <a class="robotRunLink" href=${this.comment.url}> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 816 | <span class="robotRun link">Run Details</span> |
| 817 | </a> |
| 818 | </div> |
| 819 | </div> |
| 820 | `; |
| 821 | } |
| 822 | |
| 823 | /** |
| 824 | * Deleting a comment is an admin feature. It means more than just discarding |
| 825 | * a draft. It is an action applied to published comments. |
| 826 | */ |
| 827 | private renderDeleteButton() { |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 828 | if (!this.isAdmin || isDraft(this.comment) || isRobot(this.comment)) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 829 | if (this.collapsed) return; |
| 830 | return html` |
| 831 | <gr-button |
| 832 | id="deleteBtn" |
| 833 | title="Delete Comment" |
| 834 | link |
| 835 | class="action delete" |
Kamil Musin | 462428b | 2022-12-29 11:12:08 +0100 | [diff] [blame] | 836 | @click=${(e: MouseEvent) => { |
| 837 | e.stopPropagation(); |
| 838 | this.openDeleteCommentModal(); |
| 839 | }} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 840 | > |
Chris Poucet | 1c71386 | 2022-07-25 13:12:24 +0200 | [diff] [blame] | 841 | <gr-icon id="icon" icon="delete" filled></gr-icon> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 842 | </gr-button> |
| 843 | `; |
| 844 | } |
| 845 | |
| 846 | private renderPatchset() { |
| 847 | if (!this.showPatchset) return; |
| 848 | assertIsDefined(this.comment?.patch_set, 'comment.patch_set'); |
| 849 | return html` |
| 850 | <span class="patchset-text"> Patchset ${this.comment.patch_set}</span> |
| 851 | `; |
| 852 | } |
| 853 | |
Ben Rohlfs | 0ed3359 | 2023-04-12 11:33:27 +0200 | [diff] [blame] | 854 | private renderSeparator() { |
| 855 | // This should match the condition of `renderPatchset()`. |
| 856 | if (!this.showPatchset) return; |
| 857 | // This should match the condition of `renderDate()`. |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 858 | if (this.collapsed) return; |
Ben Rohlfs | 0ed3359 | 2023-04-12 11:33:27 +0200 | [diff] [blame] | 859 | // Render separator, if both are present: patchset AND date. |
| 860 | return html`<span class="separator"></span>`; |
| 861 | } |
| 862 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 863 | private renderDate() { |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 864 | if (this.collapsed) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 865 | return html` |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 866 | <span class="date" tabindex="0" @click=${this.handleAnchorClick}> |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 867 | ${this.renderDateInner()} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 868 | </span> |
| 869 | `; |
| 870 | } |
| 871 | |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 872 | private renderDateInner() { |
| 873 | if (isError(this.comment)) return 'Error'; |
Ben Rohlfs | d98a04c | 2023-05-04 11:31:15 +0200 | [diff] [blame] | 874 | if (isSaving(this.comment) && !this.autoSaving) return 'Saving'; |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 875 | if (isNew(this.comment)) return 'New'; |
| 876 | return html` |
| 877 | <gr-date-formatter |
| 878 | withTooltip |
| 879 | .dateStr=${this.comment!.updated} |
| 880 | ></gr-date-formatter> |
| 881 | `; |
| 882 | } |
| 883 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 884 | private renderToggle() { |
Chris Poucet | b8c0639 | 2022-07-08 16:35:43 +0200 | [diff] [blame] | 885 | const icon = this.collapsed ? 'expand_more' : 'expand_less'; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 886 | const ariaLabel = this.collapsed ? 'Expand' : 'Collapse'; |
| 887 | return html` |
| 888 | <div class="show-hide" tabindex="0"> |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 889 | <label class="show-hide" aria-label=${ariaLabel}> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 890 | <input |
| 891 | type="checkbox" |
| 892 | class="show-hide" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 893 | ?checked=${this.collapsed} |
| 894 | @change=${() => (this.collapsed = !this.collapsed)} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 895 | /> |
Chris Poucet | 1c71386 | 2022-07-25 13:12:24 +0200 | [diff] [blame] | 896 | <gr-icon icon=${icon} id="icon"></gr-icon> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 897 | </label> |
| 898 | </div> |
| 899 | `; |
| 900 | } |
| 901 | |
| 902 | private renderRobotAuthor() { |
| 903 | if (!isRobot(this.comment) || this.collapsed) return; |
| 904 | return html`<div class="robotId">${this.comment.author?.name}</div>`; |
| 905 | } |
| 906 | |
| 907 | private renderEditingTextarea() { |
| 908 | if (!this.editing || this.collapsed) return; |
| 909 | return html` |
Ben Rohlfs | 6256117 | 2024-04-18 09:20:38 +0200 | [diff] [blame] | 910 | <gr-suggestion-textarea |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 911 | id="editTextarea" |
| 912 | class="editMessage" |
| 913 | autocomplete="on" |
| 914 | code="" |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 915 | rows="4" |
Dhruv Srivastava | f007abc | 2022-09-06 11:18:57 +0200 | [diff] [blame] | 916 | .placeholder=${this.messagePlaceholder} |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 917 | text=${this.messageText} |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 918 | autocompleteHint=${this.autocompleteHint?.commentCompletion ?? ''} |
Ben Rohlfs | 93e11b7 | 2024-04-30 17:33:51 +0200 | [diff] [blame] | 919 | @text-changed=${this.handleTextChanged} |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 920 | @hintShown=${this.handleHintShown} |
| 921 | @hintApplied=${this.handleHintApplied} |
Ben Rohlfs | 6256117 | 2024-04-18 09:20:38 +0200 | [diff] [blame] | 922 | ></gr-suggestion-textarea> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 923 | `; |
| 924 | } |
| 925 | |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 926 | private handleHintShown(e: CustomEvent<HintShownEventDetail>) { |
| 927 | const context = this.autocompleteCache.get(e.detail.oldValue); |
| 928 | if (context?.commentCompletion !== e.detail.hint) return; |
| 929 | |
| 930 | this.reportHintInteraction( |
| 931 | Interaction.COMMENT_COMPLETION_SUGGESTION_SHOWN, |
| 932 | context |
| 933 | ); |
| 934 | } |
| 935 | |
| 936 | private handleHintApplied(e: CustomEvent<HintAppliedEventDetail>) { |
| 937 | const context = this.autocompleteCache.get(e.detail.oldValue); |
| 938 | if (context?.commentCompletion !== e.detail.hint) return; |
| 939 | |
| 940 | this.autocompleteAcceptedHints.push(e.detail.hint); |
| 941 | this.reportHintInteraction( |
| 942 | Interaction.COMMENT_COMPLETION_SUGGESTION_ACCEPTED, |
| 943 | context |
| 944 | ); |
| 945 | } |
| 946 | |
| 947 | private reportHintInteractionSaved() { |
| 948 | const content = this.messageText.trimEnd(); |
| 949 | const acceptedHintsConcatenated = this.autocompleteAcceptedHints.join(''); |
| 950 | const numExtraCharacters = |
| 951 | content.length - acceptedHintsConcatenated.length; |
| 952 | let distance = levenshteinDistance(acceptedHintsConcatenated, content); |
| 953 | if (numExtraCharacters > 0) { |
| 954 | distance -= numExtraCharacters; |
| 955 | } |
| 956 | const context = { |
| 957 | ...this.createAutocompletionBaseContext(), |
| 958 | similarCharacters: acceptedHintsConcatenated.length - distance, |
| 959 | maxSimilarCharacters: acceptedHintsConcatenated.length, |
| 960 | acceptedSuggestionsCount: this.autocompleteAcceptedHints.length, |
| 961 | totalAcceptedCharacters: acceptedHintsConcatenated.length, |
| 962 | savedDraftLength: content.length, |
| 963 | }; |
| 964 | this.reportHintInteraction( |
| 965 | Interaction.COMMENT_COMPLETION_SAVE_DRAFT, |
| 966 | context |
| 967 | ); |
| 968 | } |
| 969 | |
| 970 | private reportHintInteraction( |
| 971 | interaction: Interaction, |
| 972 | context: Partial<AutocompletionContext> |
| 973 | ) { |
| 974 | context = { |
| 975 | ...context, |
| 976 | draftContent: '[REDACTED]', |
| 977 | commentCompletion: '[REDACTED]', |
| 978 | }; |
| 979 | this.reporting.reportInteraction(interaction, context); |
| 980 | } |
| 981 | |
Ben Rohlfs | 93e11b7 | 2024-04-30 17:33:51 +0200 | [diff] [blame] | 982 | private handleTextChanged(e: ValueChangedEvent) { |
| 983 | const oldValue = this.messageText; |
| 984 | const newValue = e.detail.value; |
| 985 | if (oldValue === newValue) return; |
| 986 | // TODO: This is causing a re-render of <gr-comment> on every key |
| 987 | // press. Try to avoid always setting `this.messageText` or at least |
| 988 | // debounce it. Most of the code can just inspect the current value |
| 989 | // of the textare instead of needing a dedicated property. |
| 990 | this.messageText = newValue; |
| 991 | |
Ben Rohlfs | 4deb8df5 | 2024-05-10 12:53:19 +0200 | [diff] [blame] | 992 | this.handleTextChangedForAutocomplete(); |
Ben Rohlfs | 93e11b7 | 2024-04-30 17:33:51 +0200 | [diff] [blame] | 993 | this.autoSaveTrigger$.next(); |
| 994 | this.generateSuggestionTrigger$.next(); |
| 995 | } |
| 996 | |
| 997 | // visible for testing |
Ben Rohlfs | 4deb8df5 | 2024-05-10 12:53:19 +0200 | [diff] [blame] | 998 | handleTextChangedForAutocomplete() { |
| 999 | const cachedHint = this.autocompleteCache.get(this.messageText); |
| 1000 | if (cachedHint) { |
| 1001 | this.autocompleteHint = cachedHint; |
| 1002 | } else { |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 1003 | this.autocompleteHint = undefined; |
Ben Rohlfs | 4deb8df5 | 2024-05-10 12:53:19 +0200 | [diff] [blame] | 1004 | this.autocompleteTrigger$.next(); |
Ben Rohlfs | 93e11b7 | 2024-04-30 17:33:51 +0200 | [diff] [blame] | 1005 | } |
Ben Rohlfs | 93e11b7 | 2024-04-30 17:33:51 +0200 | [diff] [blame] | 1006 | } |
| 1007 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1008 | private renderCommentMessage() { |
| 1009 | if (this.collapsed || this.editing) return; |
Frank Borden | f9a2999 | 2022-08-24 20:19:23 +0200 | [diff] [blame] | 1010 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1011 | return html` |
| 1012 | <!--The "message" class is needed to ensure selectability from |
| 1013 | gr-diff-selection.--> |
| 1014 | <gr-formatted-text |
| 1015 | class="message" |
Frank Borden | abdd187 | 2022-09-26 12:55:59 +0200 | [diff] [blame] | 1016 | .markdown=${true} |
| 1017 | .content=${this.comment?.message ?? ''} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1018 | ></gr-formatted-text> |
| 1019 | `; |
| 1020 | } |
| 1021 | |
| 1022 | private renderCopyLinkIcon() { |
| 1023 | // Only show the icon when the thread contains a published comment. |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1024 | if (!this.comment?.in_reply_to && isDraft(this.comment)) return; |
Ben Rohlfs | 38703a4 | 2024-04-30 16:40:02 +0200 | [diff] [blame] | 1025 | if (this.editing) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1026 | return html` |
Chris Poucet | 1c71386 | 2022-07-25 13:12:24 +0200 | [diff] [blame] | 1027 | <gr-icon |
| 1028 | icon="link" |
| 1029 | class="copy link-icon" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 1030 | @click=${this.handleCopyLink} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1031 | title="Copy link to this comment" |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1032 | role="button" |
| 1033 | tabindex="0" |
Chris Poucet | 1c71386 | 2022-07-25 13:12:24 +0200 | [diff] [blame] | 1034 | ></gr-icon> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1035 | `; |
| 1036 | } |
| 1037 | |
| 1038 | private renderHumanActions() { |
| 1039 | if (!this.account || isRobot(this.comment)) return; |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1040 | if (this.collapsed || !isDraft(this.comment)) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1041 | return html` |
| 1042 | <div class="actions"> |
Milutin Kristofic | b7929e7 | 2023-09-05 13:13:07 +0200 | [diff] [blame] | 1043 | <div class="leftActions"> |
| 1044 | <div class="action resolve"> |
| 1045 | <label> |
| 1046 | <input |
| 1047 | type="checkbox" |
| 1048 | id="resolvedCheckbox" |
Frank Borden | e00592af | 2024-02-19 12:12:29 +0100 | [diff] [blame] | 1049 | .checked=${!this.unresolved} |
Milutin Kristofic | b7929e7 | 2023-09-05 13:13:07 +0200 | [diff] [blame] | 1050 | @change=${this.handleToggleResolved} |
| 1051 | /> |
| 1052 | Resolved |
| 1053 | </label> |
| 1054 | </div> |
| 1055 | ${this.renderGenerateSuggestEditButton()} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1056 | </div> |
| 1057 | ${this.renderDraftActions()} |
| 1058 | </div> |
| 1059 | `; |
| 1060 | } |
| 1061 | |
| 1062 | private renderDraftActions() { |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1063 | if (!isDraft(this.comment)) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1064 | return html` |
| 1065 | <div class="rightActions"> |
Milutin Kristofic | 1ebae37 | 2022-11-22 20:35:38 +0100 | [diff] [blame] | 1066 | ${this.renderDiscardButton()} ${this.renderEditButton()} |
Milutin Kristofic | b7929e7 | 2023-09-05 13:13:07 +0200 | [diff] [blame] | 1067 | ${this.renderCancelButton()} ${this.renderSaveButton()} |
| 1068 | ${this.renderCopyLinkIcon()} |
Ben Rohlfs | 38703a4 | 2024-04-30 16:40:02 +0200 | [diff] [blame] | 1069 | <gr-endpoint-slot name="draft-actions-end"></gr-endpoint-slot> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1070 | </div> |
| 1071 | `; |
| 1072 | } |
| 1073 | |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 1074 | private renderSuggestEditButton() { |
Dhruv Srivastava | 66a1563 | 2022-09-06 11:57:34 +0200 | [diff] [blame] | 1075 | if ( |
Milutin Kristofic | 8238de5 | 2023-01-12 19:33:45 +0100 | [diff] [blame] | 1076 | !this.editing || |
Dhruv Srivastava | 66a1563 | 2022-09-06 11:57:34 +0200 | [diff] [blame] | 1077 | this.permanentEditingMode || |
| 1078 | this.comment?.path === SpecialFilePath.PATCHSET_LEVEL_COMMENTS |
| 1079 | ) { |
| 1080 | return nothing; |
| 1081 | } |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 1082 | assertIsDefined(this.comment, 'comment'); |
| 1083 | if (hasUserSuggestion(this.comment)) return nothing; |
| 1084 | // TODO(milutin): remove this check once suggesting on commit message is |
| 1085 | // fixed. Currently diff line doesn't match commit message line, because |
| 1086 | // of metadata in diff, which aren't in content api request. |
| 1087 | if (this.comment.path === SpecialFilePath.COMMIT_MESSAGE) return nothing; |
Milutin Kristofic | 50394b1 | 2023-02-01 11:28:52 +0000 | [diff] [blame] | 1088 | if (this.isOwner) return nothing; |
Milutin Kristofic | 7dec89b | 2022-09-13 12:11:35 +0200 | [diff] [blame] | 1089 | return html`<gr-button |
| 1090 | link |
| 1091 | class="action suggestEdit" |
Milutin Kristofic | 80c3c7e | 2023-03-16 20:22:28 +0100 | [diff] [blame] | 1092 | title="This button copies the text to make a suggestion" |
Milutin Kristofic | 7dec89b | 2022-09-13 12:11:35 +0200 | [diff] [blame] | 1093 | @click=${this.createSuggestEdit} |
Milutin Kristofic | 80c3c7e | 2023-03-16 20:22:28 +0100 | [diff] [blame] | 1094 | ><gr-icon icon="edit" id="icon" filled></gr-icon> Suggest edit</gr-button |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 1095 | >`; |
| 1096 | } |
| 1097 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1098 | private renderDiscardButton() { |
Dhruv Srivastava | 705eac9 | 2022-09-01 11:33:27 +0200 | [diff] [blame] | 1099 | if (this.editing || this.permanentEditingMode) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1100 | return html`<gr-button |
| 1101 | link |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1102 | ?disabled=${isSaving(this.comment) && !this.autoSaving} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1103 | class="action discard" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 1104 | @click=${this.discard} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1105 | >Discard</gr-button |
| 1106 | >`; |
| 1107 | } |
| 1108 | |
| 1109 | private renderEditButton() { |
| 1110 | if (this.editing) return; |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 1111 | return html`<gr-button link class="action edit" @click=${this.edit} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1112 | >Edit</gr-button |
| 1113 | >`; |
| 1114 | } |
| 1115 | |
| 1116 | private renderCancelButton() { |
Dhruv Srivastava | 705eac9 | 2022-09-01 11:33:27 +0200 | [diff] [blame] | 1117 | if (!this.editing || this.permanentEditingMode) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1118 | return html` |
| 1119 | <gr-button |
| 1120 | link |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1121 | ?disabled=${isSaving(this.comment) && !this.autoSaving} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1122 | class="action cancel" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 1123 | @click=${this.cancel} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1124 | >Cancel</gr-button |
| 1125 | > |
| 1126 | `; |
| 1127 | } |
| 1128 | |
| 1129 | private renderSaveButton() { |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1130 | if (!this.editing) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1131 | return html` |
| 1132 | <gr-button |
| 1133 | link |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 1134 | ?disabled=${this.isSaveDisabled()} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1135 | class="action save" |
Dhruv Srivastava | 705eac9 | 2022-09-01 11:33:27 +0200 | [diff] [blame] | 1136 | @click=${this.handleSaveButtonClicked} |
Dhruv Srivastava | 00831e7 | 2022-09-05 08:20:20 +0200 | [diff] [blame] | 1137 | >${this.permanentEditingMode ? 'Preview' : 'Save'}</gr-button |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1138 | > |
| 1139 | `; |
| 1140 | } |
| 1141 | |
Milutin Kristofic | bf61a4e | 2024-02-12 20:50:16 +0100 | [diff] [blame] | 1142 | private renderFixSuggestionPreview() { |
Milutin Kristofic | e28cde5 | 2024-03-20 13:07:29 +0100 | [diff] [blame] | 1143 | if ( |
| 1144 | !this.comment?.fix_suggestions || |
| 1145 | this.editing || |
| 1146 | isRobot(this.comment) || |
| 1147 | this.collapsed |
| 1148 | ) |
Milutin Kristofic | 03ec98d | 2024-02-20 19:56:39 +0100 | [diff] [blame] | 1149 | return nothing; |
| 1150 | return html`<gr-fix-suggestions |
| 1151 | .comment=${this.comment} |
| 1152 | ></gr-fix-suggestions>`; |
Milutin Kristofic | bf61a4e | 2024-02-12 20:50:16 +0100 | [diff] [blame] | 1153 | } |
| 1154 | |
Milutin Kristofic | d9e9975 | 2024-02-20 14:35:25 +0100 | [diff] [blame] | 1155 | // private but used in test |
| 1156 | showGeneratedSuggestion() { |
Milutin Kristofic | 490fa95 | 2023-09-12 20:17:36 +0200 | [diff] [blame] | 1157 | return ( |
Milutin Kristofic | 1cfc021 | 2024-02-01 20:16:06 +0100 | [diff] [blame] | 1158 | (this.flagsService.isEnabled(KnownExperimentId.ML_SUGGESTED_EDIT) || |
| 1159 | this.flagsService.isEnabled(KnownExperimentId.ML_SUGGESTED_EDIT_V2)) && |
Milutin Kristofic | a31d894 | 2023-11-17 13:32:16 +0100 | [diff] [blame] | 1160 | this.suggestionsProvider && |
Milutin Kristofic | 490fa95 | 2023-09-12 20:17:36 +0200 | [diff] [blame] | 1161 | this.editing && |
| 1162 | !this.permanentEditingMode && |
Milutin Kristofic | 490fa95 | 2023-09-12 20:17:36 +0200 | [diff] [blame] | 1163 | this.comment && |
Milutin Kristofic | a31d894 | 2023-11-17 13:32:16 +0100 | [diff] [blame] | 1164 | this.comment.path && |
Milutin Kristofic | 8afa8e4 | 2023-09-22 20:33:07 +0200 | [diff] [blame] | 1165 | this.comment.path !== SpecialFilePath.PATCHSET_LEVEL_COMMENTS && |
| 1166 | this.comment.path !== SpecialFilePath.COMMIT_MESSAGE && |
Milutin Kristofic | a31d894 | 2023-11-17 13:32:16 +0100 | [diff] [blame] | 1167 | (!this.suggestionsProvider.supportedFileExtensions || |
| 1168 | this.suggestionsProvider.supportedFileExtensions.includes( |
| 1169 | getFileExtension(this.comment.path) |
| 1170 | )) && |
Milutin Kristofic | 8afa8e4 | 2023-09-22 20:33:07 +0200 | [diff] [blame] | 1171 | this.comment === this.comments?.[0] && // Is first comment |
| 1172 | (this.comment.range || this.comment.line) && // Disabled for File comments |
Milutin Kristofic | 35c48c3 | 2023-12-11 19:52:41 +0100 | [diff] [blame] | 1173 | !hasUserSuggestion(this.comment) && |
| 1174 | this.getChangeModel().getChange()?.is_private !== true |
Milutin Kristofic | 490fa95 | 2023-09-12 20:17:36 +0200 | [diff] [blame] | 1175 | ); |
| 1176 | } |
| 1177 | |
Milutin Kristofic | 8f07c38 | 2023-10-02 13:13:21 +0200 | [diff] [blame] | 1178 | private renderGeneratedSuggestionPreview() { |
Milutin Kristofic | 03ec98d | 2024-02-20 19:56:39 +0100 | [diff] [blame] | 1179 | if ( |
| 1180 | !this.editing || |
| 1181 | !this.showGeneratedSuggestion() || |
| 1182 | !this.generateSuggestion |
| 1183 | ) |
Milutin Kristofic | 8f07c38 | 2023-10-02 13:13:21 +0200 | [diff] [blame] | 1184 | return nothing; |
Milutin Kristofic | d4274a1 | 2024-02-19 20:30:02 +0100 | [diff] [blame] | 1185 | if (!isDraft(this.comment)) return nothing; |
Milutin Kristofic | 97683a8 | 2023-11-20 20:06:22 +0100 | [diff] [blame] | 1186 | |
Milutin Kristofic | 1cfc021 | 2024-02-01 20:16:06 +0100 | [diff] [blame] | 1187 | if (this.generatedFixSuggestion) { |
| 1188 | return html`<gr-suggestion-diff-preview |
Milutin Kristofic | 847c46d | 2024-04-15 13:43:06 +0200 | [diff] [blame] | 1189 | id="suggestionDiffPreview" |
Milutin Kristofic | 03ec98d | 2024-02-20 19:56:39 +0100 | [diff] [blame] | 1190 | .fixSuggestionInfo=${this.generatedFixSuggestion} |
Milutin Kristofic | 1cfc021 | 2024-02-01 20:16:06 +0100 | [diff] [blame] | 1191 | ></gr-suggestion-diff-preview>`; |
| 1192 | } else if (this.generatedSuggestion) { |
| 1193 | return html`<gr-suggestion-diff-preview |
| 1194 | .showAddSuggestionButton=${true} |
| 1195 | .suggestion=${this.generatedSuggestion?.replacement} |
| 1196 | .uuid=${this.generatedSuggestionId} |
| 1197 | ></gr-suggestion-diff-preview>`; |
| 1198 | } else { |
| 1199 | return nothing; |
| 1200 | } |
Milutin Kristofic | 8f07c38 | 2023-10-02 13:13:21 +0200 | [diff] [blame] | 1201 | } |
| 1202 | |
Milutin Kristofic | 734df55 | 2023-08-07 10:38:50 +0200 | [diff] [blame] | 1203 | private renderGenerateSuggestEditButton() { |
Milutin Kristofic | 490fa95 | 2023-09-12 20:17:36 +0200 | [diff] [blame] | 1204 | if (!this.showGeneratedSuggestion()) { |
Milutin Kristofic | b7929e7 | 2023-09-05 13:13:07 +0200 | [diff] [blame] | 1205 | return nothing; |
| 1206 | } |
Milutin Kristofic | dca27ed | 2023-11-21 21:46:51 +0100 | [diff] [blame] | 1207 | const tooltip = |
| 1208 | 'Select to show a generated suggestion based on your comment for commented text. This suggestion can be inserted as a code block in your comment.'; |
Milutin Kristofic | 734df55 | 2023-08-07 10:38:50 +0200 | [diff] [blame] | 1209 | return html` |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 1210 | <div class="action"> |
Milutin Kristofic | dca27ed | 2023-11-21 21:46:51 +0100 | [diff] [blame] | 1211 | <label title=${tooltip}> |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 1212 | <input |
| 1213 | type="checkbox" |
| 1214 | id="generateSuggestCheckbox" |
| 1215 | ?checked=${this.generateSuggestion} |
| 1216 | @change=${() => { |
| 1217 | this.generateSuggestion = !this.generateSuggestion; |
Milutin Kristofic | 409e4a0 | 2024-03-12 13:32:36 +0100 | [diff] [blame] | 1218 | if (this.comment?.id) { |
| 1219 | this.getStorage().setEditableContentItem( |
| 1220 | ENABLE_GENERATE_SUGGESTION_STORAGE_KEY + this.comment.id, |
| 1221 | this.generateSuggestion.toString() |
| 1222 | ); |
| 1223 | } |
Milutin Kristofic | 4d96373 | 2024-01-04 12:10:03 +0100 | [diff] [blame] | 1224 | if (this.generateSuggestion) { |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 1225 | this.generateSuggestionTrigger$.next(); |
Milutin Kristofic | d4274a1 | 2024-02-19 20:30:02 +0100 | [diff] [blame] | 1226 | } else { |
| 1227 | if ( |
| 1228 | this.flagsService.isEnabled( |
| 1229 | KnownExperimentId.ML_SUGGESTED_EDIT_V2 |
| 1230 | ) |
| 1231 | ) { |
| 1232 | this.generatedFixSuggestion = undefined; |
| 1233 | this.autoSaveTrigger$.next(); |
| 1234 | } |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 1235 | } |
Milutin Kristofic | 96ac52e | 2023-09-18 10:28:58 +0200 | [diff] [blame] | 1236 | this.reporting.reportInteraction( |
| 1237 | this.generateSuggestion |
| 1238 | ? Interaction.GENERATE_SUGGESTION_ENABLED |
| 1239 | : Interaction.GENERATE_SUGGESTION_DISABLED |
| 1240 | ); |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 1241 | }} |
| 1242 | /> |
Milutin Kristofic | e28cde5 | 2024-03-20 13:07:29 +0100 | [diff] [blame] | 1243 | ${this.flagsService.isEnabled(KnownExperimentId.ML_SUGGESTED_EDIT_V2) |
Milutin Kristofic | be55323 | 2024-04-16 14:38:21 +0200 | [diff] [blame] | 1244 | ? 'Attach AI-suggested fix' |
Milutin Kristofic | e28cde5 | 2024-03-20 13:07:29 +0100 | [diff] [blame] | 1245 | : 'Generate Suggestion'} |
Milutin Kristofic | 74caea4b | 2023-11-14 20:10:56 +0100 | [diff] [blame] | 1246 | ${when( |
| 1247 | this.suggestionLoading, |
| 1248 | () => html`<span class="loadingSpin"></span>`, |
Milutin Kristofic | 97683a8 | 2023-11-20 20:06:22 +0100 | [diff] [blame] | 1249 | () => html`${this.getNumberOfSuggestions()}` |
Milutin Kristofic | 74caea4b | 2023-11-14 20:10:56 +0100 | [diff] [blame] | 1250 | )} |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 1251 | </label> |
Milutin Kristofic | 5942c9d | 2023-11-15 20:34:42 +0100 | [diff] [blame] | 1252 | <a |
Milutin Kristofic | e28cde5 | 2024-03-20 13:07:29 +0100 | [diff] [blame] | 1253 | href=${this.suggestionsProvider?.getDocumentationLink?.() || |
| 1254 | getDocUrl( |
Milutin Kristofic | 5942c9d | 2023-11-15 20:34:42 +0100 | [diff] [blame] | 1255 | this.docsBaseUrl, |
Milutin Kristofic | e28cde5 | 2024-03-20 13:07:29 +0100 | [diff] [blame] | 1256 | 'user-suggest-edits.html$_generate_suggestion' |
Milutin Kristofic | 5942c9d | 2023-11-15 20:34:42 +0100 | [diff] [blame] | 1257 | )} |
| 1258 | target="_blank" |
| 1259 | rel="noopener noreferrer" |
| 1260 | > |
| 1261 | <gr-icon |
| 1262 | icon="help" |
| 1263 | title="About Generated Suggested Edits" |
| 1264 | ></gr-icon> |
| 1265 | </a> |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 1266 | </div> |
Milutin Kristofic | 734df55 | 2023-08-07 10:38:50 +0200 | [diff] [blame] | 1267 | `; |
| 1268 | } |
| 1269 | |
Milutin Kristofic | 97683a8 | 2023-11-20 20:06:22 +0100 | [diff] [blame] | 1270 | private getNumberOfSuggestions() { |
Chris Poucet | 03bc8a3 | 2024-02-01 18:33:17 +0100 | [diff] [blame] | 1271 | if (!this.generateSuggestion) { |
| 1272 | return ''; |
| 1273 | } |
Milutin Kristofic | c3dfc02 | 2024-02-19 19:41:02 +0100 | [diff] [blame] | 1274 | if (this.generatedSuggestion || this.generatedFixSuggestion) { |
Milutin Kristofic | 97683a8 | 2023-11-20 20:06:22 +0100 | [diff] [blame] | 1275 | return '(1)'; |
| 1276 | } else { |
| 1277 | return '(0)'; |
| 1278 | } |
| 1279 | } |
| 1280 | |
Milutin Kristofic | 490fa95 | 2023-09-12 20:17:36 +0200 | [diff] [blame] | 1281 | private handleAddGeneratedSuggestion(code: string) { |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 1282 | const addNewLine = this.messageText.length !== 0; |
Milutin Kristofic | c0582fc | 2024-04-02 20:30:51 +0200 | [diff] [blame] | 1283 | this.addedGeneratedSuggestion = `${ |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 1284 | addNewLine ? '\n' : '' |
Milutin Kristofic | 490fa95 | 2023-09-12 20:17:36 +0200 | [diff] [blame] | 1285 | }${USER_SUGGESTION_START_PATTERN}${code}${'\n```'}`; |
Milutin Kristofic | c0582fc | 2024-04-02 20:30:51 +0200 | [diff] [blame] | 1286 | this.messageText += this.addedGeneratedSuggestion; |
Milutin Kristofic | fc5ccca | 2023-09-11 13:43:45 +0200 | [diff] [blame] | 1287 | } |
| 1288 | |
Milutin Kristofic | 1cfc021 | 2024-02-01 20:16:06 +0100 | [diff] [blame] | 1289 | private generateSuggestEdit() { |
| 1290 | if (this.flagsService.isEnabled(KnownExperimentId.ML_SUGGESTED_EDIT_V2)) { |
| 1291 | this.generateSuggestEdit_v2(); |
| 1292 | } else if ( |
| 1293 | this.flagsService.isEnabled(KnownExperimentId.ML_SUGGESTED_EDIT) |
| 1294 | ) { |
| 1295 | this.generateSuggestEdit_v1(); |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | private async generateSuggestEdit_v1() { |
Milutin Kristofic | a31d894 | 2023-11-17 13:32:16 +0100 | [diff] [blame] | 1300 | const suggestionsProvider = this.suggestionsProvider; |
Kamil Musin | 48ff12a | 2023-12-05 14:25:27 +0100 | [diff] [blame] | 1301 | const changeInfo = this.getChangeModel().getChange(); |
Milutin Kristofic | 8afa8e4 | 2023-09-22 20:33:07 +0200 | [diff] [blame] | 1302 | if ( |
Milutin Kristofic | 604ac71 | 2024-01-25 13:46:18 +0100 | [diff] [blame] | 1303 | !suggestionsProvider?.suggestCode || |
Milutin Kristofic | 8afa8e4 | 2023-09-22 20:33:07 +0200 | [diff] [blame] | 1304 | !this.showGeneratedSuggestion() || |
Chris Poucet | 4ca9f2f | 2024-02-01 17:28:50 +0100 | [diff] [blame] | 1305 | !this.generateSuggestion || |
Kamil Musin | 48ff12a | 2023-12-05 14:25:27 +0100 | [diff] [blame] | 1306 | !changeInfo || |
Milutin Kristofic | 8afa8e4 | 2023-09-22 20:33:07 +0200 | [diff] [blame] | 1307 | !this.comment || |
| 1308 | !this.comment.patch_set || |
| 1309 | !this.comment.path || |
| 1310 | this.messageText.length === 0 |
| 1311 | ) |
Milutin Kristofic | 734df55 | 2023-08-07 10:38:50 +0200 | [diff] [blame] | 1312 | return; |
Milutin Kristofic | 06dfda4 | 2023-11-15 09:51:13 +0100 | [diff] [blame] | 1313 | this.generatedSuggestionId = uuid(); |
Milutin Kristofic | 96ac52e | 2023-09-18 10:28:58 +0200 | [diff] [blame] | 1314 | this.reporting.reportInteraction(Interaction.GENERATE_SUGGESTION_REQUEST, { |
Milutin Kristofic | 06dfda4 | 2023-11-15 09:51:13 +0100 | [diff] [blame] | 1315 | uuid: this.generatedSuggestionId, |
Milutin Kristofic | aa120cc | 2024-02-01 13:14:43 +0100 | [diff] [blame] | 1316 | type: 'suggest-code', |
| 1317 | commentId: this.comment.id, |
Milutin Kristofic | 96ac52e | 2023-09-18 10:28:58 +0200 | [diff] [blame] | 1318 | }); |
Milutin Kristofic | 74caea4b | 2023-11-14 20:10:56 +0100 | [diff] [blame] | 1319 | this.suggestionLoading = true; |
Milutin Kristofic | d682dc7 | 2023-12-13 09:35:47 +0100 | [diff] [blame] | 1320 | let suggestionResponse; |
| 1321 | try { |
| 1322 | suggestionResponse = await suggestionsProvider.suggestCode({ |
| 1323 | prompt: this.messageText, |
| 1324 | changeInfo: changeInfo as ChangeInfo, |
| 1325 | patchsetNumber: this.comment?.patch_set, |
| 1326 | filePath: this.comment.path, |
| 1327 | range: this.comment.range, |
| 1328 | lineNumber: this.comment.line, |
| 1329 | }); |
| 1330 | } finally { |
| 1331 | this.suggestionLoading = false; |
| 1332 | } |
| 1333 | |
| 1334 | if (!suggestionResponse) return; |
Milutin Kristofic | 8f07c38 | 2023-10-02 13:13:21 +0200 | [diff] [blame] | 1335 | // TODO(milutin): The suggestionResponse can contain multiple suggestion |
| 1336 | // options. We pick the first one for now. In future we shouldn't ignore |
| 1337 | // other suggestions. |
Milutin Kristofic | 96ac52e | 2023-09-18 10:28:58 +0200 | [diff] [blame] | 1338 | this.reporting.reportInteraction(Interaction.GENERATE_SUGGESTION_RESPONSE, { |
Milutin Kristofic | 06dfda4 | 2023-11-15 09:51:13 +0100 | [diff] [blame] | 1339 | uuid: this.generatedSuggestionId, |
Milutin Kristofic | 1cfc021 | 2024-02-01 20:16:06 +0100 | [diff] [blame] | 1340 | type: 'suggest-code', |
Milutin Kristofic | fa15825 | 2024-04-24 11:58:32 +0200 | [diff] [blame] | 1341 | commentId: this.comment.id, |
Milutin Kristofic | 8f07c38 | 2023-10-02 13:13:21 +0200 | [diff] [blame] | 1342 | response: suggestionResponse.responseCode, |
| 1343 | numSuggestions: suggestionResponse.suggestions.length, |
| 1344 | hasNewRange: suggestionResponse.suggestions?.[0]?.newRange !== undefined, |
Milutin Kristofic | 96ac52e | 2023-09-18 10:28:58 +0200 | [diff] [blame] | 1345 | }); |
Milutin Kristofic | 8f07c38 | 2023-10-02 13:13:21 +0200 | [diff] [blame] | 1346 | const suggestion = suggestionResponse.suggestions?.[0]; |
Milutin Kristofic | ead5c55b | 2024-03-05 18:13:52 +0100 | [diff] [blame] | 1347 | if (!suggestion?.replacement) return; |
Milutin Kristofic | 8f07c38 | 2023-10-02 13:13:21 +0200 | [diff] [blame] | 1348 | this.generatedSuggestion = suggestion; |
Milutin Kristofic | 734df55 | 2023-08-07 10:38:50 +0200 | [diff] [blame] | 1349 | } |
| 1350 | |
Milutin Kristofic | 1cfc021 | 2024-02-01 20:16:06 +0100 | [diff] [blame] | 1351 | private async generateSuggestEdit_v2() { |
| 1352 | const suggestionsProvider = this.suggestionsProvider; |
| 1353 | const changeInfo = this.getChangeModel().getChange(); |
| 1354 | if ( |
| 1355 | !suggestionsProvider?.suggestFix || |
| 1356 | !this.showGeneratedSuggestion() || |
| 1357 | !this.generateSuggestion || |
| 1358 | !changeInfo || |
| 1359 | !this.comment || |
| 1360 | !this.comment.patch_set || |
| 1361 | !this.comment.path || |
| 1362 | this.messageText.length === 0 |
| 1363 | ) |
| 1364 | return; |
| 1365 | this.generatedSuggestionId = uuid(); |
| 1366 | this.reporting.reportInteraction(Interaction.GENERATE_SUGGESTION_REQUEST, { |
| 1367 | uuid: this.generatedSuggestionId, |
| 1368 | type: 'suggest-fix', |
| 1369 | commentId: this.comment.id, |
| 1370 | }); |
| 1371 | this.suggestionLoading = true; |
| 1372 | let suggestionResponse; |
| 1373 | try { |
| 1374 | suggestionResponse = await suggestionsProvider.suggestFix({ |
| 1375 | prompt: this.messageText, |
| 1376 | changeInfo: changeInfo as ChangeInfo, |
| 1377 | patchsetNumber: this.comment?.patch_set, |
| 1378 | filePath: this.comment.path, |
| 1379 | range: this.comment.range, |
| 1380 | lineNumber: this.comment.line, |
| 1381 | }); |
| 1382 | } finally { |
| 1383 | this.suggestionLoading = false; |
| 1384 | } |
| 1385 | |
| 1386 | if (!suggestionResponse) return; |
| 1387 | // TODO(milutin): The suggestionResponse can contain multiple suggestion |
| 1388 | // options. We pick the first one for now. In future we shouldn't ignore |
| 1389 | // other suggestions. |
| 1390 | this.reporting.reportInteraction(Interaction.GENERATE_SUGGESTION_RESPONSE, { |
| 1391 | uuid: this.generatedSuggestionId, |
| 1392 | type: 'suggest-fix', |
Milutin Kristofic | fa15825 | 2024-04-24 11:58:32 +0200 | [diff] [blame] | 1393 | commentId: this.comment.id, |
Milutin Kristofic | 1cfc021 | 2024-02-01 20:16:06 +0100 | [diff] [blame] | 1394 | response: suggestionResponse.responseCode, |
| 1395 | numSuggestions: suggestionResponse.fix_suggestions.length, |
| 1396 | }); |
| 1397 | const suggestion = suggestionResponse.fix_suggestions?.[0]; |
Milutin Kristofic | ead5c55b | 2024-03-05 18:13:52 +0100 | [diff] [blame] | 1398 | if (!suggestion?.replacements || suggestion.replacements.length === 0) { |
| 1399 | return; |
| 1400 | } |
Milutin Kristofic | 1cfc021 | 2024-02-01 20:16:06 +0100 | [diff] [blame] | 1401 | this.generatedFixSuggestion = suggestion; |
Milutin Kristofic | 847c46d | 2024-04-15 13:43:06 +0200 | [diff] [blame] | 1402 | try { |
Ben Rohlfs | 6f46d24 | 2024-04-19 09:12:22 +0200 | [diff] [blame] | 1403 | await waitUntil(() => this.getFixSuggestions() !== undefined); |
Milutin Kristofic | 847c46d | 2024-04-15 13:43:06 +0200 | [diff] [blame] | 1404 | this.autoSaveTrigger$.next(); |
| 1405 | } catch (error) { |
| 1406 | // Error is ok in some cases like quick save by user. |
| 1407 | console.warn(error); |
| 1408 | } |
Milutin Kristofic | 1cfc021 | 2024-02-01 20:16:06 +0100 | [diff] [blame] | 1409 | } |
| 1410 | |
Ben Rohlfs | 2ad9756 | 2024-04-26 18:27:16 +0200 | [diff] [blame] | 1411 | private async autocompleteComment() { |
| 1412 | const enabled = this.flagsService.isEnabled( |
| 1413 | KnownExperimentId.COMMENT_AUTOCOMPLETION |
| 1414 | ); |
| 1415 | const suggestionsProvider = this.suggestionsProvider; |
| 1416 | const change = this.getChangeModel().getChange(); |
| 1417 | if ( |
| 1418 | !enabled || |
Ben Rohlfs | b387369 | 2024-05-15 13:40:33 +0200 | [diff] [blame] | 1419 | !this.autocompleteEnabled || |
Ben Rohlfs | 2ad9756 | 2024-04-26 18:27:16 +0200 | [diff] [blame] | 1420 | !suggestionsProvider?.autocompleteComment || |
| 1421 | !change || |
| 1422 | !this.comment?.patch_set || |
| 1423 | !this.comment.path || |
| 1424 | this.messageText.length === 0 |
| 1425 | ) { |
| 1426 | return; |
| 1427 | } |
| 1428 | const commentText = this.messageText; |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 1429 | this.reporting.time(Timing.COMMENT_COMPLETION); |
Ben Rohlfs | 2ad9756 | 2024-04-26 18:27:16 +0200 | [diff] [blame] | 1430 | const response = await suggestionsProvider.autocompleteComment({ |
| 1431 | id: id(this.comment), |
| 1432 | commentText, |
| 1433 | changeInfo: change as ChangeInfo, |
| 1434 | patchsetNumber: this.comment?.patch_set, |
| 1435 | filePath: this.comment.path, |
| 1436 | range: this.comment.range, |
| 1437 | lineNumber: this.comment.line, |
| 1438 | }); |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 1439 | const elapsed = this.reporting.timeEnd(Timing.COMMENT_COMPLETION); |
| 1440 | const context = this.createAutocompletionContext( |
| 1441 | commentText, |
| 1442 | response, |
| 1443 | elapsed |
| 1444 | ); |
| 1445 | this.reportHintInteraction( |
| 1446 | Interaction.COMMENT_COMPLETION_SUGGESTION_FETCHED, |
| 1447 | context |
| 1448 | ); |
Ben Rohlfs | 2ad9756 | 2024-04-26 18:27:16 +0200 | [diff] [blame] | 1449 | if (!response?.completion) return; |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 1450 | // Note that we are setting the cache value for `commentText` and getting the value |
| 1451 | // for `this.messageText`. |
| 1452 | this.autocompleteCache.set(context); |
| 1453 | this.autocompleteHint = this.autocompleteCache.get(this.messageText); |
| 1454 | } |
| 1455 | |
| 1456 | private createAutocompletionBaseContext(): Partial<AutocompletionContext> { |
| 1457 | return { |
| 1458 | commentId: id(this.comment!), |
| 1459 | commentNumber: this.comments?.length ?? 0, |
| 1460 | filePath: this.comment!.path, |
| 1461 | fileExtension: getFileExtension(this.comment!.path ?? ''), |
| 1462 | }; |
| 1463 | } |
| 1464 | |
| 1465 | private createAutocompletionContext( |
| 1466 | draftContent: string, |
| 1467 | response: AutocompleteCommentResponse, |
| 1468 | requestDurationMs: number |
| 1469 | ): AutocompletionContext { |
| 1470 | const commentCompletion = response.completion ?? ''; |
| 1471 | return { |
| 1472 | ...this.createAutocompletionBaseContext(), |
| 1473 | |
| 1474 | draftContent, |
| 1475 | draftContentLength: draftContent.length, |
| 1476 | commentCompletion, |
| 1477 | commentCompletionLength: commentCompletion.length, |
| 1478 | |
| 1479 | isFullCommentPrediction: draftContent.length === 0, |
| 1480 | draftInSyncWithSuggestionLength: 0, |
| 1481 | modelVersion: response.modelVersion ?? '', |
| 1482 | requestDurationMs, |
| 1483 | }; |
Ben Rohlfs | 2ad9756 | 2024-04-26 18:27:16 +0200 | [diff] [blame] | 1484 | } |
| 1485 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1486 | private renderRobotActions() { |
| 1487 | if (!this.account || !isRobot(this.comment)) return; |
| 1488 | const endpoint = html` |
| 1489 | <gr-endpoint-decorator name="robot-comment-controls"> |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 1490 | <gr-endpoint-param name="comment" .value=${this.comment}> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1491 | </gr-endpoint-param> |
| 1492 | </gr-endpoint-decorator> |
| 1493 | `; |
| 1494 | return html` |
| 1495 | <div class="robotActions"> |
| 1496 | ${this.renderCopyLinkIcon()} ${endpoint} ${this.renderShowFixButton()} |
| 1497 | ${this.renderPleaseFixButton()} |
| 1498 | </div> |
| 1499 | `; |
| 1500 | } |
| 1501 | |
| 1502 | private renderShowFixButton() { |
Kamil Musin | d441863 | 2023-03-07 10:20:49 +0100 | [diff] [blame] | 1503 | const fix_suggestions = (this.comment as RobotCommentInfo)?.fix_suggestions; |
| 1504 | if (!fix_suggestions || fix_suggestions.length === 0) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1505 | return html` |
| 1506 | <gr-button |
| 1507 | link |
| 1508 | secondary |
| 1509 | class="action show-fix" |
Kamil Musin | d441863 | 2023-03-07 10:20:49 +0100 | [diff] [blame] | 1510 | @click=${() => this.handleShowFix()} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1511 | > |
| 1512 | Show Fix |
| 1513 | </gr-button> |
| 1514 | `; |
| 1515 | } |
| 1516 | |
| 1517 | private renderPleaseFixButton() { |
| 1518 | if (this.hasHumanReply()) return; |
| 1519 | return html` |
| 1520 | <gr-button |
| 1521 | link |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 1522 | ?disabled=${this.robotButtonDisabled} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1523 | class="action fix" |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1524 | @click=${this.handlePleaseFix} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1525 | > |
| 1526 | Please Fix |
| 1527 | </gr-button> |
| 1528 | `; |
| 1529 | } |
| 1530 | |
| 1531 | private renderConfirmDialog() { |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1532 | return html` |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame] | 1533 | <dialog id="confirmDeleteModal" tabindex="-1"> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1534 | <gr-confirm-delete-comment-dialog |
Kamil Musin | c7d3f28 | 2022-12-29 13:27:55 +0100 | [diff] [blame] | 1535 | id="confirmDeleteCommentDialog" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 1536 | @confirm=${this.handleConfirmDeleteComment} |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame] | 1537 | @cancel=${this.closeDeleteCommentModal} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1538 | > |
| 1539 | </gr-confirm-delete-comment-dialog> |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame] | 1540 | </dialog> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1541 | `; |
| 1542 | } |
| 1543 | |
| 1544 | private getUrlForComment() { |
Ben Rohlfs | ba44082 | 2023-04-11 18:08:03 +0200 | [diff] [blame] | 1545 | if (!this.changeNum || !this.repoName || !this.comment?.id) return ''; |
Ben Rohlfs | 731738b | 2022-09-15 15:55:33 +0200 | [diff] [blame] | 1546 | return createDiffUrl({ |
| 1547 | changeNum: this.changeNum, |
Ben Rohlfs | bfc688b | 2022-10-21 12:38:37 +0200 | [diff] [blame] | 1548 | repo: this.repoName, |
Ben Rohlfs | ba44082 | 2023-04-11 18:08:03 +0200 | [diff] [blame] | 1549 | commentId: this.comment.id, |
Ben Rohlfs | 731738b | 2022-09-15 15:55:33 +0200 | [diff] [blame] | 1550 | }); |
Dhruv Srivastava | 0287bf9 | 2020-09-11 16:56:38 +0200 | [diff] [blame] | 1551 | } |
| 1552 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1553 | private firstWillUpdateDone = false; |
| 1554 | |
| 1555 | firstWillUpdate() { |
| 1556 | if (this.firstWillUpdateDone) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1557 | assertIsDefined(this.comment, 'comment'); |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1558 | this.firstWillUpdateDone = true; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1559 | this.unresolved = this.comment.unresolved ?? true; |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1560 | if (this.permanentEditingMode) { |
| 1561 | this.edit(); |
| 1562 | } |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1563 | if (isDraft(this.comment)) { |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1564 | this.collapsed = false; |
| 1565 | } else { |
| 1566 | this.collapsed = !!this.initiallyCollapsed; |
| 1567 | } |
| 1568 | } |
| 1569 | |
Dhruv Srivastava | a49dffd | 2022-10-20 14:04:37 +0200 | [diff] [blame] | 1570 | override updated(changed: PropertyValues) { |
| 1571 | if (changed.has('editing')) { |
Dhruv Srivastava | e8b8639 | 2022-10-20 17:17:21 +0200 | [diff] [blame] | 1572 | if (this.editing && !this.permanentEditingMode) { |
Ben Rohlfs | ba9329c | 2023-05-16 10:05:12 +0200 | [diff] [blame] | 1573 | // Note that this is a bit fragile, because we are relying on the |
| 1574 | // comment to become visible soonish. If that does not happen, then we |
| 1575 | // will be waiting indefinitely and grab focus at some point in the |
| 1576 | // distant future. |
Dhruv Srivastava | a49dffd | 2022-10-20 14:04:37 +0200 | [diff] [blame] | 1577 | whenVisible(this, () => this.textarea?.putCursorAtEnd()); |
| 1578 | } |
| 1579 | } |
Milutin Kristofic | 490fa95 | 2023-09-12 20:17:36 +0200 | [diff] [blame] | 1580 | if ( |
| 1581 | changed.has('changeNum') || |
| 1582 | changed.has('comment') || |
Milutin Kristofic | 06dfda4 | 2023-11-15 09:51:13 +0100 | [diff] [blame] | 1583 | changed.has('generatedSuggestion') |
Milutin Kristofic | 490fa95 | 2023-09-12 20:17:36 +0200 | [diff] [blame] | 1584 | ) { |
Milutin Kristofic | e9dbbe9 | 2023-05-17 21:21:28 +0200 | [diff] [blame] | 1585 | if ( |
Milutin Kristofic | e9dbbe9 | 2023-05-17 21:21:28 +0200 | [diff] [blame] | 1586 | !this.changeNum || |
Milutin Kristofic | 78cdec9 | 2023-08-29 14:37:31 +0200 | [diff] [blame] | 1587 | !this.comment || |
Milutin Kristofic | 8f07c38 | 2023-10-02 13:13:21 +0200 | [diff] [blame] | 1588 | (!hasUserSuggestion(this.comment) && !this.generatedSuggestion) |
Milutin Kristofic | e9dbbe9 | 2023-05-17 21:21:28 +0200 | [diff] [blame] | 1589 | ) |
| 1590 | return; |
| 1591 | (async () => { |
Milutin Kristofic | 78cdec9 | 2023-08-29 14:37:31 +0200 | [diff] [blame] | 1592 | this.commentedText = await this.commentModel.getCommentedCode( |
| 1593 | this.comment, |
| 1594 | this.changeNum |
| 1595 | ); |
Milutin Kristofic | e9dbbe9 | 2023-05-17 21:21:28 +0200 | [diff] [blame] | 1596 | })(); |
| 1597 | } |
Dhruv Srivastava | a49dffd | 2022-10-20 14:04:37 +0200 | [diff] [blame] | 1598 | } |
| 1599 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1600 | override willUpdate(changed: PropertyValues) { |
| 1601 | this.firstWillUpdate(); |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1602 | if (changed.has('comment')) { |
| 1603 | if (isDraft(this.comment) && isError(this.comment)) { |
| 1604 | this.edit(); |
| 1605 | } |
Milutin Kristofic | e9dbbe9 | 2023-05-17 21:21:28 +0200 | [diff] [blame] | 1606 | if (this.comment) { |
| 1607 | this.commentModel.updateState({ |
| 1608 | comment: this.comment, |
| 1609 | }); |
| 1610 | } |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1611 | } |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1612 | if (changed.has('editing')) { |
Chris Poucet | afd0f7c | 2022-10-04 10:04:43 +0000 | [diff] [blame] | 1613 | this.onEditingChanged(); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1614 | } |
| 1615 | if (changed.has('unresolved')) { |
| 1616 | // The <gr-comment-thread> component wants to change its color based on |
| 1617 | // the (dirty) unresolved state, so let's notify it about changes. |
Dhruv Srivastava | 463bb33 | 2022-08-31 13:00:49 +0200 | [diff] [blame] | 1618 | fire(this, 'comment-unresolved-changed', {value: this.unresolved}); |
| 1619 | } |
| 1620 | if (changed.has('messageText')) { |
| 1621 | // GrReplyDialog updates it's state when text inside patchset level |
| 1622 | // comment changes. |
| 1623 | fire(this, 'comment-text-changed', {value: this.messageText}); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | private handlePortedMessageClick() { |
Ben Rohlfs | c1c6afd | 2021-02-18 13:13:22 +0100 | [diff] [blame] | 1628 | assertIsDefined(this.comment, 'comment'); |
Dhruv Srivastava | c8df760 | 2021-01-15 10:59:00 +0100 | [diff] [blame] | 1629 | this.reporting.reportInteraction('navigate-to-original-comment', { |
| 1630 | line: this.comment.line, |
| 1631 | range: this.comment.range, |
| 1632 | }); |
| 1633 | } |
| 1634 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1635 | private handleCopyLink() { |
Ben Rohlfs | 44f0104 | 2023-02-18 13:27:57 +0100 | [diff] [blame] | 1636 | fire(this, 'copy-comment-link', {}); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | /** Enter editing mode. */ |
Ben Rohlfs | ba9329c | 2023-05-16 10:05:12 +0200 | [diff] [blame] | 1640 | edit() { |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1641 | assert(isDraft(this.comment), 'only drafts are editable'); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1642 | if (this.editing) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1643 | this.editing = true; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | // TODO: Move this out of gr-comment. gr-comment should not have a comments |
| 1647 | // property. |
| 1648 | private hasHumanReply() { |
| 1649 | if (!this.comment || !this.comments) return false; |
| 1650 | return this.comments.some( |
| 1651 | c => c.in_reply_to && c.in_reply_to === this.comment?.id && !isRobot(c) |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 1652 | ); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | // private, but visible for testing |
Milutin Kristofic | 1ebae37 | 2022-11-22 20:35:38 +0100 | [diff] [blame] | 1656 | async createFixPreview( |
| 1657 | replacement?: string |
| 1658 | ): Promise<OpenFixPreviewEventDetail> { |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1659 | assertIsDefined(this.comment?.patch_set, 'comment.patch_set'); |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1660 | assertIsDefined(this.comment?.path, 'comment.path'); |
| 1661 | |
Milutin Kristofic | 1ebae37 | 2022-11-22 20:35:38 +0100 | [diff] [blame] | 1662 | if (hasUserSuggestion(this.comment) || replacement) { |
| 1663 | replacement = replacement ?? getUserSuggestion(this.comment); |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1664 | assert(!!replacement, 'malformed user suggestion'); |
Milutin Kristofic | 78cdec9 | 2023-08-29 14:37:31 +0200 | [diff] [blame] | 1665 | let commentedCode = this.commentedText; |
| 1666 | if (!commentedCode) { |
| 1667 | commentedCode = await this.getCommentedCode(); |
| 1668 | } |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1669 | |
| 1670 | return { |
| 1671 | fixSuggestions: createUserFixSuggestion( |
| 1672 | this.comment, |
Milutin Kristofic | 78cdec9 | 2023-08-29 14:37:31 +0200 | [diff] [blame] | 1673 | commentedCode, |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1674 | replacement |
| 1675 | ), |
| 1676 | patchNum: this.comment.patch_set, |
Milutin Kristofic | eb8f655 | 2023-02-09 22:17:35 +0100 | [diff] [blame] | 1677 | onCloseFixPreviewCallbacks: [ |
| 1678 | fixApplied => { |
| 1679 | if (fixApplied) this.handleAppliedFix(); |
| 1680 | }, |
| 1681 | ], |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1682 | }; |
| 1683 | } |
Milutin Kristofic | bf61a4e | 2024-02-12 20:50:16 +0100 | [diff] [blame] | 1684 | if ( |
| 1685 | isRobot(this.comment) && |
| 1686 | this.comment.fix_suggestions && |
| 1687 | this.comment.fix_suggestions.length > 0 |
| 1688 | ) { |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1689 | const id = this.comment.robot_id; |
| 1690 | return { |
| 1691 | fixSuggestions: this.comment.fix_suggestions.map(s => { |
| 1692 | return { |
| 1693 | ...s, |
| 1694 | description: `${id ?? ''} - ${s.description ?? ''}`, |
| 1695 | }; |
| 1696 | }), |
| 1697 | patchNum: this.comment.patch_set, |
Milutin Kristofic | eb8f655 | 2023-02-09 22:17:35 +0100 | [diff] [blame] | 1698 | onCloseFixPreviewCallbacks: [], |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1699 | }; |
| 1700 | } |
| 1701 | throw new Error('unable to create preview fix event'); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1702 | } |
| 1703 | |
Chris Poucet | afd0f7c | 2022-10-04 10:04:43 +0000 | [diff] [blame] | 1704 | private onEditingChanged() { |
| 1705 | if (this.editing) { |
| 1706 | this.collapsed = false; |
| 1707 | this.messageText = this.comment?.message ?? ''; |
| 1708 | this.unresolved = this.comment?.unresolved ?? true; |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1709 | if (!isError(this.comment) && !isSaving(this.comment)) { |
| 1710 | this.originalMessage = this.messageText; |
| 1711 | this.originalUnresolved = this.unresolved; |
| 1712 | } |
Chris Poucet | afd0f7c | 2022-10-04 10:04:43 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | // Parent components such as the reply dialog might be interested in whether |
| 1716 | // come of their child components are in editing mode. |
| 1717 | fire(this, 'comment-editing-changed', { |
| 1718 | editing: this.editing, |
| 1719 | path: this.comment?.path ?? '', |
| 1720 | }); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1721 | } |
| 1722 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1723 | // private, but visible for testing |
| 1724 | isSaveDisabled() { |
| 1725 | assertIsDefined(this.comment, 'comment'); |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1726 | if (isSaving(this.comment) && !this.autoSaving) return true; |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1727 | return !this.messageText?.trimEnd(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1728 | } |
| 1729 | |
Dhruv Srivastava | e8b8639 | 2022-10-20 17:17:21 +0200 | [diff] [blame] | 1730 | override focus() { |
Ben Rohlfs | ba9329c | 2023-05-16 10:05:12 +0200 | [diff] [blame] | 1731 | // Note that this may not work as intended, because the textarea is not |
| 1732 | // rendered yet. |
Dhruv Srivastava | e8b8639 | 2022-10-20 17:17:21 +0200 | [diff] [blame] | 1733 | this.textarea?.focus(); |
| 1734 | } |
| 1735 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1736 | private handleEsc() { |
| 1737 | // vim users don't like ESC to cancel/discard, so only do this when the |
| 1738 | // comment text is empty. |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1739 | if (!this.messageText?.trimEnd()) this.cancel(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1740 | } |
| 1741 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1742 | private handleAnchorClick() { |
| 1743 | assertIsDefined(this.comment, 'comment'); |
| 1744 | fire(this, 'comment-anchor-tap', { |
| 1745 | number: this.comment.line || FILE, |
| 1746 | side: this.comment?.side, |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 1747 | }); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1748 | } |
| 1749 | |
Dhruv Srivastava | 15950b45 | 2022-09-12 10:56:53 +0200 | [diff] [blame] | 1750 | private async handleSaveButtonClicked() { |
| 1751 | await this.save(); |
| 1752 | if (this.permanentEditingMode) { |
| 1753 | this.editing = !this.editing; |
| 1754 | } |
Dhruv Srivastava | 705eac9 | 2022-09-01 11:33:27 +0200 | [diff] [blame] | 1755 | } |
| 1756 | |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1757 | private handlePleaseFix() { |
| 1758 | const message = this.comment?.message; |
| 1759 | assert(!!message, 'empty message'); |
| 1760 | const quoted = message.replace(NEWLINE_PATTERN, '\n> '); |
| 1761 | const eventDetail: ReplyToCommentEventDetail = { |
| 1762 | content: `> ${quoted}\n\nPlease fix.`, |
| 1763 | userWantsToEdit: false, |
| 1764 | unresolved: true, |
| 1765 | }; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1766 | // Handled by <gr-comment-thread>. |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1767 | fire(this, 'reply-to-comment', eventDetail); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1768 | } |
| 1769 | |
Milutin Kristofic | eb8f655 | 2023-02-09 22:17:35 +0100 | [diff] [blame] | 1770 | private handleAppliedFix() { |
| 1771 | const message = this.comment?.message; |
| 1772 | assert(!!message, 'empty message'); |
| 1773 | const eventDetail: ReplyToCommentEventDetail = { |
| 1774 | content: 'Fix applied.', |
| 1775 | userWantsToEdit: false, |
| 1776 | unresolved: false, |
| 1777 | }; |
| 1778 | // Handled by <gr-comment-thread>. |
| 1779 | fire(this, 'reply-to-comment', eventDetail); |
| 1780 | } |
| 1781 | |
Milutin Kristofic | 1ebae37 | 2022-11-22 20:35:38 +0100 | [diff] [blame] | 1782 | private async handleShowFix(replacement?: string) { |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1783 | // Handled top-level in the diff and change view components. |
Milutin Kristofic | 1ebae37 | 2022-11-22 20:35:38 +0100 | [diff] [blame] | 1784 | fire(this, 'open-fix-preview', await this.createFixPreview(replacement)); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1785 | } |
| 1786 | |
Milutin Kristofic | 8238de5 | 2023-01-12 19:33:45 +0100 | [diff] [blame] | 1787 | async createSuggestEdit(e: MouseEvent) { |
| 1788 | e.stopPropagation(); |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1789 | const line = await this.getCommentedCode(); |
Milutin Kristofic | 7d55747 | 2023-08-21 11:23:36 +0200 | [diff] [blame] | 1790 | const addNewLine = this.messageText.length !== 0; |
| 1791 | this.messageText += `${ |
| 1792 | addNewLine ? '\n' : '' |
| 1793 | }${USER_SUGGESTION_START_PATTERN}${line}${'\n```'}`; |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1794 | } |
| 1795 | |
Milutin Kristofic | 78cdec9 | 2023-08-29 14:37:31 +0200 | [diff] [blame] | 1796 | // TODO(milutin): Remove once feature flag is rollout and use only model |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1797 | async getCommentedCode() { |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 1798 | assertIsDefined(this.comment, 'comment'); |
| 1799 | assertIsDefined(this.changeNum, 'changeNum'); |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 1800 | const file = await this.restApiService.getFileContent( |
| 1801 | this.changeNum, |
| 1802 | this.comment.path!, |
| 1803 | this.comment.patch_set! |
| 1804 | ); |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1805 | assert( |
| 1806 | !!file && isBase64FileContent(file) && !!file.content, |
| 1807 | 'file content for comment not found' |
| 1808 | ); |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 1809 | const line = getContentInCommentRange(file.content, this.comment); |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1810 | assert(!!line, 'file content for comment not found'); |
| 1811 | return line; |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 1812 | } |
| 1813 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1814 | // private, but visible for testing |
| 1815 | cancel() { |
| 1816 | assertIsDefined(this.comment, 'comment'); |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1817 | assert(isDraft(this.comment), 'only drafts are editable'); |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1818 | this.messageText = this.originalMessage; |
| 1819 | this.unresolved = this.originalUnresolved; |
| 1820 | this.save(); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1821 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1822 | |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1823 | async autoSave() { |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1824 | if (isSaving(this.comment) || this.autoSaving) return; |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1825 | if (!this.editing || !this.comment) return; |
Chris Poucet | 7722698 | 2023-08-10 18:10:04 +0200 | [diff] [blame] | 1826 | if (this.disableAutoSaving) return; |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1827 | assert(isDraft(this.comment), 'only drafts are editable'); |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1828 | const messageToSave = this.messageText.trimEnd(); |
| 1829 | if (messageToSave === '') return; |
Milutin Kristofic | d4274a1 | 2024-02-19 20:30:02 +0100 | [diff] [blame] | 1830 | if (!this.somethingToSave()) return; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1831 | |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1832 | try { |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 1833 | this.autoSaving = this.rawSave({showToast: false}); |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1834 | await this.autoSaving; |
| 1835 | } finally { |
| 1836 | this.autoSaving = undefined; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1837 | } |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | async discard() { |
| 1841 | this.messageText = ''; |
| 1842 | await this.save(); |
| 1843 | } |
| 1844 | |
Chris Poucet | 063fa50 | 2023-11-21 10:06:35 +0100 | [diff] [blame] | 1845 | async convertToCommentInputAndOrDiscard(): Promise<CommentInput | undefined> { |
Chris Poucet | 7722698 | 2023-08-10 18:10:04 +0200 | [diff] [blame] | 1846 | if (!this.somethingToSave() || !this.comment) return; |
Chris Poucet | 063fa50 | 2023-11-21 10:06:35 +0100 | [diff] [blame] | 1847 | const messageToSave = this.messageText.trimEnd(); |
| 1848 | if (messageToSave === '') { |
| 1849 | await this.getCommentsModel().discardDraft(id(this.comment)); |
| 1850 | return undefined; |
| 1851 | } else { |
| 1852 | return convertToCommentInput({ |
| 1853 | ...this.comment, |
| 1854 | message: this.messageText.trimEnd(), |
| 1855 | unresolved: this.unresolved, |
| 1856 | }); |
| 1857 | } |
Chris Poucet | 7722698 | 2023-08-10 18:10:04 +0200 | [diff] [blame] | 1858 | } |
| 1859 | |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1860 | async save() { |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1861 | assert(isDraft(this.comment), 'only drafts are editable'); |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 1862 | // There is a minimal chance of `isSaving()` being false between iterations |
| 1863 | // of the below while loop. But this will be extremely rare and just lead |
| 1864 | // to a harmless assertion error. So let's not bother. |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1865 | if (isSaving(this.comment) && !this.autoSaving) return; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1866 | |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 1867 | if (!this.permanentEditingMode) { |
| 1868 | this.editing = false; |
| 1869 | } |
| 1870 | if (this.autoSaving) { |
| 1871 | this.comment = await this.autoSaving; |
| 1872 | } |
| 1873 | // Depending on whether `messageToSave` is empty we treat this either as |
| 1874 | // a discard or a save action. |
| 1875 | const messageToSave = this.messageText.trimEnd(); |
| 1876 | if (messageToSave === '') { |
Ben Rohlfs | 0d9d0c3 | 2023-04-20 18:12:06 +0200 | [diff] [blame] | 1877 | if (!this.permanentEditingMode || this.somethingToSave()) { |
| 1878 | await this.getCommentsModel().discardDraft(id(this.comment)); |
| 1879 | } |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 1880 | } else { |
| 1881 | // No need to make a backend call when nothing has changed. |
| 1882 | while (this.somethingToSave()) { |
Milutin Kristofic | c0582fc | 2024-04-02 20:30:51 +0200 | [diff] [blame] | 1883 | this.trackGeneratedSuggestionEdit(); |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 1884 | this.comment = await this.rawSave({showToast: true}); |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1885 | if (isError(this.comment)) return; |
Ben Rohlfs | 607126f | 2021-12-07 08:21:52 +0100 | [diff] [blame] | 1886 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1887 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1888 | } |
| 1889 | |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 1890 | private somethingToSave() { |
| 1891 | if (!this.comment) return false; |
| 1892 | return ( |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1893 | isError(this.comment) || |
Milutin Kristofic | d0fc9e8 | 2024-04-17 14:29:13 +0200 | [diff] [blame] | 1894 | this.messageText.trimEnd() !== this.comment.message || |
Milutin Kristofic | d4274a1 | 2024-02-19 20:30:02 +0100 | [diff] [blame] | 1895 | this.unresolved !== this.comment.unresolved || |
Milutin Kristofic | 847c46d | 2024-04-15 13:43:06 +0200 | [diff] [blame] | 1896 | this.isFixSuggestionChanged() |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 1897 | ); |
| 1898 | } |
| 1899 | |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1900 | /** For sharing between save() and autoSave(). */ |
Ben Rohlfs | 1efb1a7 | 2023-04-12 23:25:31 +0200 | [diff] [blame] | 1901 | private rawSave(options: {showToast: boolean}) { |
Ben Rohlfs | 610bb4f | 2023-04-17 12:34:35 +0200 | [diff] [blame] | 1902 | assert(isDraft(this.comment), 'only drafts are editable'); |
| 1903 | assert(!isSaving(this.comment), 'saving already in progress'); |
Milutin Kristofic | 847c46d | 2024-04-15 13:43:06 +0200 | [diff] [blame] | 1904 | const draft: DraftInfo = { |
| 1905 | ...this.comment, |
| 1906 | message: this.messageText.trimEnd(), |
| 1907 | unresolved: this.unresolved, |
| 1908 | }; |
| 1909 | if (this.isFixSuggestionChanged()) { |
| 1910 | draft.fix_suggestions = this.getFixSuggestions(); |
| 1911 | } |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 1912 | this.reportHintInteractionSaved(); |
Milutin Kristofic | 847c46d | 2024-04-15 13:43:06 +0200 | [diff] [blame] | 1913 | return this.getCommentsModel().saveDraft(draft, options.showToast); |
| 1914 | } |
| 1915 | |
| 1916 | isFixSuggestionChanged(): boolean { |
| 1917 | // Check to not change fix suggestion when draft is not being edited only |
| 1918 | // when user quickly disable generating suggestions and click save |
| 1919 | if (!this.editing && this.generateSuggestion) return false; |
| 1920 | return !deepEqual(this.comment?.fix_suggestions, this.getFixSuggestions()); |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1921 | } |
| 1922 | |
Milutin Kristofic | d4274a1 | 2024-02-19 20:30:02 +0100 | [diff] [blame] | 1923 | getFixSuggestions(): FixSuggestionInfo[] | undefined { |
| 1924 | if (!this.flagsService.isEnabled(KnownExperimentId.ML_SUGGESTED_EDIT_V2)) |
| 1925 | return undefined; |
| 1926 | if (!this.generateSuggestion) return undefined; |
| 1927 | if (!this.generatedFixSuggestion) return undefined; |
Milutin Kristofic | d0fc9e8 | 2024-04-17 14:29:13 +0200 | [diff] [blame] | 1928 | // Disable fix suggestions when the comment already has a user suggestion |
| 1929 | if (this.comment && hasUserSuggestion(this.comment)) return undefined; |
Milutin Kristofic | 847c46d | 2024-04-15 13:43:06 +0200 | [diff] [blame] | 1930 | // we ignore fixSuggestions until they are previewed. |
| 1931 | if ( |
| 1932 | this.suggestionDiffPreview && |
| 1933 | !this.suggestionDiffPreview?.previewed && |
| 1934 | !this.suggestionLoading |
| 1935 | ) |
| 1936 | return undefined; |
Milutin Kristofic | d4274a1 | 2024-02-19 20:30:02 +0100 | [diff] [blame] | 1937 | return [this.generatedFixSuggestion]; |
| 1938 | } |
| 1939 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1940 | private handleToggleResolved() { |
| 1941 | this.unresolved = !this.unresolved; |
Dhruv Srivastava | 73f9edc | 2021-12-02 11:23:27 +0100 | [diff] [blame] | 1942 | if (!this.editing) { |
| 1943 | // messageText is only assigned a value if the comment reaches editing |
| 1944 | // state, however it is possible that the user toggles the resolved state |
| 1945 | // without editing the comment in which case we assign the correct value |
| 1946 | // to messageText here |
| 1947 | this.messageText = this.comment?.message ?? ''; |
| 1948 | this.save(); |
| 1949 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1950 | } |
| 1951 | |
Kamil Musin | 9c8833a | 2022-12-29 12:05:08 +0100 | [diff] [blame] | 1952 | private openDeleteCommentModal() { |
| 1953 | this.confirmDeleteModal?.showModal(); |
Kamil Musin | c7d3f28 | 2022-12-29 13:27:55 +0100 | [diff] [blame] | 1954 | whenVisible(this.confirmDeleteDialog!, () => { |
| 1955 | this.confirmDeleteDialog!.resetFocus(); |
| 1956 | }); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1957 | } |
| 1958 | |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame] | 1959 | private closeDeleteCommentModal() { |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame] | 1960 | this.confirmDeleteModal?.close(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1961 | } |
| 1962 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1963 | /** |
| 1964 | * Deleting a *published* comment is an admin feature. It means more than just |
| 1965 | * discarding a draft. |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1966 | */ |
| 1967 | // private, but visible for testing |
Kamil Musin | d88622f | 2023-01-02 11:52:57 +0100 | [diff] [blame] | 1968 | async handleConfirmDeleteComment() { |
Kamil Musin | c7d3f28 | 2022-12-29 13:27:55 +0100 | [diff] [blame] | 1969 | if (!this.confirmDeleteDialog || !this.confirmDeleteDialog.message) { |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 1970 | throw new Error('missing confirm delete dialog'); |
| 1971 | } |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1972 | assertIsDefined(this.changeNum, 'changeNum'); |
| 1973 | assertIsDefined(this.comment, 'comment'); |
Kamil Musin | d88622f | 2023-01-02 11:52:57 +0100 | [diff] [blame] | 1974 | |
| 1975 | await this.getCommentsModel().deleteComment( |
| 1976 | this.changeNum, |
| 1977 | this.comment, |
Kamil Musin | c7d3f28 | 2022-12-29 13:27:55 +0100 | [diff] [blame] | 1978 | this.confirmDeleteDialog.message |
Kamil Musin | d88622f | 2023-01-02 11:52:57 +0100 | [diff] [blame] | 1979 | ); |
| 1980 | this.closeDeleteCommentModal(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1981 | } |
Milutin Kristofic | c0582fc | 2024-04-02 20:30:51 +0200 | [diff] [blame] | 1982 | |
| 1983 | private trackGeneratedSuggestionEdit() { |
Milutin Kristofic | 23f923a | 2024-04-04 14:05:32 +0200 | [diff] [blame] | 1984 | const hasUserSuggestion = this.messageText.includes( |
| 1985 | USER_SUGGESTION_START_PATTERN |
| 1986 | ); |
Milutin Kristofic | c0582fc | 2024-04-02 20:30:51 +0200 | [diff] [blame] | 1987 | const wasGeneratedSuggestionEdited = |
| 1988 | this.addedGeneratedSuggestion && |
Milutin Kristofic | 23f923a | 2024-04-04 14:05:32 +0200 | [diff] [blame] | 1989 | hasUserSuggestion && |
Milutin Kristofic | c0582fc | 2024-04-02 20:30:51 +0200 | [diff] [blame] | 1990 | !this.messageText.includes(this.addedGeneratedSuggestion); |
| 1991 | if (wasGeneratedSuggestionEdited) { |
| 1992 | this.reporting.reportInteraction(Interaction.GENERATE_SUGGESTION_EDITED, { |
| 1993 | uuid: this.generatedSuggestionId, |
| 1994 | commentId: this.comment?.id ?? '', |
| 1995 | }); |
| 1996 | this.addedGeneratedSuggestion = undefined; |
| 1997 | } |
| 1998 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1999 | } |
| 2000 | |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 2001 | declare global { |
| 2002 | interface HTMLElementTagNameMap { |
| 2003 | 'gr-comment': GrComment; |
| 2004 | } |
Ben Rohlfs | 5b3c655 | 2023-02-18 13:02:46 +0100 | [diff] [blame] | 2005 | interface HTMLElementEventMap { |
| 2006 | 'copy-comment-link': CustomEvent<{}>; |
| 2007 | } |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 2008 | } |