2021-12-14 16:46:07 +00:00
|
|
|
import { logger } from './logger'
|
|
|
|
import { getIframeUri, UriOptions } from './uri'
|
|
|
|
|
|
|
|
async function shareChatUrl (registerOptions: RegisterOptions, settings: any, video: Video): Promise<void> {
|
|
|
|
const peertubeHelpers = registerOptions.peertubeHelpers
|
|
|
|
|
|
|
|
const [
|
|
|
|
labelShare,
|
|
|
|
labelReadonly,
|
2021-12-21 16:14:02 +00:00
|
|
|
tipsOBS,
|
|
|
|
labelCopy,
|
|
|
|
labelCopied,
|
|
|
|
labelError,
|
|
|
|
labelOpen
|
2021-12-14 16:46:07 +00:00
|
|
|
] = await Promise.all([
|
|
|
|
peertubeHelpers.translate('Share chat link'),
|
|
|
|
peertubeHelpers.translate('Read-only'),
|
|
|
|
// eslint-disable-next-line max-len
|
2021-12-21 16:14:02 +00:00
|
|
|
peertubeHelpers.translate('Tips for streamers: To add the chat to your OBS, generate a read-only link and use it as a browser source.'),
|
|
|
|
peertubeHelpers.translate('Copy'),
|
|
|
|
peertubeHelpers.translate('Link copied'),
|
|
|
|
peertubeHelpers.translate('Error'),
|
|
|
|
peertubeHelpers.translate('Open')
|
2021-12-14 16:46:07 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
const defaultUri = getIframeUri(registerOptions, settings, video)
|
|
|
|
if (!defaultUri) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let form: {
|
|
|
|
readonly: HTMLInputElement
|
|
|
|
url: HTMLInputElement
|
|
|
|
} | undefined
|
|
|
|
function renderContent (container: HTMLElement): void {
|
|
|
|
if (!form) {
|
|
|
|
container.childNodes.forEach(child => container.removeChild(child))
|
2021-12-15 14:00:39 +00:00
|
|
|
|
2021-12-21 16:14:02 +00:00
|
|
|
container.classList.add('peertube-plugin-livechat-shareurl-modal')
|
|
|
|
|
|
|
|
const divUrl = document.createElement('div')
|
|
|
|
divUrl.classList.add('livechat-shareurl-copy')
|
2021-12-15 14:00:39 +00:00
|
|
|
const url = document.createElement('input')
|
|
|
|
url.setAttribute('type', 'text')
|
|
|
|
url.setAttribute('readonly', '')
|
|
|
|
url.setAttribute('autocomplete', 'off')
|
|
|
|
url.setAttribute('placeholder', '')
|
|
|
|
url.classList.add('form-control', 'readonly')
|
2021-12-21 16:14:02 +00:00
|
|
|
divUrl.append(url)
|
|
|
|
const copy = document.createElement('button')
|
|
|
|
copy.classList.add('btn', 'btn-outline-secondary', 'text-uppercase')
|
|
|
|
copy.textContent = labelCopy
|
|
|
|
divUrl.append(copy)
|
|
|
|
const open = document.createElement('button')
|
|
|
|
open.classList.add('btn', 'btn-outline-secondary', 'text-uppercase')
|
|
|
|
open.textContent = labelOpen
|
|
|
|
divUrl.append(open)
|
|
|
|
container.append(divUrl)
|
|
|
|
|
|
|
|
const divTips = document.createElement('div')
|
|
|
|
divTips.textContent = tipsOBS
|
|
|
|
container.append(divTips)
|
2021-12-15 14:00:39 +00:00
|
|
|
|
2021-12-21 16:14:02 +00:00
|
|
|
const divCustom = document.createElement('div')
|
|
|
|
divCustom.classList.add('livechat-shareurl-custom')
|
|
|
|
container.append(divCustom)
|
2021-12-14 16:46:07 +00:00
|
|
|
|
2021-12-21 16:14:02 +00:00
|
|
|
const divReadonly = document.createElement('div')
|
|
|
|
divCustom.append(divReadonly)
|
2021-12-14 16:46:07 +00:00
|
|
|
const readonly = document.createElement('input')
|
|
|
|
readonly.setAttribute('type', 'checkbox')
|
|
|
|
const readonlyLabelEl = document.createElement('label')
|
|
|
|
readonlyLabelEl.textContent = labelReadonly
|
|
|
|
readonlyLabelEl.prepend(readonly)
|
2021-12-21 16:14:02 +00:00
|
|
|
divReadonly.append(readonlyLabelEl)
|
2021-12-14 16:46:07 +00:00
|
|
|
|
|
|
|
readonly.onclick = () => {
|
|
|
|
renderContent(container)
|
|
|
|
}
|
|
|
|
|
2021-12-21 16:14:02 +00:00
|
|
|
url.onclick = () => {
|
|
|
|
url.select()
|
|
|
|
url.setSelectionRange(0, 99999) /* For mobile devices */
|
|
|
|
}
|
|
|
|
|
|
|
|
copy.onclick = () => {
|
|
|
|
url.select()
|
|
|
|
url.setSelectionRange(0, 99999) /* For mobile devices */
|
|
|
|
navigator.clipboard.writeText(url.value).then(() => {
|
|
|
|
peertubeHelpers.notifier.success(labelCopied)
|
|
|
|
}, () => {
|
|
|
|
peertubeHelpers.notifier.error(labelError)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
open.onclick = () => {
|
|
|
|
window.open(url.value)
|
|
|
|
}
|
|
|
|
|
2021-12-14 16:46:07 +00:00
|
|
|
form = {
|
|
|
|
readonly,
|
|
|
|
url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: save last form state, to restore each time the modal is opened.
|
2021-12-15 14:00:39 +00:00
|
|
|
// TODO: check when the feature should be available
|
|
|
|
// TODO: check the theme? some of the options should only be available in some cases.
|
2021-12-14 16:46:07 +00:00
|
|
|
|
|
|
|
const uriOptions: UriOptions = {
|
|
|
|
ignoreAutoColors: true,
|
|
|
|
permanent: true
|
|
|
|
}
|
|
|
|
if (form.readonly.checked) {
|
|
|
|
uriOptions.readonly = true
|
|
|
|
}
|
|
|
|
const iframeUri = getIframeUri(registerOptions, settings, video, uriOptions)
|
|
|
|
form.url.setAttribute('value', iframeUri ?? '')
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info('Opening the share modal...')
|
|
|
|
const observer = new MutationObserver(mutations => {
|
|
|
|
for (const { addedNodes } of mutations) {
|
|
|
|
addedNodes.forEach(node => {
|
|
|
|
if ((node as HTMLElement).localName === 'ngb-modal-window') {
|
|
|
|
logger.info('Detecting a new modal, checking if this is the good one...')
|
|
|
|
if (!(node as HTMLElement).querySelector) { return }
|
|
|
|
const title = (node as HTMLElement).querySelector('.modal-title')
|
|
|
|
if (!(title?.textContent === labelShare)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.info('Yes, it is the good modal!')
|
|
|
|
observer.disconnect()
|
|
|
|
|
|
|
|
const modalBodyElem: HTMLElement | null = (node as HTMLElement).querySelector('.modal-body')
|
|
|
|
if (!modalBodyElem) {
|
|
|
|
logger.error('Modal has no body... Dont know how to fill it.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
renderContent(modalBodyElem)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
observer.observe(document.body, {
|
|
|
|
childList: true
|
|
|
|
})
|
2021-12-14 14:41:34 +00:00
|
|
|
peertubeHelpers.showModal({
|
2021-12-14 16:46:07 +00:00
|
|
|
title: labelShare,
|
2021-12-21 16:14:02 +00:00
|
|
|
content: `<p>${defaultUri ?? ''}</p>`, // incase the observer is broken...
|
2021-12-14 14:41:34 +00:00
|
|
|
close: true
|
|
|
|
})
|
2021-12-14 16:46:07 +00:00
|
|
|
// just in case, remove the observer after a timeout, if not already done...
|
|
|
|
setTimeout(() => {
|
|
|
|
observer.disconnect()
|
|
|
|
}, 1000)
|
2021-12-14 14:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
shareChatUrl
|
|
|
|
}
|