Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 1 | import '@material/web/button/outlined-button.js'; |
| 2 | |
Adrià Vilanova Martínez | 5bde12d | 2023-03-12 00:57:55 +0100 | [diff] [blame] | 3 | import {msg, str} from '@lit/localize'; |
Adrià Vilanova Martínez | a03d03a | 2023-03-11 23:30:07 +0100 | [diff] [blame] | 4 | import {css, html} from 'lit'; |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 5 | |
Adrià Vilanova Martínez | a03d03a | 2023-03-11 23:30:07 +0100 | [diff] [blame] | 6 | import {I18nLitElement} from '../../../../common/litI18nUtils.js'; |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 7 | import {SHARED_MD3_STYLES} from '../../../../common/styles/md3.js'; |
Adrià Vilanova Martínez | 72a4ea4 | 2024-02-09 22:35:32 +0100 | [diff] [blame] | 8 | import {openReplyEditor} from '../../utils/common.js'; |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 9 | import {getExtraInfoNodes} from '../flattenThreads.js'; |
| 10 | |
Adrià Vilanova Martínez | a03d03a | 2023-03-11 23:30:07 +0100 | [diff] [blame] | 11 | export default class TwptFlattenThreadReplyButton extends I18nLitElement { |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 12 | static properties = { |
| 13 | extraInfo: {type: Object}, |
| 14 | }; |
| 15 | |
| 16 | static styles = [ |
| 17 | SHARED_MD3_STYLES, |
| 18 | css` |
| 19 | md-outlined-button { |
| 20 | --md-outlined-button-container-shape: 0.25rem; |
| 21 | --md-outlined-button-container-height: 38px; |
Adrià Vilanova Martínez | cdbe644 | 2024-03-15 22:01:13 +0100 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * We change the color because otherwise it would have a very similar |
| 25 | * color to the "Recommend" button. |
| 26 | */ |
| 27 | --md-outlined-button-label-text-color: var(--reply-button-color); |
| 28 | --md-outlined-button-focus-label-text-color: var(--reply-button-color); |
| 29 | --md-outlined-button-hover-label-text-color: var(--reply-button-color); |
| 30 | --md-outlined-button-hover-state-layer-color: var(--reply-button-color); |
| 31 | --md-outlined-button-label-text-color: var(--reply-button-color); |
| 32 | --md-outlined-button-pressed-label-text-color: var(--reply-button-color); |
| 33 | --md-outlined-button-pressed-state-layer-color: var(--reply-button-color); |
| 34 | --md-outlined-button-focus-icon-color: var(--reply-button-color); |
| 35 | --md-outlined-button-hover-icon-color: var(--reply-button-color); |
| 36 | --md-outlined-button-icon-color: var(--reply-button-color); |
| 37 | --md-outlined-button-pressed-icon-color: var(--reply-button-color); |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 38 | } |
| 39 | `, |
| 40 | ]; |
| 41 | |
| 42 | constructor() { |
| 43 | super(); |
| 44 | this.extraInfo = {}; |
Adrià Vilanova Martínez | 72a4ea4 | 2024-02-09 22:35:32 +0100 | [diff] [blame] | 45 | this.addEventListener('twpt-click', this.openReplyEditor); |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | render() { |
| 49 | return html` |
| 50 | <md-outlined-button |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 51 | @click=${this.openReplyEditor}> |
Renovate bot | aa5fb8e | 2024-02-25 18:10:09 +0000 | [diff] [blame] | 52 | ${msg('Reply', { |
| 53 | desc: 'Button which is used to open the reply box.', |
| 54 | })} |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 55 | </md-outlined-button> |
| 56 | `; |
| 57 | } |
| 58 | |
| 59 | #defaultReply(messagePayload) { |
| 60 | const quoteHeader = document.createElement('div'); |
| 61 | const italics = document.createElement('i'); |
Adrià Vilanova Martínez | 5bde12d | 2023-03-12 00:57:55 +0100 | [diff] [blame] | 62 | const authorName = this.extraInfo?.authorName; |
| 63 | italics.textContent = msg(str`${authorName} said:`); |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 64 | quoteHeader.append(italics); |
| 65 | |
| 66 | const quote = document.createElement('blockquote'); |
| 67 | quote.innerHTML = messagePayload; |
| 68 | getExtraInfoNodes(quote)?.forEach?.(node => { |
| 69 | node.parentNode.removeChild(node); |
| 70 | }); |
| 71 | |
| 72 | const br1 = document.createElement('br'); |
| 73 | const br2 = document.createElement('br'); |
| 74 | |
| 75 | return [quoteHeader, quote, br1, br2]; |
| 76 | } |
| 77 | |
| 78 | openReplyEditor() { |
| 79 | const messageId = this.extraInfo?.id; |
| 80 | const messagePayload = document.querySelector( |
| 81 | '[data-twpt-message-id="' + messageId + |
| 82 | '"] .scTailwindThreadPostcontentroot html-blob'); |
| 83 | if (!messagePayload) { |
| 84 | console.error('[flattenThreads] Payload not found.'); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | const parentId = this.extraInfo?.parentId ?? this.extraInfo?.id; |
Adrià Vilanova Martínez | 72a4ea4 | 2024-02-09 22:35:32 +0100 | [diff] [blame] | 89 | openReplyEditor(parentId).then(editor => { |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 90 | const payload = |
| 91 | editor?.querySelector('.scTailwindSharedRichtexteditoreditor'); |
| 92 | |
| 93 | payload.prepend(...this.#defaultReply(messagePayload.innerHTML)); |
Adrià Vilanova Martínez | db38d7f | 2023-01-10 22:40:58 +0100 | [diff] [blame] | 94 | payload.scrollTop = payload.scrollHeight; |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 95 | }); |
| 96 | } |
| 97 | } |
| 98 | window.customElements.define( |
| 99 | 'twpt-flatten-thread-reply-button', TwptFlattenThreadReplyButton); |