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 '@polymer/iron-autogrow-textarea/iron-autogrow-textarea'; |
| 7 | import '../../../styles/shared-styles'; |
| 8 | import '../../plugins/gr-endpoint-decorator/gr-endpoint-decorator'; |
| 9 | import '../../plugins/gr-endpoint-param/gr-endpoint-param'; |
| 10 | import '../gr-button/gr-button'; |
| 11 | import '../gr-dialog/gr-dialog'; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 12 | import '../gr-formatted-text/gr-formatted-text'; |
Chris Poucet | 1c71386 | 2022-07-25 13:12:24 +0200 | [diff] [blame] | 13 | import '../gr-icon/gr-icon'; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 14 | import '../gr-textarea/gr-textarea'; |
| 15 | import '../gr-tooltip-content/gr-tooltip-content'; |
| 16 | import '../gr-confirm-delete-comment-dialog/gr-confirm-delete-comment-dialog'; |
| 17 | import '../gr-account-label/gr-account-label'; |
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'; |
Chris Poucet | 9221cce | 2022-01-05 16:37:11 +0100 | [diff] [blame] | 21 | import {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, |
Ben Rohlfs | 4401b23 | 2021-10-21 13:51:59 +0200 | [diff] [blame] | 25 | NumericChangeId, |
Dhruv Srivastava | 0287bf9 | 2020-09-11 16:56:38 +0200 | [diff] [blame] | 26 | RepoName, |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 27 | RobotCommentInfo, |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 28 | } from '../../../types/common'; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 29 | 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] | 30 | import { |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 31 | Comment, |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 32 | createUserFixSuggestion, |
Ben Rohlfs | 607126f | 2021-12-07 08:21:52 +0100 | [diff] [blame] | 33 | DraftInfo, |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 34 | getContentInCommentRange, |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 35 | getUserSuggestion, |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 36 | hasUserSuggestion, |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 37 | isDraftOrUnsaved, |
Ben Rohlfs | c31773c | 2021-10-01 11:50:13 +0200 | [diff] [blame] | 38 | isRobot, |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 39 | isUnsaved, |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 40 | NEWLINE_PATTERN, |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 41 | USER_SUGGESTION_START_PATTERN, |
Ben Rohlfs | 31825d8 | 2020-10-02 18:08:04 +0200 | [diff] [blame] | 42 | } from '../../../utils/comment-util'; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 43 | import { |
| 44 | OpenFixPreviewEventDetail, |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 45 | ReplyToCommentEventDetail, |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 46 | ValueChangedEvent, |
| 47 | } from '../../../types/events'; |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 48 | import {fire, fireEvent} from '../../../utils/event-util'; |
| 49 | import {assertIsDefined, assert} from '../../../utils/common-util'; |
Dhruv Srivastava | a49dffd | 2022-10-20 14:04:37 +0200 | [diff] [blame] | 50 | import {Key, Modifier, whenVisible} from '../../../utils/dom-util'; |
Chris Poucet | dae98bf | 2022-01-05 15:23:45 +0100 | [diff] [blame] | 51 | import {commentsModelToken} from '../../../models/comments/comments-model'; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 52 | import {sharedStyles} from '../../../styles/shared-styles'; |
| 53 | import {subscribe} from '../../lit/subscription-controller'; |
| 54 | import {ShortcutController} from '../../lit/shortcut-controller'; |
Frank Borden | 42c1a45 | 2022-08-11 16:27:20 +0200 | [diff] [blame] | 55 | import {classMap} from 'lit/directives/class-map.js'; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 56 | import {LineNumber} from '../../../api/diff'; |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 57 | import {CommentSide, SpecialFilePath} from '../../../constants/constants'; |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 58 | import {Subject} from 'rxjs'; |
| 59 | import {debounceTime} from 'rxjs/operators'; |
Chris Poucet | bf65b8f | 2022-01-18 21:18:12 +0000 | [diff] [blame] | 60 | import {changeModelToken} from '../../../models/change/change-model'; |
Ben Rohlfs | 19b6c72 | 2022-06-02 13:55:59 +0200 | [diff] [blame] | 61 | import {Interaction} from '../../../constants/reporting'; |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 62 | import {KnownExperimentId} from '../../../services/flags/flags'; |
| 63 | import {isBase64FileContent} from '../../../api/rest-api'; |
Ben Rohlfs | 731738b | 2022-09-15 15:55:33 +0200 | [diff] [blame] | 64 | import {createDiffUrl} from '../../../models/views/diff'; |
Chris Poucet | bb0cf83 | 2022-10-24 12:32:10 +0200 | [diff] [blame] | 65 | import {userModelToken} from '../../../models/user/user-model'; |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame^] | 66 | import {modalStyles} from '../../../styles/gr-modal-styles'; |
Wyatt Allen | 494e7d4 | 2017-09-12 17:01:42 -0700 | [diff] [blame] | 67 | |
Dhruv Srivastava | 8b015a6 | 2020-07-09 17:45:25 +0200 | [diff] [blame] | 68 | const UNSAVED_MESSAGE = 'Unable to save draft'; |
Wyatt Allen | 846ac2f | 2018-05-14 12:59:23 -0700 | [diff] [blame] | 69 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 70 | const FILE = 'FILE'; |
| 71 | |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 72 | // visible for testing |
| 73 | export const AUTO_SAVE_DEBOUNCE_DELAY_MS = 2000; |
| 74 | |
Dhruv Srivastava | 8b015a6 | 2020-07-09 17:45:25 +0200 | [diff] [blame] | 75 | export const __testOnly_UNSAVED_MESSAGE = UNSAVED_MESSAGE; |
| 76 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 77 | declare global { |
| 78 | interface HTMLElementEventMap { |
Dhruv Srivastava | ee018e9 | 2022-08-31 11:37:46 +0200 | [diff] [blame] | 79 | 'comment-editing-changed': CustomEvent<CommentEditingChangedDetail>; |
Dhruv Srivastava | 463bb33 | 2022-08-31 13:00:49 +0200 | [diff] [blame] | 80 | 'comment-unresolved-changed': ValueChangedEvent<boolean>; |
| 81 | 'comment-text-changed': ValueChangedEvent<string>; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 82 | 'comment-anchor-tap': CustomEvent<CommentAnchorTapEventDetail>; |
| 83 | } |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 86 | export interface CommentAnchorTapEventDetail { |
| 87 | number: LineNumber; |
| 88 | side?: CommentSide; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 89 | } |
Dmitrii Filippov | 3f3c205 | 2020-09-22 16:51:18 +0200 | [diff] [blame] | 90 | |
Dhruv Srivastava | ee018e9 | 2022-08-31 11:37:46 +0200 | [diff] [blame] | 91 | export interface CommentEditingChangedDetail { |
| 92 | editing: boolean; |
| 93 | path: string; |
| 94 | } |
| 95 | |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 96 | @customElement('gr-comment') |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 97 | export class GrComment extends LitElement { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 98 | /** |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 99 | * Fired when the parent thread component should create a reply. |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 100 | * |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 101 | * @event reply-to-comment |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 102 | */ |
Kasper Nilsson | d43d2a7 | 2018-10-19 14:26:41 -0700 | [diff] [blame] | 103 | |
Dmitrii Filippov | 3fd2b10 | 2019-11-15 16:16:46 +0100 | [diff] [blame] | 104 | /** |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 105 | * Fired when the open fix preview action is triggered. |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 106 | * |
| 107 | * @event open-fix-preview |
Tao Zhou | 500437d | 2020-02-14 16:57:27 +0100 | [diff] [blame] | 108 | */ |
Tao Zhou | 500437d | 2020-02-14 16:57:27 +0100 | [diff] [blame] | 109 | |
| 110 | /** |
Tao Zhou | 31f3f10 | 2020-04-27 16:15:29 +0200 | [diff] [blame] | 111 | * Fired when editing status changed. |
| 112 | * |
| 113 | * @event comment-editing-changed |
| 114 | */ |
| 115 | |
| 116 | /** |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 117 | * Fired when the comment's timestamp is tapped. |
| 118 | * |
| 119 | * @event comment-anchor-tap |
| 120 | */ |
Andrew Bonventre | 2816526 | 2016-05-19 17:24:45 -0700 | [diff] [blame] | 121 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 122 | @query('#editTextarea') |
| 123 | textarea?: GrTextarea; |
Viktar Donich | 7ad2892 | 2016-05-23 15:24:05 -0700 | [diff] [blame] | 124 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 125 | @query('#container') |
| 126 | container?: HTMLElement; |
Dhruv Srivastava | 0287bf9 | 2020-09-11 16:56:38 +0200 | [diff] [blame] | 127 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 128 | @query('#resolvedCheckbox') |
| 129 | resolvedCheckbox?: HTMLInputElement; |
Kasper Nilsson | d43d2a7 | 2018-10-19 14:26:41 -0700 | [diff] [blame] | 130 | |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame^] | 131 | @query('#confirmDeleteModal') |
| 132 | confirmDeleteModal?: HTMLDialogElement; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 133 | |
| 134 | @property({type: Object}) |
| 135 | comment?: Comment; |
| 136 | |
| 137 | // TODO: Move this out of gr-comment. gr-comment should not have a comments |
| 138 | // property. This is only used for hasHumanReply at the moment. |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 139 | @property({type: Array}) |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 140 | comments?: Comment[]; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 141 | |
| 142 | /** |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 143 | * Initial collapsed state of the comment. |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 144 | */ |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 145 | @property({type: Boolean, attribute: 'initially-collapsed'}) |
| 146 | initiallyCollapsed?: boolean; |
| 147 | |
| 148 | /** |
Dhruv Srivastava | e75f6f7 | 2022-08-25 10:14:37 +0200 | [diff] [blame] | 149 | * Hide the header for patchset level comments used in GrReplyDialog. |
| 150 | */ |
| 151 | @property({type: Boolean, attribute: 'hide-header'}) |
| 152 | hideHeader = false; |
| 153 | |
| 154 | /** |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 155 | * This is the *current* (internal) collapsed state of the comment. Do not set |
| 156 | * from the outside. Use `initiallyCollapsed` instead. This is just a |
| 157 | * reflected property such that css rules can be based on it. |
| 158 | */ |
| 159 | @property({type: Boolean, reflect: true}) |
| 160 | collapsed?: boolean; |
| 161 | |
| 162 | @property({type: Boolean, attribute: 'robot-button-disabled'}) |
| 163 | robotButtonDisabled = false; |
| 164 | |
Dhruv Srivastava | f007abc | 2022-09-06 11:18:57 +0200 | [diff] [blame] | 165 | @property({type: String}) |
| 166 | messagePlaceholder?: string; |
| 167 | |
Chris Poucet | fae532b | 2022-02-16 13:42:20 +0100 | [diff] [blame] | 168 | /* private, but used in css rules */ |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 169 | @property({type: Boolean, reflect: true}) |
| 170 | saving = false; |
| 171 | |
Dhruv Srivastava | 4e5fd11 | 2022-08-25 12:01:22 +0200 | [diff] [blame] | 172 | // GrReplyDialog requires the patchset level comment to always remain |
| 173 | // editable. |
| 174 | @property({type: Boolean, attribute: 'permanent-editing-mode'}) |
| 175 | permanentEditingMode = false; |
| 176 | |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 177 | /** |
| 178 | * `saving` and `autoSaving` are separate and cannot be set at the same time. |
| 179 | * `saving` affects the UI state (disabled buttons, etc.) and eventually |
| 180 | * leaves editing mode, but `autoSaving` just happens in the background |
| 181 | * without the user noticing. |
| 182 | */ |
| 183 | @state() |
Ben Rohlfs | 607126f | 2021-12-07 08:21:52 +0100 | [diff] [blame] | 184 | autoSaving?: Promise<DraftInfo>; |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 185 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 186 | @state() |
| 187 | changeNum?: NumericChangeId; |
| 188 | |
| 189 | @state() |
| 190 | editing = false; |
| 191 | |
| 192 | @state() |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 193 | repoName?: RepoName; |
| 194 | |
| 195 | /* The 'dirty' state of the comment.message, which will be saved on demand. */ |
| 196 | @state() |
| 197 | messageText = ''; |
| 198 | |
| 199 | /* The 'dirty' state of !comment.unresolved, which will be saved on demand. */ |
| 200 | @state() |
| 201 | unresolved = true; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 202 | |
| 203 | @property({type: Boolean}) |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame^] | 204 | showConfirmDeleteModal = false; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 205 | |
| 206 | @property({type: Boolean}) |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 207 | unableToSave = false; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 208 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 209 | @property({type: Boolean, attribute: 'show-patchset'}) |
| 210 | showPatchset = false; |
Tao Zhou | 500437d | 2020-02-14 16:57:27 +0100 | [diff] [blame] | 211 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 212 | @property({type: Boolean, attribute: 'show-ported-comment'}) |
Dhruv Srivastava | 0287bf9 | 2020-09-11 16:56:38 +0200 | [diff] [blame] | 213 | showPortedComment = false; |
| 214 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 215 | @state() |
| 216 | account?: AccountDetailInfo; |
| 217 | |
| 218 | @state() |
| 219 | isAdmin = false; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 220 | |
Milutin Kristofic | 15bfb3e | 2022-08-16 20:01:33 +0200 | [diff] [blame] | 221 | @state() |
| 222 | isOwner = false; |
| 223 | |
Chris Poucet | c6e880b | 2021-11-15 19:57:06 +0100 | [diff] [blame] | 224 | private readonly restApiService = getAppContext().restApiService; |
Ben Rohlfs | 43935a4 | 2020-12-01 19:14:09 +0100 | [diff] [blame] | 225 | |
Chris Poucet | c6e880b | 2021-11-15 19:57:06 +0100 | [diff] [blame] | 226 | private readonly reporting = getAppContext().reportingService; |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 227 | |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 228 | private readonly flagsService = getAppContext().flagsService; |
| 229 | |
Chris Poucet | bf65b8f | 2022-01-18 21:18:12 +0000 | [diff] [blame] | 230 | private readonly getChangeModel = resolve(this, changeModelToken); |
Chris Poucet | 0142248 | 2021-11-30 19:43:28 +0100 | [diff] [blame] | 231 | |
Chris Poucet | bb0cf83 | 2022-10-24 12:32:10 +0200 | [diff] [blame] | 232 | private readonly getCommentsModel = resolve(this, commentsModelToken); |
Dhruv Srivastava | db2ab60 | 2021-06-24 15:20:29 +0200 | [diff] [blame] | 233 | |
Chris Poucet | bb0cf83 | 2022-10-24 12:32:10 +0200 | [diff] [blame] | 234 | private readonly getUserModel = resolve(this, userModelToken); |
Ben Rohlfs | f92d3b5 | 2021-03-10 23:13:03 +0100 | [diff] [blame] | 235 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 236 | private readonly shortcuts = new ShortcutController(this); |
Ben Rohlfs | f92d3b5 | 2021-03-10 23:13:03 +0100 | [diff] [blame] | 237 | |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 238 | /** |
| 239 | * This is triggered when the user types into the editing textarea. We then |
| 240 | * debounce it and call autoSave(). |
| 241 | */ |
| 242 | private autoSaveTrigger$ = new Subject(); |
| 243 | |
| 244 | /** |
| 245 | * Set to the content of DraftInfo when entering editing mode. |
| 246 | * Only used for "Cancel". |
| 247 | */ |
| 248 | private originalMessage = ''; |
| 249 | |
| 250 | /** |
| 251 | * Set to the content of DraftInfo when entering editing mode. |
| 252 | * Only used for "Cancel". |
| 253 | */ |
| 254 | private originalUnresolved = false; |
| 255 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 256 | constructor() { |
| 257 | super(); |
Dhruv Srivastava | e110a37 | 2022-09-08 12:18:33 +0200 | [diff] [blame] | 258 | // Allow the shortcuts to bubble up so that GrReplyDialog can respond to |
| 259 | // them as well. |
| 260 | this.shortcuts.addLocal({key: Key.ESC}, () => this.handleEsc(), { |
| 261 | preventDefault: false, |
| 262 | }); |
Dhruv Srivastava | f43eee7 | 2022-09-14 11:03:01 +0200 | [diff] [blame] | 263 | for (const modifier of [Modifier.CTRL_KEY, Modifier.META_KEY]) { |
| 264 | this.shortcuts.addLocal( |
| 265 | {key: Key.ENTER, modifiers: [modifier]}, |
| 266 | () => { |
| 267 | this.save(); |
| 268 | }, |
| 269 | {preventDefault: false} |
| 270 | ); |
| 271 | } |
| 272 | // For Ctrl+s add shorctut with preventDefault so that it does |
| 273 | // not bubble up to the browser |
| 274 | for (const modifier of [Modifier.CTRL_KEY, Modifier.META_KEY]) { |
| 275 | this.shortcuts.addLocal({key: 's', modifiers: [modifier]}, () => { |
| 276 | this.save(); |
| 277 | }); |
Ben Rohlfs | aadbdd1 | 2021-10-19 11:49:01 +0200 | [diff] [blame] | 278 | } |
Dhruv Srivastava | a320d37 | 2022-09-06 14:42:39 +0200 | [diff] [blame] | 279 | if (this.flagsService.isEnabled(KnownExperimentId.MENTION_USERS)) { |
| 280 | this.messagePlaceholder = 'Mention others with @'; |
| 281 | } |
Chris Poucet | 0b96141 | 2022-01-05 16:24:50 +0100 | [diff] [blame] | 282 | subscribe( |
| 283 | this, |
Chris Poucet | bb0cf83 | 2022-10-24 12:32:10 +0200 | [diff] [blame] | 284 | () => this.getUserModel().account$, |
Chris Poucet | 5ec77f0 | 2022-05-12 11:25:21 +0200 | [diff] [blame] | 285 | x => (this.account = x) |
| 286 | ); |
| 287 | subscribe( |
| 288 | this, |
Chris Poucet | bb0cf83 | 2022-10-24 12:32:10 +0200 | [diff] [blame] | 289 | () => this.getUserModel().isAdmin$, |
Chris Poucet | 5ec77f0 | 2022-05-12 11:25:21 +0200 | [diff] [blame] | 290 | x => (this.isAdmin = x) |
| 291 | ); |
| 292 | |
| 293 | subscribe( |
| 294 | this, |
| 295 | () => this.getChangeModel().repo$, |
| 296 | x => (this.repoName = x) |
| 297 | ); |
| 298 | subscribe( |
| 299 | this, |
| 300 | () => this.getChangeModel().changeNum$, |
Chris Poucet | bf65b8f | 2022-01-18 21:18:12 +0000 | [diff] [blame] | 301 | x => (this.changeNum = x) |
| 302 | ); |
| 303 | subscribe( |
| 304 | this, |
Milutin Kristofic | 15bfb3e | 2022-08-16 20:01:33 +0200 | [diff] [blame] | 305 | () => this.getChangeModel().isOwner$, |
| 306 | x => (this.isOwner = x) |
| 307 | ); |
| 308 | subscribe( |
| 309 | this, |
Chris Poucet | 5ec77f0 | 2022-05-12 11:25:21 +0200 | [diff] [blame] | 310 | () => |
| 311 | this.autoSaveTrigger$.pipe(debounceTime(AUTO_SAVE_DEBOUNCE_DELAY_MS)), |
Chris Poucet | bf65b8f | 2022-01-18 21:18:12 +0000 | [diff] [blame] | 312 | () => { |
| 313 | this.autoSave(); |
| 314 | } |
| 315 | ); |
Chris Poucet | 0b96141 | 2022-01-05 16:24:50 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Gerrit Code Review | 86b969c | 2021-08-19 14:33:41 +0000 | [diff] [blame] | 318 | override disconnectedCallback() { |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 319 | // Clean up emoji dropdown. |
| 320 | if (this.textarea) this.textarea.closeDropdown(); |
Ben Rohlfs | 19b6c72 | 2022-06-02 13:55:59 +0200 | [diff] [blame] | 321 | if (this.editing) { |
| 322 | this.reporting.reportInteraction( |
| 323 | Interaction.COMMENTS_AUTOCLOSE_EDITING_DISCONNECTED |
| 324 | ); |
| 325 | } |
Ben Rohlfs | 5f520da | 2021-03-10 14:58:43 +0100 | [diff] [blame] | 326 | super.disconnectedCallback(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 327 | } |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 328 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 329 | static override get styles() { |
| 330 | return [ |
| 331 | sharedStyles, |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame^] | 332 | modalStyles, |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 333 | css` |
| 334 | :host { |
| 335 | display: block; |
| 336 | font-family: var(--font-family); |
| 337 | padding: var(--spacing-m); |
| 338 | } |
| 339 | :host([collapsed]) { |
| 340 | padding: var(--spacing-s) var(--spacing-m); |
| 341 | } |
| 342 | :host([saving]) { |
| 343 | pointer-events: none; |
| 344 | } |
| 345 | :host([saving]) .actions, |
| 346 | :host([saving]) .robotActions, |
| 347 | :host([saving]) .date { |
| 348 | opacity: 0.5; |
| 349 | } |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 350 | .header { |
| 351 | align-items: center; |
| 352 | cursor: pointer; |
| 353 | display: flex; |
Dhruv Srivastava | d8f61e7 | 2022-09-16 07:34:34 +0000 | [diff] [blame] | 354 | padding-bottom: var(--spacing-m); |
| 355 | } |
| 356 | :host([collapsed]) .header { |
| 357 | padding-bottom: 0px; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 358 | } |
| 359 | .headerLeft > span { |
| 360 | font-weight: var(--font-weight-bold); |
| 361 | } |
| 362 | .headerMiddle { |
| 363 | color: var(--deemphasized-text-color); |
| 364 | flex: 1; |
| 365 | overflow: hidden; |
| 366 | } |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 367 | .draftTooltip { |
Ben Rohlfs | ba361a4 | 2022-09-01 12:12:45 +0200 | [diff] [blame] | 368 | font-weight: var(--font-weight-bold); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 369 | display: inline; |
| 370 | } |
Ben Rohlfs | ba361a4 | 2022-09-01 12:12:45 +0200 | [diff] [blame] | 371 | .draftTooltip gr-icon { |
| 372 | color: var(--info-foreground); |
| 373 | } |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 374 | .date { |
| 375 | justify-content: flex-end; |
| 376 | text-align: right; |
| 377 | white-space: nowrap; |
| 378 | } |
| 379 | span.date { |
| 380 | color: var(--deemphasized-text-color); |
| 381 | } |
| 382 | span.date:hover { |
| 383 | text-decoration: underline; |
| 384 | } |
| 385 | .actions, |
| 386 | .robotActions { |
| 387 | display: flex; |
| 388 | justify-content: flex-end; |
| 389 | padding-top: 0; |
| 390 | } |
| 391 | .robotActions { |
| 392 | /* Better than the negative margin would be to remove the gr-button |
| 393 | * padding, but then we would also need to fix the buttons that are |
| 394 | * inserted by plugins. :-/ */ |
| 395 | margin: 4px 0 -4px; |
| 396 | } |
| 397 | .action { |
| 398 | margin-left: var(--spacing-l); |
| 399 | } |
| 400 | .rightActions { |
| 401 | display: flex; |
| 402 | justify-content: flex-end; |
| 403 | } |
| 404 | .rightActions gr-button { |
| 405 | --gr-button-padding: 0 var(--spacing-s); |
| 406 | } |
| 407 | .editMessage { |
| 408 | display: block; |
Dhruv Srivastava | 694e937 | 2022-09-13 10:29:08 +0200 | [diff] [blame] | 409 | margin-bottom: var(--spacing-m); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 410 | width: 100%; |
| 411 | } |
| 412 | .show-hide { |
| 413 | margin-left: var(--spacing-s); |
| 414 | } |
| 415 | .robotId { |
| 416 | color: var(--deemphasized-text-color); |
| 417 | margin-bottom: var(--spacing-m); |
| 418 | } |
| 419 | .robotRun { |
| 420 | margin-left: var(--spacing-m); |
| 421 | } |
| 422 | .robotRunLink { |
| 423 | margin-left: var(--spacing-m); |
| 424 | } |
| 425 | /* just for a11y */ |
| 426 | input.show-hide { |
| 427 | display: none; |
| 428 | } |
| 429 | label.show-hide { |
| 430 | cursor: pointer; |
| 431 | display: block; |
| 432 | } |
Chris Poucet | 1c71386 | 2022-07-25 13:12:24 +0200 | [diff] [blame] | 433 | label.show-hide gr-icon { |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 434 | vertical-align: top; |
| 435 | } |
| 436 | :host([collapsed]) #container .body { |
| 437 | padding-top: 0; |
| 438 | } |
| 439 | #container .collapsedContent { |
| 440 | display: block; |
| 441 | overflow: hidden; |
| 442 | padding-left: var(--spacing-m); |
| 443 | text-overflow: ellipsis; |
| 444 | white-space: nowrap; |
| 445 | } |
| 446 | .resolve, |
| 447 | .unresolved { |
| 448 | align-items: center; |
| 449 | display: flex; |
| 450 | flex: 1; |
| 451 | margin: 0; |
| 452 | } |
| 453 | .resolve label { |
| 454 | color: var(--comment-text-color); |
| 455 | } |
| 456 | gr-dialog .main { |
| 457 | display: flex; |
| 458 | flex-direction: column; |
| 459 | width: 100%; |
| 460 | } |
| 461 | #deleteBtn { |
| 462 | --gr-button-text-color: var(--deemphasized-text-color); |
| 463 | --gr-button-padding: 0; |
| 464 | } |
| 465 | |
| 466 | /** Disable select for the caret and actions */ |
| 467 | .actions, |
| 468 | .show-hide { |
| 469 | -webkit-user-select: none; |
| 470 | -moz-user-select: none; |
| 471 | -ms-user-select: none; |
| 472 | user-select: none; |
| 473 | } |
| 474 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 475 | .pointer { |
| 476 | cursor: pointer; |
| 477 | } |
| 478 | .patchset-text { |
| 479 | color: var(--deemphasized-text-color); |
| 480 | margin-left: var(--spacing-s); |
| 481 | } |
| 482 | .headerLeft gr-account-label { |
| 483 | --account-max-length: 130px; |
| 484 | width: 150px; |
| 485 | } |
| 486 | .headerLeft gr-account-label::part(gr-account-label-text) { |
| 487 | font-weight: var(--font-weight-bold); |
| 488 | } |
| 489 | .draft gr-account-label { |
| 490 | width: unset; |
| 491 | } |
Frank Borden | 0c07884 | 2022-09-19 15:47:26 +0200 | [diff] [blame] | 492 | .draft gr-formatted-text.message { |
Frank Borden | 3b3a4c9 | 2022-09-28 14:14:00 +0200 | [diff] [blame] | 493 | display: block; |
Frank Borden | 0c07884 | 2022-09-19 15:47:26 +0200 | [diff] [blame] | 494 | margin-bottom: var(--spacing-m); |
| 495 | } |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 496 | .portedMessage { |
| 497 | margin: 0 var(--spacing-m); |
| 498 | } |
| 499 | .link-icon { |
Chris Poucet | c414204 | 2022-06-28 17:51:50 +0200 | [diff] [blame] | 500 | margin-left: var(--spacing-m); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 501 | cursor: pointer; |
| 502 | } |
| 503 | `, |
| 504 | ]; |
Dhruv Srivastava | cf70e79 | 2020-07-24 15:35:39 +0200 | [diff] [blame] | 505 | } |
| 506 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 507 | override render() { |
| 508 | if (isUnsaved(this.comment) && !this.editing) return; |
| 509 | const classes = {container: true, draft: isDraftOrUnsaved(this.comment)}; |
| 510 | return html` |
Ben Rohlfs | 7a16784 | 2022-09-29 21:55:50 +0200 | [diff] [blame] | 511 | <gr-endpoint-decorator name="comment"> |
| 512 | <gr-endpoint-param name="comment" .value=${this.comment}> |
| 513 | </gr-endpoint-param> |
| 514 | <gr-endpoint-param name="editing" .value=${this.editing}> |
| 515 | </gr-endpoint-param> |
Ben Rohlfs | 57c2c59 | 2022-10-25 12:49:11 +0200 | [diff] [blame] | 516 | <gr-endpoint-param name="message" .value=${this.messageText}> |
| 517 | </gr-endpoint-param> |
| 518 | <gr-endpoint-param |
| 519 | name="isDraft" |
| 520 | .value=${isDraftOrUnsaved(this.comment)} |
| 521 | > |
| 522 | </gr-endpoint-param> |
Ben Rohlfs | 7a16784 | 2022-09-29 21:55:50 +0200 | [diff] [blame] | 523 | <div id="container" class=${classMap(classes)}> |
| 524 | ${this.renderHeader()} |
| 525 | <div class="body"> |
| 526 | ${this.renderRobotAuthor()} ${this.renderEditingTextarea()} |
| 527 | ${this.renderCommentMessage()} |
| 528 | <gr-endpoint-slot name="above-actions"></gr-endpoint-slot> |
| 529 | ${this.renderHumanActions()} ${this.renderRobotActions()} |
| 530 | ${this.renderSuggestEditActions()} |
| 531 | </div> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 532 | </div> |
Ben Rohlfs | 7a16784 | 2022-09-29 21:55:50 +0200 | [diff] [blame] | 533 | </gr-endpoint-decorator> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 534 | ${this.renderConfirmDialog()} |
| 535 | `; |
| 536 | } |
| 537 | |
Dhruv Srivastava | e75f6f7 | 2022-08-25 10:14:37 +0200 | [diff] [blame] | 538 | private renderHeader() { |
| 539 | if (this.hideHeader) return nothing; |
| 540 | return html` |
| 541 | <div |
| 542 | class="header" |
| 543 | id="header" |
| 544 | @click=${() => (this.collapsed = !this.collapsed)} |
| 545 | > |
| 546 | <div class="headerLeft"> |
| 547 | ${this.renderAuthor()} ${this.renderPortedCommentMessage()} |
| 548 | ${this.renderDraftLabel()} |
| 549 | </div> |
| 550 | <div class="headerMiddle">${this.renderCollapsedContent()}</div> |
| 551 | ${this.renderRunDetails()} ${this.renderDeleteButton()} |
| 552 | ${this.renderPatchset()} ${this.renderDate()} ${this.renderToggle()} |
| 553 | </div> |
| 554 | `; |
| 555 | } |
| 556 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 557 | private renderAuthor() { |
Ben Rohlfs | ba361a4 | 2022-09-01 12:12:45 +0200 | [diff] [blame] | 558 | if (isDraftOrUnsaved(this.comment)) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 559 | if (isRobot(this.comment)) { |
| 560 | const id = this.comment.robot_id; |
| 561 | return html`<span class="robotName">${id}</span>`; |
| 562 | } |
| 563 | const classes = {draft: isDraftOrUnsaved(this.comment)}; |
| 564 | return html` |
| 565 | <gr-account-label |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 566 | .account=${this.comment?.author ?? this.account} |
| 567 | class=${classMap(classes)} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 568 | > |
| 569 | </gr-account-label> |
| 570 | `; |
| 571 | } |
| 572 | |
| 573 | private renderPortedCommentMessage() { |
| 574 | if (!this.showPortedComment) return; |
| 575 | if (!this.comment?.patch_set) return; |
| 576 | return html` |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 577 | <a href=${this.getUrlForComment()}> |
| 578 | <span class="portedMessage" @click=${this.handlePortedMessageClick}> |
Ben Rohlfs | 9579622 | 2021-12-01 16:39:42 +0100 | [diff] [blame] | 579 | From patchset ${this.comment?.patch_set} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 580 | </span> |
| 581 | </a> |
| 582 | `; |
| 583 | } |
| 584 | |
| 585 | private renderDraftLabel() { |
| 586 | if (!isDraftOrUnsaved(this.comment)) return; |
Ben Rohlfs | ba361a4 | 2022-09-01 12:12:45 +0200 | [diff] [blame] | 587 | let label = 'Draft'; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 588 | let tooltip = |
| 589 | 'This draft is only visible to you. ' + |
| 590 | "To publish drafts, click the 'Reply' or 'Start review' button " + |
| 591 | "at the top of the change or press the 'a' key."; |
| 592 | if (this.unableToSave) { |
| 593 | label += ' (Failed to save)'; |
| 594 | tooltip = 'Unable to save draft. Please try to save again.'; |
| 595 | } |
| 596 | return html` |
| 597 | <gr-tooltip-content |
| 598 | class="draftTooltip" |
| 599 | has-tooltip |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 600 | title=${tooltip} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 601 | max-width="20em" |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 602 | > |
Ben Rohlfs | ba361a4 | 2022-09-01 12:12:45 +0200 | [diff] [blame] | 603 | <gr-icon filled icon="rate_review"></gr-icon> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 604 | <span class="draftLabel">${label}</span> |
| 605 | </gr-tooltip-content> |
| 606 | `; |
| 607 | } |
| 608 | |
| 609 | private renderCollapsedContent() { |
| 610 | if (!this.collapsed) return; |
| 611 | return html` |
| 612 | <span class="collapsedContent">${this.comment?.message}</span> |
| 613 | `; |
| 614 | } |
| 615 | |
| 616 | private renderRunDetails() { |
| 617 | if (!isRobot(this.comment)) return; |
| 618 | if (!this.comment?.url || this.collapsed) return; |
| 619 | return html` |
| 620 | <div class="runIdMessage message"> |
| 621 | <div class="runIdInformation"> |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 622 | <a class="robotRunLink" href=${this.comment.url}> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 623 | <span class="robotRun link">Run Details</span> |
| 624 | </a> |
| 625 | </div> |
| 626 | </div> |
| 627 | `; |
| 628 | } |
| 629 | |
| 630 | /** |
| 631 | * Deleting a comment is an admin feature. It means more than just discarding |
| 632 | * a draft. It is an action applied to published comments. |
| 633 | */ |
| 634 | private renderDeleteButton() { |
| 635 | if ( |
| 636 | !this.isAdmin || |
| 637 | isDraftOrUnsaved(this.comment) || |
| 638 | isRobot(this.comment) |
| 639 | ) |
| 640 | return; |
| 641 | if (this.collapsed) return; |
| 642 | return html` |
| 643 | <gr-button |
| 644 | id="deleteBtn" |
| 645 | title="Delete Comment" |
| 646 | link |
| 647 | class="action delete" |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame^] | 648 | @click=${this.openDeleteCommentModal} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 649 | > |
Chris Poucet | 1c71386 | 2022-07-25 13:12:24 +0200 | [diff] [blame] | 650 | <gr-icon id="icon" icon="delete" filled></gr-icon> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 651 | </gr-button> |
| 652 | `; |
| 653 | } |
| 654 | |
| 655 | private renderPatchset() { |
| 656 | if (!this.showPatchset) return; |
| 657 | assertIsDefined(this.comment?.patch_set, 'comment.patch_set'); |
| 658 | return html` |
| 659 | <span class="patchset-text"> Patchset ${this.comment.patch_set}</span> |
| 660 | `; |
| 661 | } |
| 662 | |
| 663 | private renderDate() { |
| 664 | if (!this.comment?.updated || this.collapsed) return; |
| 665 | return html` |
| 666 | <span class="separator"></span> |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 667 | <span class="date" tabindex="0" @click=${this.handleAnchorClick}> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 668 | <gr-date-formatter |
| 669 | withTooltip |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 670 | .dateStr=${this.comment.updated} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 671 | ></gr-date-formatter> |
| 672 | </span> |
| 673 | `; |
| 674 | } |
| 675 | |
| 676 | private renderToggle() { |
Chris Poucet | b8c0639 | 2022-07-08 16:35:43 +0200 | [diff] [blame] | 677 | const icon = this.collapsed ? 'expand_more' : 'expand_less'; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 678 | const ariaLabel = this.collapsed ? 'Expand' : 'Collapse'; |
| 679 | return html` |
| 680 | <div class="show-hide" tabindex="0"> |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 681 | <label class="show-hide" aria-label=${ariaLabel}> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 682 | <input |
| 683 | type="checkbox" |
| 684 | class="show-hide" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 685 | ?checked=${this.collapsed} |
| 686 | @change=${() => (this.collapsed = !this.collapsed)} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 687 | /> |
Chris Poucet | 1c71386 | 2022-07-25 13:12:24 +0200 | [diff] [blame] | 688 | <gr-icon icon=${icon} id="icon"></gr-icon> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 689 | </label> |
| 690 | </div> |
| 691 | `; |
| 692 | } |
| 693 | |
| 694 | private renderRobotAuthor() { |
| 695 | if (!isRobot(this.comment) || this.collapsed) return; |
| 696 | return html`<div class="robotId">${this.comment.author?.name}</div>`; |
| 697 | } |
| 698 | |
| 699 | private renderEditingTextarea() { |
| 700 | if (!this.editing || this.collapsed) return; |
| 701 | return html` |
| 702 | <gr-textarea |
| 703 | id="editTextarea" |
| 704 | class="editMessage" |
| 705 | autocomplete="on" |
| 706 | code="" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 707 | ?disabled=${this.saving} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 708 | rows="4" |
Dhruv Srivastava | f007abc | 2022-09-06 11:18:57 +0200 | [diff] [blame] | 709 | .placeholder=${this.messagePlaceholder} |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 710 | text=${this.messageText} |
| 711 | @text-changed=${(e: ValueChangedEvent) => { |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 712 | // TODO: This is causing a re-render of <gr-comment> on every key |
| 713 | // press. Try to avoid always setting `this.messageText` or at least |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 714 | // debounce it. Most of the code can just inspect the current value |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 715 | // of the textare instead of needing a dedicated property. |
| 716 | this.messageText = e.detail.value; |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 717 | this.autoSaveTrigger$.next(); |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 718 | }} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 719 | ></gr-textarea> |
| 720 | `; |
| 721 | } |
| 722 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 723 | private renderCommentMessage() { |
| 724 | if (this.collapsed || this.editing) return; |
Frank Borden | f9a2999 | 2022-08-24 20:19:23 +0200 | [diff] [blame] | 725 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 726 | return html` |
| 727 | <!--The "message" class is needed to ensure selectability from |
| 728 | gr-diff-selection.--> |
| 729 | <gr-formatted-text |
| 730 | class="message" |
Frank Borden | abdd187 | 2022-09-26 12:55:59 +0200 | [diff] [blame] | 731 | .markdown=${true} |
| 732 | .content=${this.comment?.message ?? ''} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 733 | ></gr-formatted-text> |
| 734 | `; |
| 735 | } |
| 736 | |
| 737 | private renderCopyLinkIcon() { |
| 738 | // Only show the icon when the thread contains a published comment. |
| 739 | if (!this.comment?.in_reply_to && isDraftOrUnsaved(this.comment)) return; |
| 740 | return html` |
Chris Poucet | 1c71386 | 2022-07-25 13:12:24 +0200 | [diff] [blame] | 741 | <gr-icon |
| 742 | icon="link" |
| 743 | class="copy link-icon" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 744 | @click=${this.handleCopyLink} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 745 | title="Copy link to this comment" |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 746 | role="button" |
| 747 | tabindex="0" |
Chris Poucet | 1c71386 | 2022-07-25 13:12:24 +0200 | [diff] [blame] | 748 | ></gr-icon> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 749 | `; |
| 750 | } |
| 751 | |
| 752 | private renderHumanActions() { |
| 753 | if (!this.account || isRobot(this.comment)) return; |
| 754 | if (this.collapsed || !isDraftOrUnsaved(this.comment)) return; |
| 755 | return html` |
| 756 | <div class="actions"> |
| 757 | <div class="action resolve"> |
| 758 | <label> |
| 759 | <input |
| 760 | type="checkbox" |
| 761 | id="resolvedCheckbox" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 762 | ?checked=${!this.unresolved} |
| 763 | @change=${this.handleToggleResolved} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 764 | /> |
| 765 | Resolved |
| 766 | </label> |
| 767 | </div> |
| 768 | ${this.renderDraftActions()} |
| 769 | </div> |
| 770 | `; |
| 771 | } |
| 772 | |
| 773 | private renderDraftActions() { |
| 774 | if (!isDraftOrUnsaved(this.comment)) return; |
| 775 | return html` |
| 776 | <div class="rightActions"> |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 777 | ${this.autoSaving ? html`. ` : ''} |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 778 | ${this.renderDiscardButton()} ${this.renderSuggestEditButton()} |
| 779 | ${this.renderPreviewSuggestEditButton()} ${this.renderEditButton()} |
Chris Poucet | c414204 | 2022-06-28 17:51:50 +0200 | [diff] [blame] | 780 | ${this.renderCancelButton()} ${this.renderSaveButton()} |
| 781 | ${this.renderCopyLinkIcon()} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 782 | </div> |
| 783 | `; |
| 784 | } |
| 785 | |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 786 | private renderPreviewSuggestEditButton() { |
| 787 | if (!this.flagsService.isEnabled(KnownExperimentId.SUGGEST_EDIT)) { |
| 788 | return nothing; |
| 789 | } |
| 790 | assertIsDefined(this.comment, 'comment'); |
| 791 | if (!hasUserSuggestion(this.comment)) return nothing; |
| 792 | return html` |
| 793 | <gr-button |
| 794 | link |
| 795 | secondary |
| 796 | class="action show-fix" |
| 797 | ?disabled=${this.saving} |
| 798 | @click=${this.handleShowFix} |
| 799 | > |
| 800 | Preview Fix |
| 801 | </gr-button> |
| 802 | `; |
| 803 | } |
| 804 | |
| 805 | private renderSuggestEditButton() { |
| 806 | if (!this.flagsService.isEnabled(KnownExperimentId.SUGGEST_EDIT)) { |
| 807 | return nothing; |
| 808 | } |
Dhruv Srivastava | 66a1563 | 2022-09-06 11:57:34 +0200 | [diff] [blame] | 809 | if ( |
| 810 | this.permanentEditingMode || |
| 811 | this.comment?.path === SpecialFilePath.PATCHSET_LEVEL_COMMENTS |
| 812 | ) { |
| 813 | return nothing; |
| 814 | } |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 815 | assertIsDefined(this.comment, 'comment'); |
| 816 | if (hasUserSuggestion(this.comment)) return nothing; |
| 817 | // TODO(milutin): remove this check once suggesting on commit message is |
| 818 | // fixed. Currently diff line doesn't match commit message line, because |
| 819 | // of metadata in diff, which aren't in content api request. |
| 820 | if (this.comment.path === SpecialFilePath.COMMIT_MESSAGE) return nothing; |
Milutin Kristofic | 3ca39cc | 2022-10-21 10:10:20 +0200 | [diff] [blame] | 821 | // TODO(milutin): disable user suggestions for owners, after user study. |
| 822 | // if (this.isOwner) return nothing; |
Milutin Kristofic | 7dec89b | 2022-09-13 12:11:35 +0200 | [diff] [blame] | 823 | return html`<gr-button |
| 824 | link |
| 825 | class="action suggestEdit" |
| 826 | @click=${this.createSuggestEdit} |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 827 | >Suggest Fix</gr-button |
| 828 | >`; |
| 829 | } |
| 830 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 831 | private renderDiscardButton() { |
Dhruv Srivastava | 705eac9 | 2022-09-01 11:33:27 +0200 | [diff] [blame] | 832 | if (this.editing || this.permanentEditingMode) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 833 | return html`<gr-button |
| 834 | link |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 835 | ?disabled=${this.saving} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 836 | class="action discard" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 837 | @click=${this.discard} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 838 | >Discard</gr-button |
| 839 | >`; |
| 840 | } |
| 841 | |
| 842 | private renderEditButton() { |
| 843 | if (this.editing) return; |
| 844 | return html`<gr-button |
| 845 | link |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 846 | ?disabled=${this.saving} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 847 | class="action edit" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 848 | @click=${this.edit} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 849 | >Edit</gr-button |
| 850 | >`; |
| 851 | } |
| 852 | |
| 853 | private renderCancelButton() { |
Dhruv Srivastava | 705eac9 | 2022-09-01 11:33:27 +0200 | [diff] [blame] | 854 | if (!this.editing || this.permanentEditingMode) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 855 | return html` |
| 856 | <gr-button |
| 857 | link |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 858 | ?disabled=${this.saving} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 859 | class="action cancel" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 860 | @click=${this.cancel} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 861 | >Cancel</gr-button |
| 862 | > |
| 863 | `; |
| 864 | } |
| 865 | |
| 866 | private renderSaveButton() { |
| 867 | if (!this.editing && !this.unableToSave) return; |
| 868 | return html` |
| 869 | <gr-button |
| 870 | link |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 871 | ?disabled=${this.isSaveDisabled()} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 872 | class="action save" |
Dhruv Srivastava | 705eac9 | 2022-09-01 11:33:27 +0200 | [diff] [blame] | 873 | @click=${this.handleSaveButtonClicked} |
Dhruv Srivastava | 00831e7 | 2022-09-05 08:20:20 +0200 | [diff] [blame] | 874 | >${this.permanentEditingMode ? 'Preview' : 'Save'}</gr-button |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 875 | > |
| 876 | `; |
| 877 | } |
| 878 | |
| 879 | private renderRobotActions() { |
| 880 | if (!this.account || !isRobot(this.comment)) return; |
| 881 | const endpoint = html` |
| 882 | <gr-endpoint-decorator name="robot-comment-controls"> |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 883 | <gr-endpoint-param name="comment" .value=${this.comment}> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 884 | </gr-endpoint-param> |
| 885 | </gr-endpoint-decorator> |
| 886 | `; |
| 887 | return html` |
| 888 | <div class="robotActions"> |
| 889 | ${this.renderCopyLinkIcon()} ${endpoint} ${this.renderShowFixButton()} |
| 890 | ${this.renderPleaseFixButton()} |
| 891 | </div> |
| 892 | `; |
| 893 | } |
| 894 | |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 895 | private renderSuggestEditActions() { |
| 896 | if (!this.flagsService.isEnabled(KnownExperimentId.SUGGEST_EDIT)) { |
| 897 | return nothing; |
| 898 | } |
| 899 | if ( |
| 900 | !this.account || |
| 901 | isRobot(this.comment) || |
| 902 | isDraftOrUnsaved(this.comment) |
| 903 | ) { |
| 904 | return nothing; |
| 905 | } |
| 906 | return html` |
| 907 | <div class="robotActions">${this.renderPreviewSuggestEditButton()}</div> |
| 908 | `; |
| 909 | } |
| 910 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 911 | private renderShowFixButton() { |
| 912 | if (!(this.comment as RobotCommentInfo)?.fix_suggestions) return; |
| 913 | return html` |
| 914 | <gr-button |
| 915 | link |
| 916 | secondary |
| 917 | class="action show-fix" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 918 | ?disabled=${this.saving} |
| 919 | @click=${this.handleShowFix} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 920 | > |
| 921 | Show Fix |
| 922 | </gr-button> |
| 923 | `; |
| 924 | } |
| 925 | |
| 926 | private renderPleaseFixButton() { |
| 927 | if (this.hasHumanReply()) return; |
| 928 | return html` |
| 929 | <gr-button |
| 930 | link |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 931 | ?disabled=${this.robotButtonDisabled} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 932 | class="action fix" |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 933 | @click=${this.handlePleaseFix} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 934 | > |
| 935 | Please Fix |
| 936 | </gr-button> |
| 937 | `; |
| 938 | } |
| 939 | |
| 940 | private renderConfirmDialog() { |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame^] | 941 | if (!this.showConfirmDeleteModal) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 942 | return html` |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame^] | 943 | <dialog id="confirmDeleteModal" tabindex="-1"> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 944 | <gr-confirm-delete-comment-dialog |
| 945 | id="confirmDeleteComment" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 946 | @confirm=${this.handleConfirmDeleteComment} |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame^] | 947 | @cancel=${this.closeDeleteCommentModal} |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 948 | > |
| 949 | </gr-confirm-delete-comment-dialog> |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame^] | 950 | </dialog> |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 951 | `; |
| 952 | } |
| 953 | |
| 954 | private getUrlForComment() { |
| 955 | const comment = this.comment; |
| 956 | if (!comment || !this.changeNum || !this.repoName) return ''; |
Dhruv Srivastava | 0287bf9 | 2020-09-11 16:56:38 +0200 | [diff] [blame] | 957 | if (!comment.id) throw new Error('comment must have an id'); |
Ben Rohlfs | 731738b | 2022-09-15 15:55:33 +0200 | [diff] [blame] | 958 | return createDiffUrl({ |
| 959 | changeNum: this.changeNum, |
Ben Rohlfs | bfc688b | 2022-10-21 12:38:37 +0200 | [diff] [blame] | 960 | repo: this.repoName, |
Ben Rohlfs | 731738b | 2022-09-15 15:55:33 +0200 | [diff] [blame] | 961 | commentId: comment.id, |
| 962 | }); |
Dhruv Srivastava | 0287bf9 | 2020-09-11 16:56:38 +0200 | [diff] [blame] | 963 | } |
| 964 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 965 | private firstWillUpdateDone = false; |
| 966 | |
| 967 | firstWillUpdate() { |
| 968 | if (this.firstWillUpdateDone) return; |
| 969 | this.firstWillUpdateDone = true; |
Dhruv Srivastava | 4e5fd11 | 2022-08-25 12:01:22 +0200 | [diff] [blame] | 970 | if (this.permanentEditingMode) this.editing = true; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 971 | assertIsDefined(this.comment, 'comment'); |
| 972 | this.unresolved = this.comment.unresolved ?? true; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 973 | if (isUnsaved(this.comment)) this.editing = true; |
| 974 | if (isDraftOrUnsaved(this.comment)) { |
Ben Rohlfs | 19b6c72 | 2022-06-02 13:55:59 +0200 | [diff] [blame] | 975 | this.reporting.reportInteraction( |
| 976 | Interaction.COMMENTS_AUTOCLOSE_FIRST_UPDATE, |
| 977 | {editing: this.editing, unsaved: isUnsaved(this.comment)} |
| 978 | ); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 979 | this.collapsed = false; |
| 980 | } else { |
| 981 | this.collapsed = !!this.initiallyCollapsed; |
| 982 | } |
| 983 | } |
| 984 | |
Dhruv Srivastava | a49dffd | 2022-10-20 14:04:37 +0200 | [diff] [blame] | 985 | override updated(changed: PropertyValues) { |
| 986 | if (changed.has('editing')) { |
Dhruv Srivastava | e8b8639 | 2022-10-20 17:17:21 +0200 | [diff] [blame] | 987 | if (this.editing && !this.permanentEditingMode) { |
Dhruv Srivastava | a49dffd | 2022-10-20 14:04:37 +0200 | [diff] [blame] | 988 | whenVisible(this, () => this.textarea?.putCursorAtEnd()); |
| 989 | } |
| 990 | } |
| 991 | } |
| 992 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 993 | override willUpdate(changed: PropertyValues) { |
| 994 | this.firstWillUpdate(); |
| 995 | if (changed.has('editing')) { |
Chris Poucet | afd0f7c | 2022-10-04 10:04:43 +0000 | [diff] [blame] | 996 | this.onEditingChanged(); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 997 | } |
| 998 | if (changed.has('unresolved')) { |
| 999 | // The <gr-comment-thread> component wants to change its color based on |
| 1000 | // the (dirty) unresolved state, so let's notify it about changes. |
Dhruv Srivastava | 463bb33 | 2022-08-31 13:00:49 +0200 | [diff] [blame] | 1001 | fire(this, 'comment-unresolved-changed', {value: this.unresolved}); |
| 1002 | } |
| 1003 | if (changed.has('messageText')) { |
| 1004 | // GrReplyDialog updates it's state when text inside patchset level |
| 1005 | // comment changes. |
| 1006 | fire(this, 'comment-text-changed', {value: this.messageText}); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | private handlePortedMessageClick() { |
Ben Rohlfs | c1c6afd | 2021-02-18 13:13:22 +0100 | [diff] [blame] | 1011 | assertIsDefined(this.comment, 'comment'); |
Dhruv Srivastava | c8df760 | 2021-01-15 10:59:00 +0100 | [diff] [blame] | 1012 | this.reporting.reportInteraction('navigate-to-original-comment', { |
| 1013 | line: this.comment.line, |
| 1014 | range: this.comment.range, |
| 1015 | }); |
| 1016 | } |
| 1017 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1018 | private handleCopyLink() { |
| 1019 | fireEvent(this, 'copy-comment-link'); |
| 1020 | } |
| 1021 | |
| 1022 | /** Enter editing mode. */ |
| 1023 | private edit() { |
| 1024 | if (!isDraftOrUnsaved(this.comment)) { |
| 1025 | throw new Error('Cannot edit published comment.'); |
| 1026 | } |
| 1027 | if (this.editing) return; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1028 | this.editing = true; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | // TODO: Move this out of gr-comment. gr-comment should not have a comments |
| 1032 | // property. |
| 1033 | private hasHumanReply() { |
| 1034 | if (!this.comment || !this.comments) return false; |
| 1035 | return this.comments.some( |
| 1036 | 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] | 1037 | ); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | // private, but visible for testing |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1041 | async createFixPreview(): Promise<OpenFixPreviewEventDetail> { |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1042 | assertIsDefined(this.comment?.patch_set, 'comment.patch_set'); |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1043 | assertIsDefined(this.comment?.path, 'comment.path'); |
| 1044 | |
| 1045 | if (hasUserSuggestion(this.comment)) { |
| 1046 | const replacement = getUserSuggestion(this.comment); |
| 1047 | assert(!!replacement, 'malformed user suggestion'); |
| 1048 | const line = await this.getCommentedCode(); |
| 1049 | |
| 1050 | return { |
| 1051 | fixSuggestions: createUserFixSuggestion( |
| 1052 | this.comment, |
| 1053 | line, |
| 1054 | replacement |
| 1055 | ), |
| 1056 | patchNum: this.comment.patch_set, |
| 1057 | }; |
| 1058 | } |
| 1059 | if (isRobot(this.comment) && this.comment.fix_suggestions.length > 0) { |
| 1060 | const id = this.comment.robot_id; |
| 1061 | return { |
| 1062 | fixSuggestions: this.comment.fix_suggestions.map(s => { |
| 1063 | return { |
| 1064 | ...s, |
| 1065 | description: `${id ?? ''} - ${s.description ?? ''}`, |
| 1066 | }; |
| 1067 | }), |
| 1068 | patchNum: this.comment.patch_set, |
| 1069 | }; |
| 1070 | } |
| 1071 | throw new Error('unable to create preview fix event'); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1072 | } |
| 1073 | |
Chris Poucet | afd0f7c | 2022-10-04 10:04:43 +0000 | [diff] [blame] | 1074 | private onEditingChanged() { |
| 1075 | if (this.editing) { |
| 1076 | this.collapsed = false; |
| 1077 | this.messageText = this.comment?.message ?? ''; |
| 1078 | this.unresolved = this.comment?.unresolved ?? true; |
| 1079 | this.originalMessage = this.messageText; |
| 1080 | this.originalUnresolved = this.unresolved; |
Chris Poucet | afd0f7c | 2022-10-04 10:04:43 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | // Parent components such as the reply dialog might be interested in whether |
| 1084 | // come of their child components are in editing mode. |
| 1085 | fire(this, 'comment-editing-changed', { |
| 1086 | editing: this.editing, |
| 1087 | path: this.comment?.path ?? '', |
| 1088 | }); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1089 | } |
| 1090 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1091 | // private, but visible for testing |
| 1092 | isSaveDisabled() { |
| 1093 | assertIsDefined(this.comment, 'comment'); |
| 1094 | if (this.saving) return true; |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1095 | return !this.messageText?.trimEnd(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1096 | } |
| 1097 | |
Dhruv Srivastava | e8b8639 | 2022-10-20 17:17:21 +0200 | [diff] [blame] | 1098 | override focus() { |
| 1099 | this.textarea?.focus(); |
| 1100 | } |
| 1101 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1102 | private handleEsc() { |
| 1103 | // vim users don't like ESC to cancel/discard, so only do this when the |
| 1104 | // comment text is empty. |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1105 | if (!this.messageText?.trimEnd()) this.cancel(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1106 | } |
| 1107 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1108 | private handleAnchorClick() { |
| 1109 | assertIsDefined(this.comment, 'comment'); |
| 1110 | fire(this, 'comment-anchor-tap', { |
| 1111 | number: this.comment.line || FILE, |
| 1112 | side: this.comment?.side, |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 1113 | }); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
Dhruv Srivastava | 15950b45 | 2022-09-12 10:56:53 +0200 | [diff] [blame] | 1116 | private async handleSaveButtonClicked() { |
| 1117 | await this.save(); |
| 1118 | if (this.permanentEditingMode) { |
| 1119 | this.editing = !this.editing; |
| 1120 | } |
Dhruv Srivastava | 705eac9 | 2022-09-01 11:33:27 +0200 | [diff] [blame] | 1121 | } |
| 1122 | |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1123 | private handlePleaseFix() { |
| 1124 | const message = this.comment?.message; |
| 1125 | assert(!!message, 'empty message'); |
| 1126 | const quoted = message.replace(NEWLINE_PATTERN, '\n> '); |
| 1127 | const eventDetail: ReplyToCommentEventDetail = { |
| 1128 | content: `> ${quoted}\n\nPlease fix.`, |
| 1129 | userWantsToEdit: false, |
| 1130 | unresolved: true, |
| 1131 | }; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1132 | // Handled by <gr-comment-thread>. |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1133 | fire(this, 'reply-to-comment', eventDetail); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1134 | } |
| 1135 | |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1136 | private async handleShowFix() { |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1137 | // Handled top-level in the diff and change view components. |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1138 | fire(this, 'open-fix-preview', await this.createFixPreview()); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1139 | } |
| 1140 | |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 1141 | async createSuggestEdit() { |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1142 | const line = await this.getCommentedCode(); |
| 1143 | this.messageText += `${USER_SUGGESTION_START_PATTERN}${line}${'\n```'}`; |
| 1144 | } |
| 1145 | |
| 1146 | async getCommentedCode() { |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 1147 | assertIsDefined(this.comment, 'comment'); |
| 1148 | assertIsDefined(this.changeNum, 'changeNum'); |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1149 | // TODO(milutin): Show a toast while the file is being loaded. |
| 1150 | // TODO(milutin): This should be moved into a service/model. |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 1151 | const file = await this.restApiService.getFileContent( |
| 1152 | this.changeNum, |
| 1153 | this.comment.path!, |
| 1154 | this.comment.patch_set! |
| 1155 | ); |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1156 | assert( |
| 1157 | !!file && isBase64FileContent(file) && !!file.content, |
| 1158 | 'file content for comment not found' |
| 1159 | ); |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 1160 | const line = getContentInCommentRange(file.content, this.comment); |
Ben Rohlfs | 2384388 | 2022-08-04 18:06:27 +0200 | [diff] [blame] | 1161 | assert(!!line, 'file content for comment not found'); |
| 1162 | return line; |
Milutin Kristofic | 1d21967 | 2022-06-21 14:57:25 +0200 | [diff] [blame] | 1163 | } |
| 1164 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1165 | // private, but visible for testing |
| 1166 | cancel() { |
| 1167 | assertIsDefined(this.comment, 'comment'); |
| 1168 | if (!isDraftOrUnsaved(this.comment)) { |
| 1169 | throw new Error('only unsaved and draft comments are editable'); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1170 | } |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1171 | this.messageText = this.originalMessage; |
| 1172 | this.unresolved = this.originalUnresolved; |
| 1173 | this.save(); |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1174 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1175 | |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1176 | async autoSave() { |
| 1177 | if (this.saving || this.autoSaving) return; |
| 1178 | if (!this.editing || !this.comment) return; |
| 1179 | if (!isDraftOrUnsaved(this.comment)) return; |
| 1180 | const messageToSave = this.messageText.trimEnd(); |
| 1181 | if (messageToSave === '') return; |
| 1182 | if (messageToSave === this.comment.message) return; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1183 | |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1184 | try { |
| 1185 | this.autoSaving = this.rawSave(messageToSave, {showToast: false}); |
| 1186 | await this.autoSaving; |
| 1187 | } finally { |
| 1188 | this.autoSaving = undefined; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1189 | } |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | async discard() { |
| 1193 | this.messageText = ''; |
| 1194 | await this.save(); |
| 1195 | } |
| 1196 | |
| 1197 | async save() { |
| 1198 | if (!isDraftOrUnsaved(this.comment)) throw new Error('not a draft'); |
Dhruv Srivastava | fb8e34f | 2022-10-18 10:12:13 +0200 | [diff] [blame] | 1199 | // If it's an unsaved comment then it does not have a draftID yet which |
| 1200 | // means sending another save() request will create a new draft |
| 1201 | if (isUnsaved(this.comment) && this.saving) return; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1202 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1203 | try { |
| 1204 | this.saving = true; |
| 1205 | this.unableToSave = false; |
Ben Rohlfs | 607126f | 2021-12-07 08:21:52 +0100 | [diff] [blame] | 1206 | if (this.autoSaving) { |
| 1207 | this.comment = await this.autoSaving; |
| 1208 | } |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1209 | // Depending on whether `messageToSave` is empty we treat this either as |
| 1210 | // a discard or a save action. |
| 1211 | const messageToSave = this.messageText.trimEnd(); |
| 1212 | if (messageToSave === '') { |
| 1213 | // Don't try to discard UnsavedInfo. Nothing to do then. |
| 1214 | if (this.comment.id) { |
Chris Poucet | 6c6b54f | 2021-12-09 02:53:13 +0100 | [diff] [blame] | 1215 | await this.getCommentsModel().discardDraft(this.comment.id); |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1216 | } |
| 1217 | } else { |
| 1218 | // No need to make a backend call when nothing has changed. |
| 1219 | if ( |
| 1220 | messageToSave !== this.comment?.message || |
| 1221 | this.unresolved !== this.comment.unresolved |
| 1222 | ) { |
| 1223 | await this.rawSave(messageToSave, {showToast: true}); |
| 1224 | } |
| 1225 | } |
Ben Rohlfs | 19b6c72 | 2022-06-02 13:55:59 +0200 | [diff] [blame] | 1226 | this.reporting.reportInteraction( |
| 1227 | Interaction.COMMENTS_AUTOCLOSE_EDITING_FALSE_SAVE |
| 1228 | ); |
Dhruv Srivastava | 4e5fd11 | 2022-08-25 12:01:22 +0200 | [diff] [blame] | 1229 | if (!this.permanentEditingMode) { |
| 1230 | this.editing = false; |
| 1231 | } |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1232 | } catch (e) { |
| 1233 | this.unableToSave = true; |
| 1234 | throw e; |
| 1235 | } finally { |
| 1236 | this.saving = false; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1237 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1238 | } |
| 1239 | |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1240 | /** For sharing between save() and autoSave(). */ |
| 1241 | private rawSave(message: string, options: {showToast: boolean}) { |
| 1242 | if (!isDraftOrUnsaved(this.comment)) throw new Error('not a draft'); |
Chris Poucet | 6c6b54f | 2021-12-09 02:53:13 +0100 | [diff] [blame] | 1243 | return this.getCommentsModel().saveDraft( |
Ben Rohlfs | 2e23755 | 2021-11-24 10:34:28 +0100 | [diff] [blame] | 1244 | { |
| 1245 | ...this.comment, |
| 1246 | message, |
| 1247 | unresolved: this.unresolved, |
| 1248 | }, |
| 1249 | options.showToast |
| 1250 | ); |
| 1251 | } |
| 1252 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1253 | private handleToggleResolved() { |
| 1254 | this.unresolved = !this.unresolved; |
Dhruv Srivastava | 73f9edc | 2021-12-02 11:23:27 +0100 | [diff] [blame] | 1255 | if (!this.editing) { |
| 1256 | // messageText is only assigned a value if the comment reaches editing |
| 1257 | // state, however it is possible that the user toggles the resolved state |
| 1258 | // without editing the comment in which case we assign the correct value |
| 1259 | // to messageText here |
| 1260 | this.messageText = this.comment?.message ?? ''; |
| 1261 | this.save(); |
| 1262 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1263 | } |
| 1264 | |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame^] | 1265 | private async openDeleteCommentModal() { |
| 1266 | this.showConfirmDeleteModal = true; |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1267 | await this.updateComplete; |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame^] | 1268 | await this.confirmDeleteModal?.showModal(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1269 | } |
| 1270 | |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame^] | 1271 | private closeDeleteCommentModal() { |
| 1272 | this.showConfirmDeleteModal = false; |
| 1273 | this.confirmDeleteModal?.remove(); |
| 1274 | this.confirmDeleteModal?.close(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1275 | } |
| 1276 | |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1277 | /** |
| 1278 | * Deleting a *published* comment is an admin feature. It means more than just |
| 1279 | * discarding a draft. |
| 1280 | * |
| 1281 | * TODO: Also move this into the comments-service. |
| 1282 | * TODO: Figure out a good reloading strategy when deleting was successful. |
| 1283 | * `this.comment = newComment` does not seem sufficient. |
| 1284 | */ |
| 1285 | // private, but visible for testing |
| 1286 | handleConfirmDeleteComment() { |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame^] | 1287 | const dialog = this.confirmDeleteModal?.querySelector( |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 1288 | '#confirmDeleteComment' |
| 1289 | ) as GrConfirmDeleteCommentDialog | null; |
| 1290 | if (!dialog || !dialog.message) { |
| 1291 | throw new Error('missing confirm delete dialog'); |
| 1292 | } |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1293 | assertIsDefined(this.changeNum, 'changeNum'); |
| 1294 | assertIsDefined(this.comment, 'comment'); |
| 1295 | assertIsDefined(this.comment.patch_set, 'comment.patch_set'); |
| 1296 | if (isDraftOrUnsaved(this.comment)) { |
| 1297 | throw new Error('Admin deletion is only for published comments.'); |
Milutin Kristofic | a6af5aa | 2020-09-23 09:08:14 +0200 | [diff] [blame] | 1298 | } |
Ben Rohlfs | 43935a4 | 2020-12-01 19:14:09 +0100 | [diff] [blame] | 1299 | this.restApiService |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 1300 | .deleteComment( |
| 1301 | this.changeNum, |
Ben Rohlfs | 05750b9 | 2021-10-29 08:23:08 +0200 | [diff] [blame] | 1302 | this.comment.patch_set, |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 1303 | this.comment.id, |
| 1304 | dialog.message |
| 1305 | ) |
| 1306 | .then(newComment => { |
Dhruv Srivastava | 4063d26 | 2022-11-09 18:46:29 +0530 | [diff] [blame^] | 1307 | this.closeDeleteCommentModal(); |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 1308 | this.comment = newComment; |
| 1309 | }); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 1310 | } |
| 1311 | } |
| 1312 | |
Milutin Kristofic | afae005 | 2020-09-17 10:38:08 +0200 | [diff] [blame] | 1313 | declare global { |
| 1314 | interface HTMLElementTagNameMap { |
| 1315 | 'gr-comment': GrComment; |
| 1316 | } |
| 1317 | } |