Adrià Vilanova Martínez | 3f26d43 | 2023-02-04 20:49:25 +0100 | [diff] [blame] | 1 | export const kAdditionalInfoClass = 'ck-indent-9996300035194'; |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 2 | |
| 3 | export const kReplyPayloadSelector = |
| 4 | '.scTailwindThreadMessageMessagecardcontent:not(.scTailwindThreadMessageMessagecardpromoted) .scTailwindThreadPostcontentroot html-blob'; |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 5 | export const kReplyActionButtonsSelector = |
Adrià Vilanova Martínez | 3f26d43 | 2023-02-04 20:49:25 +0100 | [diff] [blame] | 6 | '.scTailwindThreadMessageMessagecardcontent:not(.scTailwindThreadMessageMessagecardpromoted) sc-tailwind-thread-message-message-actions'; |
| 7 | export const kAdditionalInfoSelector = '.ck-indent-9996300035194'; |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 8 | export const kMatchingSelectors = [ |
| 9 | kReplyPayloadSelector, |
| 10 | kReplyActionButtonsSelector, |
Adrià Vilanova Martínez | 3f26d43 | 2023-02-04 20:49:25 +0100 | [diff] [blame] | 11 | kAdditionalInfoSelector, |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 12 | ]; |
| 13 | |
| 14 | export function getExtraInfoNodes(node) { |
Adrià Vilanova Martínez | 3f26d43 | 2023-02-04 20:49:25 +0100 | [diff] [blame] | 15 | return node.querySelectorAll(kAdditionalInfoSelector); |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 16 | } |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 17 | |
| 18 | export default class FlattenThreads { |
| 19 | construct() {} |
| 20 | |
| 21 | getExtraInfo(node) { |
Adrià Vilanova Martínez | 3f26d43 | 2023-02-04 20:49:25 +0100 | [diff] [blame] | 22 | const extraInfoNode = node.querySelector(kAdditionalInfoSelector); |
| 23 | if (!extraInfoNode) return null; |
| 24 | return JSON.parse(extraInfoNode.textContent); |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 25 | } |
| 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ínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 36 | const quote = document.createElement('twpt-flatten-thread-quote'); |
| 37 | quote.setAttribute('prevMessage', JSON.stringify(extraInfo.prevMessage)); |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 38 | content.prepend(quote); |
| 39 | } |
| 40 | |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 41 | 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ínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 48 | // 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ínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 57 | shouldInjectQuote(node) { |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 58 | return node.matches(kReplyPayloadSelector); |
| 59 | } |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 60 | |
| 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); |
| 69 | if (!extraInfo) return; |
| 70 | |
| 71 | this.injectReplyBtn(node, extraInfo); |
| 72 | } |
| 73 | |
| 74 | shouldInjectReplyBtn(node) { |
| 75 | return node.matches(kReplyActionButtonsSelector); |
| 76 | } |
Adrià Vilanova Martínez | 4d59925 | 2023-02-04 20:54:54 +0100 | [diff] [blame] | 77 | |
| 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ínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 86 | } |