Share chat modal: saving form.

This commit is contained in:
John Livingston 2021-12-21 18:39:02 +01:00
parent 4fbfeaab7d
commit ffb8be8b30
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC

View File

@ -2,6 +2,12 @@ import { logger } from './logger'
import { getIframeUri, UriOptions } from './uri' import { getIframeUri, UriOptions } from './uri'
import { isAutoColorsAvailable } from 'shared/lib/autocolors' import { isAutoColorsAvailable } from 'shared/lib/autocolors'
interface ShareForm {
readonly: HTMLInputElement
url: HTMLInputElement
autoColors?: HTMLInputElement
}
async function shareChatUrl (registerOptions: RegisterOptions, settings: any, video: Video): Promise<void> { async function shareChatUrl (registerOptions: RegisterOptions, settings: any, video: Video): Promise<void> {
const peertubeHelpers = registerOptions.peertubeHelpers const peertubeHelpers = registerOptions.peertubeHelpers
@ -31,11 +37,7 @@ async function shareChatUrl (registerOptions: RegisterOptions, settings: any, vi
return return
} }
let form: { let form: ShareForm | undefined
readonly: HTMLInputElement
url: HTMLInputElement
autoColors?: HTMLInputElement
} | undefined
function renderContent (container: HTMLElement): void { function renderContent (container: HTMLElement): void {
if (!form) { if (!form) {
container.childNodes.forEach(child => container.removeChild(child)) container.childNodes.forEach(child => container.removeChild(child))
@ -122,11 +124,13 @@ async function shareChatUrl (registerOptions: RegisterOptions, settings: any, vi
url, url,
autoColors autoColors
} }
restore(form)
} }
// TODO: save last form state, to restore each time the modal is opened. // Saving the form state, to restore each time the modal is opened.
save(form)
// TODO: check when the feature should be available // TODO: check when the feature should be available
// TODO: check the theme? some of the options should only be available in some cases.
const uriOptions: UriOptions = { const uriOptions: UriOptions = {
ignoreAutoColors: form.autoColors ? !form.autoColors.checked : true, ignoreAutoColors: form.autoColors ? !form.autoColors.checked : true,
@ -139,6 +143,41 @@ async function shareChatUrl (registerOptions: RegisterOptions, settings: any, vi
form.url.setAttribute('value', iframeUri ?? '') form.url.setAttribute('value', iframeUri ?? '')
} }
function save (form: ShareForm): void {
if (!window.localStorage) {
return
}
const v = {
version: 1, // in case we add incompatible values in a near feature
readonly: !!form.readonly.checked,
autocolors: !!form.autoColors?.checked
}
window.localStorage.setItem('peertube-plugin-livechat-shareurl', JSON.stringify(v))
}
function restore (form: ShareForm): void {
if (!window.localStorage) {
return
}
const s = window.localStorage.getItem('peertube-plugin-livechat-shareurl')
if (!s) {
return
}
let v: any
try {
v = JSON.parse(s)
if (!v || (typeof v !== 'object') || v.version !== 1) {
return
}
form.readonly.checked = !!v.readonly
if (form.autoColors) {
form.autoColors.checked = !!v.autocolors
}
} catch (err) {
logger.error(err as string)
}
}
logger.info('Opening the share modal...') logger.info('Opening the share modal...')
const observer = new MutationObserver(mutations => { const observer = new MutationObserver(mutations => {
for (const { addedNodes } of mutations) { for (const { addedNodes } of mutations) {