1
0

aThemeSwitch.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. {{define "component/themeSwitchTemplate"}}
  2. <template>
  3. <a-menu :theme="themeSwitcher.currentTheme" mode="inline" selected-keys="">
  4. <a-sub-menu>
  5. <span slot="title">
  6. <a-icon type="bulb" :theme="themeSwitcher.isDarkTheme ? 'filled' : 'outlined'"></a-icon>
  7. <span>{{ i18n "menu.theme" }}</span>
  8. </span>
  9. <a-menu-item id="change-theme" class="ant-menu-theme-switch" @mousedown="themeSwitcher.animationsOff()">
  10. <span>{{ i18n "menu.dark" }}</span>
  11. <a-switch :style="{ marginLeft: '2px' }" size="small" :default-checked="themeSwitcher.isDarkTheme"
  12. @change="themeSwitcher.toggleTheme()"></a-switch>
  13. </a-menu-item>
  14. <a-menu-item id="change-theme-ultra" v-if="themeSwitcher.isDarkTheme" class="ant-menu-theme-switch"
  15. @mousedown="themeSwitcher.animationsOffUltra()">
  16. <span>{{ i18n "menu.ultraDark" }}</span>
  17. <a-checkbox :style="{ marginLeft: '2px' }" :checked="themeSwitcher.isUltra"
  18. @click="themeSwitcher.toggleUltra()"></a-checkbox>
  19. </a-menu-item>
  20. </a-sub-menu>
  21. </a-menu>
  22. </template>
  23. {{end}}
  24. {{define "component/themeSwitchTemplateLogin"}}
  25. <template>
  26. <a-space @mousedown="themeSwitcher.animationsOff()" id="change-theme" direction="vertical" :size="10" :style="{ width: '100%' }">
  27. <a-space direction="horizontal" size="small">
  28. <a-switch size="small" :default-checked="themeSwitcher.isDarkTheme" @change="themeSwitcher.toggleTheme()"></a-switch>
  29. <span>{{ i18n "menu.dark" }}</span>
  30. </a-space>
  31. <a-space v-if="themeSwitcher.isDarkTheme" direction="horizontal" size="small">
  32. <a-checkbox :checked="themeSwitcher.isUltra" @click="themeSwitcher.toggleUltra()"></a-checkbox>
  33. <span>{{ i18n "menu.ultraDark" }}</span>
  34. </a-space>
  35. </a-space>
  36. </template>
  37. {{end}}
  38. {{define "component/aThemeSwitch"}}
  39. <script>
  40. function createThemeSwitcher() {
  41. const darkModePreference = localStorage.getItem('dark-mode');
  42. const isDarkTheme = darkModePreference === null ? true : darkModePreference === 'true';
  43. const isUltra = localStorage.getItem('isUltraDarkThemeEnabled') === 'true';
  44. if (isUltra) {
  45. document.documentElement.setAttribute('data-theme', 'ultra-dark');
  46. }
  47. const theme = isDarkTheme ? 'dark' : 'light';
  48. document.querySelector('body').setAttribute('class', theme);
  49. return {
  50. animationsOff() {
  51. document.documentElement.setAttribute('data-theme-animations', 'off');
  52. const themeAnimations = document.querySelector('#change-theme');
  53. themeAnimations.addEventListener('mouseleave', () => {
  54. document.documentElement.removeAttribute('data-theme-animations');
  55. });
  56. themeAnimations.addEventListener('touchend', () => {
  57. document.documentElement.removeAttribute('data-theme-animations');
  58. });
  59. },
  60. animationsOffUltra() {
  61. document.documentElement.setAttribute('data-theme-animations', 'off');
  62. const themeAnimationsUltra = document.querySelector('#change-theme-ultra');
  63. themeAnimationsUltra.addEventListener('mouseleave', () => {
  64. document.documentElement.removeAttribute('data-theme-animations');
  65. });
  66. themeAnimationsUltra.addEventListener('touchend', () => {
  67. document.documentElement.removeAttribute('data-theme-animations');
  68. });
  69. },
  70. isDarkTheme,
  71. isUltra,
  72. get currentTheme() {
  73. return this.isDarkTheme ? 'dark' : 'light';
  74. },
  75. toggleTheme() {
  76. this.isDarkTheme = !this.isDarkTheme;
  77. localStorage.setItem('dark-mode', this.isDarkTheme);
  78. document.querySelector('body').setAttribute('class', this.isDarkTheme ? 'dark' : 'light');
  79. document.getElementById('message').className = themeSwitcher.currentTheme;
  80. },
  81. toggleUltra() {
  82. this.isUltra = !this.isUltra;
  83. if (this.isUltra) {
  84. document.documentElement.setAttribute('data-theme', 'ultra-dark');
  85. } else {
  86. document.documentElement.removeAttribute('data-theme');
  87. }
  88. localStorage.setItem('isUltraDarkThemeEnabled', this.isUltra.toString());
  89. }
  90. };
  91. }
  92. const themeSwitcher = createThemeSwitcher();
  93. Vue.component('a-theme-switch', {
  94. template: `{{template "component/themeSwitchTemplate"}}`,
  95. data: () => ({
  96. themeSwitcher
  97. }),
  98. mounted() {
  99. this.$message.config({
  100. getContainer: () => document.getElementById('message')
  101. });
  102. document.getElementById('message').className = themeSwitcher.currentTheme;
  103. }
  104. });
  105. Vue.component('a-theme-switch-login', {
  106. template: `{{template "component/themeSwitchTemplateLogin"}}`,
  107. data: () => ({
  108. themeSwitcher
  109. }),
  110. mounted() {
  111. this.$message.config({
  112. getContainer: () => document.getElementById('message')
  113. });
  114. document.getElementById('message').className = themeSwitcher.currentTheme;
  115. }
  116. });
  117. </script>
  118. {{end}}