2020-05-19 04:15:22 +00:00
|
|
|
|
2020-09-27 16:20:33 +00:00
|
|
|
import { loadMscore, WebMscore } from './mscore'
|
2020-11-05 05:22:15 +00:00
|
|
|
import i18n from './i18n'
|
2020-09-27 16:20:33 +00:00
|
|
|
|
2020-09-28 21:23:16 +00:00
|
|
|
type BtnElement = HTMLButtonElement
|
2020-05-19 04:15:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Select the original Download Button
|
|
|
|
*/
|
|
|
|
export const getDownloadBtn = (): BtnElement => {
|
2020-11-12 16:41:03 +00:00
|
|
|
const container = document.querySelectorAll('aside>section>section')[0]
|
|
|
|
const btnsDiv = [...container.children].find((div) => {
|
|
|
|
const b = div.querySelector('button, .button')
|
|
|
|
return b && b.outerHTML.replace(/\s/g, '').includes('Download')
|
|
|
|
}) as HTMLDivElement
|
2020-05-19 04:15:22 +00:00
|
|
|
const btn = btnsDiv.querySelector('button, .button') as BtnElement
|
2020-11-12 16:41:03 +00:00
|
|
|
|
2020-05-19 04:15:22 +00:00
|
|
|
btn.onclick = null
|
|
|
|
|
|
|
|
// fix the icon of the download btn
|
|
|
|
// if the `btn` seleted was a `Print` btn, replace the `print` icon with the `download` icon
|
|
|
|
const svgPath: SVGPathElement | null = btn.querySelector('svg > path')
|
|
|
|
if (svgPath) {
|
|
|
|
svgPath.setAttribute('d', 'M9.6 2.4h4.8V12h2.784l-5.18 5.18L6.823 12H9.6V2.4zM19.2 19.2H4.8v2.4h14.4v-2.4z')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (btn.nodeName.toLowerCase() === 'button') {
|
|
|
|
btn.setAttribute('style', 'width: 205px !important')
|
|
|
|
} else {
|
|
|
|
btn.dataset.target = ''
|
|
|
|
}
|
|
|
|
|
|
|
|
return btn
|
|
|
|
}
|
|
|
|
|
|
|
|
interface BtnOptions {
|
|
|
|
readonly name: string;
|
|
|
|
readonly action: BtnAction;
|
2020-09-28 21:41:51 +00:00
|
|
|
readonly disabled?: boolean;
|
|
|
|
readonly tooltip?: string;
|
2020-05-19 04:15:22 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 19:32:59 +00:00
|
|
|
export enum BtnListMode {
|
|
|
|
InPage,
|
|
|
|
ExtWindow,
|
|
|
|
}
|
|
|
|
|
2020-05-19 04:15:22 +00:00
|
|
|
export class BtnList {
|
|
|
|
private readonly list: BtnElement[] = [];
|
|
|
|
|
2020-11-10 18:36:49 +00:00
|
|
|
constructor (private getTemplateBtn: () => BtnElement) { }
|
2020-05-19 04:15:22 +00:00
|
|
|
|
|
|
|
add (options: BtnOptions): BtnElement {
|
2020-11-10 18:36:49 +00:00
|
|
|
const btn = this.getTemplateBtn().cloneNode(true) as HTMLButtonElement
|
2020-05-19 04:15:22 +00:00
|
|
|
|
2020-11-12 14:31:11 +00:00
|
|
|
const textNode = [...btn.children].find(x => x.nodeName === 'SPAN') as HTMLSpanElement
|
2020-10-26 22:22:31 +00:00
|
|
|
|
2020-05-19 04:15:22 +00:00
|
|
|
const setText = (str: string): void => {
|
|
|
|
textNode.textContent = str
|
|
|
|
}
|
|
|
|
|
|
|
|
setText(options.name)
|
|
|
|
|
|
|
|
btn.onclick = (): void => {
|
|
|
|
options.action(options.name, btn, setText)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.list.push(btn)
|
|
|
|
|
2020-09-28 21:41:51 +00:00
|
|
|
if (options.disabled) {
|
|
|
|
btn.disabled = options.disabled
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.tooltip) {
|
|
|
|
btn.title = options.tooltip
|
|
|
|
}
|
|
|
|
|
2020-05-19 04:15:22 +00:00
|
|
|
return btn
|
|
|
|
}
|
|
|
|
|
2020-11-10 19:32:59 +00:00
|
|
|
private _commit (mode: BtnListMode) {
|
|
|
|
const btnParent = this.getTemplateBtn().parentElement as HTMLDivElement
|
|
|
|
const parent = mode === BtnListMode.InPage
|
|
|
|
? btnParent
|
|
|
|
: document.createElement('div')
|
2020-11-08 02:28:32 +00:00
|
|
|
const shadow = parent.attachShadow({ mode: 'closed' })
|
|
|
|
|
|
|
|
// style the shadow DOM from outside css
|
|
|
|
document.head.querySelectorAll('style').forEach(s => {
|
|
|
|
shadow.append(s.cloneNode(true))
|
|
|
|
})
|
|
|
|
|
|
|
|
// hide buttons using the shadow DOM
|
2020-11-10 19:32:59 +00:00
|
|
|
const newParent = btnParent.cloneNode(false) as HTMLDivElement
|
2020-11-08 02:28:32 +00:00
|
|
|
newParent.append(...this.list)
|
|
|
|
shadow.append(newParent)
|
2020-11-10 18:36:49 +00:00
|
|
|
|
2020-11-10 19:32:59 +00:00
|
|
|
return parent
|
2020-11-10 18:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* replace the template button with the list of new buttons
|
|
|
|
*/
|
2020-11-10 19:32:59 +00:00
|
|
|
commit (mode: BtnListMode = BtnListMode.InPage): void {
|
|
|
|
switch (mode) {
|
|
|
|
case BtnListMode.InPage: {
|
|
|
|
let el: Element = this._commit(mode)
|
|
|
|
const observer = new MutationObserver(() => {
|
|
|
|
// check if the buttons are still in document when dom updates
|
|
|
|
if (!document.contains(el)) {
|
|
|
|
// re-commit
|
|
|
|
// performance issue?
|
|
|
|
el = this._commit(mode)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
observer.observe(document, { childList: true, subtree: true })
|
|
|
|
break
|
2020-11-10 18:36:49 +00:00
|
|
|
}
|
2020-11-10 19:32:59 +00:00
|
|
|
|
|
|
|
case BtnListMode.ExtWindow: {
|
|
|
|
const div = this._commit(mode)
|
|
|
|
const w = window.open('', undefined, 'resizable,width=230,height=270')
|
|
|
|
// eslint-disable-next-line no-unused-expressions
|
|
|
|
w?.document.body.append(div)
|
|
|
|
window.addEventListener('unload', () => w?.close())
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error('unknown BtnListMode')
|
|
|
|
}
|
2020-05-19 04:15:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type BtnAction = (btnName: string, btnEl: BtnElement, setText: (str: string) => void) => any
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
|
|
export namespace BtnAction {
|
|
|
|
|
2020-10-18 11:21:25 +00:00
|
|
|
type Promisable<T> = T | Promise<T>
|
|
|
|
type UrlInput = Promisable<string> | (() => Promisable<string>)
|
|
|
|
|
|
|
|
const normalizeUrlInput = (url: UrlInput) => {
|
|
|
|
if (typeof url === 'function') return url()
|
|
|
|
else return url
|
|
|
|
}
|
|
|
|
|
|
|
|
export const openUrl = (url: UrlInput): BtnAction => {
|
|
|
|
return process(async (): Promise<any> => {
|
|
|
|
window.open(await normalizeUrlInput(url))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-26 01:08:39 +00:00
|
|
|
export const download = (url: UrlInput): BtnAction => {
|
2020-10-18 11:21:25 +00:00
|
|
|
return process(async (): Promise<any> => {
|
|
|
|
const _url = await normalizeUrlInput(url)
|
2020-10-26 01:08:39 +00:00
|
|
|
const a = document.createElement('a')
|
|
|
|
a.href = _url
|
|
|
|
a.dispatchEvent(new MouseEvent('click'))
|
2020-10-18 11:21:25 +00:00
|
|
|
})
|
2020-05-19 04:15:22 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 16:20:33 +00:00
|
|
|
export const mscoreWindow = (fn: (w: Window, score: WebMscore, processingTextEl: ChildNode) => any): BtnAction => {
|
|
|
|
return async (btnName, btn, setText) => {
|
|
|
|
const _onclick = btn.onclick
|
|
|
|
btn.onclick = null
|
2020-11-05 05:22:15 +00:00
|
|
|
setText(i18n('PROCESSING')())
|
2020-09-27 16:20:33 +00:00
|
|
|
|
|
|
|
const w = window.open('') as Window
|
2020-11-05 05:22:15 +00:00
|
|
|
const txt = document.createTextNode(i18n('PROCESSING')())
|
2020-09-27 16:20:33 +00:00
|
|
|
w.document.body.append(txt)
|
|
|
|
|
|
|
|
// set page hooks
|
|
|
|
// eslint-disable-next-line prefer-const
|
|
|
|
let score: WebMscore
|
|
|
|
const destroy = (): void => {
|
|
|
|
score && score.destroy()
|
|
|
|
w.close()
|
|
|
|
}
|
|
|
|
window.addEventListener('unload', destroy)
|
|
|
|
w.addEventListener('beforeunload', () => {
|
|
|
|
score && score.destroy()
|
|
|
|
window.removeEventListener('unload', destroy)
|
|
|
|
setText(btnName)
|
|
|
|
btn.onclick = _onclick
|
|
|
|
})
|
|
|
|
|
|
|
|
score = await loadMscore(w)
|
|
|
|
|
|
|
|
fn(w, score, txt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 04:15:22 +00:00
|
|
|
export const process = (fn: () => any): BtnAction => {
|
|
|
|
return async (name, btn, setText): Promise<void> => {
|
|
|
|
const _onclick = btn.onclick
|
|
|
|
|
|
|
|
btn.onclick = null
|
2020-11-05 05:22:15 +00:00
|
|
|
setText(i18n('PROCESSING')())
|
2020-05-19 04:15:22 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await fn()
|
|
|
|
setText(name)
|
|
|
|
} catch (err) {
|
2020-11-05 05:22:15 +00:00
|
|
|
setText(i18n('BTN_ERROR')())
|
2020-05-19 04:15:22 +00:00
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
btn.onclick = _onclick
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:12:44 +00:00
|
|
|
export const deprecate = (action: BtnAction): BtnAction => {
|
|
|
|
return (name, btn, setText) => {
|
2020-11-05 05:22:15 +00:00
|
|
|
alert(i18n('DEPRECATION_NOTICE')(name))
|
2020-10-22 21:12:44 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
|
|
return action(name, btn, setText)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 04:15:22 +00:00
|
|
|
}
|