blob: 8adf67672860d8c091b643a347d1141e0a2e51ba [file] [log] [blame]
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +01001import '@material/web/button/outlined-button.js';
2
Adrià Vilanova Martínez5bde12d2023-03-12 00:57:55 +01003import {msg, str} from '@lit/localize';
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +01004import {css, html} from 'lit';
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +01005
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +01006import {I18nLitElement} from '../../../../common/litI18nUtils.js';
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +01007import {SHARED_MD3_STYLES} from '../../../../common/styles/md3.js';
Adrià Vilanova Martínez72a4ea42024-02-09 22:35:32 +01008import {openReplyEditor} from '../../utils/common.js';
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +01009import {getExtraInfoNodes} from '../flattenThreads.js';
10
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +010011export default class TwptFlattenThreadReplyButton extends I18nLitElement {
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010012 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ínezcdbe6442024-03-15 22:01:13 +010022
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ínez115e3d82023-01-10 21:50:06 +010038 }
39 `,
40 ];
41
42 constructor() {
43 super();
44 this.extraInfo = {};
Adrià Vilanova Martínez72a4ea42024-02-09 22:35:32 +010045 this.addEventListener('twpt-click', this.openReplyEditor);
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010046 }
47
48 render() {
49 return html`
50 <md-outlined-button
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010051 @click=${this.openReplyEditor}>
Renovate botaa5fb8e2024-02-25 18:10:09 +000052 ${msg('Reply', {
53 desc: 'Button which is used to open the reply box.',
54 })}
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010055 </md-outlined-button>
56 `;
57 }
58
59 #defaultReply(messagePayload) {
60 const quoteHeader = document.createElement('div');
61 const italics = document.createElement('i');
Adrià Vilanova Martínez5bde12d2023-03-12 00:57:55 +010062 const authorName = this.extraInfo?.authorName;
63 italics.textContent = msg(str`${authorName} said:`);
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010064 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ínez72a4ea42024-02-09 22:35:32 +010089 openReplyEditor(parentId).then(editor => {
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010090 const payload =
91 editor?.querySelector('.scTailwindSharedRichtexteditoreditor');
92
93 payload.prepend(...this.#defaultReply(messagePayload.innerHTML));
Adrià Vilanova Martínezdb38d7f2023-01-10 22:40:58 +010094 payload.scrollTop = payload.scrollHeight;
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010095 });
96 }
97}
98window.customElements.define(
99 'twpt-flatten-thread-reply-button', TwptFlattenThreadReplyButton);