This commit is contained in:
2022-12-09 16:41:41 +08:00
parent c1cce5a7c2
commit ff7aa8774f
2003 changed files with 156639 additions and 140 deletions

View File

@@ -0,0 +1,228 @@
<template>
<div class="lay-code">
<div id="source" class="source">
<slot />
<div v-if="$slots.description" class="description">
<slot name="description" />
</div>
</div>
<div ref="meta" class="meta">
<div class="language-html">
<slot name="code" />
</div>
</div>
<div :class="{ 'is-fixed': isFixContorl }" class="control">
<i
class="layui-icon layui-icon-file btn"
@click="copy"
title="复制代码"
/>
<i
class="layui-icon layui-icon-fonts-code btn"
@click="toggle"
title="查看代码"
/>
<i
class="layui-icon layui-icon-component btn"
@click="onPlayground"
title="在 sandbox-vue 打开"
/>
<i
class="layui-icon layui-icon-chart btn"
title="在 stackblitz 打开"
@click="onStackblitz"
>
</i>
</div>
</div>
</template>
<script setup lang="ts">
import { layer } from "@layui/layer-vue";
import { onMounted, onUnmounted, ref, watch } from "vue";
import { openPlayground } from "../utils/code-playground";
import { openStackblitz } from "../utils/code-stackblitz";
const meta = ref<HTMLElement>({} as HTMLElement);
const isFixContorl = ref(false);
const codeAreaHeight = ref(0);
const show = ref(false);
const toggle = function () {
show.value = !show.value;
};
const onPlayground = async function () {
const foundCodes = meta.value.getElementsByClassName("language-html");
const foundCode = foundCodes[0];
const SourceCode = foundCode.textContent || "";
const { link } = await openPlayground(SourceCode, true);
window.open(link);
};
const onStackblitz = function () {
const foundCodes = meta.value.getElementsByClassName("language-html");
const foundCode = foundCodes[0];
const SourceCode = foundCode.textContent || "";
openStackblitz(SourceCode);
};
const copy = function () {
const foundCodes = meta.value.getElementsByClassName("language-html");
const foundCode = foundCodes[0];
let successful = false;
// 使用原生系统剪贴板只适用被授权安全的站点http下不能使用
if (navigator.clipboard && document.hasFocus()) {
const text = foundCode.textContent || "";
navigator.clipboard.writeText(text);
successful = true;
} else if (window.getSelection()) {
// 使用 document.execCommand
var range = document.createRange();
let copyDiv;
if (show.value) {
range.selectNode(foundCode);
} else {
copyDiv = document.createElement("div");
copyDiv.innerHTML = foundCode.innerHTML;
copyDiv.style.position = "fixed";
copyDiv.style.left = "-9999px";
document.body.appendChild(copyDiv);
range.selectNode(copyDiv);
}
window.getSelection()?.addRange(range);
try {
successful = document.execCommand("copy");
} catch (err) {
successful = false;
console.error(err);
}
window.getSelection()?.removeAllRanges();
copyDiv?.remove();
}
if (successful) {
layer.msg("复制成功", { icon: 1, time: 1000 }, () => {});
} else {
layer.msg("复制失败", { icon: 2, time: 1000 }, () => {});
}
};
onMounted(() => {
const foundDescs = meta.value.getElementsByClassName("description");
const foundCodes = meta.value.getElementsByClassName("language-html");
if (foundDescs.length) {
codeAreaHeight.value =
foundDescs[0].clientHeight + foundCodes[0].clientHeight + 30;
} else {
codeAreaHeight.value = foundCodes[0].clientHeight + 20;
}
});
onUnmounted(() => {
window.removeEventListener("scroll", handleScroll);
});
watch(show, (value) => {
if (value) {
meta.value.style.height = `${codeAreaHeight.value}px`;
window.addEventListener("scroll", handleScroll);
setTimeout(handleScroll, 100);
} else {
meta.value.style.height = "0";
window.removeEventListener("scroll", handleScroll);
}
});
function handleScroll() {
const { top, bottom } = meta.value.getBoundingClientRect();
isFixContorl.value =
bottom > window.innerHeight && top + 44 <= window.innerHeight;
}
</script>
<style>
.lay-code {
margin: 1rem 0;
border: 1px solid whitesmoke;
border-radius: 3px;
transition: all 0.2s;
}
.lay-code .source {
padding: 24px;
padding-bottom: 15px;
}
.lay-code .meta {
padding: 0 10px;
height: 0;
overflow: hidden;
transition: height 0.2s;
}
.lay-code .source .description {
padding: 20px;
margin: 20px 0;
margin-bottom: 0px;
border: 1px solid whitesmoke;
box-sizing: border-box;
font-size: 14px;
line-height: 22px;
word-break: break-word;
}
.lay-code .source .description p {
margin: 0 !important;
line-height: 26px !important;
}
.lay-code .source .description code {
margin: 0 1px;
padding: 0.2em 0.4em;
font-size: 0.9em;
background: #f2f4f5;
border: 1px solid rgba(0, 0, 0, 0.06);
border-radius: 3px;
}
.lay-code .control {
height: 44px;
box-sizing: border-box;
margin-top: 10px;
border-top: 1px solid whitesmoke;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
background-color: white;
text-align: center;
cursor: pointer;
width: 100%;
user-select: none;
}
.lay-code .control.is-fixed {
position: sticky;
z-index: 11;
bottom: 0;
}
.lay-code .control > i {
display: inline-block;
font-size: 16px;
line-height: 44px;
transition: all 0.3s;
padding-left: 10px;
padding-right: 10px;
}
.btn:hover::before {
color: #5fb878;
}
.btn:hover svg > path {
fill: #5fb878;
}
</style>

View File

@@ -0,0 +1 @@
x+)JMU07f040031Q<31><51>KI<4B><49>K..fpk<70>rM\<5C>/%p<><70>3<EFBFBD>_<<3C><><EFBFBD>l<10><>(<28><>Ag<41>L<EFBFBD>3<EFBFBD><33>gv<67>\<5C>iᇪ<69><E187AA>"F<><01>:!<21>

View File

@@ -0,0 +1 @@
x<01><><EFBFBD>n<EFBFBD>0 E<><45><EFBFBD>(:<14>4<>^<5E><02><><EFBFBD>mQDC<44><43><EFBFBD><EFBFBD><EFBFBD>}<7D>gI<67>fh<66>d<EFBFBD><64>$/kK<6B>C<EFBFBD><43><EFBFBD><EFBFBD><EFBFBD>w<EFBFBD>O(<18>`<60><>i<EFBFBD>Ơ넅<C6A0><EB8485>v8<76>y <20>ahDFr҃ՌPk^M̴<4D>U<EFBFBD>I<EFBFBD>G<1C><><EFBFBD>z$;1<><31><EFBFBD><08><14> <0B>"<22>8ʲP<CAB2><50>p?<3F><><1E><>yy<79>N<EFBFBD><4E><EFBFBD>Z7<5A><37><EFBFBD><EFBFBD><EFBFBD><68>_ڶU<DAB6><55><EFBFBD><EFBFBD>W!<21>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>9ꛪ<39><1B><>k<EFBFBD><6B>(<28>œb8<62><38>;'p ^5<><1F><>@??<3F>P<EFBFBD><50><03>ӥ<EFBFBD><D3A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>h<EFBFBD><68><EFBFBD>%!2h9<68><39><EFBFBD><EFBFBD>b<EFBFBD><62><1F><1D>e0<65><{YO<>:-<2D><>ôh<C3B4>Z<EFBFBD><5A>8<EFBFBD>e<EFBFBD><65>R<EFBFBD><52>gY<67>V<EFBFBD>#I<>y<EFBFBD>[f<>4<>l[ 5{<7B>;t<> M<19>Q7<51><37>2]<5D><><EFBFBD> <20><>'<27>/<2F>qm<1F>4<EFBFBD>Π<EFBFBD><CEA0><EFBFBD><06><>msH<73><48>G^<5E><><EFBFBD><EFBFBD><19><><EFBFBD>BN<42><4E><EFBFBD>u<EFBFBD><75><EFBFBD>.<2E>+<2B><<3C>rATl2<6C><32>Q<EFBFBD><51>d<16>NF<4E><46><EFBFBD>rD<72><44> 7P7Q<37>k<EFBFBD>m<EFBFBD><6D><EFBFBD><EFBFBD><EFBFBD>{<05>,s <0C><>_<EFBFBD>LO<4C>c<EFBFBD>~<7E> <0B><><08>

View File

@@ -0,0 +1 @@
x+)JMU044`040031Q<31><51>KI<4B><49>K..fx+R<7F><52><EFBFBD>C7W<37>owY<77>}l<><5F>(<28><>a<EFBFBD><61>Ӕ{<7B><>fy[<5B>UH4ZT<5A><54><EFBFBD><EFBFBD>w<EFBFBD>j<EFBFBD><6A><EFBFBD><EFBFBD>V*<2A><><EFBFBD>W<EFBFBD><57><EFBFBD>b<EFBFBD><62>"<22>ҫ<EFBFBD>92k<01>50<35>