♻️(document): 调整文档结构

This commit is contained in:
就眠儀式
2022-06-25 20:10:12 +08:00
parent d18425cb2a
commit b86b7d308c
166 changed files with 528 additions and 60 deletions

View File

@@ -0,0 +1,270 @@
<template>
<aside :class="classAside">
<div class="lay-aside-top">
<lay-button
type="primary"
size="xs"
:class="classAsideBtn"
@click="handlerBtnClick()"
>
<lay-icon :type="iconType" size="40"> </lay-icon>
</lay-button>
</div>
<lay-scroll :thumbWidth="0">
<ul>
<li
v-for="(anchor, index) in anchorList"
:key="index"
class="lay-aside-list"
:class="{ active: index === activeIndex }"
@click.prevent="handlerListItemClick(index, anchor)"
>
<a
:href="`#${anchor}`"
class="lay-aside-link"
:class="{ active: index === activeIndex }"
>{{ anchor }}</a
>
</li>
</ul>
</lay-scroll>
</aside>
</template>
<script setup lang="ts">
import { computed, onMounted, ref, shallowRef, watch } from "vue";
const props = defineProps<{
anchors?: Array<string> | string;
currIndex: number;
show: boolean;
}>();
let activeIndex = ref<number>(0);
const show = ref<boolean>(props.show);
const iconType = ref<string>("layui-icon-right");
const anchors: string | string[] | undefined = props.anchors;
const scrollTop = ref<number>(0);
const scrollRefEl = shallowRef<HTMLElement | undefined>(undefined);
let enableAnimation = false;
const anchorList = computed(() => {
return typeof anchors === "string" ? anchors?.split(",") : anchors;
});
const classAside = computed(() => [
"lay-aside",
{ "lay-aside-animation": enableAnimation },
{ "lay-aside-collapse": !show.value },
]);
const classAsideBtn = computed(() => {
let classBtn = [];
if (enableAnimation) {
classBtn = [
"lay-aside-collapse-btn",
"lay-aside-animation",
{ "lay-aside-collapse-btn-collapse": !show.value },
];
} else {
classBtn = [
"lay-aside-collapse-btn",
{ "lay-aside-collapse-btn-collapse": !show.value },
];
enableAnimation = true;
}
return classBtn;
});
const handlerBtnClick = () => {
show.value = !show.value;
};
const handlerListItemClick = (index: number, id: string) => {
activeIndex.value = index;
scrollToTitle(id);
};
/**锚点标签跟随滚动高亮 */
const handlerScroll = () => {
// 距离顶部 90 改变 activeIndex
scrollTop.value = getScrollTop(scrollRefEl.value) + 90;
anchorList.value?.forEach((item, index) => {
const elOffsetTop = document.getElementById(item)?.offsetTop;
if (elOffsetTop) {
if (index === 0 && scrollTop.value < elOffsetTop) {
activeIndex.value = 0;
} else if (scrollTop.value >= elOffsetTop) {
activeIndex.value = index;
}
}
});
};
const handlerCollapse = () => {
iconType.value = show.value ? "layui-icon-right" : "layui-icon-left";
// @ts-ignore
scrollRefEl.value!.firstElementChild!.style.marginRight = show.value
? "180px"
: "0px";
};
watch(show, () => {
handlerCollapse();
});
onMounted(() => {
// @ts-ignore TODO 封装 hooks
scrollRefEl.value = document.querySelector(".layui-body");
if (!scrollRefEl.value) {
throw new Error(`scroll element is not existed: ".layui-body"`);
}
scrollRefEl.value.scrollTop = 0;
scrollRefEl.value?.addEventListener("scroll", throttle(handlerScroll, 500));
// 如果已折叠,关闭组件初始渲染时的动画,然后自动开启
// @ts-ignore
show.value =
// @ts-ignore
scrollRefEl.value!.firstElementChild!.style.marginRight !== "0px";
enableAnimation = show.value;
});
/**获取滚动高度 */
const getScrollTop = (el: HTMLElement | undefined): number => {
return el
? el.scrollTop
: window.pageYOffset ||
document.documentElement.scrollTop ||
document.body.scrollTop ||
0;
};
/**平滑滚动 */
const scrollToTitle = (id: string): void => {
document.getElementById(id)?.scrollIntoView({
behavior: "smooth",
block: "start",
inline: "nearest",
});
};
const throttle = (func: Function, wait: number) => {
var timer: any = null;
return (...args: any) => {
if (!timer) {
timer = setTimeout(() => {
timer = null;
func.apply(this, args);
}, wait);
}
};
};
</script>
<style lang="less" scoped>
.lay-aside {
position: fixed;
top: 65px;
right: 17px;
box-sizing: border-box;
width: 180px;
padding: 0 25px;
border-left: 1px solid rgb(229 230 235);
transition: none;
-webkit-transition: none;
height: calc(100% - 60px);
}
.lay-aside-collapse {
right: -180px;
opacity: 0.7;
}
.lay-aside-top {
height: 29px;
}
.lay-aside-link {
display: inline-block;
padding: 1px 4px;
color: grey;
font-size: 13px;
line-height: 2;
max-width: 140px;
min-width: 68px;
text-decoration: none;
background-color: transparent;
border-radius: 2px;
cursor: pointer;
transition: all 0.1s cubic-bezier(0, 0, 1, 1);
}
.lay-aside-list {
position: relative;
margin: 5px 0px 0px 4px;
padding-left: 2px;
max-width: 140px;
border-radius: 2px;
list-style: none;
&:hover {
background-color: #f6f6f6 !important;
color: var(--global-checked-color);
}
&:active {
background-color: #f6f6f6 !important;
color: var(--global-checked-color);
}
&.active {
background-color: #f6f6f6 !important;
* {
color: var(--global-checked-color);
}
}
}
.lay-aside-collapse-btn {
position: fixed;
right: 197px;
top: calc(50% - 20px);
display: flex;
align-items: center;
justify-content: center;
width: 18px;
height: 40px;
background-color: #f6f6f6;
border-radius: 0px;
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
border: rgb(229 230 235) 1px solid;
border-right: none;
box-shadow: 2px 0 8px 0 rgb(29 35 41 / 5%);
transition: none;
-webkit-transition: none;
color: rgba(0, 0, 0, 0.8);
&:hover {
background-color: #e2e2e2;
}
}
.lay-aside-collapse-btn-collapse {
right: 0px;
}
.lay-aside-animation {
transition: right 200ms;
-webkit-transition: right 200ms;
}
@media screen and (max-width: 768px) {
.lay-aside {
width: 100px !important;
}
.lay-aside-collapse-btn {
right: 98px;
}
.lay-aside-collapse-btn-collapse {
right: 15px;
}
.lay-aside-list {
max-width: 68px;
}
}
</style>

View File

@@ -0,0 +1,203 @@
<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-play btn"
@click="onPlayground"
title="运行代码"
/>
<i
class="layui-icon layui-icon-file btn"
@click="copy"
title="复制代码"
/>
<i
class="layui-icon layui-icon-fonts-code btn"
@click="toggle"
title="查看代码"
/>
</div>
</div>
</template>
<script setup lang="ts">
import { layer } from "@layui/layer-vue";
import { onMounted, onUnmounted, ref, watch } from "vue";
import { usePlayGround } from "../composable/usePlayground";
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 usePlayGround(SourceCode, true);
window.open(link);
};
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 {
display: inline-block;
padding: 1px 5px;
margin: 0 4px;
height: 18px;
border-radius: 2px;
background-color: rgba(27, 31, 35, 0.05);
font-size: 12px;
line-height: 18px;
}
.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;
}
</style>

View File

@@ -0,0 +1,105 @@
<template>
<div class="contributor">
<lay-tooltip
:content="contributor.login"
v-for="contributor in contributors"
:key="contributor.id"
:is-dark="false"
>
<a :href="contributor.htmlUrl" target="_blank">
<lay-avatar :src="contributor.avatarUrl" radius></lay-avatar>
</a>
</lay-tooltip>
</div>
<lay-tooltip content="在线编辑" :is-dark="false">
<a
class="online-edit"
:href="
'https://github.com/layui/layui-vue/edit/next/package/document/src/document/zh-CN/components/' +
filePath +
'.md'
"
target="_blank"
>
<lay-icon type="layui-icon-edit"></lay-icon>
</a>
</lay-tooltip>
</template>
<script lang="ts" setup>
import axios from "axios";
import { Ref, ref } from "vue";
export interface LayContributor {
filePath: string;
}
const props = withDefaults(defineProps<LayContributor>(), {
filePath: "",
});
const suffix = ".md";
const githubAPI =
"https://api.github.com/repos/layui/layui-vue/commits?path=/package/document/src/document/zh-CN/components/";
const contributors: Ref<any> = ref([]);
var compare = function (x: any, y: any) {
if (x.commits < y.commits) {
return 1;
} else if (x.commits > y.commits) {
return -1;
} else {
return 0;
}
};
const fetchContributors = () => {
axios.get(githubAPI + props.filePath + suffix).then((result) => {
const topic: any[] = [];
result.data.forEach((item: any) => {
if (item.committer) {
const itemdata = {
id: item.committer["id"],
login: item.committer["login"],
avatarUrl: item.committer["avatar_url"],
htmlUrl: item.committer["html_url"],
commits: 1,
};
if (topic.some((e) => e.id == itemdata.id)) {
topic.forEach((item) => {
if (item.id === itemdata.id) {
item.commits = item.commits + 1;
}
});
} else {
topic.push(itemdata);
}
}
});
contributors.value = topic.sort(compare);
});
};
fetchContributors();
</script>
<style>
.contributor {
text-align: left;
margin: 30px 15px 30px 20px;
display: inline-block;
}
.contributor a {
height: 40px;
display: inline-block;
}
.contributor img {
margin: 5px;
}
.online-edit {
display: inline-block;
}
</style>

View File

@@ -0,0 +1,66 @@
<template>
<div id="docsearch"></div>
</template>
<script setup name="LaySelect" lang="ts">
import { onMounted } from "vue";
onMounted(() => {
// @ts-ignore
docsearch({
appId: "BIYZTK0F0Q",
apiKey: "de599990b844bc1b325310f61ad19a78",
indexName: "layui-vue",
container: "#docsearch",
debug: false,
});
});
</script>
<style>
.DocSearch-Button {
outline: none;
box-shadow: none;
}
.DocSearch-Button .DocSearch-Search-Icon,
.DocSearch-Button-Placeholder {
color: whitesmoke;
}
:root {
--docsearch-logo-color: var(--global-primary-color);
--docsearch-primary-color: var(--global-primary-color);
--docsearch-searchbox-background: rgba(255, 255, 255, 0.02);
--docsearch-searchbox-focus-background: rgba(255, 255, 255, 0.02);
--docsearch-container-background: rgba(0, 0, 0, 0.1);
--docsearch-searchbox-shadow: inset 0 0 0 1px var(--docsearch-primary-color);
}
.DocSearch-Button {
width: 150px;
border-radius: 50px;
}
.DocSearch-Button-Container {
padding-left: 2px;
}
.DocSearch-Button-Placeholder {
padding-left: 8px;
}
.DocSearch-Button,
.DocSearch-Button:hover,
.DocSearch-Button:active,
.DocSearch-Button:focus {
border: 1px solid rgb(224, 224, 230);
box-shadow: none;
}
.DocSearch-Button-Key {
border: 1px solid rgba(60, 60, 60, 0.29);
background: transparent;
box-shadow: none;
}
.DocSearch-Button .DocSearch-Search-Icon,
.DocSearch-Button-Placeholder {
color: rgba(0, 0, 0, 0.6);
}
.DocSearch-Button-Keys {
display: none;
}
</style>

View File

@@ -0,0 +1,38 @@
<template>
<div class="lay-table-box">
<slot></slot>
</div>
</template>
<style>
.lay-table-box table {
width: 100%;
border-collapse: collapse;
empty-cells: show;
border-right: 1px solid whitesmoke;
border-left: 1px solid whitesmoke;
border-radius: 4px;
}
.lay-table-box table tbody {
border-bottom: 1px solid whitesmoke;
}
.lay-table-box table th,
.lay-table-box table td {
font-size: 14px;
width: 50px;
max-width: 180px;
height: 50px;
border-top: 1px solid whitesmoke;
padding: 0 10px;
padding-left: 28px;
}
.lay-table-box table th {
color: #666;
font-weight: 500;
background-color: #fafafa;
}
.lay-table-box table th,
.lay-table-box table td {
white-space: nowrap;
}
</style>