Index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <div>
  3. <doc-header></doc-header>
  4. <doc-nav></doc-nav>
  5. <div class="doc-content">
  6. <div class="doc-content-document">
  7. <theme-setting :name="routername" />
  8. </div>
  9. <doc-demo-preview :url="demoUrl"></doc-demo-preview>
  10. </div>
  11. </div>
  12. </template>
  13. <script lang="ts">
  14. import { defineComponent, onMounted, reactive, toRefs, computed } from 'vue';
  15. import { nav } from '@/config.json';
  16. import { onBeforeRouteUpdate, RouteLocationNormalized, useRoute, useRouter } from 'vue-router';
  17. import Header from '@/sites/doc/components/Header.vue';
  18. import Nav from '@/sites/doc/components/Nav.vue';
  19. import DemoPreview from '@/sites/doc/components/DemoPreview.vue';
  20. import ThemeSetting from '@/sites/doc/components/ThemeSetting/Index.vue';
  21. import { RefData } from '@/sites/assets/util/ref';
  22. export default defineComponent({
  23. name: 'doc',
  24. components: {
  25. [Header.name]: Header,
  26. [Nav.name]: Nav,
  27. [DemoPreview.name]: DemoPreview,
  28. [ThemeSetting.name]: ThemeSetting
  29. },
  30. setup() {
  31. const route = useRoute();
  32. const router = useRouter();
  33. const excludeTaro = ['/intro', '/start', '/theme', '/joinus', '/starttaro', '/contributing'];
  34. const data = reactive({
  35. demoUrl: 'demo.html',
  36. routername: 'base',
  37. curKey: 'vue',
  38. tabs: [
  39. {
  40. key: 'vue',
  41. text: 'vue'
  42. },
  43. {
  44. key: 'taro',
  45. text: 'taro'
  46. }
  47. ]
  48. });
  49. const configNav = computed(() => {
  50. let tarodocs = [] as string[];
  51. nav.map((item) => {
  52. item.packages.forEach((element) => {
  53. let { tarodoc, name } = element;
  54. if (tarodoc) {
  55. tarodocs.push(name.toLowerCase());
  56. tarodocs.push(`${name.toLowerCase()}-taro`);
  57. }
  58. });
  59. });
  60. return tarodocs;
  61. });
  62. const isTaro = (router: RouteLocationNormalized) => {
  63. return router.path.indexOf('taro') > -1;
  64. };
  65. const isShow = () => {
  66. return !excludeTaro.includes(route.path);
  67. };
  68. const isShowTaroDoc = computed(() => {
  69. return configNav.value.findIndex((item) => item === route.path.substr(1)) > -1;
  70. });
  71. const watchDemoUrl = (router: RouteLocationNormalized) => {
  72. const { origin, pathname } = window.location;
  73. RefData.getInstance().currentRoute.value = router.name as string;
  74. data.demoUrl = `${origin}${pathname.replace('index.html', '')}demo.html#${router.path}`;
  75. data.routername = router.name as string;
  76. };
  77. const watchDocMd = () => {
  78. const path = route.path;
  79. router.replace(isTaro(route) ? path.substr(0, path.length - 5) : `${path}-taro`);
  80. };
  81. const handleTabs = (curKey: string) => {
  82. data.curKey = curKey;
  83. watchDocMd();
  84. };
  85. onMounted(() => {
  86. watchDemoUrl(route);
  87. data.curKey = isTaro(route) ? 'taro' : 'vue';
  88. });
  89. onBeforeRouteUpdate((to) => {
  90. watchDemoUrl(to);
  91. data.curKey = isTaro(to) ? 'taro' : 'vue';
  92. });
  93. return {
  94. ...toRefs(data),
  95. handleTabs,
  96. isShow,
  97. isShowTaroDoc
  98. };
  99. }
  100. });
  101. </script>
  102. <style lang="scss" scoped>
  103. .doc {
  104. &-content {
  105. margin-left: 290px;
  106. display: flex;
  107. flex-direction: column;
  108. &-document {
  109. min-height: 800px;
  110. }
  111. &-tabs {
  112. position: absolute;
  113. right: 445px;
  114. top: 48px;
  115. display: flex;
  116. height: 50px;
  117. align-items: center;
  118. margin-bottom: 20px;
  119. z-index: 1;
  120. .tab-item {
  121. position: relative;
  122. padding: 10px 25px;
  123. height: 100%;
  124. cursor: pointer;
  125. font-size: 16px;
  126. color: #323232;
  127. text-align: center;
  128. border-radius: 4px;
  129. &.cur {
  130. color: #fa2c19;
  131. &:after {
  132. content: ' ';
  133. position: absolute;
  134. bottom: 0;
  135. left: 0;
  136. width: 100%;
  137. height: 3px;
  138. background-color: #fa2c19;
  139. }
  140. }
  141. &:hover {
  142. background-color: #f7f8fa;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. </style>