2024-05-23 09:42:14 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
2024-04-11 09:25:04 +00:00
|
|
|
|
2024-09-09 18:01:25 +00:00
|
|
|
// FIXME: @stylistic/indent is buggy with strings literrals.
|
|
|
|
/* eslint-disable @stylistic/indent */
|
|
|
|
|
2024-04-11 09:25:04 +00:00
|
|
|
import { html } from 'lit'
|
2024-07-11 15:53:50 +00:00
|
|
|
import { api } from '@converse/headless'
|
2024-04-11 09:25:04 +00:00
|
|
|
import { until } from 'lit/directives/until.js'
|
|
|
|
import { repeat } from 'lit/directives/repeat.js'
|
|
|
|
import { unsafeHTML } from 'lit/directives/unsafe-html.js'
|
|
|
|
import { getStandaloneButtons, getDropdownButtons } from 'shared/chat/utils.js'
|
|
|
|
import tplMucHead from '../../src/plugins/muc-views/templates/muc-head.js'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clones the Peertube buttons, and add them in the template.
|
|
|
|
*/
|
|
|
|
function getPeertubeButtons () {
|
|
|
|
// searching original buttons in the DOM, and if found:
|
|
|
|
// - clone a button in ConverseJS, that triggers the original button click event.
|
|
|
|
// Note: original buttons will be hidden behind ConverseJS, so no need to hide them.
|
|
|
|
|
|
|
|
const buttonsContainer = document.querySelector(
|
|
|
|
'.peertube-plugin-livechat-buttons.peertube-plugin-livechat-buttons-open'
|
|
|
|
)
|
|
|
|
if (!buttonsContainer) { return html`` }
|
|
|
|
|
2024-04-11 13:14:50 +00:00
|
|
|
// We must remove this class, in case it is already here.
|
|
|
|
// Otherwise, the trick with offsetParent (see the forEach loop) will not work.
|
|
|
|
buttonsContainer.classList.remove('peertube-plugin-livechat-buttons-cloned')
|
|
|
|
|
2024-04-11 09:25:04 +00:00
|
|
|
const buttons = []
|
|
|
|
buttonsContainer.childNodes.forEach(button => {
|
|
|
|
try {
|
|
|
|
if (button.offsetParent === null) {
|
|
|
|
// Trick to detect if element is hidden
|
|
|
|
// (see https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.offsetParent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buttons.push(button)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!buttons.length) {
|
|
|
|
return html``
|
|
|
|
}
|
|
|
|
|
2024-04-11 13:14:50 +00:00
|
|
|
// Hidding original buttons:
|
|
|
|
console.log('[peertube-plugin-livechat] Adding class peertube-plugin-livechat-buttons-cloned')
|
|
|
|
buttonsContainer.classList.add('peertube-plugin-livechat-buttons-cloned')
|
|
|
|
|
2024-04-11 09:25:04 +00:00
|
|
|
return html`
|
|
|
|
${repeat(buttons, (node) => html`
|
|
|
|
<a
|
|
|
|
href="#"
|
2024-05-12 17:09:39 +00:00
|
|
|
title="${node.getAttribute('title') ?? ''}"
|
2024-04-11 09:25:04 +00:00
|
|
|
class="${
|
|
|
|
// adding original classes
|
|
|
|
node.className
|
|
|
|
}"
|
|
|
|
@click=${() => {
|
|
|
|
// triggering the original button click
|
|
|
|
node.click()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
${unsafeHTML(node.innerHTML)}
|
|
|
|
</a>
|
|
|
|
`)}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
export default (el) => {
|
|
|
|
if (!api.settings.get('livechat_mini_muc_head')) {
|
2024-06-25 15:23:20 +00:00
|
|
|
// original Template (this setting comes with livechatMiniMucHeadPlugin)
|
2024-04-11 09:25:04 +00:00
|
|
|
return html`${tplMucHead(el)}`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom template, with only the buttons.
|
|
|
|
const subjectHidden = true
|
|
|
|
const headingButtonsPromise = el.getHeadingButtons(subjectHidden)
|
|
|
|
return html`
|
|
|
|
<div class="livechat-mini-muc-bar-buttons">
|
|
|
|
${until(getStandaloneButtons(headingButtonsPromise), '')}
|
|
|
|
${until(getDropdownButtons(headingButtonsPromise), '')}
|
2024-04-11 13:29:39 +00:00
|
|
|
${getPeertubeButtons()}
|
2024-04-11 09:25:04 +00:00
|
|
|
</div>
|
|
|
|
`
|
|
|
|
}
|