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