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; |
| 22 | } |
| 23 | `, |
| 24 | ]; |
| 25 | |
| 26 | constructor() { |
| 27 | super(); |
| 28 | this.extraInfo = {}; |
Adrià Vilanova Martínez | 72a4ea4 | 2024-02-09 22:35:32 +0100 | [diff] [blame] | 29 | this.addEventListener('twpt-click', this.openReplyEditor); |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | render() { |
| 33 | return html` |
| 34 | <md-outlined-button |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 35 | @click=${this.openReplyEditor}> |
Renovate bot | aa5fb8e | 2024-02-25 18:10:09 +0000 | [diff] [blame] | 36 | ${msg('Reply', { |
| 37 | desc: 'Button which is used to open the reply box.', |
| 38 | })} |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 39 | </md-outlined-button> |
| 40 | `; |
| 41 | } |
| 42 | |
| 43 | #defaultReply(messagePayload) { |
| 44 | const quoteHeader = document.createElement('div'); |
| 45 | const italics = document.createElement('i'); |
Adrià Vilanova Martínez | 5bde12d | 2023-03-12 00:57:55 +0100 | [diff] [blame] | 46 | const authorName = this.extraInfo?.authorName; |
| 47 | italics.textContent = msg(str`${authorName} said:`); |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 48 | quoteHeader.append(italics); |
| 49 | |
| 50 | const quote = document.createElement('blockquote'); |
| 51 | quote.innerHTML = messagePayload; |
| 52 | getExtraInfoNodes(quote)?.forEach?.(node => { |
| 53 | node.parentNode.removeChild(node); |
| 54 | }); |
| 55 | |
| 56 | const br1 = document.createElement('br'); |
| 57 | const br2 = document.createElement('br'); |
| 58 | |
| 59 | return [quoteHeader, quote, br1, br2]; |
| 60 | } |
| 61 | |
| 62 | openReplyEditor() { |
| 63 | const messageId = this.extraInfo?.id; |
| 64 | const messagePayload = document.querySelector( |
| 65 | '[data-twpt-message-id="' + messageId + |
| 66 | '"] .scTailwindThreadPostcontentroot html-blob'); |
| 67 | if (!messagePayload) { |
| 68 | console.error('[flattenThreads] Payload not found.'); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | const parentId = this.extraInfo?.parentId ?? this.extraInfo?.id; |
Adrià Vilanova Martínez | 72a4ea4 | 2024-02-09 22:35:32 +0100 | [diff] [blame] | 73 | openReplyEditor(parentId).then(editor => { |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 74 | const payload = |
| 75 | editor?.querySelector('.scTailwindSharedRichtexteditoreditor'); |
| 76 | |
| 77 | payload.prepend(...this.#defaultReply(messagePayload.innerHTML)); |
Adrià Vilanova Martínez | db38d7f | 2023-01-10 22:40:58 +0100 | [diff] [blame] | 78 | payload.scrollTop = payload.scrollHeight; |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 79 | }); |
| 80 | } |
| 81 | } |
| 82 | window.customElements.define( |
| 83 | 'twpt-flatten-thread-reply-button', TwptFlattenThreadReplyButton); |