|
| 1 | +const codeActionButtons = [ |
| 2 | + { |
| 3 | + icon: 'copy', |
| 4 | + id: 'copy', |
| 5 | + title: 'Copy Code', |
| 6 | + show: true |
| 7 | + }, |
| 8 | + { |
| 9 | + icon: 'order', |
| 10 | + id: 'lines', |
| 11 | + title: 'Toggle Line Numbers', |
| 12 | + show: true |
| 13 | + }, |
| 14 | + { |
| 15 | + icon: 'carly', |
| 16 | + id: 'wrap', |
| 17 | + title: 'Toggle Line Wrap', |
| 18 | + show: false |
| 19 | + }, |
| 20 | + { |
| 21 | + icon: 'expand', |
| 22 | + id: 'expand', |
| 23 | + title: 'Toggle code block expand', |
| 24 | + show: false |
| 25 | + } |
| 26 | +]; |
| 27 | + |
| 28 | +const body = elem('body'); |
| 29 | +const maxLines = parseInt(body.dataset.code); |
| 30 | +const copyId = 'panel_copy'; |
| 31 | +const wrapId = 'panel_wrap'; |
| 32 | +const linesId = 'panel_lines'; |
| 33 | +const panelExpand = 'panel_expand'; |
| 34 | +const panelExpanded = 'panel_expanded'; |
| 35 | +const panelHide = 'panel_hide'; |
| 36 | +const panelFrom = 'panel_from'; |
| 37 | +const panelBox = 'panel_box'; |
| 38 | +const fullHeight = 'initial'; |
| 39 | +const highlightWrap = 'highlight_wrap' |
| 40 | + |
| 41 | +function wrapOrphanedPreElements() { |
| 42 | + const pres = elems('pre'); |
| 43 | + Array.from(pres).forEach(function(pre){ |
| 44 | + const parent = pre.parentNode; |
| 45 | + const isOrpaned = !containsClass(parent, 'highlight'); |
| 46 | + if(isOrpaned) { |
| 47 | + const preWrapper = createEl(); |
| 48 | + preWrapper.className = 'highlight'; |
| 49 | + const outerWrapper = createEl(); |
| 50 | + outerWrapper.className = highlightWrap; |
| 51 | + wrapEl(pre, preWrapper); |
| 52 | + wrapEl(preWrapper, outerWrapper); |
| 53 | + } |
| 54 | + }) |
| 55 | + /* |
| 56 | + @Todo |
| 57 | + 1. Add UI control to orphaned blocks |
| 58 | + */ |
| 59 | +} |
| 60 | + |
| 61 | +wrapOrphanedPreElements(); |
| 62 | + |
| 63 | +function codeBlocks() { |
| 64 | + const markedCodeBlocks = elems('code'); |
| 65 | + const blocks = Array.from(markedCodeBlocks).filter(function(block){ |
| 66 | + return hasClasses(block) && !Array.from(block.classList).includes('noClass'); |
| 67 | + }).map(function(block){ |
| 68 | + return block |
| 69 | + }); |
| 70 | + return blocks; |
| 71 | +} |
| 72 | + |
| 73 | +function codeBlockFits(block) { |
| 74 | + // return false if codeblock overflows |
| 75 | + const blockWidth = block.offsetWidth; |
| 76 | + const highlightBlockWidth = block.parentNode.parentNode.offsetWidth; |
| 77 | + return blockWidth <= highlightBlockWidth ? true : false; |
| 78 | +} |
| 79 | + |
| 80 | +function maxHeightIsSet(elem) { |
| 81 | + let maxHeight = elem.style.maxHeight; |
| 82 | + return maxHeight.includes('px') |
| 83 | +} |
| 84 | + |
| 85 | +function restrainCodeBlockHeight(lines) { |
| 86 | + const lastLine = lines[maxLines-1]; |
| 87 | + let maxCodeBlockHeight = fullHeight; |
| 88 | + if(lastLine) { |
| 89 | + const lastLinePos = lastLine.offsetTop; |
| 90 | + if(lastLinePos !== 0) { |
| 91 | + maxCodeBlockHeight = `${lastLinePos}px`; |
| 92 | + const codeBlock = lines[0].parentNode; |
| 93 | + const outerBlock = codeBlock.closest('.highlight'); |
| 94 | + const isExpanded = containsClass(outerBlock, panelExpanded); |
| 95 | + if(!isExpanded) { |
| 96 | + codeBlock.dataset.height = maxCodeBlockHeight; |
| 97 | + codeBlock.style.maxHeight = maxCodeBlockHeight; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +const blocks = codeBlocks(); |
| 104 | + |
| 105 | +function collapseCodeBlock(block) { |
| 106 | + const lines = elems(lineClass, block); |
| 107 | + const codeLines = lines.length; |
| 108 | + if (codeLines > maxLines) { |
| 109 | + const expandDot = createEl() |
| 110 | + pushClass(expandDot, panelExpand); |
| 111 | + pushClass(expandDot, panelFrom); |
| 112 | + expandDot.title = "Toggle code block expand"; |
| 113 | + expandDot.textContent = "..."; |
| 114 | + const outerBlock = block.closest('.highlight'); |
| 115 | + window.setTimeout(function(){ |
| 116 | + const expandIcon = outerBlock.nextElementSibling.lastElementChild; |
| 117 | + deleteClass(expandIcon, panelHide); |
| 118 | + }, 150) |
| 119 | + |
| 120 | + restrainCodeBlockHeight(lines); |
| 121 | + const highlightElement = block.parentNode.parentNode; |
| 122 | + highlightElement.appendChild(expandDot); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +blocks.forEach(function(block){ |
| 127 | + collapseCodeBlock(block); |
| 128 | +}) |
| 129 | + |
| 130 | +function actionPanel() { |
| 131 | + const panel = createEl(); |
| 132 | + panel.className = panelBox; |
| 133 | + |
| 134 | + codeActionButtons.forEach(function(button) { |
| 135 | + // create button |
| 136 | + const btn = createEl('a'); |
| 137 | + btn.href = '#'; |
| 138 | + btn.title = button.title; |
| 139 | + btn.className = `icon panel_icon panel_${button.id}`; |
| 140 | + button.show ? false : pushClass(btn, panelHide); |
| 141 | + // load icon inside button |
| 142 | + btn.style.backgroundImage = `url(${baseURL}${iconsPath}${button.icon}.svg)`; |
| 143 | + // append button on panel |
| 144 | + panel.appendChild(btn); |
| 145 | + }); |
| 146 | + |
| 147 | + return panel; |
| 148 | +} |
| 149 | + |
| 150 | +function toggleLineNumbers(elems) { |
| 151 | + elems.forEach(function (elem, index) { |
| 152 | + // mark the code element when there are no lines |
| 153 | + modifyClass(elem, 'pre_nolines') |
| 154 | + }); |
| 155 | + restrainCodeBlockHeight(elems); |
| 156 | +} |
| 157 | + |
| 158 | +function toggleLineWrap(elem) { |
| 159 | + modifyClass(elem, 'pre_wrap'); |
| 160 | + // retain max number of code lines on line wrap |
| 161 | + const lines = elems(lineClass, elem); |
| 162 | + restrainCodeBlockHeight(lines); |
| 163 | +} |
| 164 | + |
| 165 | +function copyCode(codeElement) { |
| 166 | + lineNumbers = elems('.ln', codeElement); |
| 167 | + // remove line numbers before copying |
| 168 | + if(lineNumbers.length) { |
| 169 | + lineNumbers.forEach(function(line){ |
| 170 | + line.remove(); |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + const codeToCopy = codeElement.textContent; |
| 175 | + // copy code |
| 176 | + copyToClipboard(codeToCopy); |
| 177 | +} |
| 178 | + |
| 179 | +function disableCodeLineNumbers(block){ |
| 180 | + const lines = elems(lineClass, block) |
| 181 | + toggleLineNumbers(lines); |
| 182 | +} |
| 183 | + |
| 184 | +(function codeActions(){ |
| 185 | + const blocks = codeBlocks(); |
| 186 | + |
| 187 | + const highlightWrapId = highlightWrap; |
| 188 | + blocks.forEach(function(block){ |
| 189 | + // disable line numbers if disabled globally |
| 190 | + const showLines = elem('body').dataset.lines; |
| 191 | + parseBoolean(showLines) === false ? disableCodeLineNumbers(block) : false; |
| 192 | + |
| 193 | + const highlightElement = block.parentNode.parentNode; |
| 194 | + // wrap code block in a div |
| 195 | + const highlightWrapper = createEl(); |
| 196 | + highlightWrapper.className = highlightWrapId; |
| 197 | + wrapEl(highlightElement, highlightWrapper); |
| 198 | + |
| 199 | + const panel = actionPanel(); |
| 200 | + // show wrap icon only if the code block needs wrapping |
| 201 | + const wrapIcon = elem(`.${wrapId}`, panel); |
| 202 | + codeBlockFits(block) ? false : deleteClass(wrapIcon, panelHide); |
| 203 | + |
| 204 | + // append buttons |
| 205 | + highlightWrapper.appendChild(panel); |
| 206 | + }); |
| 207 | + |
| 208 | + function isItem(target, id) { |
| 209 | + // if is item or within item |
| 210 | + return target.matches(`.${id}`) || target.closest(`.${id}`); |
| 211 | + } |
| 212 | + |
| 213 | + function showActive(target, targetClass,activeClass = 'active') { |
| 214 | + const active = activeClass; |
| 215 | + const targetElement = target.matches(`.${targetClass}`) ? target : target.closest(`.${targetClass}`); |
| 216 | + |
| 217 | + deleteClass(targetElement, active); |
| 218 | + setTimeout(function() { |
| 219 | + modifyClass(targetElement, active) |
| 220 | + }, 50) |
| 221 | + } |
| 222 | + |
| 223 | + doc.addEventListener('click', function(event){ |
| 224 | + // copy code block |
| 225 | + const target = event.target; |
| 226 | + const isCopyIcon = isItem(target, copyId); |
| 227 | + const isWrapIcon = isItem(target, wrapId); |
| 228 | + const isLinesIcon = isItem(target, linesId); |
| 229 | + const isExpandIcon = isItem(target, panelExpand); |
| 230 | + const isActionable = isCopyIcon || isWrapIcon || isLinesIcon || isExpandIcon; |
| 231 | + |
| 232 | + if(isActionable) { |
| 233 | + event.preventDefault(); |
| 234 | + showActive(target, 'icon'); |
| 235 | + const codeElement = target.closest(`.${highlightWrapId}`).firstElementChild.firstElementChild; |
| 236 | + let lineNumbers = elems(lineClass, codeElement); |
| 237 | + |
| 238 | + isWrapIcon ? toggleLineWrap(codeElement) : false; |
| 239 | + |
| 240 | + isLinesIcon ? toggleLineNumbers(lineNumbers) : false; |
| 241 | + |
| 242 | + if (isExpandIcon) { |
| 243 | + let thisCodeBlock = codeElement.firstElementChild; |
| 244 | + const outerBlock = thisCodeBlock.closest('.highlight'); |
| 245 | + if(maxHeightIsSet(thisCodeBlock)) { |
| 246 | + thisCodeBlock.style.maxHeight = fullHeight; |
| 247 | + // mark code block as expanded |
| 248 | + pushClass(outerBlock, panelExpanded) |
| 249 | + } else { |
| 250 | + thisCodeBlock.style.maxHeight = thisCodeBlock.dataset.height; |
| 251 | + // unmark code block as expanded |
| 252 | + deleteClass(outerBlock, panelExpanded) |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + if(isCopyIcon) { |
| 257 | + // clone code element |
| 258 | + const codeElementClone = codeElement.cloneNode(true); |
| 259 | + copyCode(codeElementClone); |
| 260 | + } |
| 261 | + } |
| 262 | + }); |
| 263 | + |
| 264 | + (function addLangLabel() { |
| 265 | + const blocks = codeBlocks(); |
| 266 | + blocks.forEach(function(block){ |
| 267 | + let label = block.dataset.lang; |
| 268 | + label = label === 'sh' ? 'bash' : label; |
| 269 | + if(label !== "fallback") { |
| 270 | + const labelEl = createEl(); |
| 271 | + labelEl.textContent = label; |
| 272 | + pushClass(labelEl, 'lang'); |
| 273 | + block.closest(`.${highlightWrap}`).appendChild(labelEl); |
| 274 | + } |
| 275 | + }); |
| 276 | + })(); |
| 277 | +})(); |
0 commit comments