Index.vue 7.5 KB
Newer Older
richard_1015's avatar
richard_1015 已提交
1
<template>
S
suzigang 已提交
2 3 4 5 6
  <div>
    <doc-header></doc-header>
    <doc-nav></doc-nav>
    <div class="doc-content">
      <div class="doc-content-document">
richard_1015's avatar
richard_1015 已提交
7
        <theme-setting :name="routername" />
S
suzigang 已提交
8
      </div>
A
ailululu 已提交
9
      <doc-demo-preview v-if="isShow()" :url="demoUrl" :class="{ fixed: fixed }"></doc-demo-preview>
richard_1015's avatar
richard_1015 已提交
10 11 12 13
    </div>
  </div>
</template>
<script lang="ts">
S
suzigang 已提交
14 15
import { defineComponent, onMounted, reactive, toRefs, computed } from 'vue';
import { nav } from '@/config.json';
16
import { onBeforeRouteUpdate, RouteLocationNormalized, useRoute, useRouter } from 'vue-router';
richard_1015's avatar
richard_1015 已提交
17 18 19
import Header from '@/sites/doc/components/Header.vue';
import Nav from '@/sites/doc/components/Nav.vue';
import DemoPreview from '@/sites/doc/components/DemoPreview.vue';
D
dongj0316 已提交
20
import ThemeSetting from '@/sites/doc/components/ThemeSetting/Index.vue';
richard_1015's avatar
richard_1015 已提交
21
import { RefData } from '@/sites/assets/util/ref';
richard_1015's avatar
richard_1015 已提交
22
import { initSiteLang } from '@/sites/assets/util/useTranslate';
richard_1015's avatar
richard_1015 已提交
23 24 25 26 27
export default defineComponent({
  name: 'doc',
  components: {
    [Header.name]: Header,
    [Nav.name]: Nav,
richard_1015's avatar
richard_1015 已提交
28 29
    [DemoPreview.name]: DemoPreview,
    [ThemeSetting.name]: ThemeSetting
richard_1015's avatar
richard_1015 已提交
30
  },
richard_1015's avatar
richard_1015 已提交
31
  setup() {
S
suzigang 已提交
32 33
    const route = useRoute();
    const router = useRouter();
richard_1015's avatar
richard_1015 已提交
34
    initSiteLang();
35
    const excludeTaro = ['/intro', '/start', '/theme', '/joinus', '/starttaro', '/contributing'];
A
ailululu 已提交
36
    const state = reactive({
Y
yangxiaolu3 已提交
37
      fixed: true, // 是否吸顶
A
ailululu 已提交
38 39 40 41 42 43 44
      hidden: false, // 是否隐藏
      // 组件名称
      componentName: {
        name: '',
        cName: ''
      }
    });
richard_1015's avatar
richard_1015 已提交
45
    const data = reactive({
S
suzigang 已提交
46
      demoUrl: 'demo.html',
richard_1015's avatar
richard_1015 已提交
47
      routername: 'base',
S
suzigang 已提交
48 49 50 51
      curKey: 'vue',
      tabs: [
        {
          key: 'vue',
S
suzigang 已提交
52
          text: 'vue'
S
suzigang 已提交
53 54 55
        },
        {
          key: 'taro',
S
suzigang 已提交
56
          text: 'taro'
S
suzigang 已提交
57 58 59 60 61
        }
      ]
    });

    const configNav = computed(() => {
S
suzigang 已提交
62
      let tarodocs = [] as string[];
S
suzigang 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
      nav.map((item) => {
        item.packages.forEach((element) => {
          let { tarodoc, name } = element;
          if (tarodoc) {
            tarodocs.push(name.toLowerCase());
            tarodocs.push(`${name.toLowerCase()}-taro`);
          }
        });
      });
      return tarodocs;
    });

    const isTaro = (router: RouteLocationNormalized) => {
      return router.path.indexOf('taro') > -1;
    };

    const isShow = () => {
A
ailululu 已提交
80
      // return !excludeTaro.includes(route.path);
81
      return route.path != 'zh-CN/' || 'zh-TW/' || 'en-US/';
S
suzigang 已提交
82 83
    };
    const isShowTaroDoc = computed(() => {
richard_1015's avatar
richard_1015 已提交
84 85
      let routename = route.path.toLocaleLowerCase().split('/').pop() || '';
      return configNav.value.findIndex((item) => item === routename) > -1;
richard_1015's avatar
richard_1015 已提交
86 87
    });

richard_1015's avatar
richard_1015 已提交
88
    const watchDemoUrl = (router: RouteLocationNormalized) => {
xiaozhumaopao's avatar
xiaozhumaopao 已提交
89
      const { origin, pathname } = window.location;
richard_1015's avatar
richard_1015 已提交
90
      RefData.getInstance().currentRoute.value = router.name as string;
91
      data.demoUrl = `${origin}${pathname.replace('index.html', '')}demo.html#${router.path}`;
richard_1015's avatar
richard_1015 已提交
92
      data.routername = router.name as string;
richard_1015's avatar
richard_1015 已提交
93 94
    };

A
ailululu 已提交
95
    const watchDocMd = (curKey: string) => {
S
suzigang 已提交
96
      const path = route.path;
A
ailululu 已提交
97 98 99 100 101 102
      // router.replace(isTaro(route) ? path.substr(0, path.length - 5) : `${path}-taro`);
      if (curKey.includes('taro')) {
        router.replace(isTaro(route) ? path : `${path}-taro`);
      } else {
        router.replace(isTaro(route) ? path.substr(0, path.length - 5) : path);
      }
S
suzigang 已提交
103 104 105 106
    };

    const handleTabs = (curKey: string) => {
      data.curKey = curKey;
A
ailululu 已提交
107
      watchDocMd(curKey);
S
suzigang 已提交
108 109
    };

richard_1015's avatar
richard_1015 已提交
110
    onMounted(() => {
A
ailululu 已提交
111
      componentTitle();
richard_1015's avatar
richard_1015 已提交
112
      watchDemoUrl(route);
S
suzigang 已提交
113
      data.curKey = isTaro(route) ? 'taro' : 'vue';
Y
yangxiaolu3 已提交
114
      // document.addEventListener('scroll', scrollTitle);
richard_1015's avatar
richard_1015 已提交
115 116
    });

A
ailululu 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    const scrollTitle = () => {
      let top = document.documentElement.scrollTop;
      // console.log('state.hidden', state.hidden)
      if (top > 127) {
        state.fixed = true;
        if (top < 142) {
          state.hidden = true;
        } else {
          state.hidden = false;
        }
      } else {
        state.fixed = false;
        state.hidden = false;
      }
    };

    // 获得组件名称
    const componentTitle = (to?: any) => {
      if (to?.path) {
136 137 138 139 140
        ['zh-CN/', 'zh-TW/', 'en-US/'].map((file) => {
          if (to.path.includes(file)) {
            state.componentName.name = to.path.split(file)[1];
          }
        });
A
ailululu 已提交
141
      } else {
142 143 144 145 146
        ['zh-CN/', 'zh-TW/', 'en-US/'].map((file) => {
          if (route.path.includes(file)) {
            state.componentName.name = route.path.split(file)[1];
          }
        });
A
ailululu 已提交
147 148 149 150 151 152 153 154 155 156 157 158
      }
      nav.forEach((item: any) => {
        item.packages.forEach((sItem: any) => {
          if (sItem.name.toLowerCase() == state.componentName.name) {
            state.componentName.name = sItem.name;
            state.componentName.cName = sItem.cName;
            return;
          }
        });
      });
    };

S
suzigang 已提交
159
    onBeforeRouteUpdate((to) => {
richard_1015's avatar
richard_1015 已提交
160
      watchDemoUrl(to);
S
suzigang 已提交
161
      data.curKey = isTaro(to) ? 'taro' : 'vue';
A
ailululu 已提交
162
      componentTitle(to);
richard_1015's avatar
richard_1015 已提交
163 164
    });

S
suzigang 已提交
165
    return {
A
ailululu 已提交
166
      ...toRefs(state),
S
suzigang 已提交
167 168
      ...toRefs(data),
      handleTabs,
169 170
      isShowTaroDoc,
      isShow
S
suzigang 已提交
171
    };
richard_1015's avatar
richard_1015 已提交
172 173 174 175 176
  }
});
</script>

<style lang="scss" scoped>
A
ailululu 已提交
177
$doc-title-height: 137px;
richard_1015's avatar
richard_1015 已提交
178 179
.doc {
  &-content {
richard_1015's avatar
richard_1015 已提交
180
    margin-left: 290px;
richard_1015's avatar
richard_1015 已提交
181
    display: flex;
richard_1015's avatar
richard_1015 已提交
182 183 184 185
    flex-direction: column;

    &-document {
      min-height: 800px;
A
ailululu 已提交
186 187 188 189

      .markdown-body {
        min-height: 600px;
      }
richard_1015's avatar
richard_1015 已提交
190
    }
S
suzigang 已提交
191 192
    &-tabs {
      position: absolute;
A
ailululu 已提交
193
      right: 475px;
S
suzigang 已提交
194 195
      top: 48px;
      display: flex;
A
ailululu 已提交
196
      height: 40px;
S
suzigang 已提交
197
      align-items: center;
A
ailululu 已提交
198
      justify-content: space-between;
S
suzigang 已提交
199
      z-index: 1;
A
ailululu 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212
      padding: 2px;
      box-sizing: border-box;

      border-radius: 2px;
      background: #eee;
      box-shadow: rgb(0 0 0 / 15%) 0px 2px 4px;
      &.single {
        padding: 0;
        .tab-item {
          line-height: 40px;
          cursor: auto;
        }
      }
S
suzigang 已提交
213 214
      .tab-item {
        position: relative;
A
ailululu 已提交
215 216
        padding: 0 10px;
        line-height: 36px;
S
suzigang 已提交
217
        cursor: pointer;
richard_1015's avatar
richard_1015 已提交
218
        font-size: 16px;
S
suzigang 已提交
219 220
        color: #323232;
        text-align: center;
A
ailululu 已提交
221 222
        border-radius: 2px;
        background: #eee;
S
suzigang 已提交
223
        &.cur {
A
ailululu 已提交
224 225 226
          font-weight: bold;
          color: #323232;
          background: #fff;
S
suzigang 已提交
227
        }
A
ailululu 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
      }
    }
    &-contributors {
      margin: 50px 0;
      a {
        position: relative;
      }
      img {
        height: 26px;
        height: 26px;
        border-radius: 50%;
        margin-left: 8px;
      }
      .contributors-hover {
        display: none;
        padding: 5px 10px;
        color: #fff;
        font-size: 12px;
        background-color: #000;
        border-radius: 5px;
        position: absolute;
        /* min-width:150px; */
        white-space: nowrap;
        top: -200%;
        transform: translateX(-55%);
      }
      a:hover {
        .contributors-hover {
          display: inline-block;
S
suzigang 已提交
257 258 259
        }
      }
    }
richard_1015's avatar
richard_1015 已提交
260
  }
A
ailululu 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  &-title {
    width: 100%;
    height: $doc-title-height;
    z-index: 2;
    &-position {
      top: 0px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 40px;
      // line-height: 56px;
      border-bottom: 1px solid #eee;
      background: #fff;
      visibility: visible;
      opacity: 1;
      // transition: opacity 0.8s linear, visibility 0.8s linear;
      transition: opacity 0.8s;
      &.fixed {
        width: calc(100% - 290px);
        position: fixed;
        padding: 24px 48px;
        .title {
          font-size: 24px;
          font-weight: bold;
S
suzigang 已提交
285 286
        }
      }
A
ailululu 已提交
287 288 289 290 291 292 293 294
      &.hidden {
        visibility: hidden;
        opacity: 0;
      }
      .title {
        font-size: 40px;
        font-weight: bold;
      }
S
suzigang 已提交
295
    }
richard_1015's avatar
richard_1015 已提交
296 297 298
  }
}
</style>