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