|
| 1 | +<script setup lang="ts" name="CodeBlockToggle"> |
| 2 | +import { onMounted } from "vue"; |
| 3 | +import arrowSvg from "../assets/svg/arrow"; |
| 4 | +import { useRouter } from "vitepress"; |
| 5 | +
|
| 6 | +const foldClass = "fold"; |
| 7 | +const circleClass = "circle"; |
| 8 | +const arrowClass = "arrow"; |
| 9 | +
|
| 10 | +/** |
| 11 | + * 初始化代码块 |
| 12 | + */ |
| 13 | +const initCodeBlock = () => { |
| 14 | + const modes = document.querySelectorAll(".vp-doc div[class*='language-']"); |
| 15 | +
|
| 16 | + Array.from(modes).forEach(item => { |
| 17 | + // 如果当前代码块已经初始化了,则不需要继续执行代码 |
| 18 | + if (item.querySelector(`.${circleClass}`) && item.querySelector(`.${arrowClass}`)) return; |
| 19 | +
|
| 20 | + const arrowElement = createArrowElement(item); |
| 21 | + const circleElement = createCircleElement(); |
| 22 | +
|
| 23 | + item.append(arrowElement); |
| 24 | + item.append(circleElement); |
| 25 | + }); |
| 26 | +}; |
| 27 | +
|
| 28 | +/** |
| 29 | + * 创建箭头元素,添加点击事件(折叠/展开) |
| 30 | + */ |
| 31 | +const createArrowElement = (item: Element) => { |
| 32 | + // 获取代码块原来的高度,进行备份 |
| 33 | + const modeHeight = item.offsetHeight; |
| 34 | + // 初始化代码块高度,确保第一次折叠时就有动画 |
| 35 | + item.style.height = `${modeHeight}px`; |
| 36 | + // 获取代码块的元素 |
| 37 | + const pre = item.querySelector("pre"); |
| 38 | + const wrapper = item.querySelector(".line-numbers-wrapper"); |
| 39 | +
|
| 40 | + const div = document.createElement("div"); |
| 41 | + div.classList.add(arrowClass); |
| 42 | + div.innerHTML = arrowSvg; |
| 43 | +
|
| 44 | + const codeBlockState = { |
| 45 | + expand: { height: `${modeHeight}px`, display: "block", speed: 80 }, |
| 46 | + fold: { height: "var(--tk-code-block-fold-height)", display: "none", speed: 400 }, |
| 47 | + }; |
| 48 | +
|
| 49 | + let timeoutId: NodeJS.Timeout | null = null; |
| 50 | +
|
| 51 | + // 箭头点击事件 |
| 52 | + div.onclick = () => { |
| 53 | + const isFold = div.classList.contains(foldClass); |
| 54 | + // 如果是折叠状态,则需要展开 |
| 55 | + const state = codeBlockState[isFold ? "expand" : "fold"]; |
| 56 | +
|
| 57 | + item.style.height = state.height; |
| 58 | +
|
| 59 | + clearTimeout(timeoutId); |
| 60 | +
|
| 61 | + timeoutId = setTimeout(() => { |
| 62 | + pre.style.display = state.display; |
| 63 | + wrapper.style.display = state.display; |
| 64 | + clearTimeout(timeoutId); |
| 65 | + }, state.speed); |
| 66 | +
|
| 67 | + div.classList.toggle(foldClass); |
| 68 | + }; |
| 69 | +
|
| 70 | + return div; |
| 71 | +}; |
| 72 | +/** |
| 73 | + * 创建三个圆圈元素 |
| 74 | + */ |
| 75 | +const createCircleElement = () => { |
| 76 | + const div = document.createElement("div"); |
| 77 | + div.classList.add(circleClass); |
| 78 | + return div; |
| 79 | +}; |
| 80 | +
|
| 81 | +const router = useRouter(); |
| 82 | +
|
| 83 | +const initRoute = () => { |
| 84 | + const selfOnAfterRouteChange = router.onAfterRouteChange; |
| 85 | + // 路由切换后的回调 |
| 86 | + router.onAfterRouteChange = (href: string) => { |
| 87 | + selfOnAfterRouteChange?.(href); |
| 88 | + // 路由切换后初始化代码块 |
| 89 | + initCodeBlock(); |
| 90 | + }; |
| 91 | +}; |
| 92 | +
|
| 93 | +onMounted(() => { |
| 94 | + initCodeBlock(); |
| 95 | + initRoute(); |
| 96 | +}); |
| 97 | +</script> |
| 98 | + |
| 99 | +<template></template> |
| 100 | + |
| 101 | +<style lang="scss"> |
| 102 | +.vp-doc div[class*="language-"] { |
| 103 | + transition: height 0.3s; |
| 104 | + overflow: hidden; |
| 105 | +
|
| 106 | + .vp-code { |
| 107 | + padding-top: var(--tk-code-block-fold-height); |
| 108 | + } |
| 109 | +
|
| 110 | + .line-numbers-wrapper { |
| 111 | + margin-top: var(--tk-code-block-fold-height); |
| 112 | + padding-top: 0; |
| 113 | + } |
| 114 | +
|
| 115 | + /* 代码块三个圆圈 */ |
| 116 | + .circle { |
| 117 | + position: absolute; |
| 118 | + top: calc(var(--tk-code-block-fold-height) / 2); |
| 119 | + left: 14px; |
| 120 | + transform: translateY(-50%); |
| 121 | + width: 12px; |
| 122 | + height: 12px; |
| 123 | + border-radius: 50%; |
| 124 | + background: #fc625d; |
| 125 | + -webkit-box-shadow: |
| 126 | + 20px 0 #fdbc40, |
| 127 | + 40px 0 #35cd4b; |
| 128 | + box-shadow: |
| 129 | + 20px 0 #fdbc40, |
| 130 | + 40px 0 #35cd4b; |
| 131 | + } |
| 132 | +
|
| 133 | + /* 代码块语言 */ |
| 134 | + span.lang { |
| 135 | + position: absolute; |
| 136 | + z-index: 3; |
| 137 | + top: calc(var(--tk-code-block-fold-height) / 2); |
| 138 | + left: 75px; |
| 139 | + transform: translateY(-50%); |
| 140 | + font-size: 18px; |
| 141 | + color: var(--vp-c-text-1); |
| 142 | + text-transform: uppercase; |
| 143 | + font-weight: bold; |
| 144 | + width: fit-content; |
| 145 | + } |
| 146 | +
|
| 147 | + /* 一键复制图标 */ |
| 148 | + button.copy { |
| 149 | + width: 18px; |
| 150 | + height: 18px; |
| 151 | + position: absolute; |
| 152 | + top: calc(var(--tk-code-block-fold-height) / 2); |
| 153 | + right: 36px; |
| 154 | + transform: translateY(-50%); |
| 155 | + opacity: 1; |
| 156 | + background-size: 14px; |
| 157 | + background-color: transparent; |
| 158 | + border: none; |
| 159 | + color: var(--vp-c-text-1); |
| 160 | + fill: var(--vp-c-text-1); |
| 161 | + &:hover, |
| 162 | + .copied { |
| 163 | + background-color: transparent; |
| 164 | + border: none; |
| 165 | + } |
| 166 | + } |
| 167 | +
|
| 168 | + /* 语言和一键复制图标不会消失 */ |
| 169 | + &:hover button.copy + span.lang, |
| 170 | + button.copy:focus + span.lang, |
| 171 | + &:hover > button.copy, |
| 172 | + button.copy:focus { |
| 173 | + opacity: 1; |
| 174 | + } |
| 175 | +
|
| 176 | + /* 箭头 */ |
| 177 | + .arrow { |
| 178 | + cursor: pointer; |
| 179 | + position: absolute; |
| 180 | + z-index: 3; |
| 181 | + top: calc(var(--tk-code-block-fold-height) / 2); |
| 182 | + right: 14px; |
| 183 | + transform: translateY(-50%); |
| 184 | + transition: all 0.3s; |
| 185 | +
|
| 186 | + svg { |
| 187 | + width: 16px; |
| 188 | + height: 16px; |
| 189 | + fill: var(--vp-c-text-1); |
| 190 | + } |
| 191 | +
|
| 192 | + /* 代码块折叠后后旋转 -90 度 */ |
| 193 | + &.fold { |
| 194 | + transform: rotate(90deg) translateX(-50%); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
| 198 | +</style> |
0 commit comments