blob: 557a53366724dda2f53ea28ed11a946c6e94d956 [file] [log] [blame]
Adrià Vilanova Martínez3f26d432023-02-04 20:49:25 +01001export const kAdditionalInfoClass = 'ck-indent-9996300035194';
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +01002
3export const kReplyPayloadSelector =
4 '.scTailwindThreadMessageMessagecardcontent:not(.scTailwindThreadMessageMessagecardpromoted) .scTailwindThreadPostcontentroot html-blob';
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +01005export const kReplyActionButtonsSelector =
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +01006 '.scTailwindThreadMessageMessagecardcontent:not(.scTailwindThreadMessageMessagecardpromoted) sc-tailwind-thread-message-message-actions';
Adrià Vilanova Martínez3f26d432023-02-04 20:49:25 +01007export const kAdditionalInfoSelector = '.ck-indent-9996300035194';
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +01008export const kMatchingSelectors = [
9 kReplyPayloadSelector,
10 kReplyActionButtonsSelector,
Adrià Vilanova Martínez3f26d432023-02-04 20:49:25 +010011 kAdditionalInfoSelector,
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010012];
13
14export function getExtraInfoNodes(node) {
Adrià Vilanova Martínez3f26d432023-02-04 20:49:25 +010015 return node.querySelectorAll(kAdditionalInfoSelector);
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010016}
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010017
18export default class FlattenThreads {
19 construct() {}
20
21 getExtraInfo(node) {
Adrià Vilanova Martínez3f26d432023-02-04 20:49:25 +010022 const extraInfoNode = node.querySelector(kAdditionalInfoSelector);
23 if (!extraInfoNode) return null;
24 return JSON.parse(extraInfoNode.textContent);
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010025 }
26
27 injectId(node, extraInfo) {
28 const root = node.closest('.scTailwindThreadMessageMessagecardcontent');
29 if (!root) return false;
30 root.setAttribute('data-twpt-message-id', extraInfo.id);
31 return true;
32 }
33
34 injectQuote(node, extraInfo) {
35 const content = node.closest('.scTailwindThreadPostcontentroot');
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010036 const quote = document.createElement('twpt-flatten-thread-quote');
37 quote.setAttribute('prevMessage', JSON.stringify(extraInfo.prevMessage));
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010038 content.prepend(quote);
39 }
40
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010041 injectReplyBtn(node, extraInfo) {
42 const btn = document.createElement('twpt-flatten-thread-reply-button');
43 btn.setAttribute('extraInfo', JSON.stringify(extraInfo));
44 node.prepend(btn);
45 }
46
47 injectQuoteIfApplicable(node) {
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010048 // If we injected the additional information, it means the flatten threads
49 // feature is enabled and in actual use, so we should inject the quote.
50 const extraInfo = this.getExtraInfo(node);
51 if (!extraInfo) return;
52
53 this.injectId(node, extraInfo);
54 if (extraInfo.isComment) this.injectQuote(node, extraInfo);
55 }
56
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010057 shouldInjectQuote(node) {
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010058 return node.matches(kReplyPayloadSelector);
59 }
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010060
61 injectReplyBtnIfApplicable(node) {
62 // If we injected the additional information, it means the flatten threads
63 // feature is enabled and in actual use, so we should inject the reply
64 // button.
65 const root =
66 node.closest('.scTailwindThreadMessageMessagecardcontent')
67 .querySelector('.scTailwindThreadMessageMessagecardbody html-blob');
68 const extraInfo = this.getExtraInfo(root);
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +010069 if (!extraInfo || !extraInfo.canComment) return;
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010070
71 this.injectReplyBtn(node, extraInfo);
72 }
73
74 shouldInjectReplyBtn(node) {
75 return node.matches(kReplyActionButtonsSelector);
76 }
Adrià Vilanova Martínez4d599252023-02-04 20:54:54 +010077
78 deleteAdditionalInfoElementIfApplicable(node) {
79 if (!node.closest('sc-tailwind-shared-rich-text-editor')) return;
80 node.remove();
81 }
82
83 isAdditionalInfoElement(node) {
84 return node.matches(kAdditionalInfoSelector);
85 }
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010086}