clipboard切换为vue-clipboard3

This commit is contained in:
21030442-mao 2021-07-08 17:27:01 +08:00
parent b3a6bb6a8e
commit 6c28fb5211
4 changed files with 79 additions and 54 deletions

View File

@ -8,7 +8,6 @@
},
"dependencies": {
"axios": "^0.21.1",
"clipboard": "^2.0.8",
"countup.js": "^2.0.7",
"cropperjs": "^1.5.12",
"echarts": "^5.1.2",
@ -22,6 +21,7 @@
"sortablejs": "^1.14.0",
"splitpanes": "^3.0.4",
"vue": "^3.0.11",
"vue-clipboard3": "^1.0.1",
"vue-grid-layout": "^3.0.0-beta1",
"vue-i18n": "^9.1.4",
"vue-router": "^4.0.8",

View File

@ -356,7 +356,7 @@
icon="el-icon-document-copy"
type="primary"
ref="copyConfigBtnRef"
@click="onCopyConfigClick($event.target)"
@click="onCopyConfigClick"
>{{ $t('message.layout.copyText') }}
</el-button>
</div>
@ -366,22 +366,19 @@
</template>
<script lang="ts">
import { nextTick, onUnmounted, onMounted, getCurrentInstance, defineComponent, computed, ref } from 'vue';
import { ElMessage } from 'element-plus';
import ClipboardJS from 'clipboard';
import { useI18n } from 'vue-i18n';
import { nextTick, onUnmounted, onMounted, getCurrentInstance, defineComponent, computed } from 'vue';
import { useStore } from '/@/store/index';
import { getLightColor } from '/@/utils/theme';
import Watermark from '/@/utils/wartermark';
import { verifyAndSpace } from '/@/utils/toolsValidate';
import { Local } from '/@/utils/storage';
import commonFunction from '/@/utils/commonFunction.ts';
export default defineComponent({
name: 'layoutBreadcrumbSeting',
setup() {
const { t } = useI18n();
const { proxy } = getCurrentInstance() as any;
const copyConfigBtnRef = ref();
const store = useStore();
const { copyText } = commonFunction();
//
const getThemeConfig = computed(() => {
return store.state.themeConfig.themeConfig;
@ -557,10 +554,6 @@ export default defineComponent({
//
const openDrawer = () => {
getThemeConfig.value.isDrawer = true;
nextTick(() => {
//
onCopyConfigClick(copyConfigBtnRef.value.$el);
});
};
// store
const setDispatchThemeConfig = () => {
@ -577,20 +570,11 @@ export default defineComponent({
Local.set('themeConfigStyle', document.documentElement.style.cssText);
};
//
const onCopyConfigClick = (target: any) => {
const onCopyConfigClick = () => {
let copyThemeConfig = Local.get('themeConfig');
copyThemeConfig.isDrawer = false;
const clipboard = new ClipboardJS(target, {
text: () => JSON.stringify(copyThemeConfig),
});
clipboard.on('success', () => {
copyText(JSON.stringify(copyThemeConfig)).then(() => {
getThemeConfig.value.isDrawer = false;
ElMessage.success(t('message.layout.copyTextSuccess'));
clipboard.destroy();
});
clipboard.on('error', () => {
ElMessage.error(t('message.layout.copyTextError'));
clipboard.destroy();
});
};
// 退()
@ -668,7 +652,6 @@ export default defineComponent({
onClassicSplitMenuChange,
onIsBreadcrumbChange,
onSortableTagsViewChange,
copyConfigBtnRef,
onCopyConfigClick,
};
},

View File

@ -0,0 +1,65 @@
// 通用函数
import useClipboard from 'vue-clipboard3';
import { ElMessage } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import { useI18n } from 'vue-i18n';
export default function () {
const { t } = useI18n();
const { toClipboard } = useClipboard();
//百分比格式化
const percentFormat = (row: any, column: number, cellValue: any) => {
return cellValue ? `${cellValue}%` : '-';
};
//列表日期时间格式化
const dateFormatYMD = (row: any, column: number, cellValue: any) => {
if (!cellValue) return '-';
return formatDate(new Date(cellValue), 'YYYY-mm-dd');
};
//列表日期时间格式化
const dateFormatYMDHMS = (row: any, column: number, cellValue: any) => {
if (!cellValue) return '-';
return formatDate(new Date(cellValue), 'YYYY-mm-dd HH:MM:SS');
};
//列表日期时间格式化
const dateFormatHMS = (row: any, column: number, cellValue: any) => {
if (!cellValue) return '-';
let time = 0;
if (typeof row === 'number') time = row;
if (typeof cellValue === 'number') time = cellValue;
return formatDate(new Date(time * 1000), 'HH:MM:SS');
};
// 小数格式化
const scaleFormat = (value: any = 0, scale: number = 4) => {
return Number.parseFloat(value).toFixed(scale);
};
// 小数格式化
const scale2Format = (value: any = 0) => {
return Number.parseFloat(value).toFixed(2);
};
// 点击复制文本
const copyText = (text: string) => {
return new Promise((resolve, reject) => {
try {
//复制
toClipboard(text);
//下面可以设置复制成功的提示框等操作
ElMessage.success(t('message.layout.copyTextSuccess'));
resolve(text);
} catch (e) {
//复制失败
ElMessage.error(t('message.layout.copyTextError'));
reject(e);
}
});
};
return {
percentFormat,
dateFormatYMD,
dateFormatYMDHMS,
dateFormatHMS,
scaleFormat,
scale2Format,
copyText,
};
}

View File

@ -2,14 +2,14 @@
<div id="printRref">
<el-card shadow="hover" header="复制剪切演示">
<el-alert
title="感谢优秀的 `clipboard`项目地址https://github.com/zenorocha/clipboard.js`"
title="感谢优秀的 `vue-clipboard3`项目地址https://github.com/JamieCurnow/vue-clipboard3`"
type="success"
:closable="false"
class="mb15"
></el-alert>
<el-input placeholder="请输入内容" v-model="copyVal">
<template #append>
<el-button @click="onCopyClick($event.target)" ref="copyBtnRef">复制链接</el-button>
<el-button @click="copyText(copyVal)">复制链接</el-button>
</template>
</el-input>
<el-input placeholder="先点击上方 `复制链接` 按钮,然后 `Ctrl + V` 进行粘贴! " v-model="shearVal" class="mt15"> </el-input>
@ -18,43 +18,20 @@
</template>
<script lang="ts">
import { reactive, toRefs, ref, onMounted, nextTick } from 'vue';
import { ElMessage } from 'element-plus';
import ClipboardJS from 'clipboard';
import { useI18n } from 'vue-i18n';
import { reactive, toRefs, onMounted } from 'vue';
import commonFunction from '/@/utils/commonFunction.ts';
export default {
name: 'funClipboard',
setup() {
const { t } = useI18n();
const copyBtnRef = ref();
const { copyText } = commonFunction();
const state = reactive({
copyVal: 'https://gitee.com/lyt-top/vue-next-admin',
shearVal: '',
});
//
const onCopyClick = (target: any) => {
const clipboard = new ClipboardJS(target, {
text: () => state.copyVal,
});
clipboard.on('success', () => {
ElMessage.success(t('message.layout.copyTextSuccess'));
clipboard.destroy();
});
clipboard.on('error', () => {
ElMessage.error(t('message.layout.copyTextError'));
clipboard.destroy();
});
};
//
onMounted(() => {
nextTick(() => {
//
onCopyClick(copyBtnRef.value.$el);
});
});
onMounted(() => {});
return {
copyBtnRef,
onCopyClick,
copyText,
...toRefs(state),
};
},