blob: a8aeabad26a9c28a48d2f349175c919e428b7868 [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 +01005import {waitFor} from 'poll-until-promise';
6
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +01007import {I18nLitElement} from '../../../../common/litI18nUtils.js';
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +01008import {SHARED_MD3_STYLES} from '../../../../common/styles/md3.js';
9import {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;
22 }
23 `,
24 ];
25
26 constructor() {
27 super();
28 this.extraInfo = {};
29 }
30
31 render() {
32 return html`
33 <md-outlined-button
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +010034 label="${msg('Reply', {
Adrià Vilanova Martínez5bde12d2023-03-12 00:57:55 +010035 desc: 'Button which is used to open the reply box.',
36 })}"
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010037 @click=${this.openReplyEditor}>
38 </md-outlined-button>
39 `;
40 }
41
42 #defaultReply(messagePayload) {
43 const quoteHeader = document.createElement('div');
44 const italics = document.createElement('i');
Adrià Vilanova Martínez5bde12d2023-03-12 00:57:55 +010045 const authorName = this.extraInfo?.authorName;
46 italics.textContent = msg(str`${authorName} said:`);
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010047 quoteHeader.append(italics);
48
49 const quote = document.createElement('blockquote');
50 quote.innerHTML = messagePayload;
51 getExtraInfoNodes(quote)?.forEach?.(node => {
52 node.parentNode.removeChild(node);
53 });
54
55 const br1 = document.createElement('br');
56 const br2 = document.createElement('br');
57
58 return [quoteHeader, quote, br1, br2];
59 }
60
61 openReplyEditor() {
62 const messageId = this.extraInfo?.id;
63 const messagePayload = document.querySelector(
64 '[data-twpt-message-id="' + messageId +
65 '"] .scTailwindThreadPostcontentroot html-blob');
66 if (!messagePayload) {
67 console.error('[flattenThreads] Payload not found.');
68 return;
69 }
70
71 const parentId = this.extraInfo?.parentId ?? this.extraInfo?.id;
72 const parentNodeReply =
73 document.querySelector('[data-twpt-message-id="' + parentId + '"]')
74 ?.closest?.('sc-tailwind-thread-message-message-card');
75 const parentNodeReplyButton = parentNodeReply?.querySelector?.(
76 '.scTailwindThreadMessageMessagecardadd-comment button');
77 if (!parentNodeReplyButton) {
78 // This is not critical: the reply button might already have been clicked
79 // (so it no longer exists), or the thread might be locked so replying is
80 // disabled and the button does'nt exist.
81 console.debug('[flattenThreads] Reply button not found.');
82 return;
83 }
84
85 // Click the reply button.
86 parentNodeReplyButton.click();
87
88 // Fill in the default reply text (it includes a quote of the message the
89 // user wishes to reply to).
90 waitFor(() => {
91 const editor =
92 parentNodeReply?.querySelector('sc-tailwind-thread-reply-editor');
93 if (editor) return Promise.resolve(editor);
94 return Promise.reject(new Error('Editor not found.'));
95 }, {interval: 75, timeout: 10 * 1000}).then(editor => {
96 const payload =
97 editor?.querySelector('.scTailwindSharedRichtexteditoreditor');
98
99 payload.prepend(...this.#defaultReply(messagePayload.innerHTML));
Adrià Vilanova Martínezdb38d7f2023-01-10 22:40:58 +0100100 payload.scrollTop = payload.scrollHeight;
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100101 });
102 }
103}
104window.customElements.define(
105 'twpt-flatten-thread-reply-button', TwptFlattenThreadReplyButton);