CodeModal.vue 5.2 KB
Newer Older
L
LeoKu 已提交
1 2
<template>
  <transition name="fade">
L
LeoKu 已提交
3 4
    <div v-if="props.visible" class="code-modal" @click.self="emit('close')">
      <div class="code-box">
L
LeoKu 已提交
5 6 7 8
        <div class="code-header">
          <div class="title">{{ t('text.codeModalTitle') }}</div>

          <div class="close-btn" @click="emit('close')">
L
LeoKu 已提交
9
            <img :src="IconClose" class="icon-close" :alt="t('action.close')" />
L
LeoKu 已提交
10 11 12 13 14 15 16 17
          </div>
        </div>

        <div class="code-content-box">
          <PerfectScrollbar
            class="code-scroll-wrapper"
            :options="{ suppressScrollX: false }"
          >
L
LeoKu 已提交
18
            <pre><code class="code-content" v-html="highlightedCode"></code></pre>
L
LeoKu 已提交
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
          </PerfectScrollbar>

          <button
            id="copy-code-btn"
            class="copy-btn"
            :class="{ copied: copied }"
            :data-clipboard-text="codeJSON"
          >
            {{ copied ? t('action.copied') : t('action.copyCode') }}
          </button>
        </div>
      </div>
    </div>
  </transition>
</template>

<script lang="ts" setup>
import ClipboardJS from 'clipboard'
import { computed, onMounted, onUnmounted, ref, watchEffect } from 'vue'
import { useI18n } from 'vue-i18n'

import IconClose from '@/assets/icons/icon-close.svg'
import PerfectScrollbar from '@/components/PerfectScrollbar.vue'
import { useAvatarOption } from '@/hooks'
import { highlightJSON } from '@/utils'

const props = defineProps<{ visible?: boolean }>()

const emit = defineEmits<{
  (e: 'close'): void
}>()

const { t } = useI18n()

const [avatarOption] = useAvatarOption()

const codeJSON = computed(() => JSON.stringify(avatarOption.value, null, 4))

const highlightedCode = ref('')

watchEffect(() => {
  if (codeJSON.value) {
    highlightedCode.value = highlightJSON(codeJSON.value)
  }
})

const copied = ref(false)

let clipboard: ClipboardJS

onMounted(() => {
  clipboard = new ClipboardJS('#copy-code-btn')

  clipboard.on('success', (e) => {
    copied.value = true

    setTimeout(() => {
      copied.value = false
    }, 800)

    e.clearSelection()
  })
})

onUnmounted(() => {
  clipboard.destroy()
})
</script>

<style lang="scss" scoped>
89 90
@use 'src/styles/var';

L
LeoKu 已提交
91 92 93 94 95 96 97 98 99 100
.code-modal {
  position: fixed;
  bottom: 0;
  left: 50%;
  z-index: 999;
  width: 100%;
  height: 100%;
  padding: 2rem 0;
  overflow: hidden;
  transform: translate(-50%, 0);
L
LeoKu 已提交
101
  backdrop-filter: blur(0.1rem);
L
LeoKu 已提交
102 103 104 105
}

.code-box {
  $code-header-height: 4rem;
L
LeoKu 已提交
106 107
  $code-box-side-padding-normal: 2rem;
  $code-box-side-padding-small: 1rem;
L
LeoKu 已提交
108 109 110 111 112
  position: relative;
  width: 75%;
  max-width: 800px;
  height: 100%;
  margin: 0 auto;
L
LeoKu 已提交
113 114
  padding: $code-header-height $code-box-side-padding-normal 2.5rem
    $code-box-side-padding-normal;
115
  background-color: lighten(var.$color-dark, 3);
L
LeoKu 已提交
116 117 118 119 120 121 122
  border-radius: 1rem;
  transition: width 0.2s;

  @media screen and (max-width: 1200px) {
    width: 75%;
  }

123
  @media screen and (max-width: var.$screen-md) {
L
LeoKu 已提交
124 125 126
    width: 80%;
  }

127
  @media screen and (max-width: var.$screen-sm) {
L
LeoKu 已提交
128
    width: 90%;
L
LeoKu 已提交
129 130 131 132 133 134
    padding: $code-header-height $code-box-side-padding-small 2.5rem
      $code-box-side-padding-small;

    .code-header {
      padding: 0 $code-box-side-padding-small;
    }
L
LeoKu 已提交
135 136 137 138 139 140 141 142 143 144
  }

  .code-header {
    position: absolute;
    top: 0;
    left: 0;
    display: flex;
    align-items: center;
    width: 100%;
    height: $code-header-height;
L
LeoKu 已提交
145
    padding: 0 $code-box-side-padding-normal;
L
LeoKu 已提交
146 147 148 149 150 151 152 153 154 155 156 157

    .title {
      font-weight: bold;
    }

    .close-btn {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 2rem;
      height: 2rem;
      margin-left: auto;
158
      background-color: lighten(var.$color-dark, 8);
L
LeoKu 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
      border-radius: 50%;
      cursor: pointer;

      .icon-close {
        width: 45%;
        opacity: 0.6;
        transition: opacity 0.2s;
      }

      &:hover {
        .icon-close {
          opacity: 1;
        }
      }
    }
  }

  .code-content-box {
    position: relative;
    height: 20rem;
    height: 100%;
    padding: 1rem 0;
181
    background: darken(var.$color-dark, 1);
L
LeoKu 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    border-radius: 0.8rem;

    .code-scroll-wrapper {
      height: 100%;
    }

    .copy-btn {
      position: absolute;
      top: 100%;
      left: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      width: 5rem;
      height: 2rem;
      color: #fff;
198
      background-color: var.$color-accent;
L
LeoKu 已提交
199 200 201 202 203 204
      border-radius: 0.4rem;
      transform: translate(-50%, -45%);
      cursor: pointer;
      transition: color 0.15s, background-color 0.15s;

      &.copied {
205 206
        color: var.$color-dark;
        background-color: var.$color-secondary;
L
LeoKu 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
      }
    }
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.25s ease, transform 0.25s;
}

.fade-enter-from,
.fade-leave-to {
  transform: translate(-50%, 2rem);
  opacity: 0;
}
</style>

<style lang="scss">
225 226
@use 'src/styles/var';

L
LeoKu 已提交
227 228 229 230 231 232 233 234 235
.code-content {
  display: block;
  padding: 0 1.5rem;
  color: #c0c5ce;
  color: #81cfef;
  font-size: 1.25rem;
  font-family: 'Ubuntu Mono', Fallback;
  line-height: 1.4;

236
  @media screen and (max-width: var.$screen-sm) {
L
LeoKu 已提交
237 238 239 240
    padding: 0 1rem;
    font-size: 1rem;
  }

L
LeoKu 已提交
241 242 243 244 245 246 247 248 249 250 251 252
  & > .token {
    &.key {
      color: #ffcb6b;
    }

    &.string,
    &.number {
      color: #c3e88d;
    }
  }
}
</style>