|
| 1 | +<template> |
| 2 | + <header class="site-header"> |
| 3 | + <div class="site-header__logo"> |
| 4 | + <router-link :to="{name: 'homepage'}"> |
| 5 | + <img src="/static/img/logo@2x.png" srcset="/static/img/logo.png 1x, /static/img/logo@2x.png 2x" alt=""> |
| 6 | + </router-link> |
| 7 | + </div> |
| 8 | + <div class="site-header__nav cd-clearfix"> |
| 9 | + <ul class=""> |
| 10 | + <li :class="{ active: routeName === nav.name }" |
| 11 | + @click="switchNav(nav.name)" |
| 12 | + v-for="(nav, index) in navMenu" |
| 13 | + :key="index"> |
| 14 | + {{nav.text}} |
| 15 | + </li> |
| 16 | + </ul> |
| 17 | + <theme-picker @ok="pickerOk"></theme-picker> |
| 18 | + </div> |
| 19 | + </header> |
| 20 | +</template> |
| 21 | + |
| 22 | +<script lang="ts"> |
| 23 | +import Vue from 'vue'; |
| 24 | +import { Component } from 'vue-property-decorator'; |
| 25 | +
|
| 26 | +import ThemePicker from '@/components/theme-picker.vue'; |
| 27 | +import { version } from 'codeages-design/package.json'; |
| 28 | +import { navMenu } from '@/data'; |
| 29 | +
|
| 30 | +@Component({ |
| 31 | + components: { |
| 32 | + ThemePicker, |
| 33 | + }, |
| 34 | +}) |
| 35 | +export default class extends Vue { |
| 36 | + navMenu: any[] = navMenu; |
| 37 | + routeName: string = null; |
| 38 | + chalk: string = (<any>window).chalk; |
| 39 | +
|
| 40 | + created() { |
| 41 | + this.getRoute(); |
| 42 | + } |
| 43 | +
|
| 44 | + getRoute() { |
| 45 | + this.routeName = this.$route.matched[0].name; |
| 46 | + } |
| 47 | +
|
| 48 | + switchNav(name) { |
| 49 | + this.$router.push({ name: name }); |
| 50 | + } |
| 51 | +
|
| 52 | + pickerOk(oldColor, newColor) { |
| 53 | + const chalkHandler = this.getHandler(oldColor, newColor, 'chalk-style'); |
| 54 | +
|
| 55 | + if (!this.chalk) { |
| 56 | + const url = `https://unpkg.com/codeages-design@${version}/dist/cd-main-color.css`; |
| 57 | + this.getCSSString(url, chalkHandler); |
| 58 | + } else { |
| 59 | + chalkHandler(); |
| 60 | + }; |
| 61 | +
|
| 62 | + const styles = [].slice.call(document.querySelectorAll('style')) |
| 63 | + .filter(style => { |
| 64 | + const text = style.innerText; |
| 65 | + return new RegExp(oldColor.hex, 'i').test(text) && /.site-/.test(text) && !/@font-face/.test(text); |
| 66 | + }) |
| 67 | +
|
| 68 | + styles.forEach(style => { |
| 69 | + const { innerText } = style; |
| 70 | + if (typeof innerText !== 'string') return; |
| 71 | + style.innerText = this.updateStyle(innerText, oldColor, newColor); |
| 72 | + }); |
| 73 | +
|
| 74 | + (<any>window).cd.message({ |
| 75 | + type: 'success', |
| 76 | + message: '修改主色调成功' |
| 77 | + }); |
| 78 | + } |
| 79 | +
|
| 80 | + getHandler(oldColor, newColor, id) { |
| 81 | + return () => { |
| 82 | + const newStyle = this.updateStyle(this.chalk, oldColor, newColor); |
| 83 | +
|
| 84 | + this.chalk = newStyle; |
| 85 | + (<any>window).chalk = newStyle; |
| 86 | +
|
| 87 | + let styleTag = document.getElementById(id); |
| 88 | + if (!styleTag) { |
| 89 | + styleTag = document.createElement('style'); |
| 90 | + styleTag.setAttribute('id', id); |
| 91 | + document.head.appendChild(styleTag); |
| 92 | + } |
| 93 | + styleTag.innerText = newStyle; |
| 94 | + } |
| 95 | + } |
| 96 | +
|
| 97 | + updateStyle(style, oldColor, newColor) { |
| 98 | + let newStyle = style; |
| 99 | +
|
| 100 | + const rgb = (color) => { |
| 101 | + return [color.rgba.r, color.rgba.g, color.rgba.b].join(','); |
| 102 | + } |
| 103 | +
|
| 104 | + newStyle = newStyle.replace(new RegExp(oldColor.hex, 'ig'), newColor.hex); |
| 105 | + newStyle = newStyle.replace(new RegExp(rgb(oldColor), 'ig'), rgb(newColor)); |
| 106 | +
|
| 107 | + return newStyle; |
| 108 | + } |
| 109 | +
|
| 110 | + getCSSString(url, callback) { |
| 111 | + const xhr = new XMLHttpRequest(); |
| 112 | + xhr.onreadystatechange = () => { |
| 113 | + if (xhr.readyState === 4 && xhr.status === 200) { |
| 114 | + this.chalk = xhr.responseText; |
| 115 | + (<any>window).chalk = xhr.responseText; |
| 116 | + callback(); |
| 117 | + } |
| 118 | + } |
| 119 | + xhr.open('GET', url); |
| 120 | + xhr.send(); |
| 121 | + } |
| 122 | +} |
| 123 | +</script> |
0 commit comments