wangzhibo
6 天以前 7bd866831780abcf5a59c4cbb7d1be456eea7c99
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<template v-if="text">
    <div class="cl-code-json__wrap" v-if="popover">
        <el-popover
            width="auto"
            placement="right"
            popper-class="cl-code-json__popper"
            effect="dark"
        >
            <template #reference>
                <span class="cl-code-json__text">{{ text }}</span>
            </template>
 
            <viewer />
        </el-popover>
    </div>
 
    <viewer v-else>
        <template #op>
            <slot name="op"> </slot>
        </template>
    </viewer>
</template>
 
<script lang="tsx" setup>
defineOptions({
    name: 'cl-code-json'
});
 
import { useClipboard } from '@vueuse/core';
import { ElMessage } from 'element-plus';
import { isObject, isString } from 'lodash-es';
import { computed, defineComponent } from 'vue';
import { useI18n } from 'vue-i18n';
 
const props = defineProps({
    content: null,
    modelValue: null,
    popover: Boolean,
    height: {
        type: [Number, String],
        default: '100%'
    },
    maxHeight: {
        type: [Number, String],
        default: 300
    },
    title: String
});
 
const { copy } = useClipboard();
const { t } = useI18n();
 
// 文本
const text = computed(() => {
    const v = props.modelValue || props.content;
 
    if (isString(v)) {
        return v;
    } else if (isObject(v)) {
        return JSON.stringify(v, null, 4);
    } else {
        return String(v);
    }
});
 
// 视图
const viewer = defineComponent({
    setup(_, { slots }) {
        function toCopy() {
            copy(text.value);
            ElMessage.success('复制成功');
        }
 
        return () => {
            return (
                <div class="cl-code-json">
                    <div class="cl-code-json__op">
                        {text.value != '{}' && (
                            <el-button type="success" size="small" onClick={toCopy}>
                                {t('复制')}
                            </el-button>
                        )}
 
                        {slots.op && slots.op()}
                    </div>
 
                    {props.title && <div class="cl-code-json__title">{props.title}</div>}
 
                    <el-scrollbar
                        class="cl-code-json__content"
                        max-height={props.maxHeight}
                        height={props.height}
                    >
                        <pre>
                            <code>{text.value}</code>
                        </pre>
                    </el-scrollbar>
                </div>
            );
        };
    }
});
</script>
 
<style lang="scss">
.cl-code-json {
    position: relative;
    min-width: 200px;
    max-width: 500px;
    font-size: 14px;
 
    &__op {
        position: absolute;
        right: 8px;
        top: 8px;
        z-index: 9;
    }
 
    &__title {
        padding: 10px;
        font-size: 12px;
        color: var(--el-text-color-regular);
 
        & + .cl-code-json__content {
            padding-top: 0;
        }
    }
 
    &__content {
        padding: 10px;
 
        code {
            white-space: pre-wrap;
        }
    }
 
    &__wrap {
        .cl-code-json__text {
            display: block;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
            cursor: pointer;
 
            &:hover {
                color: var(--el-color-primary);
            }
        }
    }
 
    &__popper {
        padding: 0 !important;
        border-radius: 8px !important;
    }
}
</style>