Browse Source

增加复制功能及示例

pull/79/head
zq 2 days ago
parent
commit
aeab64c1bd
  1. 14
      src/main.js
  2. 80
      src/utils/clipboard.js
  3. 19
      src/utils/dirClipBoard.js
  4. 113
      src/views/elementGroups.vue

14
src/main.js

@ -14,11 +14,19 @@ import LoadingService from './services/loading'
import { directive as clickaway } from 'vue-clickaway';
import GuipMessage from '@/components/GuipMessage/index';
import HeaderIcon from './utils/headerIcon'
// 复制到粘贴板
import clipboard from '@/utils/dirClipBoard';
import { modernCopyToClipboard } from '@/utils/clipboard';
// 复制
Vue.prototype.$copy = modernCopyToClipboard;
Vue.prototype.$loadingFn = LoadingService;
Vue.config.productionTip = false;
// 请求
Vue.prototype.$http = request;
Vue.use(ElementUI);
Vue.use(GuipMessage)
Vue.use(clipboard);
Vue.directive('clickaway', clickaway);
Vue.mixin(HeaderIcon)
new Vue({
@ -27,9 +35,3 @@ new Vue({
render: h => h(App)
}).$mount('#app')
// function q(e) {
// for (var t = 1; t < arguments.length; t++) {
// var r = null != arguments[t] ? arguments[t] : {};
// t % 2 ? F(Object(r), !0).forEach((function (t) { T(e, t, r[t]) })) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(r)) : F(Object(r)).forEach((function (t) { Object.defineProperty(e, t, Object.getOwnPropertyDescriptor(r, t)) })) } return e }

80
src/utils/clipboard.js

@ -0,0 +1,80 @@
/**
* 复制文本到剪贴板
* @param {string} text 要复制的文本
* @param {Object} options 配置选项
* @param {string} options.successMsg 成功提示信息
* @param {string} options.errorMsg 失败提示信息
* @param {Vue} options.vm Vue实例(用于调用$message)
* @returns {Promise<boolean>} 是否复制成功
*/
export function copyToClipboard(text, options = {}) {
const {
successMsg = '复制成功',
errorMsg = '复制失败,请手动复制',
vm = null
} = options;
return new Promise((resolve) => {
// 创建textarea元素
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.position = 'fixed'; // 防止页面滚动
document.body.appendChild(textarea);
textarea.select();
try {
// 执行复制命令
const successful = document.execCommand('copy');
if (successful) {
if (vm && vm.$Message) {
vm.$Message.success(successMsg);
} else {
console.log(successMsg);
}
resolve(true);
} else {
throw new Error('Copy command was unsuccessful');
}
} catch (err) {
console.error('复制失败:', err);
if (vm && vm.$Message) {
vm.$Message.error(errorMsg);
} else {
console.error(errorMsg);
}
resolve(false);
} finally {
document.body.removeChild(textarea);
}
});
}
/**
* @param {string} text 要复制的文本
* @param {Object} options 配置选项
* @returns {Promise<boolean>} 是否复制成功
*/
export async function modernCopyToClipboard(text, options = {}) {
const {
successMsg = '复制成功',
errorMsg = '复制失败,请手动复制',
vm = null
} = options;
try {
// 使用现代剪贴板API
await navigator.clipboard.writeText(text);
if (vm && vm.$Message) {
vm.$Message.success(successMsg);
} else {
console.log(successMsg);
}
return true;
} catch (err) {
console.error(errorMsg, err);
// 现代API失败后回退到传统方法
return copyToClipboard(text, options);
}
}
export default modernCopyToClipboard;

19
src/utils/dirClipBoard.js

@ -0,0 +1,19 @@
import modernCopyToClipboard from '@/utils/clipboard';
export default {
install(Vue) {
Vue.directive('clipboard', {
bind(el, binding) {
el.style.cursor = 'pointer';
el.addEventListener('click', async () => {
const text = binding.value || el.innerText;
const options = {
vm: binding.instance,
...(binding.arg || {})
};
await modernCopyToClipboard(text, options);
});
}
});
}
};

113
src/views/elementGroups.vue

@ -1,49 +1,67 @@
<template>
<div class="main-content12">
<div class="elementWrap">
<PromptText text='这是一个提示框' :type="1"/>
<PromptText text='这是一个提示框' :type="2"/>
<PromptText text='这是一个提示框' :type="3"/>
<PromptText text='这是一个提示框' :type="1" />
<PromptText text='这是一个提示框' :type="2" />
<PromptText text='这是一个提示框' :type="3" />
<PromptText text="前期准备事项" :type="1" class="beforeNotice">
<template #next_desc>
<p class="flex">
<a href="https://e.360.cn/static/zhihui/login/?rdurl=https%3A%2F%2Fe.360.cn%2F"
class="flex" target="_blank">
<img src="@/assets/site/form_linkActive.svg" alt="">前往登陆</a>登录360智慧平台
<a href="https://e.360.cn/static/zhihui/login/?rdurl=https%3A%2F%2Fe.360.cn%2F" class="flex"
target="_blank">
<img src="@/assets/site/form_linkActive.svg" alt="">前往登陆</a>登录360智慧平台
</p>
</template>
<template #desc>
<p class="mt12 flex">
1. <a href="https://e.360.cn/static/zhihui/login/?rdurl=https%3A%2F%2Fe.360.cn%2F"
class="flex" target="_blank">
<img src="@/assets/site/form_linkActive.svg" alt="">前往登陆</a>登录360智慧平台
1. <a href="https://e.360.cn/static/zhihui/login/?rdurl=https%3A%2F%2Fe.360.cn%2F" class="flex"
target="_blank">
<img src="@/assets/site/form_linkActive.svg" alt="">前往登陆</a>登录360智慧平台
</p>
<p class="mt10 flex">
2. <a href="https://dianjing.e.360.cn/ocpc/list" class="flex" target="_blank">
<img src="@/assets/site/form_linkActive.svg"
alt="">前往ocpc设置页</a>点击添加OCPC投放包按钮注意转换类型只能选择订单其他的根据表单提示填写
<img src="@/assets/site/form_linkActive.svg"
alt="">前往ocpc设置页</a>点击添加OCPC投放包按钮注意转换类型只能选择订单其他的根据表单提示填写
</p>
</template>
</PromptText>
<div class="ele-item">
<label for="">实时预览外层嵌套</label>
<!-- 默认 显示电脑端手机端 -->
<!-- :showMobile="false" :showPc="false" -->
<DevicePreview >
<template #desktop>
电脑端内容-内容自定义
</template>
<template #mobile>
内容自定义
手机端端内容-- 手机端端内容
</template>
</DevicePreview>
</div>
<label for="">实时预览外层嵌套</label>
<!-- 默认 显示电脑端手机端 -->
<!-- :showMobile="false" :showPc="false" -->
<DevicePreview>
<template #desktop>
电脑端内容-内容自定义
</template>
<template #mobile>
内容自定义
手机端端内容-- 手机端端内容
</template>
</DevicePreview>
</div>
<!-- ele-item 为类名的这种div label h3 仅在此页面进行布局使用复制时无需复制此元素 -->
<el-form :model="form" :rules="rules" class="el-row demo-ruleForm" ref="formRef">
<div class="ele-item">
<label for="">copy固定内容</label>
<!-- 复制固定文本 -->
<GuipButton size="big" v-clipboard="'渝过田晴'">复制渝过田晴</GuipButton>
<!-- 复制动态文本 -->
<GuipButton size="big" v-clipboard="content">点击复制: {{ content }}</GuipButton>
</div>
<div class="ele-item">
<label for="">手动点击copy</label>
<GuipInput v-model="form.input1">
<!-- 提示可以不添加 图标可更换 -->
<GuipToolTip content="点击复制到粘贴板" slot="suffix">
<img src="@/assets/menu/Totalprofit.svg" @click="handleClickCopy" />
</GuipToolTip>
</GuipInput>
</div>
<div class="ele-item">
<label for="">单选框(对象格式)</label>
<GuipRadio v-model="form.language" :options="languageOptions" label="选择语言" prop="language"
@ -260,7 +278,7 @@
<!-- 宽高单独传递 其余统一放置在 customStyle -->
<hover-button button-text="自定义" :default-icon="require('@/assets/site/addIcon.svg')"
:hover-icon="require('@/assets/upLoad_active.svg')" default-text-color="#626573"
hover-text-color="#006AFF" width="110px" height="32px"
hover-text-color="#006AFF" width="110px" height="32px"
:customStyle="{ fontSize: '12px', background: '#fff', borderRadius: '2px', borderColor: '#DFE2E6' }" />
</div>
@ -343,20 +361,24 @@
<label for="">开关L</label>
<!-- active-value 开启状态的值 -->
<!-- inactive-value 关闭状态的值 -->
<GuipSwitch :modelValue="switchValue" activeText="默认类型boolean" inactiveText="关闭" @change="onSwitchChange">
<GuipSwitch :modelValue="switchValue" activeText="默认类型boolean" inactiveText="关闭"
@change="onSwitchChange">
</GuipSwitch>
</div>
<div class="ele-item">
<label for="">开关L</label>
<!-- 左侧不需要添加float 属性 -->
<GuipSwitch :modelValue="switchValue" float="right" activeText="文案在右,默认左侧" inactiveText="关闭" @change="onSwitchChange">
<GuipSwitch :modelValue="switchValue" float="right" activeText="文案在右,默认左侧" inactiveText="关闭"
@change="onSwitchChange">
</GuipSwitch>
</div>
<div class="ele-item">
<label for="">开关L</label>
<GuipSwitch :modelValue="switchValue1" :active-value="1" :inactive-value="0" activeText="number" @change="onSwitchChange">
<GuipSwitch :modelValue="switchValue1" :active-value="1" :inactive-value="0" activeText="number"
@change="onSwitchChange">
</GuipSwitch>
<GuipSwitch :modelValue="switchValue2" active-value="0" inactive-value="1" activeText="string" @change="onSwitchChange">
<GuipSwitch :modelValue="switchValue2" active-value="0" inactive-value="1" activeText="string"
@change="onSwitchChange">
</GuipSwitch>
</div>
<div class="flex ele-item">
@ -488,8 +510,10 @@
</div>
<div class="flex ele-item">
<label for="">一些功能集合</label>
<GuipButton type="primary" :btnstyle="{ width: '300px' }" @click="openDialog">打开弹框标题巨左按钮居右</GuipButton>
<GuipButton type="primary" :btnstyle="{ width: '300px' }" @click="openDialog1">打开弹框标题巨中按钮居中</GuipButton>
<GuipButton type="primary" :btnstyle="{ width: '300px' }" @click="openDialog">打开弹框标题巨左按钮居右
</GuipButton>
<GuipButton type="primary" :btnstyle="{ width: '300px' }" @click="openDialog1">打开弹框标题巨中按钮居中
</GuipButton>
<GuipButton type="primary" @click="openLoading" size="page">展示加载动画 2s</GuipButton>
</div>
@ -576,6 +600,7 @@ export default {
},
data() {
return {
content:'测试一下',
domainOptions: [
{
value: '.chachongz.com',
@ -648,19 +673,19 @@ export default {
domainSuffix: '11',
domainSuffix1: '.chachongz.com',
card: '1',
input1: '',
input1: '跨年的烟火,绽放天空',
input2: '',
input3: '',
},
languageOptions1: [
{ label: 'JavaScript', value: 'js', selectedLabel: 'JavaScripthhhhhhhhhh',id:'1',name:'麻辣烫' },
{ label: 'Python', value: 'py', selectedLabel: 'JavaScripthhhhhhhhhh',id:'10',name:'易烊千玺' },
{ label: 'Java', value: 'java', disabled: true, selectedLabel: 'JavaScripthhhhhhhhhh',id:'11',name:'王鹤di' }, //
{ label: 'Go', value: 'go', selectedLabel: 'JavaScripthhhhhhhhhh',id:'12',name:'王俊凯' },
{ label: 'JavaScript', value: 'js', selectedLabel: 'JavaScripthhhhhhhhhh', id: '1', name: '麻辣烫' },
{ label: 'Python', value: 'py', selectedLabel: 'JavaScripthhhhhhhhhh', id: '10', name: '易烊千玺' },
{ label: 'Java', value: 'java', disabled: true, selectedLabel: 'JavaScripthhhhhhhhhh', id: '11', name: '王源' }, //
{ label: 'Go', value: 'go', selectedLabel: 'JavaScripthhhhhhhhhh', id: '12', name: '王俊凯' },
],
languageOptions:{
'20':'查重站',
'31':'AI站'
languageOptions: {
'20': '查重站',
'31': 'AI站'
},
rules: {
username: [
@ -920,6 +945,14 @@ export default {
}
},
methods: {
// copy
handleClickCopy(){
this.$copy(this.form.input1, {
successMsg: '内容已复制到剪贴板',
errorMsg: '复制失败,请按Ctrl+C手动复制',
vm: this
});
},
// radio label
formatLabel(option) {
return `${option.name} (ID: ${option.id})`;

Loading…
Cancel
Save