toc-doc.vue 2.8 KB
Newer Older
郭维嘉 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
<template>
  <docFrame @updateShowDoc="$emit('updateShowDoc', $event)">
    <template #title>目录</template>
    <template #content>
      <ul>
        <li
          :data-type="item.tag"
          @click.prevent.stop="scrollToTitle(item)"
          :class="{ active: dirItemActive(item) }"
          v-for="item in dirTags"
          :key="item.id"
        >
          <a href="javascript:viod(0)">
            {{ item.text }}
          </a>
        </li>
      </ul>
    </template>
  </docFrame>
</template>
<script>
import docFrame from "./doc-frame.vue";
export default {
  components: { docFrame },
  props: {
    showHelp: {
      type: [Boolean, String],
      default: false
    },
    dirTags: {
      type: Array,
      default: () => []
    },
    scrollBarTop: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      list: [
        {
          title: "一级标题",
          doc: "# 标题",
          icon: "yijibiaoti"
        },
        {
          title: "二级标题",
          doc: "## 标题",
          icon: "erjibiaoti"
        },
        {
          title: "三级标题",
          doc: "### 标题",
          icon: "sanjibiaoti"
        }
      ]
    };
  },
  computed: {
    topList() {
      return this.dirTags.map(item => Math.abs(item?.top));
    }
  },
  created() {
    this.$emit(
      "update:scrollBarTop",
      document.querySelector(".md_preview_scroll_container").scrollTop
    );
  },
  methods: {
    scrollToTitle(item) {
      const targetEl = document.getElementById(item.id);
      if (!targetEl) return;
      const targetOffsetTop = targetEl.offsetTop;
      document.querySelector(
        ".md_preview .md_preview_scroll_container"
      ).scrollTop = targetOffsetTop;
    },
    dirItemActive(item) {
      const itemScrollTop = document.getElementById(item.id)?.offsetTop;
      const top = this.scrollBarTop - itemScrollTop;
      this.$set(item, "top", top);
      return Math.abs(top) === Math.min(...this.topList);
    }
  }
};
</script>

<style lang="less" scoped>
/deep/li {
  position: relative;
  cursor: pointer;
  a {
    text-decoration: none;
    color: inherit;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }
  &:hover,
  &.active {
    a {
      color: var(--md-editor-border-color-active);
    }
    &::before {
      color: var(--md-editor-border-color-active);
    }
  }
  &::before {
    content: "\·";
    position: absolute;
    left: 0;
    top: 50%;
    font-size: 20px;
    transform: translateY(-50%);
  }
  &[data-type="h1"] {
    padding-left: 20px;
  }
  &[data-type="h2"] {
    padding-left: 30px;
  }
  &[data-type="h3"] {
    padding-left: 40px;
  }
  &[data-type="h4"] {
    padding-left: 50px;
  }
  &[data-type="h5"] {
    padding-left: 60px;
  }
  &[data-type="h6"] {
    padding-left: 70px;
  }
}
</style>