|
| 1 | +import { default as params } from '@params'; |
| 2 | +import snackbar from 'mods/snackbar/js/index.ts'; |
| 3 | +import i18n from './i18n'; |
| 4 | + |
| 5 | +export default class Panel { |
| 6 | + private pre: HTMLElement |
| 7 | + |
| 8 | + private wrapper: HTMLElement |
| 9 | + |
| 10 | + private ele: HTMLElement |
| 11 | + |
| 12 | + constructor(private code: HTMLElement) { |
| 13 | + } |
| 14 | + |
| 15 | + init() { |
| 16 | + if (!params.line_nos) { |
| 17 | + this.code.classList.add('code-no-ln') |
| 18 | + } |
| 19 | + if (params.wrap) { |
| 20 | + this.code.classList.add('code-wrap') |
| 21 | + } |
| 22 | + |
| 23 | + this.pre = this.code.parentElement as HTMLElement |
| 24 | + |
| 25 | + this.ele = document.createElement('div') |
| 26 | + this.ele.className = 'code-block-panel' |
| 27 | + |
| 28 | + this.wrapper = document.createElement('div') |
| 29 | + this.wrapper.className = 'code-block-panel-wrapper' |
| 30 | + this.wrapper.appendChild(this.ele) |
| 31 | + |
| 32 | + this.maxLines() |
| 33 | + this.language() |
| 34 | + this.lineNoButton() |
| 35 | + this.wrapButton() |
| 36 | + this.expandButton() |
| 37 | + this.copyButton() |
| 38 | + |
| 39 | + this.pre.appendChild(this.wrapper) |
| 40 | + } |
| 41 | + |
| 42 | + // Returns the lines of code block. |
| 43 | + private lines(): Array<HTMLElement> { |
| 44 | + return Array.from(this.code.querySelectorAll(':scope > span')) |
| 45 | + } |
| 46 | + |
| 47 | + private maxHeight |
| 48 | + |
| 49 | + private maxLines() { |
| 50 | + const lines = this.lines() |
| 51 | + if (params.max_lines > 0 && lines.length > params.max_lines) { |
| 52 | + const offsetTop = lines[params.max_lines - 1].offsetTop |
| 53 | + if (offsetTop > 0) { |
| 54 | + this.pre.style.maxHeight = this.maxHeight = offsetTop + 'px' |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Show the code language. |
| 60 | + private language() { |
| 61 | + const lang = this.code.getAttribute('data-lang') |
| 62 | + if (!lang || lang === 'fallback') { |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + const e = document.createElement('span') |
| 67 | + e.className = 'code-block-lang' |
| 68 | + e.innerText = lang |
| 69 | + this.pre.appendChild(e) |
| 70 | + } |
| 71 | + |
| 72 | + private button(name: string, callback: CallableFunction): HTMLButtonElement { |
| 73 | + const btn = document.createElement('button') |
| 74 | + btn.className = 'code-block-action code-block-action-' + name |
| 75 | + btn.innerHTML = params.icons[name] |
| 76 | + btn.addEventListener('click', () => { |
| 77 | + callback() |
| 78 | + }) |
| 79 | + return btn |
| 80 | + } |
| 81 | + |
| 82 | + private copyButton() { |
| 83 | + const btn = this.button('copy', () => { |
| 84 | + this.copy() |
| 85 | + }) |
| 86 | + this.ele.appendChild(btn) |
| 87 | + } |
| 88 | + |
| 89 | + private copy() { |
| 90 | + const clone = this.code.cloneNode(true) as HTMLElement; |
| 91 | + // remove line numbers. |
| 92 | + clone.querySelectorAll('.ln, :scope > span > span:first-child').forEach((ln) => { |
| 93 | + ln.remove(); |
| 94 | + }); |
| 95 | + navigator.clipboard.writeText(clone.innerText).then(() => { |
| 96 | + snackbar.add(i18n.translate('copied')) |
| 97 | + }).catch((err) => { |
| 98 | + snackbar.add(i18n.translate('copy_failed')) |
| 99 | + console.error(err) |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + private wrapButton() { |
| 104 | + const btn = this.button('wrap', () => { |
| 105 | + this.toggleClass('code-wrap') |
| 106 | + }) |
| 107 | + this.ele.appendChild(btn) |
| 108 | + } |
| 109 | + |
| 110 | + private toggleClass(className: string) { |
| 111 | + |
| 112 | + if (this.code.classList.contains(className)) { |
| 113 | + this.code.classList.remove(className) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + this.code.classList.add(className) |
| 118 | + } |
| 119 | + |
| 120 | + private lineNoButton() { |
| 121 | + const btn = this.button('ln', () => { |
| 122 | + this.toggleClass('code-no-ln') |
| 123 | + }) |
| 124 | + this.ele.appendChild(btn) |
| 125 | + } |
| 126 | + |
| 127 | + private expandButton() { |
| 128 | + const btn = this.button('expand', () => { |
| 129 | + this.expand() |
| 130 | + }) |
| 131 | + this.ele.appendChild(btn) |
| 132 | + } |
| 133 | + |
| 134 | + private expand() { |
| 135 | + if (!this.pre.style.maxHeight && !this.maxHeight) { |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + if (this.pre.style.maxHeight) { |
| 140 | + this.pre.style.maxHeight = '' |
| 141 | + return |
| 142 | + } |
| 143 | + |
| 144 | + this.pre.style.maxHeight = this.maxHeight |
| 145 | + } |
| 146 | +} |
0 commit comments