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