aboutsummaryrefslogtreecommitdiff
path: root/src/components/TextareaCopyable.vue
blob: 9bcb65ccb7de7a5016e72306042e2cd41281d46c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<template>
  <div style="overflow-x: hidden; width: 100%">
    <n-card class="result-card">
      <n-scrollbar
        x-scrollable
        trigger="none"
        :style="height ? `min-height: ${height - 40 /* card padding */ + 10 /* negative margin compensation */}px` : ''"
      >
        <n-config-provider :hljs="hljs">
          <n-code :code="value" :language="language" :trim="false" />
        </n-config-provider>
      </n-scrollbar>
      <n-tooltip v-if="value" trigger="hover">
        <template #trigger>
          <div class="copy-button" :class="[copyPlacement]">
            <n-button circle secondary size="large" @click="onCopyClicked">
              <n-icon size="22" :component="Copy" />
            </n-button>
          </div>
        </template>
        <span>{{ tooltipText }}</span>
      </n-tooltip>
    </n-card>
    <n-space v-if="copyPlacement === 'outside'" justify="center" mt-4>
      <n-button secondary @click="onCopyClicked"> {{ tooltipText }} </n-button>
    </n-space>
  </div>
</template>

<script setup lang="ts">
import { Copy } from '@vicons/tabler';
import { useClipboard, useElementSize } from '@vueuse/core';
import hljs from 'highlight.js/lib/core';
import jsonHljs from 'highlight.js/lib/languages/json';
import sqlHljs from 'highlight.js/lib/languages/sql';
import xmlHljs from 'highlight.js/lib/languages/xml';
import yamlHljs from 'highlight.js/lib/languages/yaml';
import { ref, toRefs } from 'vue';

hljs.registerLanguage('sql', sqlHljs);
hljs.registerLanguage('json', jsonHljs);
hljs.registerLanguage('html', xmlHljs);
hljs.registerLanguage('yaml', yamlHljs);

const props = withDefaults(
  defineProps<{
    value: string;
    followHeightOf?: HTMLElement | null;
    language?: string;
    copyPlacement?: 'top-right' | 'bottom-right' | 'outside' | 'none';
    copyMessage?: string;
  }>(),
  {
    followHeightOf: null,
    language: 'txt',
    copyPlacement: 'top-right',
    copyMessage: 'Copy to clipboard',
  },
);
const { value, language, followHeightOf, copyPlacement, copyMessage } = toRefs(props);
const { height } = followHeightOf ? useElementSize(followHeightOf) : { height: ref(null) };

const { copy } = useClipboard({ source: value });
const tooltipText = ref(copyMessage.value);

function onCopyClicked() {
  copy();
  tooltipText.value = 'Copied !';

  setTimeout(() => {
    tooltipText.value = copyMessage.value;
  }, 2000);
}
</script>

<style lang="less" scoped>
::v-deep(.n-scrollbar) {
  padding-bottom: 10px;
  margin-bottom: -10px;
}
.result-card {
  position: relative;
  .copy-button {
    position: absolute;
    opacity: 1;

    &.top-right {
      top: 10px;
      right: 10px;
    }

    &.bottom-right {
      bottom: 10px;
      right: 10px;
    }
    &.outside,
    &.none {
      display: none;
    }
  }
}
</style>